| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import pygments.lexers.compiled as lexer |
| 4 |
import optparse |
| 5 |
import re |
| 6 |
from pygments.token import Token |
| 7 |
import logging |
| 8 |
|
| 9 |
logg = logging.getLogger(__name__) |
| 10 |
|
| 11 |
FileComment = "FileComment" |
| 12 |
FileInclude = "FileInclude" |
| 13 |
FunctionComment = "FunctionComment" |
| 14 |
FunctionPrototype = "FunctionPrototype" |
| 15 |
|
| 16 |
# use the markdown lexer to identify elements |
| 17 |
# then filter only those we want. The returned |
| 18 |
# token list is more global flagging the role |
| 19 |
# of each token for the manual generation. |
| 20 |
class CppToMarkdown: |
| 21 |
def __init__(self): |
| 22 |
self.alldefinitions = 0 |
| 23 |
self.internaldefs = ["static"] |
| 24 |
self.filecomment_done = "" |
| 25 |
self.fileinclude_done = "" |
| 26 |
self.filecomment_text = "" |
| 27 |
self.fileinclude_text = "" |
| 28 |
self.comment_text = "" |
| 29 |
self.function_text = "" |
| 30 |
self.nesting = 0 |
| 31 |
def commentblock(self, text): |
| 32 |
emptyprefix = re.compile(r"(?s)^\s*[/][*]+[ \t]*(?=\n)") |
| 33 |
prefix = re.compile(r"(?s)^\s*[/][*]+([^\n]*)(?=\n)") |
| 34 |
suffix = re.compile(r"(?s)\n [*][/]\s*") |
| 35 |
empty = re.compile(r"(?s)\n [*][ \t]*(?=\n)") |
| 36 |
lines1 = re.compile(r"(?s)\n [*][ ][\t]") |
| 37 |
lines2 = re.compile(r"(?s)\n [*][ ]") |
| 38 |
lines3 = re.compile(r"(?s)\n [*][\t][\t]") |
| 39 |
lines4 = re.compile(r"(?s)\n [*][\t]") |
| 40 |
text = suffix.sub("\n", text) |
| 41 |
text = emptyprefix.sub("", text) |
| 42 |
text = prefix.sub("> \\1\n", text) |
| 43 |
text = empty.sub("\n", text) |
| 44 |
text = lines1.sub("\n ", text) |
| 45 |
text = lines2.sub("\n", text) |
| 46 |
text = lines3.sub("\n ", text) |
| 47 |
text = lines4.sub("\n ", text) |
| 48 |
return text |
| 49 |
def functionblock(self, text): |
| 50 |
empty = re.compile(r"(?s)\n[ \t]*(?=\n)") |
| 51 |
text = " " + text.replace("\n", "\n ") |
| 52 |
text = empty.sub("", text) |
| 53 |
return text |
| 54 |
def functionname(self, text): |
| 55 |
check1 = re.compile(r"^[^()=]*(\b\w+)\s*[(=]") |
| 56 |
found = check1.match(text) |
| 57 |
if found: |
| 58 |
return found.group(1) |
| 59 |
check2 = re.compile(r"^[^()=]*(\b\w+)\s*$") |
| 60 |
found = check2.match(text) |
| 61 |
if found: |
| 62 |
return found.group(1) |
| 63 |
return "" |
| 64 |
def run(self, filename): |
| 65 |
filetext = open(filename).read() |
| 66 |
for line in self.process(filetext, filename): |
| 67 |
print(line) |
| 68 |
def process(self, filetext, filename=""): |
| 69 |
for token, text in self.parse(filetext): |
| 70 |
if token == FileInclude: |
| 71 |
yield "## SOURCE " + filename.replace("../", "") |
| 72 |
yield " #" + text.replace("\n", "\n ") |
| 73 |
elif token == FileComment: |
| 74 |
yield "## INTRODUCTION" |
| 75 |
yield self.commentblock(text) |
| 76 |
elif token == FunctionPrototype: |
| 77 |
name = self.functionname(text) |
| 78 |
yield "-----------------------------------------" |
| 79 |
yield "### " + name |
| 80 |
yield "#### NAME" |
| 81 |
yield " " + name |
| 82 |
yield "#### SYNOPSIS" |
| 83 |
yield self.functionblock(text) |
| 84 |
elif token == FunctionComment: |
| 85 |
if text: |
| 86 |
yield "#### DESCRIPTION" |
| 87 |
yield self.commentblock(text) |
| 88 |
else: |
| 89 |
if text: |
| 90 |
yield "#### NOTES" |
| 91 |
print(token + " " + text.replace("\n", "\n ")) |
| 92 |
def isexported_function(self): |
| 93 |
function = self.function_text.strip().replace("\n"," ") |
| 94 |
logg.debug("@ --------------------------------------") |
| 95 |
logg.debug("@ ALLDEFINITIONS %s", self.alldefinitions) |
| 96 |
if function.startswith("static ") and self.alldefinitions < 3: |
| 97 |
logg.debug("@ ONLY INTERNAL %s", function) |
| 98 |
return False |
| 99 |
if not self.comment_text: |
| 100 |
if not self.alldefinitions: |
| 101 |
logg.info("@ NO COMMENT ON %s", function) |
| 102 |
return False |
| 103 |
else: |
| 104 |
logg.warn("@ NO COMMENT ON %s", function) |
| 105 |
text = self.comment_text |
| 106 |
if text.startswith("/**"): return True |
| 107 |
if text.startswith("/*!"): return True |
| 108 |
if text.startswith("///"): return True |
| 109 |
if text.startswith("//!"): return True |
| 110 |
if self.alldefinitions >= 1: |
| 111 |
if text.startswith("/*"): return True |
| 112 |
if text.startswith("//"): return True |
| 113 |
if self.alldefinitions >= 2: |
| 114 |
return True |
| 115 |
logg.debug("@ NO ** COMMENT %s", self.function_text.strip()) |
| 116 |
defs = self.function_text |
| 117 |
return False |
| 118 |
def parse(self, filetext): |
| 119 |
c = lexer.CLexer() |
| 120 |
for token, text in c.get_tokens(filetext): |
| 121 |
logg.debug("|| %s %s", token, text.replace("\n", "\n |")) |
| 122 |
# completion |
| 123 |
if token != Token.Comment.Preproc and self.fileinclude_done == "no": |
| 124 |
yield FileInclude, self.fileinclude_text |
| 125 |
if self.filecomment_text: |
| 126 |
yield FileComment, self.filecomment_text |
| 127 |
self.fileinclude_done = "done" |
| 128 |
# parsing |
| 129 |
if token == Token.Comment.Multiline: |
| 130 |
if not self.filecomment_done: |
| 131 |
self.filecomment_done = "done" |
| 132 |
self.filecomment_text = text |
| 133 |
# wait until we know it is not a function documentation |
| 134 |
self.comment_text = text |
| 135 |
else: |
| 136 |
self.comment_text = text |
| 137 |
elif token == Token.Comment.Preproc and "include" in text: |
| 138 |
if not self.fileinclude_done: |
| 139 |
self.fileinclude_done = "no" |
| 140 |
self.fileinclude_text += text |
| 141 |
self.comment_text = "" |
| 142 |
elif token == Token.Comment.Preproc and self.fileinclude_done == "no": |
| 143 |
if not "\n" in self.fileinclude_text: |
| 144 |
self.fileinclude_text += text |
| 145 |
self.comment_text = "" |
| 146 |
elif token == Token.Comment.Preproc: |
| 147 |
self.comment_text = "" |
| 148 |
self.function_text = "" |
| 149 |
elif token == Token.Operator and text == "=": |
| 150 |
if not self.nesting and self.function_text.strip(): |
| 151 |
if self.isexported_function(): |
| 152 |
yield FunctionPrototype, self.function_text |
| 153 |
yield FunctionComment, self.comment_text |
| 154 |
self.comment_text = "" |
| 155 |
self.function_text = "" |
| 156 |
elif token == Token.Punctuation and text == ";": |
| 157 |
self.comment_text = "" |
| 158 |
self.function_text = "" |
| 159 |
elif token == Token.Punctuation and text == "{": |
| 160 |
if not self.nesting and self.function_text.strip(): |
| 161 |
if self.isexported_function(): |
| 162 |
yield FunctionPrototype, self.function_text |
| 163 |
yield FunctionComment, self.comment_text |
| 164 |
self.comment_text = "" |
| 165 |
self.function_text = "" |
| 166 |
self.nesting += 1 |
| 167 |
elif token == Token.Punctuation and text == "}": |
| 168 |
self.nesting -= 1 |
| 169 |
self.comment_text = "" |
| 170 |
self.function_text = "" |
| 171 |
else: |
| 172 |
if not self.nesting: |
| 173 |
self.function_text += text |
| 174 |
else: |
| 175 |
pass # yield "|",text |
| 176 |
|
| 177 |
|
| 178 |
if __name__ == "__main__": |
| 179 |
_o = optparse.OptionParser() |
| 180 |
_o.add_option("-v", "--verbose", action="count", default=0) |
| 181 |
_o.add_option("-a", "--all", action="count", default=0, |
| 182 |
help="include all definitions in the output (not only /**)") |
| 183 |
opt, args = _o.parse_args() |
| 184 |
|
| 185 |
logg.addHandler(logging.StreamHandler()) |
| 186 |
if opt.verbose: |
| 187 |
logg.setLevel(logging.WARN - 10 * opt.verbose) |
| 188 |
|
| 189 |
c = CppToMarkdown() |
| 190 |
if opt.all: |
| 191 |
c.alldefinitions = opt.all |
| 192 |
for arg in args: |
| 193 |
c.run(arg) |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|