IffGit has a new shared runner for building Docker images in GitLab CI. Visit https://iffgit.fz-juelich.de/examples/ci-docker-in-docker for more details.

Commit 684e42fe authored by Ingo Meyer's avatar Ingo Meyer
Browse files

Merge branch 'develop'

parents 58108e8c 0361864b
.*
!.gitignore
*.pyc
__pycache__/
build/
dist/
*.egg-info/
include LICENSE
include README.md
......@@ -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:
......
from .writers import rst2html_extended_main, rst2latex_extended_main
__all__ = ('rst2html_extended_main', 'rst2latex_extended_main')
__all__ = ("rst2html_extended_main", "rst2latex_extended_main")
__version_info__ = (0, 3, 0)
__version__ = '.'.join(map(str, __version_info__))
__version_info__ = (0, 4, 0)
__version__ = ".".join(map(str, __version_info__))
from .code_directive import CodeBlock
from .embedded_image_directive import EmbeddedImage, EmbeddedFigure
from .embedded_image_directive import EmbeddedFigure, EmbeddedImage
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",
)
......@@ -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")]
# -*- 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_mode
from PIL import Image
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
......
......@@ -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")]
# -*- coding: utf-8 -*-
VALID_OUTPUT_MODES = ("latex", "html")
_output_mode = "latex"
......
# -*- 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_mode
from PIL import Image
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
......
......@@ -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"}
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")
#!/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()
#!/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()
# -*- 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(rst_filename):
with codecs.open(rst_filename, "r", "utf-8") as readme_file:
if os.path.isfile(readme_filename):
with open(readme_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 :: Unix",
"Operating System :: POSIX :: Linux",
"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",
],
......
......@@ -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
......
% Author: Ingo Heimbach
% Contact: i.heimbach@fz-juelich.de
% Author: Ingo Meyer
% Contact: i.meyer@fz-juelich.de
%
% Style for use with Docutils/rst2latex.
......
% 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.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment