Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
doc-utils
doc-utils
Commits
684e42fe
Commit
684e42fe
authored
Mar 22, 2021
by
Ingo Meyer
Browse files
Merge branch 'develop'
parents
58108e8c
0361864b
Changes
20
Hide whitespace changes
Inline
Side-by-side
.gitignore
View file @
684e42fe
.*
!.gitignore
*.pyc
__pycache__/
build/
dist/
*.egg-info/
MANIFEST.in
0 → 100644
View file @
684e42fe
include LICENSE
include README.md
README.md
View file @
684e42fe
...
...
@@ -11,9 +11,11 @@ documents from reStructuredText files.
This repository contains a Python package
`docutils_extended`
that can be installed with
`pip`
:
```
bash
pip
install
git+https://iffgit.fz-juelich.de/doc-utils/doc-utils
python3
-m
pip
install
git+https://iffgit.fz-juelich.de/doc-utils/doc-utils
```
(Starting with version
`v0.4.0`
Python 2 is no longer supported.)
After installing you can use the extended rst writers
`rst2latex-extended`
and
`rst2html-extended`
that are compatible
with the standard versions but offer additional features:
...
...
docutils_extended/__init__.py
View file @
684e42fe
from
.writers
import
rst2html_extended_main
,
rst2latex_extended_main
__all__
=
(
'rst2html_extended_main'
,
'rst2latex_extended_main'
)
__all__
=
(
"rst2html_extended_main"
,
"rst2latex_extended_main"
)
docutils_extended/_version.py
View file @
684e42fe
__version_info__
=
(
0
,
3
,
0
)
__version__
=
'.'
.
join
(
map
(
str
,
__version_info__
))
__version_info__
=
(
0
,
4
,
0
)
__version__
=
"."
.
join
(
map
(
str
,
__version_info__
))
docutils_extended/directives/__init__.py
View file @
684e42fe
from
.code_directive
import
CodeBlock
from
.embedded_image_directive
import
Embedded
Imag
e
,
Embedded
Figur
e
from
.embedded_image_directive
import
Embedded
Figur
e
,
Embedded
Imag
e
from
.jupyter_outbox_directive
import
JupyterOutbox
from
.tikz_directive
import
Tikz
,
TikzFigure
from
.output_mode
import
get_output_mode
,
set_output_mode
from
.tikz_directive
import
Tikz
,
TikzFigure
__all__
=
(
"CodeBlock"
,
"EmbeddedImage"
,
"EmbeddedFigure"
,
"JupyterOutbox"
,
"Tikz"
,
"TikzFigure"
,
"get_output_mode"
,
"set_output_mode"
)
__all__
=
(
"CodeBlock"
,
"EmbeddedImage"
,
"EmbeddedFigure"
,
"JupyterOutbox"
,
"Tikz"
,
"TikzFigure"
,
"get_output_mode"
,
"set_output_mode"
,
)
docutils_extended/directives/code_directive.py
View file @
684e42fe
...
...
@@ -11,6 +11,6 @@ class CodeBlock(Directive):
def
run
(
self
):
language
=
self
.
arguments
[
0
]
content
=
u
"
\n
"
.
join
(
self
.
content
)
latex
=
u
"
\\
begin{{minted}}{{{}}}
\n
{}
\n\\
end{{minted}}"
.
format
(
language
,
content
)
content
=
"
\n
"
.
join
(
self
.
content
)
latex
=
"
\\
begin{{minted}}{{{}}}
\n
{}
\n\\
end{{minted}}"
.
format
(
language
,
content
)
return
[
nodes
.
raw
(
""
,
latex
,
format
=
"latex"
)]
docutils_extended/directives/embedded_image_directive.py
View file @
684e42fe
# -*- coding: utf-8 -*-
import
base64
import
requests
import
os
import
shutil
import
subprocess
import
tempfile
try
:
from
urllib.parse
import
urlparse
except
ImportError
:
from
urlparse
import
urlparse
import
sys
import
tempfile
from
io
import
BytesIO
from
PIL
import
Image
from
urllib.parse
import
urlparse
import
requests
from
docutils
import
nodes
from
docutils.parsers.rst
import
Directive
from
docutils.parsers.rst
import
directives
,
states
from
docutils.nodes
import
fully_normalize_name
,
whitespace_normalize_name
from
docutils.parsers.rst
import
Directive
,
directives
,
states
from
docutils.parsers.rst.roles
import
set_classes
from
.output_mode
import
get_output_mod
e
from
PIL
import
Imag
e
from
.output_mode
import
get_output_mode
TMP_IMAGE_FILENAME
=
"_downloaded_image{i:04d}.{ext}"
TMP_QPDF_OUT_FILENAME
=
"_qpdf_out.pdf"
...
...
@@ -115,7 +112,9 @@ def load_and_convert_image(
else
:
image_format
=
"unknown"
if
convert_to_base64
:
image_reference
=
"data:image/{format};base64,"
.
format
(
format
=
image_format
)
+
base64
.
b64encode
(
image_content
)
image_reference
=
"data:image/{format};base64,"
.
format
(
format
=
image_format
)
+
base64
.
b64encode
(
image_content
).
decode
(
"ascii"
)
else
:
image_reference
=
image_filepath
self
.
image_number
+=
1
...
...
docutils_extended/directives/jupyter_outbox_directive.py
View file @
684e42fe
...
...
@@ -10,6 +10,9 @@ class JupyterOutbox(Directive):
has_content
=
True
def
run
(
self
):
content
=
u
"
\n
"
.
join
(
self
.
content
)
latex
=
u
"
\\
begin{{minted}}[bgcolor=white,breakanywhere=true,breaklines=true,linenos=false]{{text}}
\n
{}
\n\\
end{{minted}}"
.
format
(
content
)
content
=
"
\n
"
.
join
(
self
.
content
)
latex
=
(
"
\\
begin{{minted}}[bgcolor=white,breakanywhere=true,breaklines=true,linenos=false]"
"{{text}}
\n
{}
\n\\
end{{minted}}"
).
format
(
content
)
return
[
nodes
.
raw
(
""
,
latex
,
format
=
"latex"
)]
docutils_extended/directives/output_mode.py
View file @
684e42fe
# -*- coding: utf-8 -*-
VALID_OUTPUT_MODES
=
(
"latex"
,
"html"
)
_output_mode
=
"latex"
...
...
docutils_extended/directives/tikz_directive.py
View file @
684e42fe
# -*- coding: utf-8 -*-
import
base64
import
codecs
import
jinja2
import
os
import
tempfile
import
shutil
import
subprocess
import
sys
from
PIL
import
Image
import
tempfile
import
jinja2
from
docutils
import
nodes
from
docutils.parsers.rst
import
Directive
from
docutils.parsers.rst
import
directives
,
states
from
docutils.nodes
import
fully_normalize_name
,
whitespace_normalize_name
from
docutils.parsers.rst
import
Directive
,
directives
,
states
from
docutils.parsers.rst.roles
import
set_classes
from
.output_mode
import
get_output_mod
e
from
PIL
import
Imag
e
from
.output_mode
import
get_output_mode
TEMPLATE_LATEX
=
u
"""
TEMPLATE_LATEX
=
"""
\\
documentclass{%- if document_options -%}[{{ document_options }}]{%- endif -%}{standalone}
{% if not do_not_use_default_packages -%}
\\
usepackage[utf8]{inputenc}
...
...
@@ -49,19 +47,7 @@ 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"
BUILD_DIRECTORY
=
'.'
class
TemporaryDirectory
(
object
):
def
__init__
(
self
):
self
.
tmp_dir
=
tempfile
.
mkdtemp
()
def
__enter__
(
self
):
return
self
.
tmp_dir
def
__exit__
(
self
,
exception_type
,
value
,
traceback
):
shutil
.
rmtree
(
self
.
tmp_dir
)
self
.
tmp_dir
=
None
BUILD_DIRECTORY
=
"."
def
render_tikz
(
tikz_code
,
options
,
output_png
=
False
,
output_svg
=
False
):
...
...
@@ -79,7 +65,7 @@ def render_tikz(tikz_code, options, output_png=False, output_svg=False):
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
:
with
tempfile
.
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
)
...
...
@@ -143,7 +129,7 @@ def convert_image_to_html_base64(image_filepath):
if
filetype
==
"svg"
:
filetype
=
"svg+xml"
with
open
(
image_filepath
.
encode
(
sys
.
getfilesystemencoding
()),
"rb"
)
as
image_file
:
encoded_string
=
base64
.
b64encode
(
image_file
.
read
()).
decode
(
'
utf-8
'
)
encoded_string
=
base64
.
b64encode
(
image_file
.
read
()).
decode
(
"
utf-8
"
)
return
"data:image/{format};base64,"
.
format
(
format
=
filetype
)
+
encoded_string
...
...
docutils_extended/sphinx_extension.py
View file @
684e42fe
...
...
@@ -4,6 +4,6 @@ from .directives import tikz_directive
def
setup
(
app
):
tikz_directive
.
BUILD_DIRECTORY
=
app
.
outdir
app
.
add_directive
(
'
tikz
'
,
directives
.
Tikz
)
app
.
connect
(
'
builder-inited
'
,
lambda
app
:
directives
.
set_output_mode
(
app
.
builder
.
format
))
return
{
'
version
'
:
'
0.1
'
}
app
.
add_directive
(
"
tikz
"
,
directives
.
Tikz
)
app
.
connect
(
"
builder-inited
"
,
lambda
app
:
directives
.
set_output_mode
(
app
.
builder
.
format
))
return
{
"
version
"
:
"
0.1
"
}
docutils_extended/writers/__init__.py
View file @
684e42fe
from
.rst2html_extended
import
main
as
rst2html_extended_main
from
.rst2latex_extended
import
main
as
rst2latex_extended_main
__all__
=
(
'rst2html_extended_main'
,
'rst2latex_extended_main'
)
__all__
=
(
"rst2html_extended_main"
,
"rst2latex_extended_main"
)
docutils_extended/writers/rst2html_extended.py
View file @
684e42fe
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# $Id: rst2html.py 4564 2006-05-21 20:44:42Z wiemann $
# Author: David Goodger <goodger@python.org>
# Copyright: This module has been placed in the public domain.
#!/usr/bin/env python3
"""
A customized front end to the Docutils Publisher, producing HTML with support for embedded images and Tikz figures.
Based on the original `rst2html.py`
"""
try
:
...
...
@@ -16,11 +12,11 @@ try:
except
:
pass
from
docutils.core
import
publish_cmdline
,
default_description
from
docutils.core
import
default_description
,
publish_cmdline
from
docutils.parsers.rst
import
directives
from
docutils.parsers.rst.directives.body
import
ParsedLiteral
from
..directives
import
EmbeddedImage
,
EmbeddedFigure
,
Tikz
,
TikzFigure
,
set_output_mode
from
..directives
import
EmbeddedFigure
,
EmbeddedImage
,
Tikz
,
TikzFigure
,
set_output_mode
description
=
"Generates (X)HTML documents from standalone reStructuredText "
"sources. "
+
default_description
...
...
@@ -35,5 +31,5 @@ def main():
publish_cmdline
(
writer_name
=
"html"
,
description
=
description
)
if
__name__
==
'
__main__
'
:
if
__name__
==
"
__main__
"
:
main
()
docutils_extended/writers/rst2latex_extended.py
View file @
684e42fe
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# $Id: rst2latex.py 5905 2009-04-16 12:04:49Z milde $
# Author: David Goodger <goodger@python.org>
# Copyright: This module has been placed in the public domain.
#!/usr/bin/env python3
"""
A customized front end to the Docutils Publisher, producing LaTeX with valid codeblocks using the listings package and
support for Tikz images.
Based on the original `rst2latex.py`
"""
try
:
...
...
@@ -19,8 +15,8 @@ except:
from
docutils.core
import
publish_cmdline
from
docutils.parsers.rst
import
directives
from
..directives
import
CodeBlock
,
EmbeddedImage
,
EmbeddedFigure
,
JupyterOutbox
,
Tikz
,
TikzFigure
,
set_output_mode
from
..directives
import
CodeBlock
,
EmbeddedFigure
,
EmbeddedImage
,
JupyterOutbox
,
Tikz
,
TikzFigure
,
set_output_mode
description
=
(
"Generates LaTeX documents from standalone reStructuredText "
...
...
@@ -44,5 +40,5 @@ def main():
publish_cmdline
(
writer_name
=
"latex"
,
description
=
description
)
if
__name__
==
'
__main__
'
:
if
__name__
==
"
__main__
"
:
main
()
requirements.txt
deleted
100644 → 0
View file @
58108e8c
docutils
jinja2
pillow
pygments
requests
setup.py
View file @
684e42fe
# -*- coding: utf-8 -*-
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
from
__future__
import
unicode_literals
import
codecs
import
os
import
runpy
import
subprocess
from
setuptools
import
setup
,
find_packages
from
setuptools
import
find_packages
,
setup
def
get_version_from_pyfile
(
version_file
=
"docutils_extended/_version.py"
):
...
...
@@ -17,57 +9,34 @@ def get_version_from_pyfile(version_file="docutils_extended/_version.py"):
return
file_globals
[
"__version__"
]
def
get_install_requires_from_requirements
(
requirements_filename
=
"requirements.txt"
):
try
:
with
codecs
.
open
(
requirements_filename
,
"r"
,
"utf-8"
)
as
requirements_file
:
requirements
=
requirements_file
.
readlines
()
except
OSError
:
import
logging
logging
.
warning
(
"Could not read the requirements file."
)
return
requirements
def
get_long_description_from_readme
(
readme_filename
=
"README.md"
):
rst_filename
=
"{}.rst"
.
format
(
os
.
path
.
splitext
(
os
.
path
.
basename
(
readme_filename
))[
0
])
created_tmp_rst
=
False
if
not
os
.
path
.
isfile
(
rst_filename
):
try
:
subprocess
.
check_call
([
"pandoc"
,
readme_filename
,
"-t"
,
"rst"
,
"-o"
,
rst_filename
])
created_tmp_rst
=
True
except
(
OSError
,
subprocess
.
CalledProcessError
):
import
logging
logging
.
warning
(
"Could not convert the readme file to rst."
)
long_description
=
None
if
os
.
path
.
isfile
(
r
st
_filename
):
with
codecs
.
open
(
r
st
_filename
,
"r"
,
"utf-8"
)
as
readme_file
:
if
os
.
path
.
isfile
(
r
eadme
_filename
):
with
open
(
r
eadme
_filename
,
"r"
,
encoding
=
"utf-8"
)
as
readme_file
:
long_description
=
readme_file
.
read
()
if
created_tmp_rst
:
os
.
remove
(
rst_filename
)
return
long_description
version
=
get_version_from_pyfile
()
long_description
=
get_long_description_from_readme
()
install_requires
=
get_install_requires_from_requirements
()
setup
(
name
=
"docutils-extended"
,
version
=
version
,
packages
=
find_packages
(),
python_requires
=
"
>=2.7
"
,
install_requires
=
install_requires
,
python_requires
=
"
~=3.5
"
,
install_requires
=
[
"docutils"
,
"jinja2"
,
"pillow"
,
"pygments"
,
"requests"
]
,
entry_points
=
{
"console_scripts"
:
[
"rst2latex-extended = docutils_extended:rst2latex_extended_main"
,
"rst2html-extended = docutils_extended:rst2html_extended_main"
,
]
},
author
=
"Ingo
Heimbach
"
,
author_email
=
"i.
heimbach
@fz-juelich.de"
,
author
=
"Ingo
Meyer
"
,
author_email
=
"i.
meyer
@fz-juelich.de"
,
description
=
"HTML and LaTeX rst writer with support for extra directives"
,
long_description
=
long_description
,
long_description_content_type
=
"text/markdown"
,
license
=
"MIT"
,
url
=
"https://iffgit.fz-juelich.de/doc-utils/doc-utils"
,
keywords
=
[
"docutils"
,
"rst"
,
"reStructuredText"
,
"TikZ"
],
...
...
@@ -77,10 +46,15 @@ setup(
"Intended Audience :: Developers"
,
"License :: OSI Approved :: MIT License"
,
"Operating System :: MacOS"
,
"Operating System ::
Uni
x"
,
"Operating System ::
POSIX :: Linu
x"
,
"Programming Language :: Python"
,
"Programming Language :: Python :: 2"
,
"Programming Language :: Python :: 3"
,
"Programming Language :: Python :: 3.5"
,
"Programming Language :: Python :: 3.6"
,
"Programming Language :: Python :: 3.7"
,
"Programming Language :: Python :: 3.8"
,
"Programming Language :: Python :: 3.9"
,
"Programming Language :: Python :: 3 :: Only"
,
"Topic :: Documentation"
,
"Topic :: Software Development :: Documentation"
,
],
...
...
styles/rst2html_githublike_fzj.css
View file @
684e42fe
...
...
@@ -2,8 +2,8 @@
Copyright (c) 2013 Chad Skeeters (goobsoft@gmail.com)
:Author: Ingo
Heimbach
:Contact: i.
heimbach
@fz-juelich.de
:Author: Ingo
Meyer
:Contact: i.
meyer
@fz-juelich.de
Stylesheet for use with Docutils/rst2html.
Can be used with `--syntax-highlight=short` or
...
...
styles/rst2latex_fzj.sty
View file @
684e42fe
% Author: Ingo
Heimbach
% Contact: i.
heimbach
@fz-juelich.de
% Author: Ingo
Meyer
% Contact: i.
meyer
@fz-juelich.de
%
% Style for use with Docutils/rst2latex.
...
...
styles/rst2xetex_fzj.sty
View file @
684e42fe
% Author: Ingo
Heimbach
% Contact: i.
heimbach
@fz-juelich.de
% Author: Ingo
Meyer
% Contact: i.
meyer
@fz-juelich.de
%
% Style for use with Docutils/rst2latex/lualatex.
...
...
Write
Preview
Supports
Markdown
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