#!/usr/bin/python
# Author: Chris Liechti
# Contact: cliechti@gmx.net
# Revision: $Revision$
# Date: $Date$
# Copyright: This module has been placed in the public domain.
"""
A minimal front end to the Docutils Publisher, producing HTML slides using
the S5 template system.
"""
try:
import locale
locale.setlocale(locale.LC_ALL, '')
except:
pass
import pygments
from pygments.lexers import get_lexer_by_name
from pygments.formatters import get_formatter_by_name
from docutils.parsers import rst
from docutils import nodes
from docutils.writers.html4css1 import Writer, HTMLTranslator
class DocorateHeadHTMLTranslator(HTMLTranslator):
_classText = "docutils"
def __init__(self, document):
HTMLTranslator.__init__(self,document)
self.head_suffix = ['','','','']
self.body_prefix = []
self.body_suffix = []
self.stylesheet = []
def visit_document(self, node):
self.head.append('<h1 class="%s" >%s</h1>\n'
% (self._classText, self.encode(node.get('title', ''))))
def visit_title(self, node):
"""Only 6 section levels are supported by HTML."""
check_id = 0
close_tag = '</p>\n'
if isinstance(node.parent, nodes.topic):
self.body.append(
self.starttag(node, 'p', '', CLASS='topic-title first'))
elif isinstance(node.parent, nodes.sidebar):
self.body.append(
self.starttag(node, 'p', '', CLASS='sidebar-title'))
elif isinstance(node.parent, nodes.Admonition):
self.body.append(
self.starttag(node, 'p', '', CLASS='admonition-title'))
elif isinstance(node.parent, nodes.table):
self.body.append(
self.starttag(node, 'caption', ''))
close_tag = '</caption>\n'
elif isinstance(node.parent, nodes.document):
self.body.append(self.starttag(node, 'h1', '', CLASS=self._classText))
close_tag = '</h1>\n'
self.in_document_title = len(self.body)
else:
assert isinstance(node.parent, nodes.section)
h_level = self.section_level + self.initial_header_level - 1
atts = {}
if (len(node.parent) >= 2 and
isinstance(node.parent[1], nodes.subtitle)):
atts['CLASS'] = 'with-subtitle'
atts['CLASS'] = self._classText
self.body.append(
self.starttag(node, 'h%s' % h_level, '', **atts))
atts = {}
if node.hasattr('refid'):
atts['class'] = 'toc-backref'
atts['href'] = '#' + node['refid']
if atts:
self.body.append(self.starttag({}, 'a', '', **atts))
close_tag = '</a></h%s>\n' % (h_level)
else:
close_tag = '</h%s>\n' % (h_level)
self.context.append(close_tag)
def visit_subtitle(self, node):
if isinstance(node.parent, nodes.sidebar):
self.body.append(self.starttag(node, 'p', '',
CLASS='sidebar-subtitle'))
self.context.append('</p>\n')
elif isinstance(node.parent, nodes.document):
self.body.append(self.starttag(node, 'h2', '', CLASS=self._classText))
self.context.append('</h2>\n')
self.in_document_title = len(self.body)
elif isinstance(node.parent, nodes.section):
tag = 'h%s' % (self.section_level + self.initial_header_level - 1)
self.body.append(
self.starttag(node, tag, '', CLASS=self._classText) +
self.starttag({}, 'span', '', CLASS=self._classText))
self.context.append('</span></%s>\n' % tag)
_w = Writer()
_w.translator_class = DocorateHeadHTMLTranslator
def code_formatter(language, content):
lexer = get_lexer_by_name(language)
formatter = get_formatter_by_name('html', noclasses=True)
html = pygments.highlight(content, lexer, formatter)
return nodes.raw('', html, format='html')
def code_role(name, rawtext, text, lineno, inliner, options={},
content=[]):
language = options.get('language')
if not language:
args = text.split(':', 1)
language = args[0]
if len(args) == 2:
text = args[1]
else:
text = ''
reference = code_formatter(language, text)
return [reference], []
def code_block(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
"""
Create a code-block directive for docutils.
Usage: .. code-block:: language
If the language can be syntax highlighted it will be.
"""
language = arguments[0]
text = '\n'.join(content)
reference = code_formatter(language, text)
return [reference]
# These are documented
# at http://docutils.sourceforge.net/spec/howto/rst-directives.html.
code_block.arguments = (
1, # Number of required arguments.
0, # Number of optional arguments.
0) # True if final argument may contain whitespace.
# A mapping from option name to conversion function.
code_role.options = code_block.options = {
'language' :
rst.directives.unchanged # Return the text argument, unchanged
}
code_block.content = 1 # True if content is allowed.
# Register the directive with docutils.
rst.directives.register_directive('code-block', code_block)
rst.roles.register_local_role('code-block', code_role)
from docutils.core import publish_cmdline, default_description
description = ('Generates HTML documents from standalone '
'reStructuredText sources. ' + default_description)
#publish_cmdline(writer_name='html', description=description)
publish_cmdline(writer=_w , description=description)
rst2htmlc.py
標籤:
python,
restructuredtext
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言