1 ############################################################################## 
   3 # Zope Public License (ZPL) Version 1.0 
   4 # ------------------------------------- 
   6 # Copyright (c) Digital Creations.  All rights reserved. 
   8 # This license has been certified as Open Source(tm). 
  10 # Redistribution and use in source and binary forms, with or without 
  11 # modification, are permitted provided that the following conditions are 
  14 # 1. Redistributions in source code must retain the above copyright 
  15 #    notice, this list of conditions, and the following disclaimer. 
  17 # 2. Redistributions in binary form must reproduce the above copyright 
  18 #    notice, this list of conditions, and the following disclaimer in 
  19 #    the documentation and/or other materials provided with the 
  22 # 3. Digital Creations requests that attribution be given to Zope 
  23 #    in any manner possible. Zope includes a "Powered by Zope" 
  24 #    button that is installed by default. While it is not a license 
  25 #    violation to remove this button, it is requested that the 
  26 #    attribution remain. A significant investment has been put 
  27 #    into Zope, and this effort will continue if the Zope community 
  28 #    continues to grow. This is one way to assure that growth. 
  30 # 4. All advertising materials and documentation mentioning 
  31 #    features derived from or use of this software must display 
  32 #    the following acknowledgement: 
  34 #      "This product includes software developed by Digital Creations 
  35 #      for use in the Z Object Publishing Environment 
  36 #      (http://www.zope.org/)." 
  38 #    In the event that the product being advertised includes an 
  39 #    intact Zope distribution (with copyright and license included) 
  40 #    then this clause is waived. 
  42 # 5. Names associated with Zope or Digital Creations must not be used to 
  43 #    endorse or promote products derived from this software without 
  44 #    prior written permission from Digital Creations. 
  46 # 6. Modified redistributions of any form whatsoever must retain 
  47 #    the following acknowledgment: 
  49 #      "This product includes software developed by Digital Creations 
  50 #      for use in the Z Object Publishing Environment 
  51 #      (http://www.zope.org/)." 
  53 #    Intact (re-)distributions of any official Zope release do not 
  54 #    require an external acknowledgement. 
  56 # 7. Modifications are encouraged but must be packaged separately as 
  57 #    patches to official Zope releases.  Distributions that do not 
  58 #    clearly separate the patches from the original work must be clearly 
  59 #    labeled as unofficial distributions.  Modifications which do not 
  60 #    carry the name Zope may be packaged in any form, as long as they 
  61 #    conform to all of the clauses above. 
  66 #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY 
  67 #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  68 #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  69 #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS 
  70 #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  71 #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
  72 #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
  73 #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
  74 #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
  75 #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
  76 #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  80 # This software consists of contributions made by Digital Creations and 
  81 # many individuals on behalf of Digital Creations.  Specific 
  82 # attributions are listed in the accompanying credits file. 
  84 ############################################################################## 
  87 from string 
import join
, split
, find
, lstrip
 
  93       'StructuredTextDocument': 'document', 
  94       'StructuredTextParagraph': 'paragraph', 
  95       'StructuredTextExample': 'example', 
  96       'StructuredTextBullet': 'bullet', 
  97       'StructuredTextNumbered': 'numbered', 
  98       'StructuredTextDescription': 'description', 
  99       'StructuredTextDescriptionTitle': 'descriptionTitle', 
 100       'StructuredTextDescriptionBody': 'descriptionBody', 
 101       'StructuredTextSection': 'section', 
 102       'StructuredTextSectionTitle': 'sectionTitle', 
 103       'StructuredTextLiteral': 'literal', 
 104       'StructuredTextEmphasis': 'emphasis', 
 105       'StructuredTextStrong': 'strong', 
 106       'StructuredTextLink': 'link', 
 107       'StructuredTextXref': 'xref', 
 108       'StructuredTextSGML': 'sgml', 
 111    def dispatch(self
, doc
, level
, output
): 
 112       getattr(self
, self
.element_types
[doc
.getNodeName()])(doc
, level
, output
) 
 114    def __call__(self
, doc
, level
=1): 
 116       self
.dispatch(doc
, level
-1, r
.append
) 
 119    def _text(self
, doc
, level
, output
): 
 120       if doc
.getNodeName() == 'StructuredTextLiteral': 
 121          output(doc
.getNodeValue()) 
 123          output(lstrip(doc
.getNodeValue()))          
 125    def document(self
, doc
, level
, output
): 
 126       output('<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n') 
 128       children
=doc
.getChildNodes() 
 130           children
[0].getNodeName() == 'StructuredTextSection'): 
 131          output('<title>%s</title>' % children
[0].getChildNodes()[0].getNodeValue()) 
 133          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 136    def section(self
, doc
, level
, output
): 
 137       output('\n<section>\n') 
 138       children
=doc
.getChildNodes() 
 140          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
+1, output
) 
 141       output('\n</section>\n') 
 143    def sectionTitle(self
, doc
, level
, output
): 
 145       for c 
in doc
.getChildNodes(): 
 147             getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 149             print "failed", c
.getNodeName(), c
 
 152    def description(self
, doc
, level
, output
): 
 153       p
=doc
.getPreviousSibling() 
 154       if p 
is None or  p
.getNodeName() is not doc
.getNodeName():          
 155          output('<variablelist>\n') 
 156       for c 
in doc
.getChildNodes(): 
 157          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 158       n
=doc
.getNextSibling() 
 159       if n 
is None or n
.getNodeName() is not doc
.getNodeName():          
 160          output('</variablelist>\n') 
 162    def descriptionTitle(self
, doc
, level
, output
): 
 163       output('<varlistentry><term>\n') 
 164       for c 
