Inline SVG

This commit is contained in:
Tony Garnock-Jones 2022-02-11 13:53:48 +01:00
parent 82cbcee377
commit 7660b22d28
3 changed files with 25 additions and 16 deletions

View File

@ -7,3 +7,6 @@ title = "The Synit Manual"
[preprocessor.ditaa]
command = "./mdbook-ditaa figures/ditaa"
[output.html]
additional-css = ["style.css"]

View File

@ -65,28 +65,31 @@ def expand_ditaa(m, context, directory):
if cached:
svg = cached[0][0]
else:
svg = subprocess.check_output(['ditaa', '--svg', '-'], input=ditaa_source.encode('utf-8'))
svg = subprocess.check_output(['ditaa', '-T', '--svg', '-'], input=ditaa_source.encode('utf-8'))
svg = svg.decode('utf-8')
cache.cursor().execute('INSERT INTO diagrams VALUES (?, ?)', (ditaa_source, svg))
cache.cursor().execute('COMMIT')
svgfilename = os.path.join(sourcedir, output_prefix, filename)
os.makedirs(os.path.dirname(svgfilename), exist_ok=True)
# svgfilename = os.path.join(sourcedir, output_prefix, filename)
# os.makedirs(os.path.dirname(svgfilename), exist_ok=True)
need_write = True
if os.path.exists(svgfilename):
with open(svgfilename, 'rt') as f:
existing = f.read()
if existing == svg:
need_write = False
if need_write:
sys.stderr.write('updated\n')
with open(svgfilename, 'wt') as f:
f.write(svg)
else:
sys.stderr.write('unchanged\n')
# need_write = True
# if os.path.exists(svgfilename):
# with open(svgfilename, 'rt') as f:
# existing = f.read()
# if existing == svg:
# need_write = False
# if need_write:
# sys.stderr.write('updated\n')
# with open(svgfilename, 'wt') as f:
# f.write(svg)
# else:
# sys.stderr.write('unchanged\n')
return f'<p><img class="ditaa" alt="{filename}" src="{os.path.join(baseurl, output_prefix, filename)}"></p>\n'
# return f'<p><img class="ditaa" alt="{filename}" src="{os.path.join(baseurl, output_prefix, filename)}"></p>\n'
sys.stderr.write('inlined\n')
return f'<div class="ditaa">{svg}</div>\n'
def structure(items):
answer = []

3
style.css Normal file
View File

@ -0,0 +1,3 @@
svg text {
font-family: "Source Code Pro", Consolas, "Ubuntu Mono", Menlo, "DejaVu Sans Mono", monospace, monospace !important;
}