From 836259072ee674bcadb57ae0dcb23ab7db6ae211 Mon Sep 17 00:00:00 2001 From: Shaun McCance Date: Tue, 23 Aug 2011 10:43:34 -0400 Subject: g-ir-doc-tool: Added MallardFormatter class --- giscanner/mallard-C-class.tmpl | 2 +- giscanner/mallard-C-default.tmpl | 2 +- giscanner/mallard-C-function.tmpl | 24 ++++++- giscanner/mallard-C-method.tmpl | 12 ++-- giscanner/mallard-C-property.tmpl | 6 +- giscanner/mallard-C-record.tmpl | 3 +- giscanner/mallard-C-signal.tmpl | 6 +- giscanner/mallard-Python-class.tmpl | 2 +- giscanner/mallard-Python-default.tmpl | 3 +- giscanner/mallard-Python-function.tmpl | 3 +- giscanner/mallard-Python-method.tmpl | 3 +- giscanner/mallard-Python-property.tmpl | 6 +- giscanner/mallard-Python-signal.tmpl | 6 +- giscanner/mallardwriter.py | 116 ++++++++++++++++++++------------- 14 files changed, 122 insertions(+), 72 deletions(-) diff --git a/giscanner/mallard-C-class.tmpl b/giscanner/mallard-C-class.tmpl index 935146a5..9ca3fece 100644 --- a/giscanner/mallard-C-class.tmpl +++ b/giscanner/mallard-C-class.tmpl @@ -9,7 +9,7 @@ ${node.ctype} -${format(node.doc)} +${formatter.format(node.doc)} Hierarchy diff --git a/giscanner/mallard-C-default.tmpl b/giscanner/mallard-C-default.tmpl index 3b6cb267..a30d10e7 100644 --- a/giscanner/mallard-C-default.tmpl +++ b/giscanner/mallard-C-default.tmpl @@ -7,4 +7,4 @@ ${node.namespace.name}${node.name} -${format(node.doc)} +${formatter.format(node.doc)} diff --git a/giscanner/mallard-C-function.tmpl b/giscanner/mallard-C-function.tmpl index 8fcd30a4..fd87a83a 100644 --- a/giscanner/mallard-C-function.tmpl +++ b/giscanner/mallard-C-function.tmpl @@ -12,4 +12,26 @@ ${node.symbol} -${format(node.doc)} + +${formatter.format_type(node.retval.type)} ${node.symbol} (\ +% if len(node.parameters) == 0: +); +% endif +% for arg, ix in zip(node.parameters, range(len(node.parameters))): +% if ix != 0: +${' ' * (len(formatter.format_type(node.retval.type)) + len(node.symbol) + 3)}\ +% endif +% if arg.type.ctype == '': +...\ +% else: +${formatter.format_type(arg.type)} ${arg.argname}\ +% endif +% if ix == len(node.parameters) - 1: +); +% else: +, +%endif +% endfor + +${formatter.format(node.doc)} + diff --git a/giscanner/mallard-C-method.tmpl b/giscanner/mallard-C-method.tmpl index 47f1051f..e80931f1 100644 --- a/giscanner/mallard-C-method.tmpl +++ b/giscanner/mallard-C-method.tmpl @@ -9,7 +9,7 @@ - ${node.retval.type.ctype} + ${formatter.format_type(node.retval.type)} ${node.symbol} @@ -21,7 +21,7 @@ % else: - ${arg.type.ctype} + ${formatter.format_type(arg.type)} ${arg.argname} % endif @@ -30,18 +30,18 @@ ${node.symbol} -${node.retval.type.ctype} ${node.symbol} (${node.parent.ctype} *self\ +${formatter.format_type(node.retval.type)} ${node.symbol} (${node.parent.ctype} *self\ % if len(node.parameters) == 0: ); % else: , % endif % for arg, ix in zip(node.parameters, range(len(node.parameters))): -${' ' * (len(node.retval.type.ctype) + len(node.symbol) + 3)}\ +${' ' * (len(formatter.format_type(node.retval.type)) + len(node.symbol) + 3)}\ % if arg.type.ctype == '': ...\ % else: -${arg.type.ctype} ${arg.argname}\ +${formatter.format_type(arg.type)} ${arg.argname}\ % endif % if ix == len(node.parameters) - 1: ); @@ -50,4 +50,4 @@ ${arg.type.ctype} ${arg.argname}\ %endif % endfor -${format(node.doc)} +${formatter.format(node.doc)} diff --git a/giscanner/mallard-C-property.tmpl b/giscanner/mallard-C-property.tmpl index 84f33c68..2d37ba10 100644 --- a/giscanner/mallard-C-property.tmpl +++ b/giscanner/mallard-C-property.tmpl @@ -5,9 +5,9 @@ xmlns="http://projectmallard.org/1.0/" xmlns:ui="http://projectmallard.org/experimental/ui/"> - + ${node.name} - ${parent.ctype}:${node.name} -${format(node.doc)} + ${node.parent.ctype}:${node.name} +${formatter.format(node.doc)} diff --git a/giscanner/mallard-C-record.tmpl b/giscanner/mallard-C-record.tmpl index e6424fd7..a173e77a 100644 --- a/giscanner/mallard-C-record.tmpl +++ b/giscanner/mallard-C-record.tmpl @@ -8,4 +8,5 @@ ${node.namespace.name}${node.name} -${format(node.doc)} +${formatter.format(node.doc)} + diff --git a/giscanner/mallard-C-signal.tmpl b/giscanner/mallard-C-signal.tmpl index 08fc7a99..7aae3ae4 100644 --- a/giscanner/mallard-C-signal.tmpl +++ b/giscanner/mallard-C-signal.tmpl @@ -5,9 +5,9 @@ xmlns="http://projectmallard.org/1.0/" xmlns:ui="http://projectmallard.org/experimental/ui/"> - + ${node.name} - ${parent.ctype}::${node.name} -${format(node.doc)} + ${node.parent.ctype}::${node.name} +${formatter.format(node.doc)} diff --git a/giscanner/mallard-Python-class.tmpl b/giscanner/mallard-Python-class.tmpl index bd283ff6..6803dfbb 100644 --- a/giscanner/mallard-Python-class.tmpl +++ b/giscanner/mallard-Python-class.tmpl @@ -8,7 +8,7 @@ ${node.namespace.name}.${node.name} -${format(node.doc)} +${formatter.format(node.doc)} Hierarchy diff --git a/giscanner/mallard-Python-default.tmpl b/giscanner/mallard-Python-default.tmpl index 93409400..4a321e6b 100644 --- a/giscanner/mallard-Python-default.tmpl +++ b/giscanner/mallard-Python-default.tmpl @@ -7,4 +7,5 @@ ${node.namespace.name}.${node.name} -${format(node.doc)} +${formatter.format(node.doc)} + diff --git a/giscanner/mallard-Python-function.tmpl b/giscanner/mallard-Python-function.tmpl index 1f88001c..54d4a7aa 100644 --- a/giscanner/mallard-Python-function.tmpl +++ b/giscanner/mallard-Python-function.tmpl @@ -10,4 +10,5 @@ ${node.symbol} ${node.symbol} -${format(node.doc)} +${formatter.format(node.doc)} + diff --git a/giscanner/mallard-Python-method.tmpl b/giscanner/mallard-Python-method.tmpl index 45417251..feaade6b 100644 --- a/giscanner/mallard-Python-method.tmpl +++ b/giscanner/mallard-Python-method.tmpl @@ -9,4 +9,5 @@ ${node.symbol} -${format(node.doc)} +${formatter.format(node.doc)} + diff --git a/giscanner/mallard-Python-property.tmpl b/giscanner/mallard-Python-property.tmpl index 33b420e7..3570a7fe 100644 --- a/giscanner/mallard-Python-property.tmpl +++ b/giscanner/mallard-Python-property.tmpl @@ -5,9 +5,9 @@ xmlns="http://projectmallard.org/1.0/" xmlns:ui="http://projectmallard.org/experimental/ui/"> - + ${node.name} - ${namespace.name}.${parent.name}:${node.name} -${format(node.doc)} + ${namespace.name}.${node.parent.name}:${node.name} +${formatter.format(node.doc)} diff --git a/giscanner/mallard-Python-signal.tmpl b/giscanner/mallard-Python-signal.tmpl index 2ae7ae92..41c211a7 100644 --- a/giscanner/mallard-Python-signal.tmpl +++ b/giscanner/mallard-Python-signal.tmpl @@ -5,9 +5,9 @@ xmlns="http://projectmallard.org/1.0/" xmlns:ui="http://projectmallard.org/experimental/ui/"> - + ${node.name} - ${namespace.name}.${parent.name}::${node.name} -${format(node.doc)} + ${namespace.name}.${node.parent.name}::${node.name} +${formatter.format(node.doc)} diff --git a/giscanner/mallardwriter.py b/giscanner/mallardwriter.py index dbf76dd2..14922540 100644 --- a/giscanner/mallardwriter.py +++ b/giscanner/mallardwriter.py @@ -30,40 +30,59 @@ from mako.runtime import supports_caller from . import ast -def format(doc): - if doc is None: - return '' - - result = '' - for para in doc.split('\n\n'): - result += '

