make the lua bindings generation a bit better
This commit is contained in:
parent
c2ce13d8d7
commit
2aa7f739db
1 changed files with 85 additions and 30 deletions
|
@ -1,6 +1,19 @@
|
|||
import os
|
||||
import re
|
||||
|
||||
try:
|
||||
import pygments
|
||||
from pygments.lexers import CppLexer
|
||||
from pygments.formatters import HtmlFormatter
|
||||
def highlight(src):
|
||||
return pygments.highlight(src, CppLexer(), HtmlFormatter())
|
||||
CSS = HtmlFormatter().get_style_defs()
|
||||
except:
|
||||
print("Pygments is not installed, no syntax highlighting")
|
||||
def highlight(src):
|
||||
return src.replace("\n", "<br>").replace(" ", " ").replace("\t", " ")
|
||||
CSS = ""
|
||||
|
||||
from os.path import join, dirname, basename, abspath
|
||||
|
||||
|
||||
|
@ -9,7 +22,7 @@ END_OF_DOC_RE = re.compile(r'\*/\s*$')
|
|||
C_COMMENT_RE = re.compile(r'/\*[^*]*\*+(?:[^/*][^*]*\*+)*/', re.MULTILINE);
|
||||
BLANK_RE = re.compile(r'^\s*$')
|
||||
COMMENT_RE = re.compile(r'^\s*/?\*+/?', re.MULTILINE)
|
||||
PKG_EXPORT_RE = re.compile(r'(?:(?:extern )?[a-zA-Z0-9_]+ ([a-zA-Z0-9_]+\([^)]+\))|(?:extern )?([a-zA-Z0-9_]+ [a-zA-Z0-9_\[\]\*]+));')
|
||||
PKG_EXPORT_RE = re.compile(r'^(?:extern )?((?:unsigned )?[a-zA-Z0-9_]+ [a-zA-Z0-9_\[\]\*]+(?:\([^)]+\))?);')
|
||||
|
||||
|
||||
def get_c_func_re(c_func):
|
||||
|
@ -53,31 +66,45 @@ def walk_lua_exports(filename):
|
|||
else:
|
||||
m = PKG_EXPORT_RE.search(line)
|
||||
if m:
|
||||
yield "".join(current_comment), m.group(1) or m.group(2)
|
||||
item = m.group(1)
|
||||
item = item.replace("void ", "").replace("tolua_readonly", "readonly")
|
||||
yield "".join(current_comment), item
|
||||
current_comment = []
|
||||
|
||||
|
||||
def extract_docstring(filename, c_func):
|
||||
"""Return the docstring for function and the start of the line for that function"""
|
||||
"""Return the docstring for function, the start of the line for that function, and its source"""
|
||||
with open(filename, "r", errors="ignore") as f:
|
||||
content = f.read()
|
||||
m = get_c_func_re(c_func).search(content)
|
||||
if m:
|
||||
beginning = m.start
|
||||
preamble = content[:m.start()]
|
||||
lines = preamble.split("\n")
|
||||
if len(lines) > 2:
|
||||
if END_OF_DOC_RE.search(lines[-2]):
|
||||
# line prior to declaration looks like end of comment
|
||||
idx = 2
|
||||
while len(lines) > idx:
|
||||
if BLANK_RE.match(lines[-idx]):
|
||||
break
|
||||
idx += 1
|
||||
m = C_COMMENT_RE.search("\n".join(lines[-idx:-1]))
|
||||
docstring = m.group(0)
|
||||
return COMMENT_RE.sub("", docstring), len(lines)
|
||||
return "<documentation not found>", -1
|
||||
beginning = m.start()
|
||||
all_lines = content.split("\n")
|
||||
src = []
|
||||
comment = []
|
||||
docstring = ""
|
||||
cnt = 0
|
||||
for idx, line in enumerate(all_lines):
|
||||
cnt += len(line) + 1
|
||||
if src and line.startswith("}"):
|
||||
src.append(line)
|
||||
return COMMENT_RE.sub("", docstring), idx, "\n".join(src)
|
||||
elif src:
|
||||
src.append(line)
|
||||
elif cnt > beginning:
|
||||
src = [line]
|
||||
elif "*/" in line:
|
||||
docstring = "\n".join(comment)
|
||||
comment = []
|
||||
elif comment and line.strip().startswith("*"):
|
||||
comment.append(line)
|
||||
elif line.strip().startswith("/*"):
|
||||
comment = [line]
|
||||
else:
|
||||
# hit a non-comment line, but not the desired src yet, ignore this one
|
||||
docstring = ""
|
||||
comment = []
|
||||
return "", -1, ""
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -86,29 +113,58 @@ if __name__ == "__main__":
|
|||
with open("index.html", "w") as toc:
|
||||
toc.write("""
|
||||
<html>
|
||||
<head><title>TOC</title></head>
|
||||
<head>
|
||||
<title>TOC</title>
|
||||
<style type=text/css>{0}</style>
|
||||
</head>
|
||||
<body>
|
||||
""")
|
||||
""".format(CSS))
|
||||
for cppfile in walk_files(root, exts=[".cpp"]):
|
||||
rule = False
|
||||
for lua_register in walk_lua_registers(cppfile):
|
||||
if not rule:
|
||||
toc.write("<hr>")
|
||||
toc.write(cppfile.replace(root, ""))
|
||||
toc.write("<hr>")
|
||||
rule = True
|
||||
luaname, cname = lua_register
|
||||
doc, line = extract_docstring(cppfile, cname)
|
||||
doc, line, src = extract_docstring(cppfile, cname)
|
||||
doc = doc.replace("\n", "<br>")
|
||||
toc.write("""
|
||||
<details>
|
||||
<summary>{0}</summary>
|
||||
<p>{1}</p>
|
||||
<p>{2}:{3} {4}</p>
|
||||
</details>
|
||||
""".format(luaname, doc, cppfile.replace(root, ""), line, cname))
|
||||
src = highlight(src)
|
||||
if src:
|
||||
if doc:
|
||||
toc.write("""
|
||||
<details>
|
||||
<summary>{0}</summary>
|
||||
<p>{1}</p>
|
||||
<details>
|
||||
<summary>{2}:{3} {4}</summary>
|
||||
<p>{5}</p>
|
||||
</details>
|
||||
</details>
|
||||
""".format(luaname, doc, cppfile.replace(root, ""), line, cname, src))
|
||||
else:
|
||||
toc.write("""
|
||||
<details>
|
||||
<summary>{0}</summary>
|
||||
<p>{1}:{2} {3}</p>
|
||||
<p>{4}</p>
|
||||
</details>
|
||||
""".format(luaname, cppfile.replace(root, ""), line, cname, src))
|
||||
else:
|
||||
toc.write("""
|
||||
<details>
|
||||
<summary>{0}</summary>
|
||||
<p>Lua function implemented as {1}, but didn't find that in {2}</p>
|
||||
</details>
|
||||
""".format(luaname, cname, cppfile.replace(root, "")))
|
||||
|
||||
for pkgfile in walk_files(root, exts=[".pkg"]):
|
||||
rule = False
|
||||
for lua_register in walk_lua_exports(pkgfile):
|
||||
if not rule:
|
||||
toc.write("<hr>")
|
||||
toc.write(pkgfile.replace(root, ""))
|
||||
toc.write("<hr>")
|
||||
rule = True
|
||||
doc, code = lua_register
|
||||
|
@ -121,7 +177,6 @@ if __name__ == "__main__":
|
|||
</details>
|
||||
""".format(code, doc))
|
||||
else:
|
||||
code = code.replace(" ", " ").replace("\t", " ")
|
||||
code = code.split("\n")
|
||||
toc.write("""
|
||||
<details>
|
||||
|
@ -129,7 +184,7 @@ if __name__ == "__main__":
|
|||
<p>{1}</p>
|
||||
<p>{2}</p>
|
||||
</details>
|
||||
""".format(code[0], doc, "<br>".join(code)))
|
||||
""".format(code[0], doc, highlight("\n".join(code))))
|
||||
toc.write("""
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue