|
| 1 | + |
| 2 | +import re |
| 3 | + |
| 4 | +def join(s): |
| 5 | + return ' '.join(l.strip() for l in s.split('\n')) |
| 6 | + |
| 7 | +class DocBlock(object): |
| 8 | + def __init__(self): |
| 9 | + self.description = '' |
| 10 | + self.return_doc = None |
| 11 | + self.params = [] |
| 12 | + |
| 13 | + self.authors = [] |
| 14 | + self.deprecated = False |
| 15 | + |
| 16 | + # @exception and @throw are equivalent |
| 17 | + self.throws = {} |
| 18 | + self.exceptions = self.throws |
| 19 | + |
| 20 | + self.tags = {} |
| 21 | + |
| 22 | + def add_block(self, name, value): |
| 23 | + value = value.strip() |
| 24 | + |
| 25 | + if name == 'param': |
| 26 | + try: |
| 27 | + param, description = value.split(None, 1) |
| 28 | + except ValueError: |
| 29 | + param, description = value, '' |
| 30 | + self.params.append((param, join(description))) |
| 31 | + |
| 32 | + elif name in ('throws', 'exception'): |
| 33 | + try: |
| 34 | + ex, description = value.split(None, 1) |
| 35 | + except ValueError: |
| 36 | + ex, description = value, '' |
| 37 | + self.throws[ex] = join(description) |
| 38 | + |
| 39 | + elif name == 'return': |
| 40 | + self.return_doc = value |
| 41 | + |
| 42 | + elif name == 'author': |
| 43 | + self.authors.append(value) |
| 44 | + |
| 45 | + elif name == 'deprecated': |
| 46 | + self.deprecated = True |
| 47 | + |
| 48 | + self.tags.setdefault(name, []).append(value) |
| 49 | + |
| 50 | +blocks_re = re.compile('(^@)', re.MULTILINE) |
| 51 | +leading_space_re = re.compile(r'^\s*\*', re.MULTILINE) |
| 52 | +blocks_justify_re = re.compile(r'^\s*@', re.MULTILINE) |
| 53 | + |
| 54 | +def _sanitize(s): |
| 55 | + s = s.strip() |
| 56 | + |
| 57 | + if not (s[:3] == '/**' and s[-2:] == '*/'): |
| 58 | + raise ValueError('not a valid Javadoc comment') |
| 59 | + |
| 60 | + s = s.replace('\t', ' ') |
| 61 | + |
| 62 | + return s |
| 63 | + |
| 64 | +def _uncomment(s): |
| 65 | + # Remove /** and */ |
| 66 | + s = s[3:-2].strip() |
| 67 | + |
| 68 | + return leading_space_re.sub('', s) |
| 69 | + |
| 70 | +def _get_indent_level(s): |
| 71 | + return len(s) - len(s.lstrip()) |
| 72 | + |
| 73 | +def _left_justify(s): |
| 74 | + lines = s.rstrip().splitlines() |
| 75 | + |
| 76 | + if not lines: |
| 77 | + return '' |
| 78 | + |
| 79 | + indent_levels = [] |
| 80 | + for line in lines: |
| 81 | + if line.strip(): |
| 82 | + indent_levels.append(_get_indent_level(line)) |
| 83 | + indent_levels.sort() |
| 84 | + |
| 85 | + common_indent = indent_levels[0] |
| 86 | + if common_indent == 0: |
| 87 | + return s |
| 88 | + else: |
| 89 | + lines = [line[common_indent:] for line in lines] |
| 90 | + return '\n'.join(lines) |
| 91 | + |
| 92 | +def _force_blocks_left(s): |
| 93 | + return blocks_justify_re.sub('@', s) |
| 94 | + |
| 95 | +def parse(raw): |
| 96 | + sanitized = _sanitize(raw) |
| 97 | + uncommented = _uncomment(sanitized) |
| 98 | + justified = _left_justify(uncommented) |
| 99 | + justified_fixed = _force_blocks_left(justified) |
| 100 | + prepared = justified_fixed |
| 101 | + |
| 102 | + blocks = blocks_re.split(prepared) |
| 103 | + |
| 104 | + doc = DocBlock() |
| 105 | + |
| 106 | + if blocks[0] != '@': |
| 107 | + doc.description = blocks[0].strip() |
| 108 | + blocks = blocks[2::2] |
| 109 | + else: |
| 110 | + blocks = blocks[1::2] |
| 111 | + |
| 112 | + for block in blocks: |
| 113 | + try: |
| 114 | + tag, value = block.split(None, 1) |
| 115 | + except ValueError: |
| 116 | + tag, value = block, '' |
| 117 | + |
| 118 | + doc.add_block(tag, value) |
| 119 | + |
| 120 | + return doc |
0 commit comments