[gobject-introspection/mallard-templates] g-ir-doc-tool: Added MallardFormatter class



commit 6272bae202ac7d51b3e458c5da7504bc325eee95
Author: Shaun McCance <shaunm gnome org>
Date:   Tue Aug 23 10:43:34 2011 -0400

    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 935146a..9ca3fec 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 3b6cb26..a30d10e 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 8fcd30a..fd87a83 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 47f1051..e80931f 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 84f33c6..2d37ba1 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 e6424fd..a173e77 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 08fc7a9..7aae3ae 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 bd283ff..6803dfb 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 9340940..4a321e6 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 1f88001..54d4a7a 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 4541725..feaade6 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 33b420e..3570a7f 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 2ae7ae9..41c211a 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 dbf76dd..1492254 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')



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]