forked from Kozea/pygal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygal_sphinx_directives.py
170 lines (144 loc) · 5.29 KB
/
pygal_sphinx_directives.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
# -*- coding: utf-8 -*-
# This file is part of pygal_sphinx_directives
#
# Pygal sphinx integration
# Copyright © 2012-2016 Florian Mounier
#
# This library is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
from traceback import format_exc, print_exc
import docutils.core
from docutils.parsers.rst import Directive
import pygal
from sphinx.directives.code import CodeBlock
# Patch default style
pygal.config.Config.style.value = pygal.style.RotateStyle(
'#2980b9',
background='#fcfcfc',
plot_background='#ffffff',
foreground='#707070',
foreground_strong='#404040',
foreground_subtle='#909090',
opacity='.8',
opacity_hover='.9',
transition='400ms ease-in')
class PygalDirective(Directive):
"""Execute the given python file and puts its result in the document."""
required_arguments = 0
optional_arguments = 2
final_argument_whitespace = True
has_content = True
def run(self):
width, height = map(int, self.arguments[:2]) if len(
self.arguments) >= 2 else (600, 400)
if len(self.arguments) == 1:
self.render_fix = bool(self.arguments[0])
elif len(self.arguments) == 3:
self.render_fix = bool(self.arguments[2])
else:
self.render_fix = False
self.content = list(self.content)
content = list(self.content)
if self.render_fix:
content[-1] = 'rv = ' + content[-1]
code = '\n'.join(content)
scope = {'pygal': pygal}
try:
exec(code, scope)
except Exception:
print(code)
print_exc()
return [docutils.nodes.system_message(
'An exception as occured during code parsing:'
' \n %s' % format_exc(), type='ERROR', source='/',
level=3)]
if self.render_fix:
rv = scope['rv']
else:
chart = None
for key, value in scope.items():
if isinstance(value, pygal.graph.graph.Graph):
chart = value
self.content.append(key + '.render()')
break
if chart is None:
return [docutils.nodes.system_message(
'No instance of graph found', level=3,
type='ERROR', source='/')]
chart.config.width = width
chart.config.height = height
chart.explicit_size = True
try:
svg = '<embed src="%s" />' % chart.render_data_uri()
except Exception:
return [docutils.nodes.system_message(
'An exception as occured during graph generation:'
' \n %s' % format_exc(), type='ERROR', source='/',
level=3)]
return [docutils.nodes.raw('', svg, format='html')]
class PygalWithCode(PygalDirective):
def run(self):
node_list = super(PygalWithCode, self).run()
node_list.extend(CodeBlock(
self.name,
['python'],
self.options,
self.content,
self.lineno,
self.content_offset,
self.block_text,
self.state,
self.state_machine).run())
return [docutils.nodes.compound('', *node_list)]
class PygalTable(Directive):
"""Execute the given python file and puts its result in the document."""
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = True
has_content = True
def run(self):
self.content = list(self.content)
content = list(self.content)
content[-1] = 'rv = ' + content[-1]
code = '\n'.join(content)
scope = {'pygal': pygal}
try:
exec(code, scope)
except Exception:
print_exc()
return [docutils.nodes.system_message(
'An exception as occured during code parsing:'
' \n %s' % format_exc(), type='ERROR', source='/',
level=3)]
rv = scope['rv']
return [docutils.nodes.raw('', rv, format='html')]
class PygalTableWithCode(PygalTable):
def run(self):
node_list = super(PygalTableWithCode, self).run()
node_list.extend(CodeBlock(
self.name,
['python'],
self.options,
self.content,
self.lineno,
self.content_offset,
self.block_text,
self.state,
self.state_machine).run())
return [docutils.nodes.compound('', *node_list)]
def setup(app):
app.add_directive('pygal', PygalDirective)
app.add_directive('pygal-code', PygalWithCode)
app.add_directive('pygal-table', PygalTable)
app.add_directive('pygal-table-code', PygalTableWithCode)
return {'version': '1.0.1'}