tvl-depot/users/aspen/web/orgExportHTML.nix
sterni 7ebf6be3a3 feat(aspen/web/orgExportHTML): allow bringing your own config.el
`configFile` is chosen to match the terminology used in NixOS modules. I
think this is quite useful since you can do a lot via the config file
since you can not only set the HTML export setting via the org file
header, but also through various emacs variables (see
<https://orgmode.org/manual/HTML-specific-export-settings.html>).

Change-Id: If4d66348e3043f62782106e7fd34f5cf5c7071a4
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12651
Reviewed-by: aspen <root@gws.fyi>
Autosubmit: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
2024-12-25 18:43:28 +00:00

62 lines
1.4 KiB
Nix

{ pkgs, depot, ... }:
with pkgs;
with lib;
opts:
let
src = if isAttrs opts then opts.src else opts;
headline = if isAttrs opts then opts.headline else null;
configFile = opts.configFile or ./config.el;
bn = builtins.baseNameOf src;
filename = elemAt (splitString "." bn) 0;
outName =
if isNull headline
then
let
bn = builtins.baseNameOf src;
filename = elemAt (splitString "." bn) 0;
in
if depot.nix.utils.isDirectory src
then filename
else filename + ".html"
else "${filename}-${replaceStrings [" "] ["-"] filename}.html";
escapeDoubleQuotes = replaceStrings [ "\"" ] [ "\\\"" ];
navToHeadline = optionalString (! isNull headline) ''
(search-forward "${escapeDoubleQuotes headline}")
(org-narrow-to-subtree)
'';
in
runCommand outName { inherit src; } ''
buildFile() {
cp "$1" file.org
${pkgs.emacs}/bin/emacs --batch \
--load ${configFile} \
--visit file.org \
--eval "(progn
${escapeDoubleQuotes navToHeadline}
(org-html-export-to-html))" \
--kill
rm file.org
substitute file.html "$2" \
--replace-quiet '<title>&lrm;</title>' ""
rm file.html
}
if [ -d $src ]; then
for file in $src/*; do
result=''${file/$src/$out}
mkdir -p $(dirname $result)
buildFile $file ''${result/.org/.html}
done
else
buildFile $src $out
fi
''