Skip to content

Commit

Permalink
script can now regenerate API pages
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre committed Dec 15, 2021
1 parent 1b0bbc6 commit c175642
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ A system that stops using Qt Assistant, allows people to still use the wiki (for
* The Help system can display pages outside the official FreeCAD documentation, so it can be used in addons as well
* The help system is available in non-GUI mode too
* Markdown is becoming widely universal, and is much more recognised and parsable by all kinds of applications or systems than the MediaWiki syntax. So our documentation becomes much more integratable into other systems.
* Python API documentation can be regenerated each time, precisely tailored and tighly integrated into the documentation
* The documentation can be much better integrated everywhere: In menus, in tooltips (for ex. tooltips could display some contents from the docs, ex. images)

99 changes: 97 additions & 2 deletions migrate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!/usr/bin/env python

#***************************************************************************
#* Copyright (c) 2021 Yorik van Havre <[email protected]> *
Expand Down Expand Up @@ -106,6 +106,10 @@ def __init__(self,url=BASE_URL):
self.translationfolder = "translations"
self.rootpage = "README.md"
self.workbenches = self.getWorkbenches()
self.icon_function = os.path.join(self.imagefolder,"type_method.svg")
self.icon_module = os.path.join(self.imagefolder,"type_module.svg")
self.icon_type = os.path.join(self.imagefolder,"type_class.svg")
self.icon_builtin = os.path.join(self.imagefolder,"Type_enum.svg")


### UTILS
Expand Down Expand Up @@ -143,7 +147,7 @@ def getWorkbenches(self):


def getWorkbench(self,page):

"""getWorkbench(page):
Returns the workbench this page belongs to"""

Expand Down Expand Up @@ -400,6 +404,8 @@ def getPageCategory(self,page):
"""getPageCategory(self,page):
Returns the first category a page belongs to, or None"""

if "API" in page:
return "API"
if page in self.pagecontents:
content = self.pagecontents[page]
cats = re.findall("Category:(.*?)[{}\[]",content)
Expand Down Expand Up @@ -918,6 +924,94 @@ def updateReadme(self):
f.close()


### API docs


def getAPIPages(self):

"""getAPIPages():
Returns a list of API pages"""

return [p for p in os.listdir(self.output) if (p.endswith("_API.md") and (not "Category" in p))]


def rebuildAAllPI(self):

"""rebuildAllAPI():
Rebuilds all the API pages"""

apipages = self.getAPIPages()
for apipage in apipages:
self.rebuildAPIPage(apipage)


def rebuildAPIPage(self,apipage):

"""rebuildAPIPage(apipage):
Rebuilds an API page"""

def make_icon(icon):
return "<img src=\""+icon+"\" style=\"max-width:24px;\">"

modname = apipage.split("_")[0]
md = "# " + modname + " API\n\n"
module = None
if modname == "Object":
d = FreeCAD.newDocument()
module = d.addObject("App::FeaturePython","Test")
elif modname == "ViewObject":
import FreeCADGui
d = FreeCAD.newDocument()
o = d.addObject("App::FeaturePython","Test")
module = o.ViewObject
elif modname == "Selection":
#import FreeCADGui
#module = FreeCADGui.Selection
pass # TODO : handle this
else:
import importlib
try:
module = importlib.import_module(modname)
except:
print("Unable to import module",modname)
if module:
import inspect
if module.__doc__:
md += module.__doc__
md += "\n\n\n\n"
for membername in [m for m in dir(module) if not(m.startswith("_"))]:
member = getattr(module,membername)
membertype = str(type(member))
memberattrs = None
if callable(member):
try:
memberattrs = str(inspect.signature(member))
except:
pass
md += "#### "
if ("builtin" in membertype) or ("function" in membertype):
md += make_icon(self.icon_function)
elif("module" in membertype):
md += make_icon(self.icon_module)
elif("Type" in membertype):
md += make_icon(self.icon_type)
else:
md += make_icon(self.icon_builtin)
md += membername
if memberattrs:
md += " <small>" + memberattrs + "</small>"
md += "\n\n"
if member.__doc__:
md += member.__doc__
md += "\n\n\n\n"
md += "\n\n"
md = self.addFooter(apipage[:-3],md)
fname = os.path.join(self.output,apipage)
with open(fname,"w") as apifile:
apifile.write(md)
print("Regenerated",apipage)



### GENERAL FUNCTIONS (USABLE THROUGH COMMAND-LINE)

Expand Down Expand Up @@ -958,6 +1052,7 @@ def update():
wiki.printProgress()
wiki.getAllImages()
wiki.updateReadme()
wiki.rebuildAAllPI()
print("All done!\n")
if errors:
print("page with write errors: ",errors)
Expand Down

0 comments on commit c175642

Please sign in to comment.