summaryrefslogtreecommitdiff
path: root/repo/www/helpers.py
diff options
context:
space:
mode:
Diffstat (limited to 'repo/www/helpers.py')
-rw-r--r--repo/www/helpers.py88
1 files changed, 70 insertions, 18 deletions
diff --git a/repo/www/helpers.py b/repo/www/helpers.py
index 48ebccf..12d9a41 100644
--- a/repo/www/helpers.py
+++ b/repo/www/helpers.py
@@ -2,8 +2,10 @@ from collections import defaultdict
from dataclasses import dataclass, field
from itertools import chain
from os import environ, path
-from subprocess import run
-from typing import Iterator
+from pathlib import Path
+from subprocess import CalledProcessError, run
+from tempfile import NamedTemporaryFile
+from typing import Dict, Iterator, Union
@dataclass
@@ -56,26 +58,76 @@ def deserialize_directories(directories):
}
-def pandoc(page, output, template, filters, stylesheets, include_after=(),
- variables=None, metadata=None):
- cmd = (
- 'pandoc', '-s', page, '-o', output, '--template', template,
- *chain(*(('--lua-filter', f) for f in filters)),
- *chain(*(('--css', s) for s in stylesheets)),
- *chain(*(('--include-after-body', f) for f in include_after))
- )
+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='.org')
+ 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
+class PandocRunner:
+ output: _PathArg
+ template: _PathArg
+ filters: Iterator[_PathArg]
+ stylesheets: Iterator[_PathArg]
+ variables: Dict[str, str] = field(default_factory=dict)
+
+ def run(self, page, include_after=(), metadata=None):
+ cmd = (
+ '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))
+ )
- if variables is not None:
- cmd += tuple(chain(
- *(('-V', f'{k}={v}') for k, v in variables.items())
- ))
- if metadata is not None:
cmd += tuple(chain(
- *(('-M', f'{k}={v}') for k, v in metadata.items())
+ *(('-V', f'{k}={v}') for k, v in self.variables.items())
))
+ if metadata is not None:
+ cmd += tuple(chain(
+ *(('-M', f'{k}={v}') for k, v in metadata.items())
+ ))
+
+ environ['LUA_PATH'] = '.cache/?.lua;;'
+
+ _, ext = path.splitext(page)
+ preprocessor = _PREPROCESSORS[ext[1:]]
- environ['LUA_PATH'] = '.cache/?.lua;;'
- run(cmd, check=True)
+ with preprocessor(page) as preproc:
+ cmd = cmd + (preproc.output,)
+ run(cmd, check=True)
def generate_crumbs(target):