Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
marvel
GitHub Repository: marvel/qnf
Path: blob/master/elisp/emacs-for-python/yasnippet/doc/compile-doc.py
990 views
1
#!/usr/bin/python
2
# Compile document to HTML use docutils.
3
4
# ========================================
5
# Pygments syntax highlighting
6
# ========================================
7
from pygments.formatters import HtmlFormatter
8
9
# Set to True if you want inline CSS styles instead of classes
10
INLINESTYLES = True
11
12
from pygments.formatters import HtmlFormatter
13
14
# The default formatter
15
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES)
16
17
# Add name -> formatter pairs for every variant you want to use
18
VARIANTS = {
19
# 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),
20
}
21
22
from docutils import nodes
23
from docutils.parsers.rst import directives
24
25
from pygments import highlight
26
from pygments.lexers import get_lexer_by_name, TextLexer
27
28
def pygments_directive(name, arguments, options, content, lineno,
29
content_offset, block_text, state, state_machine):
30
try:
31
lexer = get_lexer_by_name(arguments[0])
32
except ValueError:
33
# no lexer found - use the text one instead of an exception
34
lexer = TextLexer()
35
# take an arbitrary option if more than one is given
36
formatter = options and VARIANTS[options.keys()[0]] or DEFAULT
37
parsed = highlight(u'\n'.join(content), lexer, formatter)
38
return [nodes.raw('', parsed, format='html')]
39
40
pygments_directive.arguments = (1, 0, 1)
41
pygments_directive.content = 1
42
pygments_directive.options = dict([(key, directives.flag) for key in VARIANTS])
43
44
directives.register_directive('sourcecode', pygments_directive)
45
46
# =================
47
# Youtube embedding
48
# =================
49
50
from docutils import nodes
51
from docutils.parsers.rst import directives
52
53
CODE = """\
54
<object type="application/x-shockwave-flash"
55
width="%(width)s"
56
height="%(height)s"
57
align="%(align)s"
58
class="youtube-embed"
59
data="http://www.youtube.com/v/%(yid)s">
60
<param name="movie" value="http://www.youtube.com/v/%(yid)s"></param>
61
<param name="wmode" value="transparent"></param>%(extra)s
62
</object>
63
"""
64
65
PARAM = """\n <param name="%s" value="%s"></param>"""
66
67
def youtube(name, args, options, content, lineno,
68
contentOffset, blockText, state, stateMachine):
69
""" Restructured text extension for inserting youtube embedded videos """
70
if len(content) == 0:
71
return
72
string_vars = {
73
'yid': content[0],
74
'width': 425,
75
'height': 344,
76
'align': "right",
77
'extra': ''
78
}
79
extra_args = content[1:] # Because content[0] is ID
80
extra_args = [ea.strip().split("=") for ea in extra_args] # key=value
81
extra_args = [ea for ea in extra_args if len(ea) == 2] # drop bad lines
82
extra_args = dict(extra_args)
83
if 'width' in extra_args:
84
string_vars['width'] = extra_args.pop('width')
85
if 'align' in extra_args:
86
string_vars['align'] = extra_args.pop('align')
87
if 'height' in extra_args:
88
string_vars['height'] = extra_args.pop('height')
89
if extra_args:
90
params = [PARAM % (key, extra_args[key]) for key in extra_args]
91
string_vars['extra'] = "".join(params)
92
return [nodes.raw('', CODE % (string_vars), format='html')]
93
youtube.content = True
94
directives.register_directive('youtube', youtube)
95
96
97
# ========================================
98
# Command line processing
99
# ========================================
100
from docutils.core import publish_cmdline, default_description
101
102
description = ('Generates (X)HTML documents from standalone reStructuredText '
103
'sources. ' + default_description)
104
overrides = {'stylesheet_path' : 'styles.css',
105
'embed_stylesheet' : False,
106
'template' : 'doc/template.txt'}
107
108
publish_cmdline(writer_name='html',
109
description=description,
110
settings_overrides=overrides)
111
112