Commit 267efae4 authored by Matthias Braun's avatar Matthias Braun
Browse files

merge gen_docu.py with gen_ir.py

parent 8d00fec2
......@@ -156,19 +156,17 @@ $(docdir)/libfirm.tag: doc/Doxyfile doc/logo.png $(IR_SPEC_GENERATED_INCLUDES) $
@echo Doxygen $@
$(Q)$(DOXYGEN) $<
DOCU_GENERATOR = $(srcdir)/scripts/gen_docu.py
DOCU_GENERATOR_DEPS = $(srcdir)/scripts/spec_util.py
DOCU_TEMPLATE = $(srcdir)/scripts/docu_templates/nodes.html
DOCU_TEMPLATE_DEPS = $(srcdir)/scripts/docu_templates/style.css
$(docdir)/html/nodes.html: $(docdir)/libfirm.tag $(DOCU_GENERATOR) $(DOCU_GENERATOR_DEPS) $(IR_SPEC) $(DOCU_TEMPLATE) $(DOCU_TEMPLATE_DEPS)
@echo gen_docu.py $@
$(Q)$(DOCU_GENERATOR) $(IR_SPEC) $(DOCU_TEMPLATE) $(docdir)/libfirm.tag "" > $@
DOCU_TEMPLATE = $(srcdir)/scripts/templates/nodes.html
DOCU_TEMPLATE_DEPS = $(srcdir)/scripts/templates/style.css
$(docdir)/html/nodes.html: $(docdir)/libfirm.tag $(IR_SPEC_GENERATOR) $(IR_SPEC_GENERATOR_DEPS) $(IR_SPEC) $(DOCU_TEMPLATE) $(DOCU_TEMPLATE_DEPS)
@echo GEN $@
$(Q)$(IR_SPEC_GENERATOR) $(IR_SPEC) $(DOCU_TEMPLATE) --tagfile $(docdir)/libfirm.tag > $@
$(Q)cp $(DOCU_TEMPLATE_DEPS) $(docdir)/html
NODES_TEMPLATE = firm-homepage/nodes_templates/Nodes
$(docdir)/Nodes: $(docdir)/libfirm.tag $(DOCU_GENERATOR) $(DOCU_GENERATOR_DEPS) $(IR_SPEC) $(NODES_TEMPLATE)
@echo gen_docu.py $@
$(Q)$(DOCU_GENERATOR) $(IR_SPEC) $(NODES_TEMPLATE) $(docdir)/libfirm.tag "" > $@
$(docdir)/Nodes: $(docdir)/libfirm.tag $(IR_SPEC_GENERATOR) $(IR_SPEC_GENERATOR_DEPS) $(IR_SPEC) $(NODES_TEMPLATE)
@echo GEN $@
$(Q)$(IR_SPEC_GENERATOR) $(IR_SPEC) $(NODES_TEMPLATE) --tagfile $(docdir)/libfirm.tag -I $(dir $(NODES_TEMPLATE)) > $@
.PHONY: doc
doc: $(docdir)/libfirm.tag $(docdir)/html/nodes.html
......
#!/usr/bin/env python
#
# This file is part of libFirm.
# Copyright (C) 2012 Karlsruhe Institute of Technology
import sys
import docutils.core
import docutils.writers.html4css1
import os.path
from datetime import datetime
from jinja2 import Environment, Template, FileSystemLoader
from spec_util import isAbstract, load_spec
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("//tagfile/compound[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()
document = docutils.core.publish_parts(string, writer=writer)['body']
return document
def prepare_nodes(nodes):
real_nodes = []
for node in nodes:
if isAbstract(node):
continue
real_nodes.append(node)
return real_nodes
def parse_tagfile(filename):
global tags
tagfile = open(filename)
try:
from lxml import etree
tags = etree.parse(tagfile)
except:
tags = None
def main(argv):
output = sys.stdout
if len(argv) < 3:
sys.stderr.write("usage: %s specfile templatefile [linkbase doxygen-tag-file]\n" % argv[0])
sys.exit(1)
specfile = argv[1]
templatefile = argv[2]
if len(argv) == 5:
sys.stderr.write("loading tagfile\n")
parse_tagfile(argv[3])
global linkbase
linkbase = argv[4]
if linkbase != "":
linkbase += "/"
spec = load_spec(specfile)
prepared_nodes = prepare_nodes(spec.nodes)
basedir = os.path.dirname(templatefile)
if basedir == "":
basedir = "."
loader = FileSystemLoader(basedir)
env = Environment(loader=loader)
env.filters['docutils'] = format_docutils
env.filters['doxylink'] = format_doxylink
env.filters['doxygrouplink'] = format_doxygrouplink
env.globals['nodes'] = prepared_nodes
env.globals['time'] = datetime.now().replace(microsecond=0).isoformat(' ')
template = env.get_template(os.path.basename(templatefile))
output.write(template.render())
main(sys.argv)
......@@ -3,6 +3,8 @@
# This file is part of libFirm.
# Copyright (C) 2012 Karlsruhe Institute of Technology.
import sys
import argparse
from datetime import datetime
from jinja2 import Environment, Template, BaseLoader
from spec_util import is_dynamic_pinned, isAbstract, setdefault, load_spec, Attribute
from filters import format_arguments, format_filtjoin, filter_has, filter_hasnot
......@@ -178,21 +180,83 @@ def format_simplify_type(string):
res = res[3:]
return res
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)
tags = None
linkbase = None
def format_doxylink(string, link=None):
global tags
if link == None:
link = string
if tags == None:
return string
e = tags.xpath("//tagfile/compound[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):
import docutils.writers.html4css1
import docutils.core
writer = docutils.writers.html4css1.Writer()
document = docutils.core.publish_parts(string, writer=writer)['body']
return document
def parse_tagfile(filename):
global tags
tagfile = open(filename)
try:
from lxml import etree
tags = etree.parse(tagfile)
except:
tags = None
# simple FileSystemLoader variant. Compared to the default loader in jinja
# it does not perform searchpath magic and does not reject paths containig ".."
# for security.
# Note that we want to use a loader instead of simply env.from_string because
# we want to see the filename in error messages from jinja
class SimpleLoader(BaseLoader):
def __init__(self):
super(SimpleLoader, self).__init__()
self.includedirs = [""]
def get_source(self, environment, name):
contents = open(name).read()
def uptodate():
return False
return contents, name, uptodate
for dir in self.includedirs:
try:
path = name if dir == "" else "%s/%s" % (dir, name)
contents = open(path).read()
except:
continue
def uptodate():
return False
return contents, name, uptodate
raise Exception("Could not open '%s'" % name)
def list_template(self):
return []
env = Environment(loader=SimpleLoader(), keep_trailing_newline=True)
loader = SimpleLoader()
env = Environment(loader=loader, keep_trailing_newline=True)
env.filters['a_an'] = format_a_an
env.filters['args'] = format_args
env.filters['arguments'] = format_arguments
......@@ -221,6 +285,9 @@ env.filters['parameters'] = format_parameters
env.filters['pinned'] = format_pinned
env.filters['simplify_type'] = format_simplify_type
env.filters['stringformat'] = format_stringformat
env.filters['docutils'] = format_docutils
env.filters['doxylink'] = format_doxylink
env.filters['doxygrouplink'] = format_doxygrouplink
def preprocess_node(node):
setdefault(node, "attrs_name", node.name.lower())
......@@ -290,14 +357,19 @@ def prepare_nodes(nodes):
return (real_nodes, abstract_nodes)
def main(argv):
if len(argv) < 3:
print("usage: %s specfile templatefile" % argv[0])
sys.exit(1)
specfile = argv[1]
templatefile = argv[2]
spec = load_spec(specfile)
parser = argparse.ArgumentParser(description='Generate code/docu from node specification', add_help=True)
parser.add_argument('--tagfile', dest='tagfile', action='store',
help='doxygen tag file for link generation')
parser.add_argument('-I', dest='includedirs', action='store', nargs='*',
default=[],
help='include directories for template require directives')
parser.add_argument('specfile', action='store',
help='node specification file')
parser.add_argument('templatefile', action='store',
help='jinja2 template file')
config = parser.parse_args()
spec = load_spec(config.specfile)
(nodes, abstract_nodes) = prepare_nodes(spec.nodes)
env.globals['nodes'] = nodes
......@@ -306,8 +378,10 @@ def main(argv):
env.globals['len'] = len
env.globals['hasattr'] = hasattr
env.globals['is_dynamic_pinned'] = is_dynamic_pinned
env.globals['time'] = datetime.now().replace(microsecond=0).isoformat(' ')
env.globals['warning'] = "/* Warning: automatically generated file */"
template = env.get_template(templatefile)
loader.includedirs += config.includedirs
template = env.get_template(config.templatefile)
result = template.render()
sys.stdout.write(result)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment