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.py44
1 files changed, 27 insertions, 17 deletions
diff --git a/repo/www/helpers.py b/repo/www/helpers.py
index 48ebccf..dbab622 100644
--- a/repo/www/helpers.py
+++ b/repo/www/helpers.py
@@ -2,8 +2,9 @@ from collections import defaultdict
from dataclasses import dataclass, field
from itertools import chain
from os import environ, path
+from pathlib import Path
from subprocess import run
-from typing import Iterator
+from typing import Dict, Iterator, Union
@dataclass
@@ -56,26 +57,35 @@ 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))
- )
+_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', page, '-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;;'
- run(cmd, check=True)
+ environ['LUA_PATH'] = '.cache/?.lua;;'
+ run(cmd, check=True)
def generate_crumbs(target):