[Twisted-Python] BookLatexSpitter

Cory Dodt corydodt at yahoo.com
Fri Feb 14 21:09:34 EST 2003


The following patch is my first step toward engineering out book.tex.  It
will work with howto/index.html with minor modifications to that file,
which I have made.  Check out my sample book and the modified index.html
at:

http://twistedmatrix.com/users/moonfallen/sample-book.zip





Index: default.py
===================================================================
RCS file: /cvs/Twisted/twisted/lore/default.py,v
retrieving revision 1.6
diff -u -r1.6 default.py
--- default.py	10 Feb 2003 00:39:07 -0000	1.6
+++ default.py	14 Feb 2003 17:51:05 -0000
@@ -43,7 +43,9 @@
 
     latexSpitters = {None: latex.LatexSpitter,
                      'section': latex.SectionLatexSpitter,
-                     'chapter': latex.ChapterLatexSpitter}
+                     'chapter': latex.ChapterLatexSpitter,
+                     'book': latex.BookLatexSpitter,
+                     }
 
     def generate_latex(self, d):
         spitter = self.latexSpitters[None]
Index: latex.py
===================================================================
RCS file: /cvs/Twisted/twisted/lore/latex.py,v
retrieving revision 1.85
diff -u -r1.85 latex.py
--- latex.py	12 Feb 2003 11:18:51 -0000	1.85
+++ latex.py	14 Feb 2003 17:51:05 -0000
@@ -309,6 +309,62 @@
         self.visitNodeDefault(node)
         self.start_p = LatexSpitter.start_p
 
+class BookLatexSpitter(LatexSpitter):
+    def visitNode_link(self, node):
+        if not node.hasAttribute('rel'):
+            return self.visitNodeDefault(node)
+        node.tagName += '_'+node.getAttribute('rel')
+        self.visitNode(node)
+       
+    def visitNode_link_author(self, node):
+        self.writer('\\author{%s}\n' % node.getAttribute('text'))
+
+    def visitNode_link_stylesheet(self, node):
+        if node.hasAttribute('type') and node.hasAttribute('href'):
+            if node.getAttribute('type')=='application/x-latex':
+                packagename=node.getAttribute('href')
+                packagebase,ext=os.path.splitext(packagename)
+                self.writer('\\usepackage{%s}\n' % packagebase)
+
+    start_html = r'''\documentclass[oneside]{book}
+\usepackage{graphicx}
+\usepackage{times,mathptmx}
+'''
+    
+    start_body = r'''\begin{document}
+\maketitle
+\tableofcontents
+'''
+
+    start_li=''
+    end_li=''
+    start_ul=''
+    end_ul=''
+
+
+    def visitNode_a(self, node):
+        if node.hasAttribute('class'):
+            a_class=node.getAttribute('class')
+            if a_class.endswith('listing'):
+                return self.visitNode_a_listing(node)
+            else:
+                return getattr(self, 'visitNode_a_%s' % a_class)(node)
+        if node.hasAttribute('href'):
+            return self.visitNode_a_href(node)
+        if node.hasAttribute('name'):
+            return self.visitNode_a_name(node)
+        self.visitNodeDefault(node)
+
+    def visitNode_a_chapter(self, node):
+        self.writer('\\chapter{')
+        self.visitNodeDefault(node)
+        self.writer('}\n')
+        
+    def visitNode_a_sect(self, node):
+        base,ext=os.path.splitext(node.getAttribute('href'))
+        self.writer('\\input{%s}\n' % base)
+        
+        
 
 def processFile(spitter, fin):
     dom = microdom.parse(fin).documentElement


__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com




More information about the Twisted-Python mailing list