summaryrefslogtreecommitdiff
path: root/repo/www/helpers.py
diff options
context:
space:
mode:
authorKévin Le Gouguec <kevin.legouguec@gmail.com>2020-09-28 22:10:56 +0200
committerKévin Le Gouguec <kevin.legouguec@gmail.com>2020-10-01 22:53:27 +0200
commitbe0d907de78c4689708573c03105059e04d2419f (patch)
tree14e259ecdb20af1006a38bd20fd2f017186ed2ea /repo/www/helpers.py
parent50aef4a8e2b6a53c12febf3728dfd0587915d248 (diff)
downloadmemory-leaks-be0d907de78c4689708573c03105059e04d2419f.tar.xz
Factor some code out
To make it easier to add a pre-processing step for Org files.
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):