"Fossies" - the Fresh Open Source Software Archive 
Member "Tahchee-1.0.0/Sources/tahchee/plugins/rest.py" (17 Feb 2009, 4250 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 "rest.py" see the
Fossies "Dox" file reference documentation.
1 # vim: ts=4
2 # -----------------------------------------------------------------------------
3 # Project : Tahchee <http://www.ivy.fr/tachee>
4 # -----------------------------------------------------------------------------
5 # Author : Joerg Zinke <umaxx@oleco.net>
6 # Sebastien Pierre <sebastien@ivy.fr>
7 # License : Revised BSD License
8 # -----------------------------------------------------------------------------
9 # Creation date : 29-Jan-2007
10 # Last mod. : 31-Oct-2007
11 # -----------------------------------------------------------------------------
12
13 import os, sys, StringIO
14
15 try:
16 from docutils import core, io
17 except ImportError:
18 core = io = None
19
20 NAME = "rest"
21 VERSION = None
22 SUMMARY = "reStructuredText to HTML conversion functions."
23
24 class RestPlugin:
25
26 DEFAULT_ENCODING = "iso-8859-1"
27 DEFAULT_OUTPUT_ENCODING = "iso-8859-1"
28
29 def __init__( self, site ):
30 self.site = site
31
32 def name(self): return NAME
33 def summary(self): return SUMMARY
34 def version(self): return VERSION
35 def doc(self): return __doc__
36
37 def install(self, localdict):
38 localdict["rest"] = self
39
40 def __html_body(self, input_string, source_path=None, destination_path=None,
41 input_encoding='unicode', output_encoding='unicode',
42 doctitle=1, initial_header_level=1):
43 """
44 Given an input string, returns an HTML fragment as a string.
45
46 The return value is the contents of the <body> element.
47
48 Parameters:
49
50 - `input_string`: A multi-line text string; required.
51 - `source_path`: Path to the source file or object. Optional, but useful
52 for diagnostic output (system messages).
53 - `destination_path`: Path to the file or object which will receive the
54 output; optional. Used for determining relative paths (stylesheets,
55 source links, etc.).
56 - `input_encoding`: The encoding of `input_string`. If it is an encoded
57 8-bit string, provide the correct encoding. If it is a Unicode string,
58 use "unicode", the default.
59 - `doctitle`: Disable the promotion of a lone top-level section title to
60 document title (and subsequent section title to document subtitle
61 promotion); enabled by default.
62 - `initial_header_level`: The initial level for header elements (e.g. 1
63 for "<h1>").
64
65 - `output_encoding`: The desired encoding of the output. If a Unicode
66 string is desired, use the default value of "unicode" .
67 """
68 overrides = {
69 'input_encoding': input_encoding,
70 'doctitle_xform': doctitle,
71 'initial_header_level': initial_header_level
72 }
73 parts = core.publish_parts(
74 source=input_string, source_path=source_path,
75 destination_path=destination_path,
76 writer_name='html', settings_overrides=overrides)
77 fragment = parts['html_body']
78 if output_encoding != 'unicode':
79 fragment = fragment.encode(output_encoding)
80 return fragment
81
82 def include(self, path, encoding=None):
83 encoding = encoding or self.DEFAULT_ENCODING
84 outputEncoding = outputEncoding or self.DEFAULT_OUTPUT_ENCODING
85 if not path[0] == "/":
86 path = self.site.pagesDir + "/" + path
87 r = self.__html_body(input_string=unicode(text, encoding), source_path=path, output_encoding=outputEncoding)
88 return r
89 else:
90 self.site.warn("Docutils are not available, but you used the $site.rest function")
91 self.site.info("You can get Docutils from <http://docutils.sourceforge.net/>")
92 return path
93
94 def process(self, text, encoding=None, outputEncoding=None):
95 """If docutils are available, the given text will be interpreted as rest
96 markup and HTML will be generated from it. If docutils are not available, a
97 warning will be issued, and the text will be displayed as-is."""
98 encoding = encoding or self.DEFAULT_ENCODING
99 outputEncoding = outputEncoding or self.DEFAULT_OUTPUT_ENCODING
100 if core and io:
101 r = self.__html_body(input_string=unicode(text, encoding), output_encoding=outputEncoding)
102 return r
103 else:
104 self.site.warn("Docutils are not available, but you used the $site.rest function")
105 self.site.info("You can get Docutils from <http://docutils.sourceforge.net/>")
106 return text
107
108 def __call__(self, text, encoding=None):
109 return self.process(text, encoding)
110
111 # EOF