Commit 59c30ca0 authored by Christian Heimes's avatar Christian Heimes

Added a render() method to reStructuredText which returns the rendered parts and the warning object

Added default configuration values to App.config
parent ee4a2a75
......@@ -18,6 +18,8 @@ The configuration values are represented as a single object with
attributes for each bit of information.
"""
import sys
_config = None
def getConfiguration():
......@@ -60,7 +62,6 @@ def setConfiguration(cfg):
os.environ["SOFTWARE_HOME"] = cfg.softwarehome
os.environ["ZOPE_HOME"] = cfg.zopehome
import sys
if "Globals" in sys.modules:
# XXX We *really* want to avoid this if Globals hasn't already
# been imported, due to circular imports. ;-(
......@@ -85,3 +86,9 @@ class DefaultConfiguration:
self.dbtab = None
self.debug_mode = True
self.enable_product_installation = True
# restructured text
default_enc = sys.getdefaultencoding()
self.rest_input_encoding = default_enc
self.rest_output_encoding = default_enc
self.rest_header_level = 3
......@@ -25,7 +25,7 @@ default_output_encoding = getConfiguration().rest_output_encoding or default_enc
default_input_encoding = getConfiguration().rest_input_encoding or default_enc
# starting level for <H> elements (default behaviour inside Zope is <H3>)
default_level = int(os.environ.get('STX_DEFAULT_LEVEL', 3))
default_level = 3
initial_header_level = getConfiguration().rest_header_level or default_level
# default language
......@@ -42,6 +42,38 @@ class Warnings:
def write(self, message):
self.messages.append(message)
def render(src,
writer='html4css1',
report_level=1,
stylesheet='default.css',
input_encoding=default_input_encoding,
output_encoding=default_output_encoding,
language_code=default_lang,
initial_header_level = initial_header_level,
settings = {}):
"""get the rendered parts of the document the and warning object
"""
# Docutils settings:
settings = settings.copy()
settings['input_encoding'] = input_encoding
settings['output_encoding'] = output_encoding
settings['stylesheet'] = stylesheet
settings['language_code'] = language_code
# starting level for <H> elements:
settings['initial_header_level'] = initial_header_level
# set the reporting level to something sane:
settings['report_level'] = report_level
# don't break if we get errors:
settings['halt_level'] = 6
# remember warnings:
settings['warning_stream'] = warning_stream = Warnings()
parts = publish_parts(source=src, writer_name=writer,
settings_overrides=settings,
config_section='zope application')
return parts, warning_stream
def HTML(src,
writer='html4css1',
report_level=1,
......@@ -49,9 +81,9 @@ def HTML(src,
input_encoding=default_input_encoding,
output_encoding=default_output_encoding,
language_code=default_lang,
initial_header_level = initial_header_level,
warnings = None,
settings = {}):
""" render HTML from a reStructuredText string
- 'src' -- string containing a valid reST document
......@@ -70,39 +102,34 @@ def HTML(src,
- 'language_code' - docutils language
- 'initial_header_level' - level of the first header tag
- 'warnings' - will be overwritten with a string containing the warnings
- 'settings' - dict of settings to pass in to Docutils, with priority
"""
# Docutils settings:
settings = settings.copy()
settings['input_encoding'] = input_encoding
settings['output_encoding'] = output_encoding
settings['stylesheet'] = stylesheet
settings['language_code'] = language_code
# starting level for <H> elements:
settings['initial_header_level'] = initial_header_level
# set the reporting level to something sane:
settings['report_level'] = report_level
# don't break if we get errors:
settings['halt_level'] = 6
# remember warnings:
settings['warning_stream'] = warning_stream = Warnings()
parts = publish_parts(source=src, writer_name=writer,
settings_overrides=settings,
config_section='zope application')
output = ('<h%(level)s class="title">%(title)s</h%(level)s>\n%(body)s'
% {'level': initial_header_level,
'title': parts['title'],
'body': parts['body']}).encode(output_encoding)
parts, warning_stream = render(src,
writer = writer,
report_level = report_level,
stylesheet = stylesheet,
input_encoding = input_encoding,
output_encoding = output_encoding,
language_code=language_code,
initial_header_level = initial_header_level,
settings = settings)
output = ('<h%(level)s class="title">%(title)s</h%(level)s>\n'
'%(docinfo)s%(body)s' % {
'level': initial_header_level,
'title': parts['title'],
'docinfo': parts['docinfo'],
'body': parts['body']
}).encode(output_encoding)
# what to do with this? (not used in the original code)
warnings = ''.join(warning_stream.messages)
return output
__all__ = ("HTML", )
__all__ = ("HTML", 'render')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment