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 ++++++++++++++++++++++++++++++++++++++++++---- repo/www/preprocess-org.el | 20 +++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 repo/www/preprocess-org.el (limited to 'repo') 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): diff --git a/repo/www/preprocess-org.el b/repo/www/preprocess-org.el new file mode 100644 index 0000000..f7be936 --- /dev/null +++ b/repo/www/preprocess-org.el @@ -0,0 +1,20 @@ +(defun preprocess-org (input) + (with-temp-buffer + (insert-file-contents input) + (org-mode) + (while (re-search-forward org-heading-regexp nil t) + (save-excursion + (save-match-data + (when-let ((tags (org-get-tags (point)))) + (insert "\n#+begin_tags\n") + (dolist (tag tags) + (insert "- " tag "\n")) + (insert "#+end_tags\n"))))) + (let ((org-export-with-properties t) + (org-export-with-section-numbers nil) + (org-export-with-sub-superscripts '{}) + (org-export-with-tags nil) + (org-export-with-title nil) + (org-export-with-toc nil)) + (org-md-export-as-markdown)) + (princ (buffer-string)))) -- cgit v1.2.3