From a3759866b6eeafafe8ae304a4b19501bfa793c74 Mon Sep 17 00:00:00 2001 From: m1kadev <67912368+m1kadev@users.noreply.github.com> Date: Fri, 19 Dec 2025 19:06:07 +0100 Subject: [PATCH] update pastes; add templating --- .gitignore | 5 +-- .vscode/settings.json | 5 +++ bricks/base-header.html | 4 ++ bricks/extended-footer.html | 13 +++++++ bricks/footer.html | 10 +++++ bricks/highlight.html | 3 ++ bricks/navbar.html | 13 +++++++ build.exs | 75 ++++++++++++++++++++++++------------- pastes/main.rs | 3 ++ pastes/paste.html | 47 +++++------------------ pull-hljs.sh | 8 ++-- src/colophon.fxg | 4 +- src/colophon.html | 41 ++++---------------- src/index.html | 39 +++++-------------- src/index.js | 2 - 15 files changed, 135 insertions(+), 137 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 bricks/base-header.html create mode 100644 bricks/extended-footer.html create mode 100644 bricks/footer.html create mode 100644 bricks/highlight.html create mode 100644 bricks/navbar.html create mode 100644 pastes/main.rs diff --git a/.gitignore b/.gitignore index 1fab970ce6485aebafbaac07eb5ce01b19e6ee26..cfb90ebecbc71abdb19d3d72f5cbb75a19e53772 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,2 @@ /build -/static/highlight.min.js -/static/rust.min.js -/static/html.min.js -/static/arta.min.css \ No newline at end of file +/static/*.min.* \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..4c5fb46dd2396e93217cea58771cb78edc49b6f6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "*.html": "mustache" + } +} \ No newline at end of file diff --git a/bricks/base-header.html b/bricks/base-header.html new file mode 100644 index 0000000000000000000000000000000000000000..a16aa4b2f25d000d27f0de5c43456062e241d5ac --- /dev/null +++ b/bricks/base-header.html @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/bricks/extended-footer.html b/bricks/extended-footer.html new file mode 100644 index 0000000000000000000000000000000000000000..93974af2ca1c906285f688b19714a7dea2925297 --- /dev/null +++ b/bricks/extended-footer.html @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/bricks/footer.html b/bricks/footer.html new file mode 100644 index 0000000000000000000000000000000000000000..4ff7e91e24126d40173fd0b32ddd4e398c4225ba --- /dev/null +++ b/bricks/footer.html @@ -0,0 +1,10 @@ + \ No newline at end of file diff --git a/bricks/highlight.html b/bricks/highlight.html new file mode 100644 index 0000000000000000000000000000000000000000..74e32f7d11cf4a137be530f4133e628e84d73a67 --- /dev/null +++ b/bricks/highlight.html @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/bricks/navbar.html b/bricks/navbar.html new file mode 100644 index 0000000000000000000000000000000000000000..df7a27e05d9f1ce9c95f1db900385092bd6232d0 --- /dev/null +++ b/bricks/navbar.html @@ -0,0 +1,13 @@ +
+

.nl

+

m1kadev

+ +
\ No newline at end of file diff --git a/build.exs b/build.exs index bec6346f7a5472ed39aeeb4574f03fe0f9a9d234..1f6e2418405533e926119247442941f1fe08919d 100644 --- a/build.exs +++ b/build.exs @@ -2,7 +2,8 @@ require Mix require EEx Mix.install([ - {:tzdata, "~> 1.1"} + {:tzdata, "~> 1.1"}, + {:mustache, "~> 0.5.0"} ]) Calendar.put_time_zone_database(Tzdata.TimeZoneDatabase) @@ -79,47 +80,53 @@ defmodule Builders do ]) end - def fxg(input_path, template) do + def fxg(input_path, template, bricks) do own_template = input_path |> String.replace_suffix("fxg", "html") case File.read(own_template) do - {:ok, ctemplate} -> _fxg(input_path, ctemplate) - _ -> _fxg(input_path, template) + {:ok, ctemplate} -> _fxg(input_path, ctemplate, bricks) + _ -> _fxg(input_path, template, bricks) end end - def paste(input_path, template) do - {:ok, rcode} = File.read(input_path) + def paste(input_path, template, bricks) do + {:ok, code} = File.read(input_path) + IO.inspect(code) output = "build/" <> input_path <> ".html" - code = - rcode - |> String.replace("&", "&") - |> String.replace("<", "<") - |> String.replace(">", ">") - lang = Path.extname(input_path) |> String.slice(1..-1//1) file = Path.basename(input_path) - {:ok, %File.Stat{mtime: mtime}} = File.stat(input_path, time: :posix) + time = FileX.created(input_path) result = - template - |> String.replace("$LANG", lang) - |> String.replace("$CODE", code) - |> String.replace("$FILENAME", input_path) - |> String.replace("$FILEMETA", "Created on " <> Integer.to_string(mtime)) + Mustache.render( + template, + Map.merge( + %{ + filename: input_path, + about_paste: "Created on " <> time, + language: "", + codetag: "
 lang <> "\">",
+            code: code
+          },
+          bricks
+        )
+      )
+
+    IO.inspect(input_path)
 
     :ok = File.write(output, result)
   end
 
-  defp _fxg(input_path, template) do
+  defp _fxg(input_path, template, bricks) do
     output_path =
       String.replace_prefix(input_path, "src", "build")
       |> String.replace_suffix("fxg", "html")
 
-    {output, 0} = System.cmd("fxg", [input_path])
-    output = String.replace(template, "", output)
+    {content, 0} = System.cmd("fxg", [input_path])
+
+    output = Mustache.render(template, Map.merge(%{fxg_content: content}, bricks))
     File.write(output_path, output)
   end
 end
@@ -129,6 +136,12 @@ defmodule FileX do
     {:ok, %File.Stat{mtime: {{y, m, d}, _}}} = File.stat(path, time: :local)
     Integer.to_string(d) <> "/" <> Integer.to_string(m) <> "/" <> Integer.to_string(y)
   end
+
+  def sanitised_name(path) do
+    Path.basename(path)
+    |> String.trim_trailing(".html")
+    |> String.replace("-", "_")
+  end
 end
 
 {commit, 0} = System.cmd("git", ["rev-parse", "HEAD"])
@@ -150,6 +163,10 @@ end
 # |> Task.async_stream(fn file -> Builders.html(file) end)
 # |> Enum.to_list()
 
+bricks =
+  Path.wildcard("bricks/*.html")
+  |> Map.new(fn x -> {FileX.sanitised_name(x), File.read!(x)} end)
+
 {:ok, main_template} = File.read("src/index.html")
 
 Path.wildcard("src/**/*.css")
