32
32
33
33
logger = logging .getLogger (__name__ )
34
34
35
- DEFAULT_FORMATS = dict (html = ' svg' , latex = ' pdf' , text = None )
35
+ DEFAULT_FORMATS = dict (html = " svg" , latex = " pdf" , text = None )
36
36
37
37
38
38
def merge_dict (dst , src ):
@@ -42,17 +42,17 @@ def merge_dict(dst, src):
42
42
return dst
43
43
44
44
45
- def get_basename (text , options , prefix = ' aafig' ):
45
+ def get_basename (text , options , prefix = " aafig" ):
46
46
options = options .copy ()
47
- if ' format' in options :
48
- del options [' format' ]
47
+ if " format" in options :
48
+ del options [" format" ]
49
49
hashkey = text + str (options )
50
- id = sha (hashkey .encode (' utf-8' )).hexdigest ()
51
- return ' %s-%s' % (prefix , id )
50
+ id = sha (hashkey .encode (" utf-8" )).hexdigest ()
51
+ return " %s-%s" % (prefix , id )
52
52
53
53
54
54
class AafigError (SphinxError ):
55
- category = ' aafig error'
55
+ category = " aafig error"
56
56
57
57
58
58
class AafigDirective (images .Image ):
@@ -76,22 +76,22 @@ class AafigDirective(images.Image):
76
76
77
77
def run (self ):
78
78
aafig_options = dict ()
79
- own_options_keys = [self .own_option_spec .keys ()] + [' scale' ]
79
+ own_options_keys = [self .own_option_spec .keys ()] + [" scale" ]
80
80
for (k , v ) in self .options .items ():
81
81
if k in own_options_keys :
82
82
# convert flags to booleans
83
83
if v is None :
84
84
v = True
85
85
# convert percentage to float
86
- if k == ' scale' or k == ' aspect' :
86
+ if k == " scale" or k == " aspect" :
87
87
v = float (v ) / 100.0
88
88
aafig_options [k ] = v
89
89
del self .options [k ]
90
- self .arguments = ['' ]
90
+ self .arguments = ["" ]
91
91
(image_node ,) = images .Image .run (self )
92
92
if isinstance (image_node , nodes .system_message ):
93
93
return [image_node ]
94
- text = ' \n ' .join (self .content )
94
+ text = " \n " .join (self .content )
95
95
image_node .aafig = dict (options = aafig_options , text = text )
96
96
return [image_node ]
97
97
@@ -101,45 +101,45 @@ def render_aafig_images(app, doctree):
101
101
merge_dict (format_map , DEFAULT_FORMATS )
102
102
if aafigure is None :
103
103
logger .warn (
104
- ' aafigure module not installed, ASCII art images '
105
- ' will be redered as literal text'
104
+ " aafigure module not installed, ASCII art images "
105
+ " will be redered as literal text"
106
106
)
107
107
for img in doctree .traverse (nodes .image ):
108
- if not hasattr (img , ' aafig' ):
108
+ if not hasattr (img , " aafig" ):
109
109
continue
110
110
if aafigure is None :
111
111
continue
112
- options = img .aafig [' options' ]
113
- text = img .aafig [' text' ]
112
+ options = img .aafig [" options" ]
113
+ text = img .aafig [" text" ]
114
114
format = app .builder .format
115
115
merge_dict (options , app .builder .config .aafig_default_options )
116
116
if format in format_map :
117
- options [' format' ] = format_map [format ]
117
+ options [" format" ] = format_map [format ]
118
118
else :
119
119
logger .warn (
120
120
'unsupported builder format "%s", please '
121
- ' add a custom entry in aafig_format config '
122
- ' option for this builder' % format
121
+ " add a custom entry in aafig_format config "
122
+ " option for this builder" % format
123
123
)
124
124
img .replace_self (nodes .literal_block (text , text ))
125
125
continue
126
- if options [' format' ] is None :
126
+ if options [" format" ] is None :
127
127
img .replace_self (nodes .literal_block (text , text ))
128
128
continue
129
129
try :
130
130
fname , outfn , id , extra = render_aafigure (app , text , options )
131
131
except AafigError as exc :
132
- logger .warn (' aafigure error: ' + str (exc ))
132
+ logger .warn (" aafigure error: " + str (exc ))
133
133
img .replace_self (nodes .literal_block (text , text ))
134
134
continue
135
- img [' uri' ] = fname
135
+ img [" uri" ] = fname
136
136
# FIXME: find some way to avoid this hack in aafigure
137
137
if extra :
138
138
(width , height ) = [x .split ('"' )[1 ] for x in extra .split ()]
139
- if ' width' not in img :
140
- img [' width' ] = width
141
- if ' height' not in img :
142
- img [' height' ] = height
139
+ if " width" not in img :
140
+ img [" width" ] = width
141
+ if " height" not in img :
142
+ img [" height" ] = height
143
143
144
144
145
145
def render_aafigure (app , text , options ):
@@ -148,36 +148,36 @@ def render_aafigure(app, text, options):
148
148
"""
149
149
150
150
if aafigure is None :
151
- raise AafigError (' aafigure module not installed' )
151
+ raise AafigError (" aafigure module not installed" )
152
152
153
153
fname = get_basename (text , options )
154
- fname = ' %s.%s' % (get_basename (text , options ), options [' format' ])
155
- if app .builder .format == ' html' :
154
+ fname = " %s.%s" % (get_basename (text , options ), options [" format" ])
155
+ if app .builder .format == " html" :
156
156
# HTML
157
- imgpath = relative_uri (app .builder .env .docname , ' _images' )
157
+ imgpath = relative_uri (app .builder .env .docname , " _images" )
158
158
relfn = posixpath .join (imgpath , fname )
159
- outfn = path .join (app .builder .outdir , ' _images' , fname )
159
+ outfn = path .join (app .builder .outdir , " _images" , fname )
160
160
else :
161
161
# Non-HTML
162
- if app .builder .format != ' latex' :
162
+ if app .builder .format != " latex" :
163
163
logger .warn (
164
- ' aafig: the builder format %s is not officially '
165
- ' supported, aafigure images could not work. '
166
- ' Please report problems and working builder to '
167
- ' avoid this warning inthe future' % app .builder .format
164
+ " aafig: the builder format %s is not officially "
165
+ " supported, aafigure images could not work. "
166
+ " Please report problems and working builder to "
167
+ " avoid this warning inthe future" % app .builder .format
168
168
)
169
169
relfn = fname
170
170
outfn = path .join (app .builder .outdir , fname )
171
- metadata_fname = ' %s.aafig' % outfn
171
+ metadata_fname = " %s.aafig" % outfn
172
172
173
173
try :
174
174
if path .isfile (outfn ):
175
175
extra = None
176
- if options [' format' ].lower () == ' svg' :
176
+ if options [" format" ].lower () == " svg" :
177
177
f = None
178
178
try :
179
179
try :
180
- f = open (metadata_fname , 'r' )
180
+ f = open (metadata_fname , "r" )
181
181
extra = f .read ()
182
182
except Exception :
183
183
raise AafigError ()
@@ -197,20 +197,20 @@ def render_aafigure(app, text, options):
197
197
raise AafigError (str (e ))
198
198
199
199
extra = None
200
- if options [' format' ].lower () == ' svg' :
200
+ if options [" format" ].lower () == " svg" :
201
201
extra = visitor .get_size_attrs ()
202
- f = open (metadata_fname , 'w' )
202
+ f = open (metadata_fname , "w" )
203
203
f .write (extra )
204
204
f .close ()
205
205
206
206
return relfn , outfn , id , extra
207
207
208
208
209
209
def setup (app ):
210
- app .add_directive (' aafig' , AafigDirective )
211
- app .connect (' doctree-read' , render_aafig_images )
212
- app .add_config_value (' aafig_format' , DEFAULT_FORMATS , ' html' )
213
- app .add_config_value (' aafig_default_options' , dict (), ' html' )
210
+ app .add_directive (" aafig" , AafigDirective )
211
+ app .connect (" doctree-read" , render_aafig_images )
212
+ app .add_config_value (" aafig_format" , DEFAULT_FORMATS , " html" )
213
+ app .add_config_value (" aafig_default_options" , dict (), " html" )
214
214
215
215
216
216
# vim: set expandtab shiftwidth=4 softtabstop=4 :
0 commit comments