From f95afe807f91c49802501f756e92211c3a553f04 Mon Sep 17 00:00:00 2001 From: Ivan Dolgov Date: Fri, 22 Sep 2017 17:08:45 +0300 Subject: Implement PlantUML support --- ql-ikiwiki-publish | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 6 deletions(-) diff --git a/ql-ikiwiki-publish b/ql-ikiwiki-publish index be5b1a1..01cc404 100755 --- a/ql-ikiwiki-publish +++ b/ql-ikiwiki-publish @@ -22,6 +22,81 @@ import shutil import subprocess import sys import tempfile +import fnmatch +import re + + +def process_uml_blocks(mdwnpath, mdwndir, filename): + blocks = [] + tmproot = os.path.join(T, os.path.splitext(filename)[0] + '_uml') + + with open(mdwnpath) as f: + content = f.read() + + # Find UML code blocks to be replaced. + blocks = re.findall( + r"(?:\t| {4})@startuml[\s\S]*?(?:\t| {4})@enduml", + content + ) + + for i, block in enumerate(blocks, start=1): + umlpath = '%s%d.puml' % (tmproot, i) + imgpath = '%s%d.png' % (tmproot, i) + + # Remove whitespace and save UML file. + with open(umlpath, 'w') as f: + f.write(re.sub(r"(?m)^(\t| {4})", "", block) + '\n') + + # Create a diagram image at imgpath and copy it. + subprocess.check_call(['plantuml', umlpath]) + shutil.copy2(imgpath, mdwndir) + + # Replace diagram code with a link to the image. + content = '' + with open(mdwnpath) as f: + content = f.read() + with open(mdwnpath, 'w') as f: + link = '[[!img %s]]' % os.path.basename(imgpath) + f.write(re.sub(block, link, content)) + + +def process_uml_includes(mdwnpath, mdwndir): + includes = [] + + with open(mdwnpath) as f: + content = f.read() + + # Find include statements, + # put file name in a separate group. + includes = re.findall( + r"(?m)^()", + content + ) + + for inc in includes: + # Don't need to copy an image this time + subprocess.check_call(['plantuml', os.path.join(mdwndir, inc[1])]) + + # Replace include statement with a link to the image. + content = '' + with open(mdwnpath) as f: + content = f.read() + with open(mdwnpath, 'w') as f: + link = '[[!img %s.png]]' % os.path.splitext(inc[1])[0] + f.write(re.sub('(?m)^' + inc[0], link, content)) + + +def process_uml(T, dirname='.', ignore=['.git', '.ikiwiki']): + for topdir, dirs, files in os.walk(os.path.abspath(dirname), topdown=True): + # Filter dirs by modifying the list in-place, + # as described in os.walk documentation. + dirs[:] = [d for d in dirs if d not in ignore] + + for filename in fnmatch.filter(files, '*.mdwn'): + mdwnpath = os.path.join(topdir, filename) + + process_uml_blocks(mdwnpath, topdir, filename) + process_uml_includes(mdwnpath, topdir) def mangle_setup(src, dest, html): @@ -42,18 +117,23 @@ def mangle_setup(src, dest, html): f.write(mangled) -static_http = sys.argv[1] -dirname = sys.argv[2] -rsync_target = 'static@{}:/srv/http/{}/.'.format(static_http, dirname) - -T = tempfile.mkdtemp() -try: +def run_ikiwiki(T): setup = os.path.join(T, 'ikiwiki.setup') html = os.path.join(T, 'html') mangle_setup('ikiwiki.setup', setup, html) subprocess.check_call(['ikiwiki', '--setup', setup, '--gettime']) subprocess.check_call( ['rsync', '-ahHSvs', '--delete', html + '/.', rsync_target]) + + +static_http = sys.argv[1] +dirname = sys.argv[2] +rsync_target = 'static@{}:/srv/http/{}/.'.format(static_http, dirname) + +T = tempfile.mkdtemp() +try: + process_uml(T) + run_ikiwiki(T) except BaseException as e: shutil.rmtree(T) raise -- cgit v1.2.1