"Fossies" - the Fresh Open Source Software Archive 
Member "Tahchee-1.0.0/Sources/tahchee/plugins/_kiwi/kiwi2lout.py" (22 Oct 2009, 7345 Bytes) of package /linux/privat/old/tahchee-1.0.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "kiwi2lout.py" see the
Fossies "Dox" file reference documentation.
1 #!/usr/bin/env python
2 # Encoding: iso-8859-1
3 # vim: tw=80 ts=4 sw=4 noet
4 # -----------------------------------------------------------------------------
5 # Project : Kiwi
6 # -----------------------------------------------------------------------------
7 # Author : Sebastien Pierre <sebastien@type-z.org>
8 # -----------------------------------------------------------------------------
9 # Creation date : 17-Aug-2006
10 # Last mod. : 17-Aug-2006
11 # -----------------------------------------------------------------------------
12
13 import re, xml.dom
14 import sys
15 from formatting import *
16 import templates
17
18 #------------------------------------------------------------------------------
19 #
20 # Processing functions
21 #
22 #------------------------------------------------------------------------------
23
24 class Processor(templates.Processor):
25
26 def generate( self, xmlDocument, bodyOnly=False, variables={} ):
27 node = xmlDocument.getElementsByTagName("Document")[0]
28 self.variables = variables
29 if bodyOnly:
30 for child in node.childNodes:
31 if child.nodeName == "Content":
32 return convertContent_bodyonly(child)
33 else:
34 return convertDocument(node)
35
36 #------------------------------------------------------------------------------
37 #
38 # Actual element processing
39 #
40 #------------------------------------------------------------------------------
41
42 def convertDocument(element):
43 return process(element, """\
44 @Include { doc }
45 @Document
46 @PageHeaders { Simple }
47 @FirstPageNumber { 1 }
48 @PageOrientation { Portrait }
49 //
50 @Text @Begin
51 $(Header)
52 $(Header:title)
53 @BeginSections
54 $(Content)
55 @EndSections
56 @End @Text
57 """)
58
59 def convertContent( element ):
60 return process(element, wdiv(element, """<div id='content'>$(*)</div>"""))
61
62 def convertContent_bodyonly( element ):
63 return process(element, wdiv(element, """$(*)"""))
64
65 def convertContent_table( element ):
66 return process(element, """<tbody%s>$(*)</tbody>""" % (wattrs(element)))
67
68 def convertHeader( element ):
69 return process(element, "<title%s>$(Title/title)</title>" % (wattrs(element)))
70
71 def convertHeading( element ):
72 return process(element, wspan(element, "$(*)"))
73
74 def convertSection( element ):
75 level = int(element.getAttributeNS(None, "_depth")) + 1
76 return process(element,
77 '<div class="section"><a class="link">'
78 + '<h%d class="heading">$(Heading)</h%d></a>' % (level, level)
79 + '<div class="level%d">$(Content:section)</div></div>' % (level)
80 )
81
82 def convertReferences( element ):
83 return process(element, """<div id="references">$(Entry)</div>""")
84
85 def convertEntry( element ):
86 return process(element, """<div class="entry"><div class="name"><a name="%s">%s</a></div><div class="content">$(*)</div></div>""" %
87 (element.getAttributeNS(None, "id"), element.getAttributeNS(None, "id")))
88
89 def convertHeader_title( element ):
90 return process(element, """<div
91 class="title">$(Title/title:header)$(Title/subtitle:header)</div>$(Meta)""")
92
93 def converttitle_header( element ):
94 return process(element, """<h1%s>$(*)</h1>""" % (wattrs(element)))
95
96 def convertsubtitle_header( element ):
97 return process(element, """<h2%s>$(*)</h2>""" % (wattrs(element)))
98
99 def convertParagraph( element ):
100 return process(element, """<p%s>$(*)</p>""" % (wattrs(element)))
101
102 def convertParagraph_cell( element ):
103 return process(element, """$(*)<br />""")
104
105 def convertList( element ):
106 return process(element, """<ul%s>$(*)</ul>""" % (wattrs(element)))
107
108 def convertListItem( element ):
109 return process(element, """<li%s>$(*)</li>""" % (wattrs(element)))
110
111 def convertTable( element ):
112 return process(element, """<table cellpadding="0" cellspacing="0" align="center">$(Caption)$(Content:table)</table>""")
113
114 def convertDefinition( element ):
115 return process(element, """<dl%s>$(*)</dl>""" % (wattrs(element)))
116
117 def convertDefinitionItem( element ):
118 return process(element, """<dt>$(Title)</dt><dd>$(Content)</dd>""")
119
120 def convertCaption( element ):
121 return process(element, """<caption%s>$(*)</caption>""" % (wattrs(element)))
122
123 def convertRow( element ):
124 try: index = element.parentNode.childNodes.index(element) % 2 + 1
125 except: index = 0
126 classes = ( "", "even", "odd" )
127 return process(element, """<tr class='%s'%s>$(*)</tr>""" % (classes[index], wattrs(element)))
128
129 def convertCell( element ):
130 return process(element, """<td%s>$(*:cell)</td>""" % (wattrs(element)))
131
132 def convertBlock( element ):
133 title = element.getAttributeNS(None,"title") or element.getAttributeNS(None, "type") or ""
134 css_class = ""
135 if title:
136 css_class=" class='ann%s'" % (element.getAttributeNS(None, "type").capitalize())
137 title = "<div class='title'>%s</div>" % (title.capitalize())
138 div_type = "div"
139 elif not element.getAttributeNS(None, "type"):
140 div_type = "blockquote"
141 return process(element, """<%s%s>%s<div class='content'%s>$(*)</div></%s>""" % (div_type, css_class, title, wattrs(element), div_type))
142
143 def convertlink( element ):
144 if element.getAttributeNS(None, "type") == "ref":
145 return process(element, """<a href="#%s">$(*)</a>""" %
146 (element.getAttributeNS(None, "target")))
147 else:
148 # TODO: Support title
149 return process(element, """<a href="%s">$(*)</a>""" %
150 (element.getAttributeNS(None, "target")))
151
152 def convertMeta( element ):
153 return process(element, "<table id='meta'>$(*)</table>")
154
155 def convertmeta( element ):
156 return process(element,
157 "<tr><td width='0px' class='name'>%s</td><td width='100%%' class='value'>$(*)</td></tr>" %
158 (element.getAttributeNS(None, "name")))
159
160 def convertemail( element ):
161 mail = ""
162 for c in process(element, """$(*)"""):
163 mail += "&#%d;" % (ord(c))
164 return """<a href="mailto:%s">%s</a>""" % (mail, mail)
165
166 def converturl( element ):
167 return process(element, """<a href="$(*)">$(*)</a>""")
168
169 def converturl_header( element ):
170 return process(element, """<div class='url'>%s</div>""" % (
171 converturl(element)))
172
173 def convertterm( element ):
174 return process(element, """<span class='term'>$(*)</span>""")
175
176 def convertquote( element ):
177 return process(element, """“<span class='quote'>$(*)</span>”""")
178
179 def convertcitation( element ):
180 return process(element, """«<span class='citation'>$(*)</span>»""")
181
182 def convertemphasis( element ):
183 return process(element, """<b>$(*)</b>""")
184
185 def convertstrong( element ):
186 return process(element, """<strong>$(*)</strong>""")
187
188 def convertpre( element ):
189 return process(element, """<pre%s>$(*)</pre>""" % (wattrs(element)))
190
191 def convertcode( element ):
192 return process(element, """<code>$(*)</code>""")
193
194 def convertbreak( element ):
195 return process(element, """<br />""")
196
197 def convertnewline( element ):
198 return process(element, """<br />""")
199
200 def convertarrow( element ):
201 arrow = element.getAttributeNS(None, "type")
202 if arrow == "left":
203 return "←"
204 elif arrow == "right":
205 return "→"
206 else:
207 return "↔"
208
209 def convertdots( element ):
210 return "…"
211
212 def convertendash( element ):
213 return "–"
214
215 def convertemdash( element ):
216 return "—"
217
218 def convertentity( element ):
219 return "&%s;" % (element.getAttributeNS( None, "num"))
220
221 # We create the processor, register the rules and define the process variable
222 processor = Processor()
223 name2functions = {}
224 for symbol in filter(lambda x:x.startswith("convert"), dir()):
225 name2functions[symbol] = eval(symbol)
226 processor.register(name2functions)
227 process = processor.process
228
229 # EOF