summaryrefslogtreecommitdiffstats
path: root/mdbook.nix
blob: e55606a2d9d8ac2099e4779051b954f65657ed90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{ outputAttrPath, optionsAttrPath, optionsInternal ? true, }:

{ config, lib, pkgs, ... }:

with lib;

let
  cfg = getAttrFromPath (optionsAttrPath ++ [ "mdbook" ]) config;

  documentation-highlighter = pkgs.callPackage ./documentation-highlighter { };
in
{
  options = setAttrByPath optionsAttrPath {
    mdbook = {
      src = mkOption {
        type = with types; either path package;
        description = ''
          Root directory of mdbook sources to compile.
        '';
        internal = optionsInternal;
      };

      pages = mkOption {
        type = with types; attrsOf (submodule ({ name, config, ... }: {
          options = {
            target = mkOption {
              type = types.str;
              default = name;
              description = ''
                Where to install the page, relative to the `src/` directory.
              '';
              internal = optionsInternal;
            };

            text = mkOption {
              type = types.lines;
              description = ''
                Content of the page.
              '';
              internal = optionsInternal;
            };

            source = mkOption {
              type = types.path;
              description = ''
                Path of the source file for this page.

                If both `text` and `source` are defined, `source` takes
                precedence.
              '';
              internal = optionsInternal;
            };
          };

          config.source = mkDefault (pkgs.writeText name config.text);
        }));
        default = { };
        example = {
          "my-page.md".text = ''
            # Title

            hello, world!
          '';
        };
        description = ''
          Pages to add to the source directory before building.
        '';
        internal = optionsInternal;
      };

      preBuild = mkOption {
        type = types.lines;
        description = ''
          Extra commands executed before running `mdbook build`.
        '';
        default = "";
        internal = optionsInternal;
      };

      postBuild = mkOption {
        type = types.lines;
        description = ''
          Extra commands executed after running `mdbook build`.
        '';
        default = "";
        internal = optionsInternal;
      };
    };
  };

  config = mkMerge [
    (setAttrByPath (optionsAttrPath ++ [ "mdbook" ]) {
      pages."options.md".text = ''
        # Available options

        You can use the following options:


        ${readFile (getAttrFromPath (outputAttrPath ++ [ "doc-options-md" ]) config)}
      '';
    })

    (setAttrByPath outputAttrPath {
      # TODO: make pandoc pre-processor
      mdbook = pkgs.runCommand "mdbook"
        {
          src = cfg.src;
          nativeBuildInputs = with pkgs; [ mdbook ];
        } ''
        unpackFile "$src"
        chmod -R u+w .
        cd */

        mkdir theme
        cp ${documentation-highlighter}/highlight.min.js theme/highlight.js
        cp ${documentation-highlighter}/mono-blue.min.css theme/highlight.css

        ${concatMapStrings (page: ''
          cp "${page.source}" "src/${page.target}"
        '') (attrValues cfg.pages)}

        ${cfg.preBuild}

        mdbook build

        ${cfg.postBuild}

        cp -r book "$out"
      '';
    })
  ];
}