[KSS-checkins] r44761 - kukit/docs/introducing_kss/trunk

jvloothuis at codespeak.net jvloothuis at codespeak.net
Thu Jul 5 23:16:04 CEST 2007


Author: jvloothuis
Date: Thu Jul  5 23:16:04 2007
New Revision: 44761

Added:
   kukit/docs/introducing_kss/trunk/rstxml2texinfo.py
Log:

Added an initial version of a convertor for restructured text xml
output to the texinfo format. This can be used to generate online
documentation for Emacs.


Added: kukit/docs/introducing_kss/trunk/rstxml2texinfo.py
==============================================================================
--- (empty file)
+++ kukit/docs/introducing_kss/trunk/rstxml2texinfo.py	Thu Jul  5 23:16:04 2007
@@ -0,0 +1,161 @@
+from elementtree import ElementTree
+from optparse import OptionParser
+import sys
+import os
+
+def main():
+    parser = OptionParser()
+    (options, args) = parser.parse_args()
+
+    tree = ElementTree.parse(args[0])
+    root = tree.getroot()
+
+    output_filename = os.path.splitext(os.path.basename(
+            root.attrib['source']))[0] + '.info'
+    output = sys.stdout
+
+    # Header
+    output.write('\n'.join(['\input texinfo   @c -*-texinfo-*-',
+                 '@c %**start of header',
+                 '@setfilename %s' % output_filename,
+                 '@settitle %s' % root.find('title').text,
+                 '@c %**end of header', '']))
+
+    output.write(create_frontpage(root))
+    output.write(create_sections(root, 0))
+
+    output.close()
+
+def create_menu(node):
+    sections = node.findall('section')
+    if len(sections) == 0:
+        return ''
+
+    output = []
+    output.append('@menu')
+    for section in sections:
+        output.append('* %s::' % section_title(section))
+    output.append('@end menu\n')
+    return '\n'.join(output)
+
+
+def create_frontpage(node):
+    output = []
+    first_section = node.find('section')
+    title = section_title(node)
+    output.append('@node Top, %s, (dir), (dir)\n' % section_title(first_section))
+    output.append('@top %s\n' % title)
+    output.append(create_menu(node))
+#     output.append('@node %s, %s, Top, Top\n' % (title, 
+#                                                 section_title(sections[0])))
+#     output.append('@unnumbered %s\n' % title)
+#     for paragraph in node.findall('paragraph'):
+#         output.append(paragraph.text + '\n\n')
+    return ''.join(output)
+
+
+section_levels = ['chapter', 'section', 'subsection', 'subsubsection']
+def create_sections(node, level, parent='Top'):
+    sections = node.findall('section')
+    output = []
+    for i, section in enumerate(sections):
+        title = section_title(section)
+
+        # Node and section creation
+        if i + 1 == len(sections):
+            next_title = ''
+        else:
+            next_title = section_title(sections[i + 1])
+
+        if i == 0:
+            previous_title = parent
+        else:
+            previous_title = section_title(sections[i - 1])
+
+        output.append('@node %s, %s, %s, %s\n' % (title, 
+                                                  next_title,
+                                                  previous_title,
+                                                  parent))
+        output.append('@%s %s\n' % (section_levels[level], title))
+
+        
+        output.append(create_menu(section))
+
+        # Page contents
+        for node in section.getchildren():
+            tag = node.tag
+            if tag in ['section', 'title']:
+                continue
+            output.append(dispatch(node))
+
+        output.append(create_sections(section, level + 1, parent=title))
+    return ''.join(output)
+
+def dispatch(node):
+    func = globals().get('create_' + node.tag, None)
+    if func is not None:
+        return func(node)
+    else:
+        raise node.tag
+    return ''
+
+def create_image(node):
+    return '@image{%s}\n\n' % os.path.splitext(node.get('uri'))[0]
+                      
+def create_paragraph(node):
+    text = node.text
+    if text is None:
+        text = ''
+    output = [escape(text)]
+    for child in node.getchildren():
+        output.append(dispatch(child))
+        output.append(child.tail)
+    return ''.join(output) + '\n\n'
+
+def create_strong(node):
+    return '@strong{%s}' % escape(node.text)
+
+def create_literal(node):
+    return '@samp{%s}' % escape(node.text)
+        
+def create_bullet_list(node):
+    output = []
+    output.append('@itemize @bullet')
+    for bullet_node in node.getchildren():
+        if bullet_node.tag == 'list_item':
+            output.append('@item')
+            for child in bullet_node.getchildren():
+                output.append(dispatch(child))
+        else:
+            output.append(dispatch(node))
+    output.append('@end itemize\n')
+    return '\n'.join(output)
+
+def create_literal_block(node):
+    output = ['@example']
+    output.append(escape(node.text))
+    output.append('@end example\n\n')
+    return '\n'.join(output)
+
+def create_block_quote(node):
+    output = ['@quotation']
+    for child in node.getchildren():
+        output.append(dispatch(child))
+    output.append('@end quotation\n\n')
+    return '\n'.join(output)
+    
+
+def section_title(section):
+    return section.find('title').text
+
+def escape(text, comma=False):
+    replacements = ['@', '{', '}']
+    if comma:
+        replacements.append(',')
+    
+    for replacement in replacements:
+        text = text.replace(replacement, '@' + replacement)
+    return text
+
+if __name__ == '__main__':
+    main()


More information about the Kukit-checkins mailing list