summaryrefslogtreecommitdiff
path: root/default.nix
blob: 2eaa55e6cf3c45c1ba1e69b6650a54e774a1c41a (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
{
  pkgs,
  html,
  data,
}: let
  commonArgs = {
    inherit data html make;
    inherit (pkgs) lib;
  };
  make = path: overrides: let
    f = import path;
  in
    f ((builtins.intersectAttrs (builtins.functionArgs f) commonArgs)
      // overrides);

  indexHTML = builtins.toFile "index.html" (make ./html {});
  classlessCSS = let
    setOption = option: value: {"${option}" = value;};
    setOptions = options: value:
      builtins.foldl' (tmp: option: tmp // setOption option value) {} options;
    enable = options: setOptions options true;
    disable = options: setOptions options false;
  in
    builtins.toFile "classless.css" (make ./css/classless.nix
      (disable ["tables" "hr"]
        // enable [
          "tooltip-citations"
          "navbar"
          "details-cards"
          "big-first-letter"
          "printing"
          "grid"
        ]));

  robotsTXT = builtins.toFile "robots.txt" ''
    user-agent: *
    disallow: /static/
    allow: /static/icon.png
  '';
in
  pkgs.callPackage ({
    # Packages
    line-awesome,
    line-awesome-css,
    uncss,
    yuicompressor,
    imagemagick,
    # Source files
    index-html ? indexHTML,
    classless-css ? classlessCSS,
    files ? data.files,
    icon ? ./static/icon.png,
  }: let
    compress = "${yuicompressor}/bin/yuicompressor";
    clean = "${uncss}/bin/uncss";
    compressJPEG = size: ''
      ${imagemagick}/bin/convert -sampling-factor 4:2:0 \
                                 -strip \
                                 -quality 85 \
                                 -interlace JPEG \
                                 -colorspace RGB \
                                 -resize ${size} \
                                 -colorspace sRGB \
    '';
  in
    pkgs.runCommand "webpage" {} ''
      set -o xtrace
      mkdir $out && pushd "$_"
      ln -sT ${index-html} index.html
      ln -sT ${robotsTXT} robots.txt
      popd

      mkdir $out/static && pushd "$_"
      ln -sT ${icon} icon.png
      cp -r ${files} files
      chmod a+w files
      pushd files
      ${compressJPEG "128x128"} avatar.jpg avatar.jpg.128
      ${compressJPEG "256x256"} avatar.jpg avatar.jpg.256
      ${compressJPEG "512x512"} avatar.jpg avatar.jpg.512
      popd
      chmod a-w files
      popd

      mkdir -p $out/static/css && pushd "$_"
      ${clean} ${index-html} --stylesheets file://${classless-css} \
      | ${compress} --type css >classless.min.css
      popd

      mkdir -p $out/static/css/fonts/line-awesome && pushd "$_"
      ln -sT ${line-awesome}/share/fonts/woff2 webfonts
      ${clean} ${index-html} --stylesheets file://${line-awesome-css} \
      | ${compress} --type css >line-awesome.min.css
      popd

    '') {}