1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-12-23 10:19:48 +01:00

pandoc: add new module

Add a module for pandoc that provides the following:

1. Setting default configuration options.
2. Installing templates.
3. Installing citation styles.
This commit is contained in:
Kirill Elagin 2022-01-01 14:51:06 -05:00 committed by Robert Helgesson
parent e622c5d836
commit c47c350f65
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
14 changed files with 227 additions and 0 deletions

3
.github/CODEOWNERS vendored
View file

@ -176,6 +176,9 @@
/modules/programs/openssh.nix @rycee
/modules/programs/pandoc.nix @kirelagin
/tests/modules/programs/pandoc @kirelagin
/modules/programs/password-store.nix @pacien
/modules/programs/pazi.nix @marsam

View file

@ -2380,6 +2380,13 @@ in
A new module is available: 'programs.tint2'.
'';
}
{
time = "2022-01-22T17:39:20+00:00";
message = ''
A new module is available: 'programs.pandoc'.
'';
}
];
};
}

View file

@ -117,6 +117,7 @@ let
./programs/octant.nix
./programs/offlineimap.nix
./programs/opam.nix
./programs/pandoc.nix
./programs/password-store.nix
./programs/pazi.nix
./programs/pet.nix

104
modules/programs/pandoc.nix Normal file
View file

@ -0,0 +1,104 @@
{ config, lib, pkgs, ... }:
let
cfg = config.programs.pandoc;
inherit (lib) literalExpression mkEnableOption mkIf mkOption types;
jsonFormat = pkgs.formats.json { };
makeTemplateFile = name: file:
lib.nameValuePair "pandoc/templates/${name}" { source = file; };
getFileName = file:
# This is actually safe here, since it is just a file name
builtins.unsafeDiscardStringContext (baseNameOf file);
makeCslFile = file:
lib.nameValuePair "pandoc/csl/${getFileName file}" { source = file; };
in {
meta.maintainers = [ lib.maintainers.kirelagin ];
options.programs.pandoc = {
enable = mkEnableOption "pandoc";
package = mkOption {
type = types.package;
default = pkgs.pandoc;
defaultText = literalExpression "pkgs.pandoc";
description = "The pandoc package to use.";
};
# We wrap the executable to pass some arguments
finalPackage = mkOption {
type = types.package;
readOnly = true;
description = "Resulting package.";
};
defaults = mkOption {
type = jsonFormat.type;
default = { };
example = literalExpression ''
{
metadata = {
author = "John Doe";
};
pdf-engine = "xelatex";
citeproc = true;
}
'';
description = ''
Options to set by default.
These will be converted to JSON and written to a defaults
file (see Default files in pandoc documentation).
'';
};
defaultsFile = mkOption {
type = types.path;
readOnly = true;
description = "Resulting defaults file.";
};
templates = mkOption {
type = types.attrsOf types.path;
default = { };
example = literalExpression ''
{
"default.latex" = path/to/your/template;
}
'';
description = "Custom templates.";
};
citationStyles = mkOption {
type = types.listOf types.path;
default = [ ];
example = literalExpression "[ path/to/file.csl ]";
description = "List of .csl files to install.";
};
};
config = mkIf cfg.enable {
programs.pandoc = {
defaultsFile = jsonFormat.generate "hm.json" cfg.defaults;
finalPackage = pkgs.symlinkJoin {
name = "pandoc-with-defaults";
paths = [ cfg.package ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram "$out/bin/pandoc" \
--add-flags '--defaults "${cfg.defaultsFile}"'
'';
};
};
home.packages = [ cfg.finalPackage ];
xdg.dataFile = lib.mapAttrs' makeTemplateFile cfg.templates
// lib.listToAttrs (map makeCslFile cfg.citationStyles);
};
}

View file

@ -86,6 +86,7 @@ import nmt {
./modules/programs/nix-index
./modules/programs/nnn
./modules/programs/nushell
./modules/programs/pandoc
./modules/programs/pet
./modules/programs/powerline-go
./modules/programs/qutebrowser

View file

@ -0,0 +1,18 @@
{ config, ... }:
{
programs.pandoc = {
enable = true;
citationStyles = [ ./example.csl ];
};
test.stubs.pandoc = import ./stub.nix;
nmt.script = ''
assertFileExists home-files/.local/share/pandoc/csl/example.csl
assertFileContent home-files/.local/share/pandoc/csl/example.csl \
${./example.csl}
'';
}

View file

@ -0,0 +1,5 @@
{
pandoc-citation-styles = ./csl.nix;
pandoc-defaults = ./defaults.nix;
pandoc-templates = ./templates.nix;
}

View file

@ -0,0 +1,7 @@
{
"citeproc": true,
"metadata": {
"author": "John Doe"
},
"pdf-engine": "xelatex"
}

View file

@ -0,0 +1,30 @@
{ config, lib, ... }:
let cfg = config.programs.pandoc;
in {
config = lib.mkIf config.test.enableBig {
programs.pandoc = {
enable = true;
defaults = {
metadata = { author = "John Doe"; };
pdf-engine = "xelatex";
citeproc = true;
};
};
nmt.script = ''
assertFileContent ${cfg.defaultsFile} ${./defaults-expected.json}
# Test that defaults are set by looking at the metadata for an empty file
# (it should contain the author that we set in defaults).
output=$(mktemp)
${cfg.finalPackage}/bin/pandoc --standalone \
-f markdown /dev/null \
-t native -o "$output"
assertFileContent "$output" ${./output-expected}
'';
};
}

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" version="1.0" default-locale="en-US">
<info>
<title>A trivial CSL style for tests</title>
<id>https://github.com/nix-community/home-manager/tree/master/tests/modules/tools/pandoc/example.csl</id>
<link href="https://github.com/nix-community/home-manager/tree/master/tests/modules/tools/pandoc/example.csl" rel="self"/>
<link href=""https://www.zotero.org/styles/association-for-computing-machinery rel="independent-parent"/>
<link href="http://aem.asm.org/" rel="documentation"/>
<category citation-format="numeric"/>
<category field="engineering"/>
</info>
</style>

View file

@ -0,0 +1,2 @@
Pandoc (Meta {unMeta = fromList [("author",MetaString "John Doe")]})
[]

View file

@ -0,0 +1,10 @@
{
name = "pandoc-stub";
outPath = null;
buildScript = ''
mkdir -p "$out"/bin
pandoc="$out"/bin/pandoc
echo 'Stub to make the wrapper happy' > "$pandoc"
chmod a+x "$pandoc"
'';
}

View file

@ -0,0 +1,9 @@
\documentclass[a4paper]{scrartcl}
$if(title)$\title{$title$}$endif$
$if(author)$\author{$for(author)$$author$$sep$ \and $endfor$}$endif$
\begin{document}
\maketitle
$body$
\end{document}

View file

@ -0,0 +1,18 @@
{ config, ... }:
{
programs.pandoc = {
enable = true;
templates = { "default.latex" = ./template.latex; };
};
test.stubs.pandoc = import ./stub.nix;
nmt.script = ''
assertFileExists home-files/.local/share/pandoc/templates/default.latex
assertFileContent home-files/.local/share/pandoc/templates/default.latex \
${./template.latex}
'';
}