bepeephole: Inline be_peephole_new_node() into its only caller.
[libfirm] / scripts / gen_docu.py
index f747af3..6dba71c 100755 (executable)
@@ -1,38 +1,49 @@
 #!/usr/bin/env python
+#
+# This file is part of libFirm.
+# Copyright (C) 2012 Karlsruhe Institute of Technology
 import sys
-import re
 import docutils.core
 import docutils.writers.html4css1
 from datetime import datetime
 from jinja2 import Environment, Template
-from jinja2.filters import do_dictsort
-from spec_util import is_dynamic_pinned, verify_node, isAbstract, setdefault
-from ir_spec import nodes
+from spec_util import isAbstract, load_spec
 
-def trim(docstring):
-    if not docstring:
-        return ''
-    # Convert tabs to spaces (following the normal Python rules)
-    # and split into a list of lines:
-    lines = docstring.expandtabs().splitlines()
-    # Determine minimum indentation (first line doesn't count):
-    indent = sys.maxint
-    for line in lines[1:]:
-        stripped = line.lstrip()
-        if stripped:
-            indent = min(indent, len(line) - len(stripped))
-    # Remove indentation (first line is special):
-    trimmed = [lines[0].strip()]
-    if indent < sys.maxint:
-        for line in lines[1:]:
-            trimmed.append(line[indent:].rstrip())
-    # Strip off trailing and leading blank lines:
-    while trimmed and not trimmed[-1]:
-        trimmed.pop()
-    while trimmed and not trimmed[0]:
-        trimmed.pop(0)
-    # Return a single string:
-    return '\n'.join(trimmed)
+tags = None
+linkbase = None
+
+def format_doxygrouplink(string, link=None):
+       global tags
+       if link == None:
+               link = string
+       if tags == None:
+               return string
+       e = tags.xpath("//compound[name/text()='%s']" % link)
+       if len(e) == 0:
+               return string
+       e = e[0]
+       anchorfile = e.xpath("filename/text()")
+       if len(anchorfile) == 0:
+               return string
+       global linkbase
+       return "<a href=\"%s%s\">%s</a>" % (linkbase, anchorfile[0], string)
+
+def format_doxylink(string, link=None):
+       global tags
+       if link == None:
+               link = string
+       if tags == None:
+               return string
+       e = tags.xpath("//member[name/text()='%s']" % link)
+       if len(e) == 0:
+               return string
+       e = e[0]
+       anchorfile = e.xpath("anchorfile/text()")
+       anchor = e.xpath("anchor/text()")
+       if len(anchorfile) == 0 or len(anchor) == 0:
+               return string
+       global linkbase
+       return "<a href=\"%s%s#%s\">%s</a>" % (linkbase, anchorfile[0], anchor[0], string)
 
 def format_docutils(string):
        writer = docutils.writers.html4css1.Writer()
@@ -41,6 +52,8 @@ def format_docutils(string):
 
 env = Environment()
 env.filters['docutils'] = format_docutils
+env.filters['doxylink'] = format_doxylink
+env.filters['doxygrouplink'] = format_doxygrouplink
 
 docu_template = env.from_string(
 '''<html>
@@ -84,8 +97,17 @@ docu_template = env.from_string(
                                        {% endfor %}
                                        {% endif %}
                                        </dl>
+                                       {% set comma = joiner(", ") %}
                                        <h5>Flags</h5>
-                                       {% for flag in node.flags %} {{flag}} {% endfor %}
+                                       {% if node.flags.__len__() > 0 %}
+                                       {% for flag in node.flags -%}
+                                               {{comma()}}{{flag|doxylink("irop_flag_" + flag)}}
+                                       {%- endfor %}
+                                       {% else %}
+                                       None
+                                       {% endif %}
+                                       <h5>{{"API"|doxygrouplink(node.name)}}</h5>
+                                       <hr/>
                                </div>
                                {% endfor %}
                        </div></div>
@@ -113,13 +135,9 @@ docu_template = env.from_string(
 
 #############################
 
-def preprocess_node(node):
-       node.doc = trim(node.__doc__)
-
-def prepare_nodes():
+def prepare_nodes(nodes):
        real_nodes = []
        for node in nodes:
-               preprocess_node(node)
                if isAbstract(node):
                        continue
                real_nodes.append(node)
@@ -127,13 +145,27 @@ def prepare_nodes():
        return real_nodes
 
 def main(argv):
+       global tags
        output = sys.stdout
-       if len(argv) > 1:
-               output = open(argv[1], "w")
+       specfile = argv[1]
+       if len(argv) > 2:
+               output = open(argv[-1], "w")
+       if len(argv) > 4:
+               tagfile = open(argv[-3], "r")
+               global linkbase
+               linkbase = argv[-2]
+               if linkbase != "":
+                       linkbase += "/"
+               try:
+                       from lxml import etree
+                       tags = etree.parse(tagfile)
+               except:
+                       tags = None
 
-       real_nodes = prepare_nodes()
+       spec = load_spec(specfile)
+       real_nodes = prepare_nodes(spec.nodes)
        time = datetime.now().replace(microsecond=0).isoformat(' ')
-       output.write(docu_template.render(nodes = real_nodes, time=time))
+       output.write(docu_template.render(nodes=real_nodes, time=time))
        if output != sys.stdout:
                output.close()