forked from MicroEJ/docs
/
microej.py
217 lines (175 loc) · 6.82 KB
/
microej.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Copyright 2008-2020, MicroEJ Corp. Content in this space is free for read and redistribute. Except if otherwise stated, modification is subject to MicroEJ Corp prior approval.
# MicroEJ is a trademark of MicroEJ Corp. All other trademarks and copyrights are the property of their respective owners.
import os
from docutils import nodes
from pygments.style import Style
from pygments.token import (
Comment,
Error,
Keyword,
Literal,
Generic,
Operator,
Name,
Text,
Whitespace,
)
from sphinx.util.docutils import ReferenceRole, SphinxRole
class MicroEJStyle(Style):
"""
Pygments style for MicroEJ.
Styles are defined here so that they affect both HTML and PDF output. For
HTML output, a pygments.css is generated by Sphinx simply by using the
MicroEJ Sphinx theme. For PDF output, the following option is required::
pygments_style = 'microej.MicroEJStyle'
This option also affects the HTML output, but is the same pygments class.
"""
background_color = "#f8f8f8"
default_style = ""
styles = {
Comment: "italic #48A23F",
Comment.Hashbang: "italic #48A23F",
Comment.Multiline: "italic #48A23F",
Comment.Preproc: "italic #48A23F",
Comment.PreprocFile: "italic #48A23F",
Comment.Single: "italic #48A23F",
Comment.Special: "italic #48A23F",
Error: "#FF0000 bg:#f8f8f8",
Keyword: "bold #cf4520",
Keyword.Constant: "bold #cf4520",
Keyword.Declaration: "bold #cf4520",
Keyword.Namespace: "bold #cf4520",
Keyword.Pseudo: "nobold",
Keyword.Reserved: "bold #cf4520",
Keyword.Type: "nobold #B00040",
Literal: "#666666",
Literal.Number: "#666666",
Literal.String: "#ee502e",
Literal.Number.Bin: "#666666",
Literal.Number.Float: "#666666",
Literal.Number.Hex: "#666666",
Literal.Number.Integer: "#666666",
Literal.Number.Oct: "#666666",
Literal.String.Affix: "#ee502e",
Literal.String.Backtick: "#ee502e",
Literal.String.Char: "#ee502e",
Literal.String.Delimiter: "#ee502e",
Literal.String.Doc: "#ee502e italic",
Literal.String.Double: "#ee502e",
Literal.String.Escape: "#BB6622 bold",
Literal.String.Heredoc: "#ee502e",
Literal.String.Interpol: "#BB6688 bold",
Literal.String.Other: "#cf4520",
Literal.String.Regex: "#BB6688",
Literal.String.Single: "#ee502e",
Literal.String.Symbol: "#19177C",
Literal.Number.Integer.Long: "#666666",
Operator: "#666666",
Generic.Deleted: "#A00000",
Generic.Error: "#FF0000",
Generic.Emph: "italic",
Generic.Heading: "bold #000080",
Generic.Inserted: "#00A000",
Generic.Output: "#888",
Generic.Prompt: "bold #008eaa",
Generic.Strong: "bold",
Generic.Subheading: "bold #800080",
Generic.Traceback: "#04D",
Name.Attribute: "#7D9029",
Name.Builtin: "#cf4520",
Name.Class: "#008eaa bold",
Name.Constant: "#880000",
Name.Decorator: "#AA22FF",
Name.Entity: "#999999 bold",
Name.Exception: "#D2413A bold",
Name.Function: "#008eaa",
Name.Label: "#A0A000",
Name.Namespace: "#008eaa bold",
Name.Tag: "#cf4520 bold",
Name.Variable: "#19177C",
Name.Builtin.Pseudo: "#cf4520",
Name.Function.Magic: "#008eaa",
Name.Variable.Class: "#19177C",
Name.Variable.Global: "#19177C",
Name.Variable.Instance: "#19177C",
Name.Variable.Magic: "#19177C",
Operator.Word: "#AA22FF bold",
Text.Whitespace: "#bbbbbb",
Whitespace: "#bbbbbb",
}
class HttpCodeRef(ReferenceRole):
"""
A custom role to format external links as code/literals.
In the source docbook, links were styled as monospace text. This preserves
that functionality and also makes the monospace text a clickable link. A
normal URL link can also be used instead, but will be formatted with the
default font.
"""
def run(self):
attrs = {}
if self.target.startswith('http'):
attrs['target'] = '_blank'
reference = nodes.reference(
'',
'',
internal=False,
refuri=self.target,
**attrs,
)
if self.has_explicit_title:
reference += nodes.literal(self.title, self.title)
else:
reference += nodes.literal(self.target, self.target)
return [reference], []
def can_build_independent_docs():
return os.environ.get('MICROEJ_DOCSET', None) is not None
def get_project_name():
"""
Set up the project name if we're building multiple docs from single source.
This documentation project supports building multiple documentation sets
from a common source. In order to enable this while building, define an
environment variable ``MICROEJ_DOCSET`` -- this is the name of the path that
you are building an independent documentation set for. For instance::
MICROEJ_DOCSET=SandboexAppDevGuide make clean html
This environment variable can also be defined on Read the Docs.
"""
docset = os.environ.get('MICROEJ_DOCSET', None)
if docset == 'ApplicationDeveloperGuide':
return 'Application Developer Guide'
elif docset == 'PlatformDeveloperGuide':
return 'Platform Developer Guide'
elif docset == 'KernelDeveloperGuide':
return 'Kernel Developer Guide'
elif docset == 'overview':
return 'Overview'
elif docset == 'glossary':
return 'Glossary'
elif docset == 'Landing':
return 'MicroEJ Documentation'
def set_project_version(app, config):
"""
Event listener to replace the project's release with changeset.
This will only work on Read the Docs currently, it uses information passed
into the project's build to obtain the commit id. This sets some additional
variables in the tex output. See ``microej.sty`` for more information.
"""
# We have to do this here, as Read the Docs appends this to the end of the
# config file. It's not ready until we're in the build.
commit_id = config.html_context.get('commit', None)
if commit_id:
config.release = f'Commit {commit_id}'
config.latex_elements['preamble'] += (
r'\renewcommand{\microejversion}{Revision \texttt ' + commit_id + "}"
)
def setup(app):
from docutils.parsers.rst import roles
roles.register_local_role('http', HttpCodeRef())
if can_build_independent_docs():
docset = os.environ.get('MICROEJ_DOCSET', None)
app.srcdir += '/' + docset
app.connect('config-inited', set_project_version)
return {
'version': '1.0.0',
'parallel_read_safe': True,
'parallel_write_safe': True,
}