From 3e230d40ab1255aec292df17d7b127b681a55710 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Thu, 1 Oct 2020 22:40:43 +0200 Subject: DAMMIT ox-md does not syntax-highlight source blocks, and trips over definition lists. --- repo/www/helpers.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'repo/www/helpers.py') diff --git a/repo/www/helpers.py b/repo/www/helpers.py index dbab622..34f274f 100644 --- a/repo/www/helpers.py +++ b/repo/www/helpers.py @@ -3,7 +3,8 @@ from dataclasses import dataclass, field from itertools import chain from os import environ, path from pathlib import Path -from subprocess import run +from subprocess import CalledProcessError, run +from tempfile import NamedTemporaryFile from typing import Dict, Iterator, Union @@ -57,6 +58,42 @@ def deserialize_directories(directories): } +class _NullPreprocessor: + def __init__(self, source_path): + self._source_path = source_path + + def __enter__(self): + self.output = self._source_path + return self + + def __exit__(self, *args): + pass + +class _OrgPreprocessor: + def __init__(self, source_path): + self._source_path = source_path + + def __enter__(self): + self._output = NamedTemporaryFile(mode='w+', suffix='.md') + try: + run(( + 'emacs', '-Q', '--batch', '--load', 'preprocess-org.el', + '--eval', f'(preprocess-org "{self._source_path}")' + ), check=True, stdout=self._output) + except CalledProcessError: + self._output.close() + raise + + self.output = self._output.name + return self + + def __exit__(self, *args): + self._output.close() + +_PREPROCESSORS = defaultdict(lambda: _NullPreprocessor, + (('org', _OrgPreprocessor),)) + + _PathArg = Union[Path, str, bytes] @dataclass @@ -69,8 +106,7 @@ class PandocRunner: def run(self, page, include_after=(), metadata=None): cmd = ( - 'pandoc', '-s', page, '-o', self.output, - '--template', self.template, + 'pandoc', '-s', '-o', self.output, '--template', self.template, *chain(*(('--lua-filter', f) for f in self.filters)), *chain(*(('--css', s) for s in self.stylesheets)), *chain(*(('--include-after-body', f) for f in include_after)) @@ -85,7 +121,13 @@ class PandocRunner: )) environ['LUA_PATH'] = '.cache/?.lua;;' - run(cmd, check=True) + + _, ext = path.splitext(page) + preprocessor = _PREPROCESSORS[ext[1:]] + + with preprocessor(page) as preproc: + cmd = cmd + (preproc.output,) + run(cmd, check=True) def generate_crumbs(target): -- cgit v1.2.3