in doc
.getChildNodes(): 
 165          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 168    def descriptionBody(self
, doc
, level
, output
): 
 169       output('<listitem><para>\n') 
 170       for c 
in doc
.getChildNodes(): 
 171          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 172       output('</para></listitem>\n') 
 173       output('</varlistentry>\n') 
 175    def bullet(self
, doc
, level
, output
): 
 176       p
=doc
.getPreviousSibling() 
 177       if p 
is None or p
.getNodeName() is not doc
.getNodeName():          
 178          output('<itemizedlist>\n') 
 179       output('<listitem><para>\n') 
 181       for c 
in doc
.getChildNodes(): 
 182          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 183       n
=doc
.getNextSibling() 
 184       output('</para></listitem>\n') 
 185       if n 
is None or n
.getNodeName() is not doc
.getNodeName():          
 186          output('</itemizedlist>\n') 
 188    def numbered(self
, doc
, level
, output
): 
 189       p
=doc
.getPreviousSibling() 
 190       if p 
is None or p
.getNodeName() is not doc
.getNodeName():          
 191          output('<orderedlist>\n') 
 192       output('<listitem><para>\n') 
 193       for c 
in doc
.getChildNodes(): 
 194          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 195       n
=doc
.getNextSibling() 
 196       output('</para></listitem>\n') 
 197       if n 
is None or n
.getNodeName() is not doc
.getNodeName():          
 198          output('</orderedlist>\n') 
 200    def example(self
, doc
, level
, output
): 
 202       for c 
in doc
.getChildNodes(): 
 204             output('<programlisting>\n<![CDATA[\n') 
 206             ## eek.  A ']]>' in your body will break this... 
 208             output(prestrip(c
.getNodeValue())) 
 209             output('\n]]></programlisting>\n') 
 211             getattr(self
, self
.element_types
[c
.getNodeName()])( 
 214    def paragraph(self
, doc
, level
, output
): 
 216       for c 
in doc
.getChildNodes(): 
 217          getattr(self
, self
.element_types
[c
.getNodeName()])( 
 219       output('</para>\n\n') 
 221    def link(self
, doc
, level
, output
): 
 222       output('<ulink url="%s">' % doc
.href
) 
 223       for c 
in doc
.getChildNodes(): 
 224          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 227    def emphasis(self
, doc
, level
, output
): 
 229       for c 
in doc
.getChildNodes(): 
 230          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 231       output('</emphasis> ') 
 233    def literal(self
, doc
, level
, output
): 
 235       for c 
in doc
.getChildNodes(): 
 236          output(c
.getNodeValue()) 
 239    def strong(self
, doc
, level
, output
): 
 241       for c 
in doc
.getChildNodes(): 
 242          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 243       output('</emphasis>') 
 245    def xref(self
, doc
, level
, output
): 
 246       output('<xref linkend="%s"/>' % doc
.getNodeValue()) 
 248    def sgml(self
, doc
, level
, output
): 
 249       output(doc
.getNodeValue()) 
 253    v
=string
.replace(v
, '\r\n', '\n') 
 254    v
=string
.replace(v
, '\r', '\n') 
 255    v
=string
.replace(v
, '\t', '        ') 
 256    lines
=string
.split(v
, '\n') 
 259       if not len(line
): continue 
 260       i
=len(line
)-len(string
.lstrip(line
)) 
 265       nlines
.append(line
[indent
:]) 
 266    return string
.join(nlines
, '\n') 
 269 class DocBookChapter(DocBookClass
): 
 271    def document(self
, doc
, level
, output
): 
 272       output('<chapter>\n') 
 273       children
=doc
.getChildNodes() 
 275           children
[0].getNodeName() == 'StructuredTextSection'): 
 276          output('<title>%s</title>' % children
[0].getChildNodes()[0].getNodeValue()) 
 277       for c 
in children
[0].getChildNodes()[1:]: 
 278          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 279       output('</chapter>\n') 
 281 ets 
= DocBookClass
.element_types
 
 282 ets
.update({'StructuredTextImage': 'image'}
)       
 284 class DocBookChapterWithFigures(DocBookChapter
): 
 288     def image(self
, doc
, level
, output
): 
 289        if hasattr(doc
, 'key'): 
 290           output('<figure id="%s"><title>%s</title>\n' % (doc
.key
, doc
.getNodeValue()) ) 
 292           output('<figure><title>%s</title>\n' % doc
.getNodeValue()) 
 293 ##        for c in doc.getChildNodes(): 
 294 ##            getattr(self, self.element_types[c.getNodeName()])(c, level, output) 
 295        output('<graphic fileref="%s"></graphic>\n</figure>\n' % doc
.href
)     
 297 class DocBookArticle(DocBookClass
): 
 299    def document(self
, doc
, level
, output
): 
 300       output('<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n') 
 301       output('<article>\n') 
 302       children
=doc
.getChildNodes() 
 304           children
[0].getNodeName() == 'StructuredTextSection'): 
 305          output('<articleinfo>\n<title>%s</title>\n</articleinfo>\n' % 
 306                 children
[0].getChildNodes()[0].getNodeValue()) 
 308          getattr(self
, self
.element_types
[c
.getNodeName()])(c
, level
, output
) 
 309       output('</article>\n') 
 314    def __init__(self
, title
=''): 
 318    def addChapter(self
, chapter
): 
 319       self
.chapters
.append(chapter
) 
 322       out 
= '<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n<book>\n' 
 323       out 
= out 
+ '<title>%s</title>\n' % self
.title
 
 324       for chapter 
in self
.chapters
: 
 325          out 
= out 
+ chapter 
+ '\n</book>\n'