2016-10-17 06:50:25 +00:00
|
|
|
#!/usr/bin/env python3
|
2015-09-27 05:55:00 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#
|
|
|
|
# makedocs.py: Generate documentation for Open Project Wiki
|
2017-01-01 21:01:57 +00:00
|
|
|
# Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.
|
2017-04-07 22:11:42 +00:00
|
|
|
# Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md)
|
2015-09-27 05:55:00 +00:00
|
|
|
# Contributor: Jorge Araya Navarro <elcorreo@deshackra.com>
|
|
|
|
#
|
|
|
|
|
2015-09-28 01:25:13 +00:00
|
|
|
# IMPORTANT NOTICE:
|
|
|
|
# If you are going to modify anything from this file, please be sure to follow
|
|
|
|
# the Style Guide for Python Code or often called "PEP8". To do this
|
|
|
|
# automagically just install autopep8:
|
|
|
|
#
|
|
|
|
# $ sudo pip3 install autopep8
|
|
|
|
#
|
|
|
|
# and run:
|
|
|
|
#
|
|
|
|
# $ autopep8 makedocs.py
|
|
|
|
#
|
|
|
|
# Before committing your changes. Also be sure to delete any trailing
|
|
|
|
# whitespace you may left.
|
|
|
|
#
|
|
|
|
# TODO:
|
2015-10-07 01:50:55 +00:00
|
|
|
# * Refactor code.
|
2015-09-28 01:25:13 +00:00
|
|
|
# * Adapt this script for generating content in other markup formats like
|
2015-12-12 23:01:04 +00:00
|
|
|
# reStructuredText, Markdown, DokuWiki, etc.
|
2015-09-28 01:25:13 +00:00
|
|
|
#
|
|
|
|
# Also check other TODO entries in this script for more information on what is
|
|
|
|
# left to do.
|
2015-09-27 05:55:00 +00:00
|
|
|
import argparse
|
2015-10-07 19:24:52 +00:00
|
|
|
import gettext
|
2015-09-27 05:55:00 +00:00
|
|
|
import logging
|
2015-10-07 01:50:55 +00:00
|
|
|
import re
|
2015-09-28 01:25:13 +00:00
|
|
|
from itertools import zip_longest
|
2015-10-07 19:24:52 +00:00
|
|
|
from os import path, listdir
|
2015-09-27 05:55:00 +00:00
|
|
|
from xml.etree import ElementTree
|
|
|
|
|
2015-10-07 01:50:55 +00:00
|
|
|
|
2015-09-28 01:25:13 +00:00
|
|
|
# add an option to change the verbosity
|
2015-09-27 05:55:00 +00:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
2015-10-07 19:11:17 +00:00
|
|
|
|
|
|
|
def getxmlfloc():
|
|
|
|
""" Returns the supposed location of the XML file
|
|
|
|
"""
|
|
|
|
filepath = path.dirname(path.abspath(__file__))
|
|
|
|
return path.join(filepath, "class_list.xml")
|
|
|
|
|
|
|
|
|
|
|
|
def langavailable():
|
|
|
|
""" Return a list of languages available for translation
|
|
|
|
"""
|
|
|
|
filepath = path.join(
|
|
|
|
path.dirname(path.abspath(__file__)), "locales")
|
|
|
|
files = listdir(filepath)
|
|
|
|
choices = [x for x in files]
|
|
|
|
choices.insert(0, "none")
|
|
|
|
return choices
|
|
|
|
|
|
|
|
|
|
|
|
desc = "Generates documentation from a XML file to different markup languages"
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description=desc)
|
|
|
|
parser.add_argument("--input", dest="xmlfp", default=getxmlfloc(),
|
|
|
|
help="Input XML file, default: {}".format(getxmlfloc()))
|
|
|
|
parser.add_argument("--output-dir", dest="outputdir", required=True,
|
|
|
|
help="Output directory for generated files")
|
|
|
|
parser.add_argument("--language", choices=langavailable(), default="none",
|
|
|
|
help=("Choose the language of translation"
|
|
|
|
" for the output files. Default is English (none). "
|
|
|
|
"Note: This is NOT for the documentation itself!"))
|
|
|
|
# TODO: add an option for outputting different markup formats
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
# Let's check if the file and output directory exists
|
|
|
|
if not path.isfile(args.xmlfp):
|
|
|
|
logging.critical("File not found: {}".format(args.xmlfp))
|
|
|
|
exit(1)
|
|
|
|
elif not path.isdir(args.outputdir):
|
|
|
|
logging.critical("Path does not exist: {}".format(args.outputdir))
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
_ = gettext.gettext
|
|
|
|
if args.language != "none":
|
|
|
|
lang = gettext.translation(domain="makedocs",
|
|
|
|
localedir="locales",
|
|
|
|
languages=[args.language])
|
|
|
|
lang.install()
|
|
|
|
|
|
|
|
_ = lang.gettext
|
|
|
|
|
2015-10-07 01:50:55 +00:00
|
|
|
# Strings
|
2015-10-07 19:11:17 +00:00
|
|
|
C_LINK = _("\"<code>{gclass}</code>(Go to page of class"
|
|
|
|
" {gclass})\":/class_{lkclass}")
|
|
|
|
MC_LINK = _("\"<code>{gclass}.{method}</code>(Go "
|
|
|
|
"to page {gclass}, section {method})\""
|
|
|
|
":/class_{lkclass}#{lkmethod}")
|
|
|
|
TM_JUMP = _("\"<code>{method}</code>(Jump to method"
|
|
|
|
" {method})\":#{lkmethod}")
|
|
|
|
GTC_LINK = _(" \"{rtype}(Go to page of class {rtype})\":/class_{link} ")
|
|
|
|
DFN_JUMP = _("\"*{funcname}*(Jump to description for"
|
|
|
|
" node {funcname})\":#{link} <b>(</b> ")
|
2015-10-07 01:50:55 +00:00
|
|
|
M_ARG_DEFAULT = C_LINK + " {name}={default}"
|
|
|
|
M_ARG = C_LINK + " {name}"
|
|
|
|
|
2015-10-07 19:11:17 +00:00
|
|
|
OPENPROJ_INH = _("h4. Inherits: ") + C_LINK + "\n\n"
|
2015-10-07 01:50:55 +00:00
|
|
|
|
2015-09-27 05:55:00 +00:00
|
|
|
|
2015-09-28 01:25:13 +00:00
|
|
|
def tb(string):
|
|
|
|
""" Return a byte representation of a string
|
|
|
|
"""
|
|
|
|
return bytes(string, "UTF-8")
|
|
|
|
|
|
|
|
|
2015-09-27 05:55:00 +00:00
|
|
|
def sortkey(c):
|
|
|
|
""" Symbols are first, letters second
|
|
|
|
"""
|
|
|
|
if "_" == c.attrib["name"][0]:
|
2015-09-28 01:25:13 +00:00
|
|
|
return "A"
|
2015-09-27 05:55:00 +00:00
|
|
|
else:
|
|
|
|
return c.attrib["name"]
|
|
|
|
|
|
|
|
|
|
|
|
def toOP(text):
|
|
|
|
""" Convert commands in text to Open Project commands
|
|
|
|
"""
|
2015-10-07 01:50:55 +00:00
|
|
|
# TODO: Make this capture content between [command] ... [/command]
|
2015-09-27 05:55:00 +00:00
|
|
|
groups = re.finditer((r'\[html (?P<command>/?\w+/?)(\]| |=)?(\]| |=)?(?P<a'
|
|
|
|
'rg>\w+)?(\]| |=)?(?P<value>"[^"]+")?/?\]'), text)
|
|
|
|
alignstr = ""
|
|
|
|
for group in groups:
|
|
|
|
gd = group.groupdict()
|
|
|
|
if gd["command"] == "br/":
|
|
|
|
text = text.replace(group.group(0), "\n\n", 1)
|
|
|
|
elif gd["command"] == "div":
|
|
|
|
if gd["value"] == '"center"':
|
2015-09-28 01:25:13 +00:00
|
|
|
alignstr = ("{display:block; margin-left:auto;"
|
|
|
|
" margin-right:auto;}")
|
2015-09-27 05:55:00 +00:00
|
|
|
elif gd["value"] == '"left"':
|
|
|
|
alignstr = "<"
|
|
|
|
elif gd["value"] == '"right"':
|
|
|
|
alignstr = ">"
|
|
|
|
text = text.replace(group.group(0), "\n\n", 1)
|
|
|
|
elif gd["command"] == "/div":
|
|
|
|
alignstr = ""
|
|
|
|
text = text.replace(group.group(0), "\n\n", 1)
|
|
|
|
elif gd["command"] == "img":
|
|
|
|
text = text.replace(group.group(0), "!{align}{src}!".format(
|
|
|
|
align=alignstr, src=gd["value"].strip('"')), 1)
|
|
|
|
elif gd["command"] == "b" or gd["command"] == "/b":
|
|
|
|
text = text.replace(group.group(0), "*", 1)
|
|
|
|
elif gd["command"] == "i" or gd["command"] == "/i":
|
|
|
|
text = text.replace(group.group(0), "_", 1)
|
|
|
|
elif gd["command"] == "u" or gd["command"] == "/u":
|
|
|
|
text = text.replace(group.group(0), "+", 1)
|
2015-09-28 01:25:13 +00:00
|
|
|
# Process other non-html commands
|
|
|
|
groups = re.finditer((r'\[method ((?P<class>[aA0-zZ9_]+)(?:\.))'
|
|
|
|
r'?(?P<method>[aA0-zZ9_]+)\]'), text)
|
|
|
|
for group in groups:
|
|
|
|
gd = group.groupdict()
|
|
|
|
if gd["class"]:
|
2015-10-07 01:50:55 +00:00
|
|
|
replacewith = (MC_LINK.format(gclass=gd["class"],
|
|
|
|
method=gd["method"],
|
|
|
|
lkclass=gd["class"].lower(),
|
|
|
|
lkmethod=gd["method"].lower()))
|
2015-09-28 01:25:13 +00:00
|
|
|
else:
|
|
|
|
# The method is located in the same wiki page
|
2015-10-07 01:50:55 +00:00
|
|
|
replacewith = (TM_JUMP.format(method=gd["method"],
|
|
|
|
lkmethod=gd["method"].lower()))
|
2015-09-28 01:25:13 +00:00
|
|
|
|
|
|
|
text = text.replace(group.group(0), replacewith, 1)
|
|
|
|
# Finally, [Classes] are around brackets, make them direct links
|
|
|
|
groups = re.finditer(r'\[(?P<class>[az0-AZ0_]+)\]', text)
|
|
|
|
for group in groups:
|
|
|
|
gd = group.groupdict()
|
2015-10-07 01:50:55 +00:00
|
|
|
replacewith = (C_LINK.
|
2015-09-28 01:25:13 +00:00
|
|
|
format(gclass=gd["class"],
|
|
|
|
lkclass=gd["class"].lower()))
|
|
|
|
text = text.replace(group.group(0), replacewith, 1)
|
|
|
|
|
2015-09-27 05:55:00 +00:00
|
|
|
return text + "\n\n"
|
|
|
|
|
2015-09-28 01:25:13 +00:00
|
|
|
|
|
|
|
def mkfn(node, is_signal=False):
|
|
|
|
""" Return a string containing a unsorted item for a function
|
|
|
|
"""
|
|
|
|
finalstr = ""
|
|
|
|
name = node.attrib["name"]
|
|
|
|
rtype = node.find("return")
|
|
|
|
if rtype:
|
|
|
|
rtype = rtype.attrib["type"]
|
|
|
|
else:
|
|
|
|
rtype = "void"
|
|
|
|
# write the return type and the function name first
|
|
|
|
finalstr += "* "
|
|
|
|
# return type
|
|
|
|
if not is_signal:
|
|
|
|
if rtype != "void":
|
2015-10-07 01:50:55 +00:00
|
|
|
finalstr += GTC_LINK.format(
|
2015-09-28 01:25:13 +00:00
|
|
|
rtype=rtype,
|
|
|
|
link=rtype.lower())
|
|
|
|
else:
|
|
|
|
finalstr += " void "
|
|
|
|
|
|
|
|
# function name
|
|
|
|
if not is_signal:
|
2015-10-07 01:50:55 +00:00
|
|
|
finalstr += DFN_JUMP.format(
|
2015-09-28 01:25:13 +00:00
|
|
|
funcname=name,
|
|
|
|
link=name.lower())
|
|
|
|
else:
|
|
|
|
# Signals have no description
|
|
|
|
finalstr += "*{funcname}* <b>(</b>".format(funcname=name)
|
|
|
|
# loop for the arguments of the function, if any
|
|
|
|
args = []
|
|
|
|
for arg in sorted(
|
|
|
|
node.iter(tag="argument"),
|
|
|
|
key=lambda a: int(a.attrib["index"])):
|
|
|
|
|
|
|
|
ntype = arg.attrib["type"]
|
|
|
|
nname = arg.attrib["name"]
|
|
|
|
|
|
|
|
if "default" in arg.attrib:
|
2015-10-07 01:50:55 +00:00
|
|
|
args.insert(-1, M_ARG_DEFAULT.format(
|
|
|
|
gclass=ntype,
|
|
|
|
lkclass=ntype.lower(),
|
|
|
|
name=nname,
|
|
|
|
default=arg.attrib["default"]))
|
2015-09-28 01:25:13 +00:00
|
|
|
else:
|
|
|
|
# No default value present
|
2015-10-07 01:50:55 +00:00
|
|
|
args.insert(-1, M_ARG.format(gclass=ntype,
|
|
|
|
lkclass=ntype.lower(), name=nname))
|
2015-09-28 01:25:13 +00:00
|
|
|
# join the arguments together
|
|
|
|
finalstr += ", ".join(args)
|
|
|
|
# and, close the function with a )
|
|
|
|
finalstr += " <b>)</b>"
|
|
|
|
# write the qualifier, if any
|
|
|
|
if "qualifiers" in node.attrib:
|
|
|
|
qualifier = node.attrib["qualifiers"]
|
|
|
|
finalstr += " " + qualifier
|
|
|
|
|
|
|
|
finalstr += "\n"
|
|
|
|
|
|
|
|
return finalstr
|
|
|
|
|
2015-09-27 05:55:00 +00:00
|
|
|
# Let's begin
|
|
|
|
tree = ElementTree.parse(args.xmlfp)
|
|
|
|
root = tree.getroot()
|
|
|
|
|
|
|
|
# Check version attribute exists in <doc>
|
|
|
|
if "version" not in root.attrib:
|
2015-10-07 19:11:17 +00:00
|
|
|
logging.critical(_("<doc>'s version attribute missing"))
|
2015-09-27 05:55:00 +00:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
version = root.attrib["version"]
|
|
|
|
classes = sorted(root, key=sortkey)
|
|
|
|
# first column is always longer, second column of classes should be shorter
|
2015-09-28 01:25:13 +00:00
|
|
|
zclasses = zip_longest(classes[:int(len(classes) / 2 + 1)],
|
|
|
|
classes[int(len(classes) / 2 + 1):],
|
|
|
|
fillvalue="")
|
2015-09-27 05:55:00 +00:00
|
|
|
|
|
|
|
# We write the class_list file and also each class file at once
|
|
|
|
with open(path.join(args.outputdir, "class_list.txt"), "wb") as fcl:
|
|
|
|
# Write header of table
|
2015-09-28 01:25:13 +00:00
|
|
|
fcl.write(tb("|^.\n"))
|
2015-10-07 19:11:17 +00:00
|
|
|
fcl.write(tb(_("|_. Index symbol |_. Class name "
|
|
|
|
"|_. Index symbol |_. Class name |\n")))
|
2015-09-28 01:25:13 +00:00
|
|
|
fcl.write(tb("|-.\n"))
|
2015-09-27 05:55:00 +00:00
|
|
|
|
|
|
|
indexletterl = ""
|
|
|
|
indexletterr = ""
|
|
|
|
for gdclassl, gdclassr in zclasses:
|
|
|
|
# write a row #
|
|
|
|
# write the index symbol column, left
|
|
|
|
if indexletterl != gdclassl.attrib["name"][0]:
|
|
|
|
indexletterl = gdclassl.attrib["name"][0]
|
2015-09-28 01:25:13 +00:00
|
|
|
fcl.write(tb("| *{}* |".format(indexletterl.upper())))
|
2015-09-27 05:55:00 +00:00
|
|
|
else:
|
|
|
|
# empty cell
|
2015-09-28 01:25:13 +00:00
|
|
|
fcl.write(tb("| |"))
|
2015-09-27 05:55:00 +00:00
|
|
|
# write the class name column, left
|
2015-10-07 01:50:55 +00:00
|
|
|
fcl.write(tb(C_LINK.format(
|
|
|
|
gclass=gdclassl.attrib["name"],
|
|
|
|
lkclass=gdclassl.attrib["name"].lower())))
|
2015-09-27 05:55:00 +00:00
|
|
|
|
|
|
|
# write the index symbol column, right
|
|
|
|
if isinstance(gdclassr, ElementTree.Element):
|
|
|
|
if indexletterr != gdclassr.attrib["name"][0]:
|
|
|
|
indexletterr = gdclassr.attrib["name"][0]
|
2015-09-28 01:25:13 +00:00
|
|
|
fcl.write(tb("| *{}* |".format(indexletterr.upper())))
|
2015-09-27 05:55:00 +00:00
|
|
|
else:
|
|
|
|
# empty cell
|
2015-09-28 01:25:13 +00:00
|
|
|
fcl.write(tb("| |"))
|
2015-09-27 05:55:00 +00:00
|
|
|
# We are dealing with an empty string
|
|
|
|
else:
|
|
|
|
# two empty cell
|
2015-09-28 01:25:13 +00:00
|
|
|
fcl.write(tb("| | |\n"))
|
2015-09-27 05:55:00 +00:00
|
|
|
# We won't get the name of the class since there is no ElementTree
|
|
|
|
# object for the right side of the tuple, so we iterate the next
|
|
|
|
# tuple instead
|
|
|
|
continue
|
|
|
|
|
|
|
|
# write the class name column (if any), right
|
2015-10-07 01:50:55 +00:00
|
|
|
fcl.write(tb(C_LINK.format(
|
|
|
|
gclass=gdclassl.attrib["name"],
|
|
|
|
lkclass=gdclassl.attrib["name"].lower()) + "|\n"))
|
2015-09-27 05:55:00 +00:00
|
|
|
|
|
|
|
# row written #
|
|
|
|
# now, let's write each class page for each class
|
|
|
|
for gdclass in [gdclassl, gdclassr]:
|
2015-09-28 01:25:13 +00:00
|
|
|
if not isinstance(gdclass, ElementTree.Element):
|
2015-09-27 05:55:00 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
classname = gdclass.attrib["name"]
|
|
|
|
with open(path.join(args.outputdir, "{}.txt".format(
|
|
|
|
classname.lower())), "wb") as clsf:
|
|
|
|
# First level header with the name of the class
|
2015-09-28 01:25:13 +00:00
|
|
|
clsf.write(tb("h1. {}\n\n".format(classname)))
|
2015-09-27 05:55:00 +00:00
|
|
|
# lay the attributes
|
|
|
|
if "inherits" in gdclass.attrib:
|
|
|
|
inh = gdclass.attrib["inherits"].strip()
|
2015-10-07 01:50:55 +00:00
|
|
|
clsf.write(tb(OPENPROJ_INH.format(gclass=inh,
|
|
|
|
lkclass=inh.lower())))
|
2015-09-27 05:55:00 +00:00
|
|
|
if "category" in gdclass.attrib:
|
2015-10-07 19:11:17 +00:00
|
|
|
clsf.write(tb(_("h4. Category: {}\n\n").
|
2015-09-28 01:25:13 +00:00
|
|
|
format(gdclass.attrib["category"].strip())))
|
2015-09-27 05:55:00 +00:00
|
|
|
# lay child nodes
|
2015-09-28 01:25:13 +00:00
|
|
|
briefd = gdclass.find("brief_description")
|
|
|
|
if briefd.text.strip():
|
2015-10-07 19:11:17 +00:00
|
|
|
clsf.write(tb(_("h2. Brief Description\n\n")))
|
2015-09-28 01:25:13 +00:00
|
|
|
clsf.write(tb(toOP(briefd.text.strip()) +
|
2015-10-07 19:11:17 +00:00
|
|
|
_("\"read more\":#more\n\n")))
|
2015-09-28 01:25:13 +00:00
|
|
|
|
|
|
|
# Write the list of member functions of this class
|
|
|
|
methods = gdclass.find("methods")
|
|
|
|
if methods and len(methods) > 0:
|
2015-10-07 19:11:17 +00:00
|
|
|
clsf.write(tb(_("\nh3. Member Functions\n\n")))
|
2015-09-28 01:25:13 +00:00
|
|
|
for method in methods.iter(tag='method'):
|
|
|
|
clsf.write(tb(mkfn(method)))
|
|
|
|
|
|
|
|
signals = gdclass.find("signals")
|
|
|
|
if signals and len(signals) > 0:
|
2015-10-07 19:11:17 +00:00
|
|
|
clsf.write(tb(_("\nh3. Signals\n\n")))
|
2015-09-28 01:25:13 +00:00
|
|
|
for signal in signals.iter(tag='signal'):
|
|
|
|
clsf.write(tb(mkfn(signal, True)))
|
|
|
|
# TODO: <members> tag is necessary to process? it does not
|
|
|
|
# exists in class_list.xml file.
|
|
|
|
|
|
|
|
consts = gdclass.find("constants")
|
|
|
|
if consts and len(consts) > 0:
|
2015-10-07 19:11:17 +00:00
|
|
|
clsf.write(tb(_("\nh3. Numeric Constants\n\n")))
|
2015-09-28 01:25:13 +00:00
|
|
|
for const in sorted(consts, key=lambda k:
|
|
|
|
k.attrib["name"]):
|
|
|
|
if const.text.strip():
|
|
|
|
clsf.write(tb("* *{name}* = *{value}* - {desc}\n".
|
|
|
|
format(
|
|
|
|
name=const.attrib["name"],
|
|
|
|
value=const.attrib["value"],
|
|
|
|
desc=const.text.strip())))
|
|
|
|
else:
|
|
|
|
# Constant have no description
|
|
|
|
clsf.write(tb("* *{name}* = *{value}*\n".
|
|
|
|
format(
|
|
|
|
name=const.attrib["name"],
|
|
|
|
value=const.attrib["value"])))
|
|
|
|
descrip = gdclass.find("description")
|
2015-10-07 19:11:17 +00:00
|
|
|
clsf.write(tb(_("\nh3(#more). Description\n\n")))
|
2015-09-28 01:25:13 +00:00
|
|
|
if descrip.text:
|
|
|
|
clsf.write(tb(descrip.text.strip() + "\n"))
|
|
|
|
else:
|
2015-10-07 19:11:17 +00:00
|
|
|
clsf.write(tb(_("_Nothing here, yet..._\n")))
|
2015-09-28 01:25:13 +00:00
|
|
|
|
|
|
|
# and finally, the description for each method
|
|
|
|
if methods and len(methods) > 0:
|
2015-10-07 19:11:17 +00:00
|
|
|
clsf.write(tb(_("\nh3. Member Function Description\n\n")))
|
2015-09-28 01:25:13 +00:00
|
|
|
for method in methods.iter(tag='method'):
|
|
|
|
clsf.write(tb("h4(#{n}). {name}\n\n".format(
|
|
|
|
n=method.attrib["name"].lower(),
|
|
|
|
name=method.attrib["name"])))
|
|
|
|
clsf.write(tb(mkfn(method) + "\n"))
|
|
|
|
clsf.write(tb(toOP(method.find(
|
|
|
|
"description").text.strip())))
|