' - result += format_inline(para) - result += '

' - return result - -def escape(text): - return saxutils.escape(text.encode('utf-8')).decode('utf-8') - -def format_inline(para): - result = '' - - poss = [] - poss.append((para.find('#'), '#')) - poss = [pos for pos in poss if pos[0] >= 0] - poss.sort(cmp=lambda x, y: cmp(x[0], y[0])) - if len(poss) == 0: - result += escape(para) - elif poss[0][1] == '#': - pos = poss[0][0] - result += escape(para[:pos]) - rest = para[pos + 1:] - link = re.split('[^a-zA-Z_:-]', rest, maxsplit=1)[0] - xref = link #self.writer._xrefs.get(link, link) - result += '%s' % (xref, link) - if len(link) < len(rest): - result += format_inline(rest[len(link):]) - - return result +class MallardFormatter(object): + @classmethod + def escape(cls, text): + return saxutils.escape(text.encode('utf-8')).decode('utf-8') + + @classmethod + def format(cls, doc): + if doc is None: + return '' + + result = '' + for para in doc.split('\n\n'): + result += '

' + result += cls.format_inline(para) + result += '

' + return result + + @classmethod + def format_inline(cls, para): + result = '' + + poss = [] + poss.append((para.find('#'), '#')) + poss = [pos for pos in poss if pos[0] >= 0] + poss.sort(cmp=lambda x, y: cmp(x[0], y[0])) + if len(poss) == 0: + result += cls.escape(para) + elif poss[0][1] == '#': + pos = poss[0][0] + result += cls.escape(para[:pos]) + rest = para[pos + 1:] + link = re.split('[^a-zA-Z_:-]', rest, maxsplit=1)[0] + xref = link #self.writer._xrefs.get(link, link) + result += '%s' % (xref, link) + if len(link) < len(rest): + result += cls.format_inline(rest[len(link):]) + + return result + + @classmethod + def format_type(cls, type_): + raise NotImplementedError + +class MallardFormatterC(MallardFormatter): + @classmethod + def format_type(cls, type_): + if type_.ctype is not None: + return type_.ctype + else: + return type_.target_fundamental + +class MallardFormatterPython(MallardFormatter): + pass class MallardWriter(object): def __init__(self, transformer, language): @@ -79,14 +98,14 @@ class MallardWriter(object): self._render_node(node, output) if isinstance(node, (ast.Class, ast.Record)): for method in node.methods: - self._render_node(method, output, node) + self._render_node(method, output) if isinstance(node, ast.Class): for property_ in node.properties: - self._render_node(property_, output, node) + self._render_node(property_, output) for signal in node.signals: - self._render_node(signal, output, node) + self._render_node(signal, output) - def _render_node(self, node, output, parent=None): + def _render_node(self, node, output): namespace = self._transformer.namespace if isinstance(node, ast.Namespace): template_name = 'mallard-%s-namespace.tmpl' % self._language @@ -97,18 +116,18 @@ class MallardWriter(object): elif isinstance(node, ast.Record): template_name = 'mallard-%s-record.tmpl' % self._language page_id = '%s.%s' % (namespace.name, node.name) - elif isinstance(node, ast.Function) and parent is not None: + elif isinstance(node, ast.Function) and node.parent is not None: template_name = 'mallard-%s-method.tmpl' % self._language - page_id = '%s.%s.%s' % (namespace.name, parent.name, node.name) + page_id = '%s.%s.%s' % (namespace.name, node.parent.name, node.name) elif isinstance(node, ast.Function): template_name = 'mallard-%s-function.tmpl' % self._language page_id = '%s.%s' % (namespace.name, node.name) - elif isinstance(node, ast.Property) and parent is not None: + elif isinstance(node, ast.Property) and node.parent is not None: template_name = 'mallard-%s-property.tmpl' % self._language - page_id = '%s.%s-%s' % (namespace.name, parent.name, node.name) - elif isinstance(node, ast.Signal) and parent is not None: + page_id = '%s.%s-%s' % (namespace.name, node.parent.name, node.name) + elif isinstance(node, ast.Signal) and node.parent is not None: template_name = 'mallard-%s-signal.tmpl' % self._language - page_id = '%s.%s-%s' % (namespace.name, parent.name, node.name) + page_id = '%s.%s-%s' % (namespace.name, node.parent.name, node.name) else: template_name = 'mallard-%s-default.tmpl' % self._language page_id = '%s.%s' % (namespace.name, node.name) @@ -121,10 +140,15 @@ class MallardWriter(object): file_name = os.path.join(template_dir, template_name) template = Template(filename=file_name, output_encoding='utf-8') + if self._language == 'C': + formatter = MallardFormatterC + elif self._language == 'Python': + formatter = MallardFormatterPython + else: + formatter = MallardFormatter result = template.render(namespace=namespace, node=node, - format=format, - parent=parent) + formatter=formatter) output_file_name = os.path.join(os.path.dirname(output), page_id + '.page') -- cgit v1.2.1