Skip to content

Instantly share code, notes, and snippets.

@jacobtolar
Created December 8, 2016 18:56
Show Gist options
  • Select an option

  • Save jacobtolar/46f03ced47ce6f15c005e8201ba601e1 to your computer and use it in GitHub Desktop.

Select an option

Save jacobtolar/46f03ced47ce6f15c005e8201ba601e1 to your computer and use it in GitHub Desktop.

Revisions

  1. jacobtolar created this gist Dec 8, 2016.
    49 changes: 49 additions & 0 deletions clean-up-javadocs.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    import os
    import re

    exts = set(['.java', '.groovy'])

    BEGIN_JAVADOC = re.compile('^\s*\/\*\*.*')
    END_JAVADOC = re.compile('^\s*\*\/.*')
    MISSING_PARAGRAPH = re.compile('^(\s*)\*\s*$')


    class State(object):
    NO_COMMENT = 1
    IN_COMMENT = 2


    def fixup(p):
    result = []

    with open(p) as fh:
    state = State.NO_COMMENT
    for line in fh:
    if state == State.NO_COMMENT:
    result.append(line)
    m = BEGIN_JAVADOC.match(line)
    if m:
    if line.find('*/') == -1: # Also ends the comment
    state = State.IN_COMMENT
    elif state == State.IN_COMMENT:
    match_end = END_JAVADOC.match(line)
    match_missing = MISSING_PARAGRAPH.match(line)

    if match_end:
    result.append(line)
    state = State.NO_COMMENT
    elif match_missing:
    result.append(match_missing.group(1) + '* <p>\n')
    else:
    result.append(line)
    else:
    raise Exception('Unknown state')

    with open(p, 'w') as fh:
    fh.write(''.join(result))

    for dirpath, dnames, files in os.walk("./"):
    for f in (f for f in files if os.path.splitext(f)[1] in exts):
    p = os.path.join(dirpath, f)
    print 'fixup {}'.format(p)
    fixup(p)