|
rpm-build |
d9acb6 |
#!/usr/bin/python
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
"""
|
|
rpm-build |
d9acb6 |
A minimal front end to the Docutils Publisher, producing HTML with
|
|
rpm-build |
d9acb6 |
Pygments syntax highlighting.
|
|
rpm-build |
d9acb6 |
"""
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
# Set to True if you want inline CSS styles instead of classes
|
|
rpm-build |
d9acb6 |
INLINESTYLES = False
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
try:
|
|
rpm-build |
d9acb6 |
import locale
|
|
rpm-build |
d9acb6 |
locale.setlocale(locale.LC_ALL, '')
|
|
rpm-build |
d9acb6 |
except:
|
|
rpm-build |
d9acb6 |
pass
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
# set up Pygments
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
from pygments.formatters import HtmlFormatter
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
# The default formatter
|
|
rpm-build |
d9acb6 |
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES, cssclass='syntax')
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
# Add name -> formatter pairs for every variant you want to use
|
|
rpm-build |
d9acb6 |
VARIANTS = {
|
|
rpm-build |
d9acb6 |
# 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),
|
|
rpm-build |
d9acb6 |
}
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
from docutils import nodes
|
|
rpm-build |
d9acb6 |
from docutils.parsers.rst import directives
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
from pygments import highlight
|
|
rpm-build |
d9acb6 |
from pygments.lexers import get_lexer_by_name, TextLexer
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
def pygments_directive(name, arguments, options, content, lineno,
|
|
rpm-build |
d9acb6 |
content_offset, block_text, state, state_machine):
|
|
rpm-build |
d9acb6 |
try:
|
|
rpm-build |
d9acb6 |
lexer = get_lexer_by_name(arguments[0])
|
|
rpm-build |
d9acb6 |
except ValueError, e:
|
|
rpm-build |
d9acb6 |
# no lexer found - use the text one instead of an exception
|
|
rpm-build |
d9acb6 |
lexer = TextLexer()
|
|
rpm-build |
d9acb6 |
# take an arbitrary option if more than one is given
|
|
rpm-build |
d9acb6 |
formatter = options and VARIANTS[options.keys()[0]] or DEFAULT
|
|
rpm-build |
d9acb6 |
parsed = highlight(u'\n'.join(content), lexer, formatter)
|
|
rpm-build |
d9acb6 |
return [nodes.raw('', parsed, format='html')]
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
pygments_directive.arguments = (1, 0, 1)
|
|
rpm-build |
d9acb6 |
pygments_directive.content = 1
|
|
rpm-build |
d9acb6 |
pygments_directive.options = dict([(key, directives.flag) for key in VARIANTS])
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
directives.register_directive('sourcecode', pygments_directive)
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
# run the generation
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
from docutils.core import publish_cmdline, default_description
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
description = ('Generates (X)HTML documents from standalone reStructuredText '
|
|
rpm-build |
d9acb6 |
'sources. ' + default_description)
|
|
rpm-build |
d9acb6 |
|
|
rpm-build |
d9acb6 |
publish_cmdline(writer_name='html', description=description)
|