1import os
2import tempfile
3from jinja2 import Template
4from bs4 import BeautifulSoup
5
6from plasTeX import NoCharSubEnvironment, Command
7from plasTeX.PackageResource import PackageResource
8from plasTeX.Packages import tikz
9
10from plasTeX.Logging import getLogger
11log = getLogger()
12
13class tikzcd(NoCharSubEnvironment):
14    """
15    A tikz-cd diagram whose content will be converted in the processFileContent callback.
16    """
17
18
19    class ar(Command):
20        pass
21
22    class rar(Command):
23        pass
24
25    class lar(Command):
26        pass
27
28    class uar(Command):
29        pass
30
31    class drar(Command):
32        pass
33
34    class dar(Command):
35        pass
36
37    class dlar(Command):
38        pass
39
40    class ular(Command):
41        pass
42
43    class urar(Command):
44        pass
45
46
47def ProcessOptions(options, document):
48    """This is called when the package is loaded."""
49
50    try:
51        with open(document.config['html5']['tikz-cd-template'], "r") as file:
52            template = file.read()
53    except IOError:
54        log.info('Using default TikZ template.')
55        template = u"\\documentclass{standalone}\n\\usepackage{tikz-cd}" + \
56                   u"\\begin{document}{{ tikzpicture }}\\end{document}"
57    document.userdata['tikzcd'] = {
58            'template': Template(template),
59            'tmp_dir': tempfile.mkdtemp(),
60            'compiler': document.config['html5']['tikz-compiler'],
61            'pdf2svg': document.config['html5']['tikz-converter'],
62            }
63
64    def convert(document, content):
65        return tikz.tikzConvert(
66                document,
67                content,
68                'tikzcd',
69                'Commutative diagram')
70
71    cb = PackageResource(
72            renderers='html5',
73            key='processFileContents',
74            data=convert)
75    document.addPackageResource(cb)
76