"Fossies" - the Fresh Open Source Software Archive 
Member "Tahchee-1.0.0/Sources/tahchee/plugins/_kiwi/main.py" (22 Oct 2009, 10025 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 "main.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 # License : Revised BSD License
9 # -----------------------------------------------------------------------------
10 # Creation date : 19-Nov-2003
11 # Last mod. : 26-Jul-2008
12 # -----------------------------------------------------------------------------
13
14 import os, sys, StringIO
15
16 __doc__ = """Kiwi is an advanced markup text processor, which can be used as
17 an embedded processor in any application. It is fast, extensible and outputs an
18 XML DOM."""
19
20 __version__ = "0.8.6"
21 __pychecker__ = "blacklist=cDomlette,cDomlettec"
22
23 import re, string, operator, getopt, codecs
24
25 # NOTE: I disabled 4Suite support, as minidom is good as it is right now
26 # We use 4Suite domlette
27 #import Ft.Xml.Domlette
28 #dom = Ft.Xml.Domlette.implementation
29 # We use minidom implementation
30 import xml.dom.minidom
31 dom = xml.dom.minidom.getDOMImplementation()
32
33 import core, kiwi2html, kiwi2lout, kiwi2twiki
34
35 FORMATS = {
36 "html":kiwi2html,
37 # "lout":kiwi2lout,
38 "twiki":kiwi2twiki
39 }
40
41 #------------------------------------------------------------------------------
42 #
43 # Command-line interface
44 #
45 #------------------------------------------------------------------------------
46
47 USAGE = u"Kiwi v."+__version__+u""",
48 A flexible tool for converting plain text markup to XML and HTML.
49 Kiwi can be used to easily generate documentation from plain files or to
50 convert exiting Wiki markup to other formats.
51
52 See <http://www.ivy.fr/kiwi>
53
54 Usage: kiwi [options] source [destination]
55
56 source:
57 The text file to be parsed (usually an .stx file, "-" for stdin)
58 destination:
59 The optional destination file (otherwise result is dumped on stdout)
60
61 Options:
62
63 -i --input-encoding Allows to specify the input encoding
64 -o --output-encoding Allows to specify the output encoding
65 -t --tab The value for tabs (tabs equal N sapces).
66 Set to 4 by default.
67 -f --offsets Add offsets information
68 -p --pretty Pretty prints the output XML, this should only
69 be used for viewing the output.
70 -m --html Outputs an HTML file corresponding to the Kiwi
71 document
72 --no-style Does not include the default CSS in the HTML
73 --body-only Only returns the content of the <body< element
74 --level=n If n>0, n will transform HTML h1 to h2, etc...
75 -O --output-format Specifies and alternate output FORMAT
76 (see below)
77
78 The available encodings are %s
79 The available formats are %s
80
81 Misc:
82 -h, --help prints this help.
83 -v, --version prints the version of Kiwi.
84 """
85
86 # Error codes
87
88 ERROR = -1
89 INFO = 0
90 SUCCESS = 1
91
92 # Normalised encodings
93
94 ASCII = "us-ascii"
95 LATIN1 = "iso-8859-1"
96 LATIN2 = "iso-8859-2"
97 UTF8 = "utf-8"
98 UTF16 = "utf-16"
99 MACROMAN = "macroman"
100 NORMALISED_ENCODINGS = (LATIN1, LATIN2, UTF8, UTF16, MACROMAN)
101
102 # Supported encodings
103
104 ENCODINGS = {
105 ASCII:ASCII, "usascii":ASCII, "plain":ASCII, "text":ASCII, "ascii":ASCII,
106 LATIN1:LATIN1, "latin-1":LATIN1, "latin1":LATIN1, "iso8859":LATIN1,
107 "iso8859-1":LATIN1, "iso88591":LATIN1, "iso-88591":LATIN1,
108 LATIN2:LATIN2, "latin2":LATIN2, "latin-2":LATIN2, "iso8859-2":LATIN2,
109 "iso88592":LATIN2, "iso-88592":LATIN2,
110 UTF8:UTF8, "utf8":UTF8,
111 UTF16:UTF16, "utf16":UTF16,
112 MACROMAN:MACROMAN, "mac-roman":MACROMAN
113 }
114
115 def run( arguments, input=None, noOutput=False ):
116 """Returns a couple (STATUS, VALUE), where status is 1 when OK, 0 when
117 informative, and -1 when error, and value is a string.
118
119 The given arguments can be either a string or an array, the input can be
120 None (it will be then taken from the arguments), or be a file-like object,
121 and the noOutput flag will not output the result on stdout or whatever file
122 is given on the command line.
123 """
124 if type(arguments) == str: arguments = arguments.split()
125
126 # --We extract the arguments
127 try:
128 optlist, args = getopt.getopt(arguments, "hpmfO:vi:o:t:",\
129 ["input-encoding=", "output-encoding=", "output-format=",
130 "offsets", "help", "html", "tab=", "version",
131 "pretty", "no-style", "nostyle",
132 "body-only", "bodyonly", "level="])
133 except:
134 args=[]
135 optlist = []
136
137 # We get the list of available encodings
138 available_enc = []
139 ENCODINGS_LIST=""
140 for encoding in NORMALISED_ENCODINGS:
141 try:
142 codecs.lookup(encoding)
143 available_enc.append(encoding)
144 ENCODINGS_LIST+=encoding+", "
145 except:
146 pass
147 ENCODINGS_LIST=ENCODINGS_LIST[:-2]+"."
148
149 usage = USAGE % (ENCODINGS_LIST, ", ".join(FORMATS.keys()))
150
151 # We set attributes
152 pretty_print = 0
153 show_offsets = False
154 validate_output = 0
155 generate_html = 1
156 no_style = 0
157 body_only = 0
158 level_offset = 0
159 input_enc = ASCII
160 output_enc = ASCII
161 output_format = "html"
162 if LATIN1 in ENCODINGS:
163 input_enc = LATIN1
164 output_enc = LATIN1
165 elif UTF8 in ENCODINGS:
166 input_enc = UTF8
167 output_enc = UTF8
168
169 # We parse the options
170 for opt, arg in optlist:
171 if opt in ('-h', '--help'):
172 return (INFO, usage.encode(LATIN1))
173 elif opt in ('-v', '--version'):
174 return (INFO, __version__)
175 elif opt in ('-i', '--input-encoding'):
176 arg = string.lower(arg)
177 if arg in ENCODINGS.keys() and ENCODINGS[arg] in available_enc:
178 input_enc=output_enc=ENCODINGS[arg]
179 else:
180 r = "Kiwi error : Specified input encoding is not available, choose between:"
181 r += ENCODINGS_LIST
182 return (ERROR, r)
183 elif opt in ('-o', '--output-encoding'):
184 arg = string.lower(arg)
185 if arg in ENCODINGS.keys() and ENCODINGS[arg] in available_enc:
186 output_enc=ENCODINGS[arg]
187 else:
188 r = "Kiwi error: Specified output encoding is not available, choose between:"
189 r += ENCODINGS_LIST
190 return (ERROR, r)
191 elif opt in ('-O', '--output-format'):
192 arg = string.lower(arg)
193 if arg in FORMATS.keys():
194 output_format=arg
195 else:
196 r = "Kiwi error: Given format (%s) not supported. Choose one of:\n" % (arg)
197 r += "\n - ".join(FORMATS)
198 return (ERROR, r)
199 elif opt in ('-t', '--tab'):
200 TAB_SIZE = int(arg)
201 if TAB_SIZE<1:
202 return (ERROR, "Kiwi error: Specified tab value (%s) should be superior to 0." %\
203 (TAB_SIZE))
204 elif opt in ('--no-style', "--nostyle"):
205 no_style = 1
206 generate_html = 1
207 pretty_print = 0
208 elif opt in ('--body-only', "--bodyonly"):
209 no_style = 1
210 body_only = 1
211 generate_html = 1
212 pretty_print = 0
213 elif opt in ('-p', '--pretty'):
214 pretty_print = 1
215 generate_html = 0
216 elif opt in ('-m', '--html'):
217 generate_html = 1
218 output_format = "html"
219 pretty_print = 0
220 elif opt in ('-f', '--offsets'):
221 show_offsets = True
222 elif opt in ('--level'):
223 level_offset = min(10, max(0, int(arg)))
224
225 # We check the arguments
226 if input==None and len(args)<1:
227 return (INFO, usage.encode("iso-8859-1"))
228
229 # We set default values
230 if input == None: source = args[0]
231 else: source = None
232 output = None
233 if len(args)>1: output = args[1]
234
235 #sys.stderr.write("Kiwi started with input as %s and output as %s.\n"\
236 #% (input_enc, output_enc))
237 if input: base_dir = os.getcwd()
238 elif source=='-': base_dir = os.path.abspath(".")
239 else: base_dir = os.path.abspath(os.path.dirname(source))
240
241 parser = core.Parser(base_dir, input_enc, output_enc)
242
243 if source == output and not noOutput:
244 return(ERROR, "Cannot overwrite the source file.")
245
246 # We open the input file, taking care of stdin
247 if input != None:
248 ifile = input
249 elif source=="-":
250 ifile = sys.stdin
251 else:
252 try:
253 ifile = codecs.open(source,"r",input_enc)
254 except:
255 return (ERROR, "Unable to open input file: %s" % (input))
256
257 if noOutput: pass
258 elif output==None: ofile = sys.stdout
259 else: ofile = open(output,"w")
260
261 try:
262 data = ifile.read()
263 except UnicodeDecodeError, e:
264 r = "Impossible to decode input %s as %s\n" % (source, input_enc)
265 r += "--> %s\n" % (e)
266 return (ERROR, r)
267
268 if source!="-": ifile.close()
269
270 if type(data) != unicode:
271 data = data.decode(input_enc)
272 xml_document = parser.parse(data, offsets=show_offsets)
273
274 result = None
275 if generate_html:
276 variables = {}
277 variables["LEVEL"] = level_offset
278 css_file = file(os.path.join(os.path.dirname(kiwi2html.__file__), "screen-kiwi.css"))
279 if not no_style:
280 variables["HEADER"] = "\n<style><!-- \n%s --></style>" % (css_file.read())
281 variables["ENCODING"] = output_enc
282 css_file.close()
283 result = FORMATS[output_format].processor.generate(xml_document, body_only, variables)
284 if result: result = result.encode(output_enc)
285 else: result = ""
286 if not noOutput: ofile.write(result)
287 elif pretty_print:
288 #Ft.Xml.Lib.Print.PrettyPrint(xml_document, ofile, output_enc)
289 #MiniDom:
290 result = xml_document.toprettyxml(" ").encode(output_enc)
291 if not noOutput: ofile.write(result)
292 else:
293 #Ft.Xml.Lib.Print.Print(xml_document, ofile, output_enc)
294 #MiniDom:
295 result = xml_document.toxml().encode(output_enc)
296 if not noOutput: ofile.write(result)
297 return (SUCCESS, result)
298
299 def text2htmlbody( text, inputEncoding=None, outputEncoding=None ):
300 """Converts the given text to HTML, returning only the body."""
301 s = StringIO.StringIO(text)
302 command = "-m --body-only"
303 if inputEncoding: command += " -i " + inputEncoding
304 if outputEncoding: command += " -o " + outputEncoding
305 _, text = run(command + " --", s, noOutput=True)
306 s.close()
307 return text
308
309 def runAsCommand():
310 status, result = run(sys.argv[1:])
311 if status == ERROR:
312 sys.stderr.write(result + "\n")
313 sys.exit(-1)
314 elif status == INFO:
315 sys.stdout.write(result + "\n")
316 sys.exit(0)
317
318 if __name__ == "__main__":
319 runAsCommand()
320
321 # EOF