Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

g-ir-doc-tool: Added MallardFormatter class

  • Loading branch information...
commit 836259072ee674bcadb57ae0dcb23ab7db6ae211 1 parent f2ab701
@shaunix shaunix authored tomeuv committed
View
2  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>
View
2  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>
View
24 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>
View
12 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>
View
6 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>
View
3  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>
View
6 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>
View
2  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>
View
3  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>
View
3  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>
View
3  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>
View
6 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>
View
6 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>
View
116 giscanner/mallardwriter.py
@@ -30,40 +30,59 @@
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 @@ def write(self, output):
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 @@ def _render_node(self, node, output, parent=None):
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 @@ def _render_node(self, node, output, parent=None):
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')
Please sign in to comment.
Something went wrong with that request. Please try again.