Commit c5482ffc authored by Christian Heimes's avatar Christian Heimes

Merging from tiran-restfixing-brnach

parent 7e454840
......@@ -10,20 +10,30 @@
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""Wrapper to integrate reStructuredText into Zope
""" Wrapper to integrate reStructuredText into Zope """
This implementation requires docutils 0.3.4+ from http://docutils.sf.net/
"""
__all__ = ("HTML", )
import sys, os
import docutils.core
from docutils.io import StringOutput, StringInput
from App.config import getConfiguration
import sys, os, locale
from App.config import getConfiguration
from docutils.core import publish_parts
# get encoding
default_enc = sys.getdefaultencoding()
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))
initial_header_level = getConfiguration().rest_header_level or default_level
# default language
default_lang = getConfiguration().locale or locale.getdefaultlocale()[0]
if default_lang and '_' in default_lang:
default_lang = default_lang[:default_lang.index('_')]
class Warnings:
def __init__(self):
......@@ -32,12 +42,15 @@ class Warnings:
def write(self, message):
self.messages.append(message)
def HTML(src,
writer='html4zope',
report_level=1,
def HTML(src,
writer='html4css1',
report_level=1,
stylesheet='default.css',
input_encoding=default_input_encoding,
output_encoding=default_output_encoding):
input_encoding=default_input_encoding,
output_encoding=default_output_encoding,
language_code=default_lang,
warnings = None,
settings = {}):
""" render HTML from a reStructuredText string
......@@ -52,48 +65,44 @@ def HTML(src,
- 'input_encoding' - encoding of the reST input string
- 'output_encoding' - encoding of the rendered HTML output
"""
pub = docutils.core.Publisher()
pub.set_reader('standalone', None, 'restructuredtext')
pub.set_writer(writer)
# go with the defaults
pub.get_settings()
pub.settings.stylesheet = stylesheet
# this is needed, but doesn't seem to do anything
pub.settings._destination = ''
# set the reporting level to something sane
pub.settings.report_level = report_level
# don't break if we get errors
pub.settings.halt_level = 6
# remember warnings
pub.settings.warning_stream = Warnings()
# input
pub.source = StringInput(source=src, encoding=input_encoding)
# output - not that it's needed
pub.destination = StringOutput(encoding=output_encoding)
# parse!
document = pub.reader.read(pub.source, pub.parser, pub.settings)
# transform
pub.apply_transforms(document)
warnings = ''.join(pub.settings.warning_stream.messages)
# do the format
return pub.writer.write(document, pub.destination)
- 'report_level' - verbosity of reST parser
from docutils import writers
import html4zope
- 'language_code' - docutils language
- 'warnings' - will be overwritten with a string containing the warnings
- 'settings' - dict of settings to pass in to Docutils, with priority
writers.html4zope = html4zope
sys.modules['docutils.writers.html4zope'] = html4zope
"""
# 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)
# what to do with this? (not used in the original code)
warnings = ''.join(warning_stream.messages)
return output
__all__ = ("HTML", )
# Author: Andreas Jung
# Contact: andreas@andreas-jung.com
# Revision: $Revision: 1.1 $
# Date: $Date: 2003/11/30 16:16:06 $
# Copyright: This module has been placed in the public domain.
"""
Writer module to integrate reST into Zope. This writer subclasses the standard
html4css1 writer and changes the starting level for <H> elements from 1 to 3
(default behaviour inside Zope.
"""
__docformat__ = 'reStructuredText'
from docutils import nodes
from docutils.writers.html4css1 import Writer as CSS1Writer, HTMLTranslator as CSS1HTMLTranslator
import os
default_level = int(os.environ.get('STX_DEFAULT_LEVEL', 3))
class Writer(CSS1Writer):
def __init__(self):
CSS1Writer.__init__(self)
self.translator_class = HTMLTranslator
class HTMLTranslator(CSS1HTMLTranslator):
def astext(self):
return ''.join(self.body)
def visit_title(self, node):
"""Only 6 section levels are supported by HTML."""
if isinstance(node.parent, nodes.topic):
self.body.append(
self.starttag(node, 'p', '', CLASS='topic-title'))
if node.parent.hasattr('id'):
self.body.append(
self.starttag({}, 'a', '', name=node.parent['id']))
self.context.append('</a></p>\n')
else:
self.context.append('</p>\n')
elif self.section_level == 0:
# document title
self.head.append('<title>%s</title>\n'
% self.encode(node.astext()))
self.body.append(self.starttag(node, 'h%d' % default_level, '', CLASS='title'))
self.context.append('</h%d>\n' % default_level)
else:
self.body.append(
self.starttag(node, 'h%s' % (default_level+self.section_level-1), ''))
atts = {}
if node.parent.hasattr('id'):
atts['name'] = node.parent['id']
if node.hasattr('refid'):
atts['class'] = 'toc-backref'
atts['href'] = '#' + node['refid']
self.body.append(self.starttag({}, 'a', '', **atts))
self.context.append('</a></h%s>\n' % ((default_level+self.section_level-1)))
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