From 1ace76bd96a3554b4c1981bd90ce35bc86d3aff8 Mon Sep 17 00:00:00 2001 From: DavHau Date: Wed, 17 Jan 2024 14:13:09 +0700 Subject: [PATCH] automatically include docs from clan-core --- README.md | 15 ++++++--- flake-parts/website.nix | 67 +++++++++++++++++++++++++++++++++++++++++ flake.lock | 8 ++--- flake.nix | 17 +++-------- 4 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 flake-parts/website.nix diff --git a/README.md b/README.md index 771afff..0d07e93 100644 --- a/README.md +++ b/README.md @@ -5,15 +5,22 @@ We use [zola](https://www.getzola.org/) for building a static website. ## Build the homepage -``` -$ zola build +This will build the website including pages defined in the clan-core repo under `/docs`: + +```command +$ nix build ``` ## Start a local webserver +This server will include all the pages from clan-core, but doesn't automatically refresh. Also make sure to wipe your browsers cache after each change. + +```command +$ nix run .#serve ``` -$ zola serve -``` + +## zola build & serve +Alternatively `zola build` and `zola serve` can be used for development but the result will be missing the pages defined in the clan-core repo. ## Contributing diff --git a/flake-parts/website.nix b/flake-parts/website.nix new file mode 100644 index 0000000..db8d2e9 --- /dev/null +++ b/flake-parts/website.nix @@ -0,0 +1,67 @@ +{ + self, + inputs, + ... +}: { + perSystem = { + lib, + pkgs, + self', + ... + }: let + build = baseUrl: pkgs.runCommand "website" { + buildInputs = [ pkgs.zola ]; + } '' + mkdir -p $out + cp -r ${self}/* . + chmod -R u+w . + + substituteInPlace config.toml --replace \ + 'base_url = "https://clan.lol"' \ + 'base_url = "${baseUrl}"' \ + + # generates a zola compatible .md from a clan-core/docs/**/*.md + generatePage() { + local sourceFile="$1" + local targetFile="$2" + + # generate title by reading first non-empty line of $file and stripping all '#' symbols + title=$(sed -n '/./{p;q}' "$sourceFile" | sed 's/#*//g') + echo "generating page from clan-core: $title" + + # generate header with title, template, weight to make zola happy + echo -e "+++\ntitle = \"$title\"\ntemplate = \"docs/page.html\"\nweight = 0+++" > "$targetFile" + + # append everything from the file but remove header line starting with '#' and all preceding non-empty lines + tail -n +2 "$sourceFile" >> "$targetFile" + } + + # inject pages from clan-core + for dir in ${inputs.clan-core}/docs/*; do + subdir=$(basename $dir) + targetDir="content/docs/$subdir" + mkdir -p "$targetDir" + for file in ${inputs.clan-core}/docs/$subdir/*.md; do + target="content/docs/$subdir/$(basename $file)" + # only generate page if file doesn't start with _ + if [[ $(basename $file) == _* ]]; then + cat "$file" > "$target" + continue + fi + generatePage "$file" "$target" + done + done + + zola build + cp -r public/* public/.* $out + ''; + in { + packages.default = self'.packages.website; + packages.website = build "https://clan.lol"; + packages.website-localhost = build "http://localhost:1111"; + packages.serve = pkgs.writeScriptBin "serve-local" '' + ${pkgs.python3}/bin/python -m http.server 1111 \ + -d ${self'.packages.website-localhost} + ''; + }; +} diff --git a/flake.lock b/flake.lock index 551438b..90f5cd5 100644 --- a/flake.lock +++ b/flake.lock @@ -14,11 +14,11 @@ "treefmt-nix": "treefmt-nix" }, "locked": { - "lastModified": 1705080849, - "narHash": "sha256-GdBbhCgscYWF2uK0kbU8w30V7GlOQXVm/eXgLye3gR8=", + "lastModified": 1705474551, + "narHash": "sha256-nWBj2Ob/bxhf16BXV5gBnmDYbOSAWtZRCFUGWjtYnb4=", "ref": "refs/heads/main", - "rev": "07a0e1db0981c0db3ca6f7cbd8a68d86b2245eff", - "revCount": 1779, + "rev": "de65c4062af43e5a976254899da2b0734c7d263a", + "revCount": 1789, "type": "git", "url": "https://git.clan.lol/clan/clan-core" }, diff --git a/flake.nix b/flake.nix index f3b8194..a494068 100644 --- a/flake.nix +++ b/flake.nix @@ -10,24 +10,17 @@ clan-core.inputs.nixpkgs.follows = "nixpkgs"; }; - outputs = inputs@{ self, flake-parts, ... }: + outputs = inputs@{ self, flake-parts, clan-core, ... }: flake-parts.lib.mkFlake { inherit inputs; } ({ lib, ... }: { systems = [ "x86_64-linux" ]; imports = [ ./flake-parts/deploy.nix - ./flake-parts/new-post.nix ./flake-parts/devShells + ./flake-parts/new-post.nix + ./flake-parts/website.nix ]; - perSystem = { pkgs, ... }: { - packages.default = pkgs.runCommand "website" { - buildInputs = [ pkgs.zola ]; - } '' - mkdir -p $out - cp -r ${self}/* . - chmod -R u+w . - zola build - cp -r public/* public/.* $out - ''; + perSystem = {pkgs, ...}: { + formatter = pkgs.writeShellScriptBin "true" "true"; }; }); }