aboutsummaryrefslogtreecommitdiff
path: root/tools/docdump/makedocs.py
blob: d11515e94ade2e8cd3c4027f6606c0edaeecfe52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python3
# -*- coding: utf-8 -*-

#
# makedocs.py: Generate documentation for Open Project Wiki
#
# Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur.
#
# Contributor: Jorge Araya Navarro <elcorreo@deshackra.com>
#

import re
import argparse
import logging
from os import path
from itertools import izip_longest
from xml.etree import ElementTree

logging.basicConfig(level=logging.INFO)


def getxmlfloc():
    """ Returns the supposed location of the XML file
    """
    filepath = path.dirname(path.abspath(__file__))
    return path.join(filepath, "class_list.xml")


def sortkey(c):
    """ Symbols are first, letters second
    """
    if "_" == c.attrib["name"][0]:
        return True
    else:
        return c.attrib["name"]


def toOP(text):
    """ Convert commands in text to Open Project commands
    """
    # We are going to do something very complicated with all HTML commands
    # sadly, some commands are embedded inside other commands, so some steps
    # are needed before converting some commands to Textile markup
    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"':
                alignstr = "{display:block; margin-left:auto; margin-right:auto;}"
            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)
    # TODO: Process other non-html commands
    return text + "\n\n"

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")
# 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)

# Let's begin
tree = ElementTree.parse(args.xmlfp)
root = tree.getroot()

# Check version attribute exists in <doc>
if "version" not in root.attrib:
    logging.critical("<doc>'s version attribute missing")
    exit(1)

version = root.attrib["version"]
classes = sorted(root, key=sortkey)
# first column is always longer, second column of classes should be shorter
zclasses = izip_longest(classes[:len(classes) / 2 + 1],
                        classes[len(classes) / 2 + 1:],
                        fillvalue="")

# 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
    fcl.write("|^.\n")
    fcl.write("|_. Index symbol |_. Class name "
              "|_. Index symbol |_. Class name |\n")
    fcl.write("|-.\n")

    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]
            fcl.write("| *{}* |".format(indexletterl.upper()))
        else:
            # empty cell
            fcl.write("| |")
        # write the class name column, left
        fcl.write("\"{name}({title})\":/class_{link}".format(
            name=gdclassl.attrib["name"],
            title="Go to page of class " + gdclassl.attrib["name"],
            link="class_" + gdclassl.attrib["name"].lower()))

        # write the index symbol column, right
        if isinstance(gdclassr, ElementTree.Element):
            if indexletterr != gdclassr.attrib["name"][0]:
                indexletterr = gdclassr.attrib["name"][0]
                fcl.write("| *{}* |".format(indexletterr.upper()))
            else:
                # empty cell
                fcl.write("| |")
        # We are dealing with an empty string
        else:
            # two empty cell
            fcl.write("| | |\n")
            # 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
        fcl.write("\"{name}({title})\":/{link} |\n".format(
            name=gdclassr.attrib["name"],
            title="Go to page of class " + gdclassr.attrib["name"],
            link=gdclassr.attrib["name"].lower()))

        # row written #
        # now, let's write each class page for each class
        for gdclass in [gdclassl, gdclassr]:
            if not isinstance(ElementTree.Element, gdclass):
                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
                clsf.write("h1. {}\n".format(classname))
                # lay the attributes
                if "inherits" in gdclass.attrib:
                    inh = gdclass.attrib["inherits"].strip()
                    clsf.write(
                        "*Inherits:* \"{name}({title})\":/class_{link}\n".
                        format(
                            name=classname,
                            title="Go to page of class " + classname,
                            link=classname.lower()))
                if "category" in gdclass.attrib:
                    clsf.write("*Category:* {}".
                               format(gdclass.attrib["category"].strip()))
                # lay child nodes
                for gdchild in gdclass.iter():
                    if gdchild.tag == "brief_description":
                        clsf.write("h2. Brief Description\n")
                        clsf.write(toOP(gdchild.text.strip()))
                        # convert commands in text