summaryrefslogtreecommitdiffstats
path: root/mdbook.nix
diff options
context:
space:
mode:
Diffstat (limited to 'mdbook.nix')
-rw-r--r--mdbook.nix131
1 files changed, 70 insertions, 61 deletions
diff --git a/mdbook.nix b/mdbook.nix
index e55606a..931dbc1 100644
--- a/mdbook.nix
+++ b/mdbook.nix
@@ -1,15 +1,18 @@
1{ outputAttrPath, optionsAttrPath, optionsInternal ? true, }:
2
3{ config, lib, pkgs, ... }:
4
5with lib;
6
7let
8 cfg = getAttrFromPath (optionsAttrPath ++ [ "mdbook" ]) config;
9
10 documentation-highlighter = pkgs.callPackage ./documentation-highlighter { };
11in
12{ 1{
2 outputAttrPath,
3 optionsAttrPath,
4 optionsInternal ? true,
5}: {
6 config,
7 lib,
8 pkgs,
9 ...
10}:
11with lib; let
12 cfg = getAttrFromPath (optionsAttrPath ++ ["mdbook"]) config;
13
14 documentation-highlighter = pkgs.callPackage ./documentation-highlighter {};
15in {
13 options = setAttrByPath optionsAttrPath { 16 options = setAttrByPath optionsAttrPath {
14 mdbook = { 17 mdbook = {
15 src = mkOption { 18 src = mkOption {
@@ -21,40 +24,45 @@ in
21 }; 24 };
22 25
23 pages = mkOption { 26 pages = mkOption {
24 type = with types; attrsOf (submodule ({ name, config, ... }: { 27 type = with types;
25 options = { 28 attrsOf (submodule ({
26 target = mkOption { 29 name,
27 type = types.str; 30 config,
28 default = name; 31 ...
29 description = '' 32 }: {
30 Where to install the page, relative to the `src/` directory. 33 options = {
31 ''; 34 target = mkOption {
32 internal = optionsInternal; 35 type = types.str;
33 }; 36 default = name;
34 37 description = ''
35 text = mkOption { 38 Where to install the page, relative to the `src/` directory.
36 type = types.lines; 39 '';
37 description = '' 40 internal = optionsInternal;
38 Content of the page. 41 };
39 ''; 42
40 internal = optionsInternal; 43 text = mkOption {
44 type = types.lines;
45 description = ''
46 Content of the page.
47 '';
48 internal = optionsInternal;
49 };
50
51 source = mkOption {
52 type = types.path;
53 description = ''
54 Path of the source file for this page.
55
56 If both `text` and `source` are defined, `source` takes
57 precedence.
58 '';
59 internal = optionsInternal;
60 };
41 }; 61 };
42 62
43 source = mkOption { 63 config.source = mkDefault (pkgs.writeText name config.text);
44 type = types.path; 64 }));
45 description = '' 65 default = {};
46 Path of the source file for this page.
47
48 If both `text` and `source` are defined, `source` takes
49 precedence.
50 '';
51 internal = optionsInternal;
52 };
53 };
54
55 config.source = mkDefault (pkgs.writeText name config.text);
56 }));
57 default = { };
58 example = { 66 example = {
59 "my-page.md".text = '' 67 "my-page.md".text = ''
60 # Title 68 # Title
@@ -89,44 +97,45 @@ in
89 }; 97 };
90 98
91 config = mkMerge [ 99 config = mkMerge [
92 (setAttrByPath (optionsAttrPath ++ [ "mdbook" ]) { 100 (setAttrByPath (optionsAttrPath ++ ["mdbook"]) {
93 pages."options.md".text = '' 101 pages."options.md".text = ''
94 # Available options 102 # Available options
95 103
96 You can use the following options: 104 You can use the following options:
97 105
98 106
99 ${readFile (getAttrFromPath (outputAttrPath ++ [ "doc-options-md" ]) config)} 107 ${readFile (getAttrFromPath (outputAttrPath ++ ["doc-options-md"]) config)}
100 ''; 108 '';
101 }) 109 })
102 110
103 (setAttrByPath outputAttrPath { 111 (setAttrByPath outputAttrPath {
104 # TODO: make pandoc pre-processor 112 # TODO: make pandoc pre-processor
105 mdbook = pkgs.runCommand "mdbook" 113 mdbook =
114 pkgs.runCommand "mdbook"
106 { 115 {
107 src = cfg.src; 116 src = cfg.src;
108 nativeBuildInputs = with pkgs; [ mdbook ]; 117 nativeBuildInputs = with pkgs; [mdbook];
109 } '' 118 } ''
110 unpackFile "$src" 119 unpackFile "$src"
111 chmod -R u+w . 120 chmod -R u+w .
112 cd */ 121 cd */
113 122
114 mkdir theme 123 mkdir theme
115 cp ${documentation-highlighter}/highlight.min.js theme/highlight.js 124 cp ${documentation-highlighter}/highlight.min.js theme/highlight.js
116 cp ${documentation-highlighter}/mono-blue.min.css theme/highlight.css 125 cp ${documentation-highlighter}/mono-blue.min.css theme/highlight.css
117 126
118 ${concatMapStrings (page: '' 127 ${concatMapStrings (page: ''
119 cp "${page.source}" "src/${page.target}" 128 cp "${page.source}" "src/${page.target}"
120 '') (attrValues cfg.pages)} 129 '') (attrValues cfg.pages)}
121 130
122 ${cfg.preBuild} 131 ${cfg.preBuild}
123 132
124 mdbook build 133 mdbook build
125 134
126 ${cfg.postBuild} 135 ${cfg.postBuild}
127 136
128 cp -r book "$out" 137 cp -r book "$out"
129 ''; 138 '';
130 }) 139 })
131 ]; 140 ];
132} 141}