diff options
author | Shaun McCance <shaunm@gnome.org> | 2011-08-23 10:43:34 -0400 |
---|---|---|
committer | Tomeu Vizoso <tomeu.vizoso@collabora.com> | 2012-02-17 10:27:40 +0100 |
commit | 836259072ee674bcadb57ae0dcb23ab7db6ae211 (patch) | |
tree | 0235e87fb86cee696090e1904e92f8bc4ce6b5c9 /giscanner | |
parent | f2ab70164e5e72369fb6591670d3214d06f451bd (diff) | |
download | gobject-introspection-836259072ee674bcadb57ae0dcb23ab7db6ae211.tar.gz |
g-ir-doc-tool: Added MallardFormatter class
Diffstat (limited to 'giscanner')
-rw-r--r-- | giscanner/mallard-C-class.tmpl | 2 | ||||
-rw-r--r-- | giscanner/mallard-C-default.tmpl | 2 | ||||
-rw-r--r-- | giscanner/mallard-C-function.tmpl | 24 | ||||
-rw-r--r-- | giscanner/mallard-C-method.tmpl | 12 | ||||
-rw-r--r-- | giscanner/mallard-C-property.tmpl | 6 | ||||
-rw-r--r-- | giscanner/mallard-C-record.tmpl | 3 | ||||
-rw-r--r-- | giscanner/mallard-C-signal.tmpl | 6 | ||||
-rw-r--r-- | giscanner/mallard-Python-class.tmpl | 2 | ||||
-rw-r--r-- | giscanner/mallard-Python-default.tmpl | 3 | ||||
-rw-r--r-- | giscanner/mallard-Python-function.tmpl | 3 | ||||
-rw-r--r-- | giscanner/mallard-Python-method.tmpl | 3 | ||||
-rw-r--r-- | giscanner/mallard-Python-property.tmpl | 6 | ||||
-rw-r--r-- | giscanner/mallard-Python-signal.tmpl | 6 | ||||
-rw-r--r-- | 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 @@ <link type="guide" xref="index" group="class"/> </info> <title>${node.ctype}</title> -${format(node.doc)} +${formatter.format(node.doc)} <synopsis ui:expanded="no"> <title>Hierarchy</title> <tree> 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 @@ <info> </info> <title>${node.namespace.name}${node.name}</title> -${format(node.doc)}</page> +${formatter.format(node.doc)}</page> 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 @@ </api:function> </info> <title>${node.symbol}</title> -${format(node.doc)}</page> +<synopsis><code> +${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 == '<varargs>': +...\ +% else: +${formatter.format_type(arg.type)} ${arg.argname}\ +% endif +% if ix == len(node.parameters) - 1: +); +% else: +, +%endif +% endfor +</code></synopsis> +${formatter.format(node.doc)} +</page> 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 @@ <link type="guide" xref="${namespace.name}.${node.parent.name}" group="method"/> <api:function> <api:returns> - <api:type>${node.retval.type.ctype}</api:type> + <api:type>${formatter.format_type(node.retval.type)}</api:type> </api:returns> <api:name>${node.symbol}</api:name> <api:arg> @@ -21,7 +21,7 @@ <api:varargs/> % else: <api:arg> - <api:type>${arg.type.ctype}</api:type> + <api:type>${formatter.format_type(arg.type)}</api:type> <api:name>${arg.argname}</api:name> </api:arg> % endif @@ -30,18 +30,18 @@ </info> <title>${node.symbol}</title> <synopsis><code> -${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 == '<varargs>': ...\ % 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 </code></synopsis> -${format(node.doc)}</page> +${formatter.format(node.doc)}</page> 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/"> <info> - <link type="guide" xref="${namespace.name}.${parent.name}" group="property"/> + <link type="guide" xref="${namespace.name}.${node.parent.name}" group="property"/> <title type="link" role="topic">${node.name}</title> </info> - <title>${parent.ctype}:${node.name}</title> -${format(node.doc)} + <title>${node.parent.ctype}:${node.name}</title> +${formatter.format(node.doc)} </page> 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 @@ <link type="guide" xref="index"/> </info> <title>${node.namespace.name}${node.name}</title> -${format(node.doc)}</page> +${formatter.format(node.doc)} +</page> 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/"> <info> - <link type="guide" xref="${namespace.name}.${parent.name}" group="signal"/> + <link type="guide" xref="${namespace.name}.${node.parent.name}" group="signal"/> <title type="link" role="topic">${node.name}</title> </info> - <title>${parent.ctype}::${node.name}</title> -${format(node.doc)} + <title>${node.parent.ctype}::${node.name}</title> +${formatter.format(node.doc)} </page> 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 @@ <link type="guide" xref="index" group="class"/> </info> <title>${node.namespace.name}.${node.name}</title> -${format(node.doc)} +${formatter.format(node.doc)} <synopsis ui:expanded="no"> <title>Hierarchy</title> <tree> 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 @@ <info> </info> <title>${node.namespace.name}.${node.name}</title> -${format(node.doc)}</page> +${formatter.format(node.doc)} +</page> 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 @@ <c:identifier>${node.symbol}</c:identifier> </info> <title>${node.symbol}</title> -${format(node.doc)}</page> +${formatter.format(node.doc)} +</page> 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 @@ <link type="guide" xref="${namespace.name}.${node.parent.name}" group="method"/> </info> <title>${node.symbol}</title> -${format(node.doc)}</page> +${formatter.format(node.doc)} +</page> 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/"> <info> - <link type="guide" xref="${namespace.name}.${parent.name}" group="property"/> + <link type="guide" xref="${namespace.name}.${node.parent.name}" group="property"/> <title type="link" role="topic">${node.name}</title> </info> - <title>${namespace.name}.${parent.name}:${node.name}</title> -${format(node.doc)} + <title>${namespace.name}.${node.parent.name}:${node.name}</title> +${formatter.format(node.doc)} </page> 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/"> <info> - <link type="guide" xref="${namespace.name}.${parent.name}" group="signal"/> + <link type="guide" xref="${namespace.name}.${node.parent.name}" group="signal"/> <title type="link" role="topic">${node.name}</title> </info> - <title>${namespace.name}.${parent.name}::${node.name}</title> -${format(node.doc)} + <title>${namespace.name}.${node.parent.name}::${node.name}</title> +${formatter.format(node.doc)} </page> 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 += '<p>' - result += format_inline(para) - result += '</p>' - 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 += '<link xref="%s">%s</link>' % (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 += '<p>' + result += cls.format_inline(para) + result += '</p>' + 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 += '<link xref="%s">%s</link>' % (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') |