grace/typst_web_magic
git clone https://git.gae.moe/grace/typst_web_magic
5b6c1ba
main
1{ pkgs ? import <nixpkgs> { } } : 2 3let 4inherit ( pkgs ) lib ; 5 6package = pkgs . runCommand "typst_web_magic-0.1.0" { } '' 7destination =" $ out /share/typst/packages/local/typst_web_magic/0.1.0" 8mkdir -p " $ destination " 9cp ${ ./lib.typ } " $ destination /lib.typ" 10cp ${ ./pdf.typ } " $ destination /pdf.typ" 11cp ${ ./metadata.typ } " $ destination /metadata.typ" 12cp ${ ./blog.typ } " $ destination /blog.typ" 13cp ${ ./default.css } " $ destination /default.css" 14cp -r ${ ./color } " $ destination /color" 15cp ${ ./typst.toml } " $ destination /typst.toml" 16'' ; 17 18files = directory : 19let entries = builtins . readDir directory ; 20in lib . concatMap ( name : 21if entries .${ name } == "directory" then 22map ( child :" ${ name } / ${ child } " ) ( files ( directory + "/ ${ name } " )) 23else lib . optional ( entries .${ name } == "regular" ) name 24) ( builtins . attrNames entries ); 25 26content = src : 27let 28pages = builtins . filter ( lib . hasSuffix ".typ" ) ( files ( src + "/src" )); 29assets = directory :prefix : 30if builtins . pathExists ( src + "/ ${ directory } " ) then 31map ( file :'' 32(path: ${ builtins . toJSON ( prefix + file )} , source: path( ${ builtins . toJSON "/ ${ directory } / ${ file } " } )), 33'' ) ( files ( src + "/ ${ directory } " )) 34else []; 35in pkgs . writeText "content.typ" '' 36#let pages = ( 37${ lib . concatMapStrings ( file : '' 38(path: ${ builtins . toJSON ( lib . removeSuffix ".typ" file + ".html" )} , entry: { 39import ${ builtins . toJSON "/src/ ${ file } " } as source 40source.entry 41}, body: include ${ builtins . toJSON "/src/ ${ file } " } ), 42'' ) pages } 43) 44#let assets = ( 45${ lib . concatStrings ( assets "assets" "assets/" ++ assets "public" "" )} 46) 47'' ; 48 49clean = src :lib . cleanSourceWith { 50src = lib . cleanSource src ; 51filter = path :type : 52let name = baseNameOf path ; 53in ! ( builtins . elem name [ "site" ".generated" ".jj" "__pycache__" ]) 54&& ! ( lib . hasPrefix "result" name ); 55}; 56 57environment = fonts :{ 58nativeBuildInputs = [ pkgs . typst ]; 59TYPST_PACKAGE_PATH = " ${ package } /share/typst/packages" ; 60TYPST_FONT_PATHS = lib . makeSearchPath "share/fonts" ([ pkgs . montserrat pkgs . fira-math ] ++ fonts ); 61}; 62 63mkSite = { src , name ? "website" , fonts ? [] } : 64let 65source = clean src ; 66prepare = '' 67mkdir -p .generated 68cp -f ${ content ( toString source )} .generated/content.typ 69'' ; 70in pkgs . stdenvNoCC . mkDerivation (( environment fonts ) // { 71pname = name ; 72version = "0.1.0" ; 73src = source ; 74shellHook = prepare ; 75passthru . shell = pkgs . mkShell (( environment fonts ) // { 76packages = [ pkgs . tinymist ]; 77shellHook = prepare ; 78}); 79buildPhase = '' 80runHook preBuild 81${ prepare } 82typst compile --format bundle --features bundle,html site.typ site 83runHook postBuild 84'' ; 85installPhase = '' 86runHook preInstall 87mkdir -p " $ out " 88cp -r site/. " $ out /" 89runHook postInstall 90'' ; 91}); 92 93mkPdf = { src , name ? "document" , main ? "main.typ" , fonts ? [] } : 94pkgs . stdenvNoCC . mkDerivation (( environment fonts ) // { 95pname = name ; 96version = "0.1.0" ; 97src = clean src ; 98passthru . shell = pkgs . mkShell (( environment fonts ) // { 99packages = [ pkgs . tinymist ]; 100}); 101buildPhase = '' 102runHook preBuild 103typst compile --root . ${ lib . escapeShellArg main } document.pdf 104runHook postBuild 105'' ; 106installPhase = '' 107runHook preInstall 108mkdir -p " $ out " 109cp document.pdf " $ out /" ${ lib . escapeShellArg ( name + ".pdf" )} 110runHook postInstall 111'' ; 112}); 113in { 114inherit package mkSite mkPdf ; 115init = pkgs . writeShellApplication { 116name = "typst-web-init" ; 117runtimeInputs = [ pkgs . coreutils ]; 118text = '' 119if [ " $ #" -ne 1 ]; then 120echo "Usage: typst-web-init DIRECTORY" >&2 121exit 1 122fi 123 124destination =" $ 1 " 125mkdir -- " $ destination " 126cp -R --no-preserve=mode ${ ./template } /. " $ destination /" 127echo "Created $ destination . Edit site.typ, then run nix-build inside it." 128'' ; 129}; 130}