| 27 | import sys |
| 28 | |
| 29 | def process_comment(fd, comment, first_param, last_param): |
| 30 | if first_param < 0: |
| 31 | # Nothing to do: just copy the comment |
| 32 | fd.write("".join(comment)) |
| 33 | else: |
| 34 | params = list() |
| 35 | |
| 36 | # Measure the indentation of the first param and use that to create an empty comment line string: |
| 37 | m = re.match(r" */", comment[0]) |
| 38 | |
| 39 | if not m: |
| 40 | raise Exception("{}: Not a comment ? '{}'".format(path,comment[first_param])) |
| 41 | |
| 42 | line_prefix = " " * len(m.group(0)) + "*" |
| 43 | empty_line = line_prefix +"\n" |
| 44 | |
| 45 | fd.write(comment[0]) |
| 46 | # Copy the non param lines with the correct indentation: |
| 47 | for comment_line in range(1,first_param): |
| 48 | line = comment[comment_line] |
| 49 | m = re.match(" *\*(.*)", line) |
| 50 | if not m: |
| 51 | raise Exception("{}:{}: Not a comment line ? ".format(path, n_line - len(comment) + comment_line + 1)) |
| 52 | fd.write(line_prefix+ m.group(1)+"\n") |
| 53 | |
| 54 | # For each param split the line into 3 columns: param, param_name, description |
| 55 | for param in range(first_param, last_param): |
| 56 | m = re.match(r"[^@]+(@param\[[^\]]+\]) +(\S+) +(.+)", comment[param]) |
| 57 | |
| 58 | if m: |
| 59 | params.append( (" "+m.group(1), m.group(2), m.group(3)) ) |
| 60 | else: |
| 61 | # If it's not a match then it must be a multi-line param description: |
| 62 | |
| 63 | m = re.match(" *\* +(.*)", comment[param]) |
| 64 | if not m: |
| 65 | raise Exception("{}:{}: Not a comment line ? ".format(path, n_line - len(comment) + param + 1)) |
| 66 | |
| 67 | params.append( ("", "", m.group(1)) ) |
| 68 | |
| 69 | # Now that we've got a list of params, find what is the longest string for each column: |
| 70 | max_len = [0, 0] |
| 71 | |
| 72 | for p in params: |
| 73 | for l in range(len(max_len)): |
| 74 | max_len[l] = max(max_len[l], len(p[l])) |
| 75 | |
| 76 | # Insert an empty line if needed before the first param to make it easier to read: |
| 77 | m = re.match(r" *\* *$", comment[first_param - 1]) |
| 78 | |
| 79 | if not m: |
| 80 | # insert empty line |
| 81 | fd.write(empty_line) |
| 82 | |
| 83 | # Write out the formatted list of params: |
| 84 | for p in params: |
| 85 | fd.write("{}{}{} {}{} {}\n".format( line_prefix, |
| 86 | p[0], " " * (max_len[0] - len(p[0])), |