@@ -161,7 +178,7 @@ Path.wildcard("src/**/*.js")
 |> Enum.to_list()
 
 Path.wildcard("src/**/*.fxg")
-|> Task.async_stream(fn file -> Builders.fxg(file, main_template) end)
+|> Task.async_stream(fn file -> Builders.fxg(file, main_template, bricks) end)
 |> Enum.to_list()
 
 Path.wildcard("static/**/*")
@@ -172,10 +189,12 @@ File.mkdir_p("build/pastes")
 
 {:ok, paste_template} = File.read("pastes/paste.html")
 
-pastes = Path.wildcard("pastes/**/*")
+pastes =
+  Path.wildcard("pastes/**/*")
+  |> Enum.filter(fn x -> x != "pastes/paste.html" end)
 
 pastes
-|> Task.async_stream(fn file -> Builders.paste(file, paste_template) end)
+|> Task.async_stream(fn file -> Builders.paste(file, paste_template, bricks) end)
 |> Enum.to_list()
 
 # i cant test this :)) hope it works
@@ -190,9 +209,13 @@ pastes_header = """
 
""" +# main_template +# |> String.replace("", pastes_header <> "

" <> paste_data <> "

") paste_index = - main_template - |> String.replace("", pastes_header <> "

" <> paste_data <> "

") + Mustache.render( + main_template, + Map.merge(bricks, %{fxg_content: pastes_header <> "

" <> paste_data <> "

"}) + ) File.write("build/pastes/index.html", paste_index) diff --git a/pastes/main.rs b/pastes/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..e7a11a969c037e00a796aafeff6258501ec15e9a --- /dev/null +++ b/pastes/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/pastes/paste.html b/pastes/paste.html index 75a9705e4bb8e3b5f0a01059ed7ffd4a62115a2b..b009307a9e1c9fb20fbe8986ddbcd0b20a8933e0 100644 --- a/pastes/paste.html +++ b/pastes/paste.html @@ -2,52 +2,25 @@ - - - home - m1kadev.nl - - - - - + {{ &base_header }} + + {{ &language }} + {{ &highlight }}
-

$FILENAME

- $FILEMETA -

-$CODE
-            
+

{{ filename }}

+

{{ about_paste }}

+
+ {{ &codetag }}{{ code }}

- + {{ &footer }} -
-

.nl

-

m1kadev

- -
+ {{ &navbar }} diff --git a/pull-hljs.sh b/pull-hljs.sh index e7edf8fd2638f87e8310576c31e2bee3860fbad9..a062bbd91705cec350cb1b56fb9482a75be32bc9 100755 --- a/pull-hljs.sh +++ b/pull-hljs.sh @@ -4,14 +4,16 @@ cd static rm highlight.min.js -rm rust.min.js +rm rs.min.js rm html.min.js -rn arta.min.css +rm arta.min.css wget https://raw.githubusercontent.com/highlightjs/cdn-release/refs/heads/main/build/highlight.min.js wget https://raw.githubusercontent.com/highlightjs/cdn-release/refs/heads/main/build/languages/rust.min.js wget https://raw.githubusercontent.com/highlightjs/cdn-release/refs/heads/main/build/languages/html.min.js -wget https://raw.githubusercontent.com/highlightjs/cdn-release/refs/heads/main/build/styles/arta.min.css \ No newline at end of file +wget https://raw.githubusercontent.com/highlightjs/cdn-release/refs/heads/main/build/styles/arta.min.css + +mv rust.min.js rs.min.js \ No newline at end of file diff --git a/src/colophon.fxg b/src/colophon.fxg index cd7f4b5f9c8c003eaa653e65918458b3133fcf6a..65956d7df017b1099e65e7efdf30965e6e66381f 100644 --- a/src/colophon.fxg +++ b/src/colophon.fxg @@ -1,5 +1,5 @@ -= Colophon = += colophon = -== About == +== about == you're looking at a <#https://en.wiktionary.org/wiki/beautiful#English beautiful> site, with <#https://caddyserver.com/ caddy> as its backend. all source pages are written in my own markup language, <#https://github.com/m1kadev/fxg fxg>. site generation is done by a homebrewn <#https://github.com/m1kadev/www.m1kadev.nl/blob/main/build.exs elixir script>. all code (including <#https://github.com/m1kadev/fxg fxg>) is licenced under the MIT licence. \ No newline at end of file diff --git a/src/colophon.html b/src/colophon.html index 23cf7adfccdd8054215c3b24fa327b5ea809756a..622ab9aef3b869e7b291f762f67321d970fa82d5 100644 --- a/src/colophon.html +++ b/src/colophon.html @@ -2,48 +2,23 @@ - - - home - m1kadev.nl - - - - + + {{ &base_header }} + + colophon - m1kadev.nl +
- + {{ &fxg_content }}
- + {{ &extended_footer }}
-
-

.nl

-

m1kadev

- -
- + {{ &navbar }} \ No newline at end of file diff --git a/src/index.html b/src/index.html index ccb20cac6a91184b585371b520f0dc35f05f1a4e..5693f75348394cf4aba71589e13556a3f011a469 100644 --- a/src/index.html +++ b/src/index.html @@ -2,47 +2,26 @@ - - - home - m1kadev.nl - - + {{ &base_header }} + + {{ &highlight }} + - - + + home - m1kadev
- + {{ &fxg_content }}
- + {{ &footer }}
-
-

.nl

-

m1kadev

- -
+ {{ &navbar }} diff --git a/src/index.js b/src/index.js index 84a52a40fa5432b75792b3645ddfe41d132eddf7..ff9ad2a5579c8a7d5418faf3de791b373c32c1a1 100644 --- a/src/index.js +++ b/src/index.js @@ -4,8 +4,6 @@ const htmlMinifierNext = Array.from(document.getElementsByClassName("html-minifi const lightningcss = Array.from(document.getElementsByClassName("lightningcss")); const uglifyjs = Array.from(document.getElementsByClassName("uglifyjs")); -hljs.highlightAll(); - fetch("/info.txt").then(async data => { const res = await data.text(); let map = new Map();