Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
doc-utils
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
doc-utils
doc-utils
Commits
407460fd
Commit
407460fd
authored
Jul 13, 2018
by
Ingo Meyer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Export tikz graphics as svg in html mode if `pdf2svg` is available
parent
ffe8553a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
9 deletions
+32
-9
docutils_extended/_version.py
docutils_extended/_version.py
+1
-1
docutils_extended/directives/tikz_directive.py
docutils_extended/directives/tikz_directive.py
+31
-8
No files found.
docutils_extended/_version.py
View file @
407460fd
__version_info__
=
(
0
,
1
,
6
)
__version_info__
=
(
0
,
1
,
7
)
__version__
=
'.'
.
join
(
map
(
str
,
__version_info__
))
docutils_extended/directives/tikz_directive.py
View file @
407460fd
...
...
@@ -46,6 +46,8 @@ TMP_PDF_FILENAME = "tikz_picture.pdf"
OUT_PDF_FILENAME
=
"_tikz_rendered{:04d}.pdf"
TMP_PNG_FILENAME
=
"tikz_picture.png"
OUT_PNG_FILENAME
=
"_tikz_rendered{:04d}.png"
TMP_SVG_FILENAME
=
"tikz_picture.svg"
OUT_SVG_FILENAME
=
"_tikz_rendered{:04d}.svg"
VALID_OUTPUT_MODES
=
(
"latex"
,
"html"
)
...
...
@@ -77,7 +79,7 @@ class TemporaryDirectory(object):
self
.
tmp_dir
=
None
def
render_tikz
(
tikz_code
,
options
,
output_png
=
False
):
def
render_tikz
(
tikz_code
,
options
,
output_png
=
False
,
output_svg
=
False
):
self
=
render_tikz
if
not
hasattr
(
self
,
"image_number"
):
self
.
image_number
=
1
...
...
@@ -91,10 +93,12 @@ def render_tikz(tikz_code, options, output_png=False):
latex_output
=
template
.
render
(
tikz_code
=
tikz_code
,
**
options
)
out_pdf_filename
=
OUT_PDF_FILENAME
.
format
(
self
.
image_number
)
out_png_filename
=
OUT_PNG_FILENAME
.
format
(
self
.
image_number
)
out_svg_filename
=
OUT_SVG_FILENAME
.
format
(
self
.
image_number
)
with
TemporaryDirectory
()
as
tmp_dir
:
tmp_latex_path
=
os
.
path
.
join
(
tmp_dir
,
TMP_LATEX_FILENAME
)
tmp_pdf_path
=
os
.
path
.
join
(
tmp_dir
,
TMP_PDF_FILENAME
)
tmp_png_path
=
os
.
path
.
join
(
tmp_dir
,
TMP_PNG_FILENAME
)
tmp_svg_path
=
os
.
path
.
join
(
tmp_dir
,
TMP_SVG_FILENAME
)
with
codecs
.
open
(
tmp_latex_path
,
"w"
,
"utf-8"
)
as
f
:
f
.
write
(
latex_output
)
with
open
(
os
.
devnull
,
"w"
)
as
devnull
:
...
...
@@ -111,6 +115,15 @@ def render_tikz(tikz_code, options, output_png=False):
)
as
f
:
sys
.
stderr
.
write
(
f
.
read
())
raise
if
output_svg
:
try
:
subprocess
.
check_call
(
[
"pdf2svg"
,
TMP_PDF_FILENAME
,
TMP_SVG_FILENAME
],
cwd
=
tmp_dir
,
stdout
=
devnull
,
stderr
=
devnull
)
output_png
=
False
except
(
OSError
,
subprocess
.
CalledProcessError
):
output_png
=
True
output_svg
=
False
if
output_png
:
for
command
in
([
"mudraw"
],
[
"mutool"
,
"draw"
]):
try
:
...
...
@@ -125,17 +138,27 @@ def render_tikz(tikz_code, options, output_png=False):
last_exception
=
e
else
:
raise
last_exception
if
output_png
:
if
output_svg
:
shutil
.
move
(
tmp_svg_path
,
out_svg_filename
)
out_filename
=
out_svg_filename
elif
output_png
:
shutil
.
move
(
tmp_png_path
,
out_png_filename
)
out_filename
=
out_png_filename
else
:
shutil
.
move
(
tmp_pdf_path
,
out_pdf_filename
)
return
out_png_filename
if
output_png
else
out_pdf_filename
out_filename
=
out_pdf_filename
return
out_filename
def
convert_png_to_html_base64
(
png_filepath
):
with
open
(
png_filepath
.
encode
(
sys
.
getfilesystemencoding
()),
"rb"
)
as
image_file
:
def
convert_image_to_html_base64
(
image_filepath
):
filetype
=
os
.
path
.
splitext
(
image_filepath
)[
1
].
lower
()
if
filetype
.
startswith
(
"."
):
filetype
=
filetype
[
1
:]
if
filetype
==
"svg"
:
filetype
=
"svg+xml"
with
open
(
image_filepath
.
encode
(
sys
.
getfilesystemencoding
()),
"rb"
)
as
image_file
:
encoded_string
=
base64
.
b64encode
(
image_file
.
read
())
return
"data:image/
png;base64,"
+
encoded_string
return
"data:image/
{format};base64,"
.
format
(
format
=
filetype
)
+
encoded_string
class
Tikz
(
Directive
):
...
...
@@ -182,9 +205,9 @@ class Tikz(Directive):
content
=
f
.
read
()
else
:
content
=
self
.
content
rendered_tikz_filename
=
render_tikz
(
"
\n
"
.
join
(
content
),
self
.
options
,
output_
pn
g
=
is_output_mode_html
)
rendered_tikz_filename
=
render_tikz
(
"
\n
"
.
join
(
content
),
self
.
options
,
output_
sv
g
=
is_output_mode_html
)
if
is_output_mode_html
:
reference
=
directives
.
uri
(
convert_
png
_to_html_base64
(
rendered_tikz_filename
))
reference
=
directives
.
uri
(
convert_
image
_to_html_base64
(
rendered_tikz_filename
))
else
:
reference
=
directives
.
uri
(
rendered_tikz_filename
)
self
.
options
[
"uri"
]
=
reference
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment