Path: blob/master/elisp/emacs-for-python/yasnippet/doc/compile-doc.py
990 views
#!/usr/bin/python1# Compile document to HTML use docutils.23# ========================================4# Pygments syntax highlighting5# ========================================6from pygments.formatters import HtmlFormatter78# Set to True if you want inline CSS styles instead of classes9INLINESTYLES = True1011from pygments.formatters import HtmlFormatter1213# The default formatter14DEFAULT = HtmlFormatter(noclasses=INLINESTYLES)1516# Add name -> formatter pairs for every variant you want to use17VARIANTS = {18# 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),19}2021from docutils import nodes22from docutils.parsers.rst import directives2324from pygments import highlight25from pygments.lexers import get_lexer_by_name, TextLexer2627def pygments_directive(name, arguments, options, content, lineno,28content_offset, block_text, state, state_machine):29try:30lexer = get_lexer_by_name(arguments[0])31except ValueError:32# no lexer found - use the text one instead of an exception33lexer = TextLexer()34# take an arbitrary option if more than one is given35formatter = options and VARIANTS[options.keys()[0]] or DEFAULT36parsed = highlight(u'\n'.join(content), lexer, formatter)37return [nodes.raw('', parsed, format='html')]3839pygments_directive.arguments = (1, 0, 1)40pygments_directive.content = 141pygments_directive.options = dict([(key, directives.flag) for key in VARIANTS])4243directives.register_directive('sourcecode', pygments_directive)4445# =================46# Youtube embedding47# =================4849from docutils import nodes50from docutils.parsers.rst import directives5152CODE = """\53<object type="application/x-shockwave-flash"54width="%(width)s"55height="%(height)s"56align="%(align)s"57class="youtube-embed"58data="http://www.youtube.com/v/%(yid)s">59<param name="movie" value="http://www.youtube.com/v/%(yid)s"></param>60<param name="wmode" value="transparent"></param>%(extra)s61</object>62"""6364PARAM = """\n <param name="%s" value="%s"></param>"""6566def youtube(name, args, options, content, lineno,67contentOffset, blockText, state, stateMachine):68""" Restructured text extension for inserting youtube embedded videos """69if len(content) == 0:70return71string_vars = {72'yid': content[0],73'width': 425,74'height': 344,75'align': "right",76'extra': ''77}78extra_args = content[1:] # Because content[0] is ID79extra_args = [ea.strip().split("=") for ea in extra_args] # key=value80extra_args = [ea for ea in extra_args if len(ea) == 2] # drop bad lines81extra_args = dict(extra_args)82if 'width' in extra_args:83string_vars['width'] = extra_args.pop('width')84if 'align' in extra_args:85string_vars['align'] = extra_args.pop('align')86if 'height' in extra_args:87string_vars['height'] = extra_args.pop('height')88if extra_args:89params = [PARAM % (key, extra_args[key]) for key in extra_args]90string_vars['extra'] = "".join(params)91return [nodes.raw('', CODE % (string_vars), format='html')]92youtube.content = True93directives.register_directive('youtube', youtube)949596# ========================================97# Command line processing98# ========================================99from docutils.core import publish_cmdline, default_description100101description = ('Generates (X)HTML documents from standalone reStructuredText '102'sources. ' + default_description)103overrides = {'stylesheet_path' : 'styles.css',104'embed_stylesheet' : False,105'template' : 'doc/template.txt'}106107publish_cmdline(writer_name='html',108description=description,109settings_overrides=overrides)110111112