~mika/www.m1kadev.nl

3020fb369cb40b7ea2971099dbce6ac6b9eca69a — m1kadev 5 months ago eb04e08
kethel: final touches
6 files changed, 55 insertions(+), 6 deletions(-)

D kethel/lib/scriprs
M kethel/lib/thoughts.ex
M kethel/main.exs
M pages/colophon.fxg
M styles/thoughts.css
A templates/inline_thought.thtml
D kethel/lib/scriprs => kethel/lib/scriprs +0 -0
M kethel/lib/thoughts.ex => kethel/lib/thoughts.ex +20 -5
@@ 1,5 1,5 @@
defmodule Thoughts do
  defstruct [:thoughts, :template, :bricks, :project_root]
  defstruct [:thoughts, :template, :bricks, :project_root, :index_template, :inline_template ]

  @spec urlify(binary()) :: binary()
  defp urlify(name) do


@@ 21,7 21,9 @@ defmodule Thoughts do
        Path.wildcard("#{project_root}/thoughts/*")
        |> Enum.map(fn path -> {path, get_file_commit_date(path), File.read!(path)} end)
        |> Enum.sort_by(fn {_, date, _ } -> date end, :desc),
      inline_template: File.read!("#{project_root}/templates/inline_thought.thtml"),
      template: File.read!("#{project_root}/templates/thought.thtml"),
      index_template: File.read!("#{project_root}/templates/thoughts.thtml"),
      bricks: bricks,
      project_root: project_root
    }


@@ 42,11 44,25 @@ defmodule Thoughts do
      compile_file(thought, thoughts.template, thoughts.bricks, thoughts.project_root) end)
    |> Enum.to_list()

    t_end = System.monotonic_time(:millisecond)
    # generate index
    index_content = thoughts.thoughts
      |> Enum.map(fn thought -> inline_thought(thought, thoughts) end)
      |> Enum.join()
    index = Mustache.render(thoughts.index_template, Map.merge(thoughts.bricks, %{fxg_content: index_content}))
    File.write!("#{thoughts.project_root}/build/thoughts/index.html", index)
    IO.puts("[COMPILED] #{thoughts.project_root}/build/thoughts/index.html")

    t_end = System.monotonic_time(:millisecond)
    t_end - t_begin
  end

  defp inline_thought({path, date, thought}, context) do
    stripped_name = String.replace_prefix(path, "#{context.project_root}/thoughts/", "")
    location = "https://m1kadev.nl/thoughts/#{urlify(stripped_name)}.html"
    title = "<a href=\"#{location}\">#{stripped_name}</a>"
    Mustache.render(context.inline_template, %{name: title, date: date, thought: thought})
  end

  defp compile_file({path, date, thought}, template, bricks, project_root) do
    name = trim_abs_path(path, project_root)
    output_path = "#{project_root}/build/thoughts/#{urlify(name)}.html"


@@ 58,7 74,6 @@ defmodule Thoughts do

  def get_file_commit_date(path) do
    {ts_string, 0} = System.cmd("git", ["log", "--format=%ct", path])
    
    String.trim_trailing(ts_string)
      |> String.to_integer()
  end


@@ 67,8 82,8 @@ defmodule Thoughts do
    location = String.replace_prefix(path, project_root <> "/thoughts/", "")
    link = "https://m1kadev.nl/thoughts/" <> urlify(location) <> ".html"
    rfc_date = date
    |> DateTime.from_unix!()
    |> Calendar.strftime("%a, %d %b %Y %H:%M:%S %Z")
      |> DateTime.from_unix!()
      |> Calendar.strftime("%a, %d %b %Y %H:%M:%S %Z")
    Mustache.render(template, %{title: location, date: rfc_date, description: thought, category: "thoughts", link: link } ) 
  end
end

M kethel/main.exs => kethel/main.exs +19 -0
@@ 57,6 57,25 @@ if folder != nil do
  File.write!("#{project_root}/build/rss.xml", rss_header <> rss_thoughts <> rss_footer)

  IO.puts("[COMPILED] #{project_root}/build/rss.xml")
  
  { lcss_version, 0 }= System.cmd("npx", [ "lightningcss", "-V" ])
  { hmn_version, 0 } = System.cmd("npx", [ "html-minifier-next", "-V" ])
  { ujs_version, 0 } = System.cmd("npx", [ "uglifyjs", "-V" ])
  { git_commit, 0 } = System.cmd("git", [ "rev-parse", "HEAD" ])

  { build_date, 0 } = System.cmd("date", ["+%H:%M %d/%m/%y"])

  info_txt = """
  commit=#{git_commit}
  lightningcss=#{lcss_version}
  html_minifier_next=#{hmn_version}
  uglifyjs=#{ujs_version}
  build_time=#{build_date}
  """
  
  File.write!("#{project_root}/build/info.txt", info_txt)

  IO.puts("[COMPILED] #{project_root}/build/info.txt")

  IO.puts("Finalising...");


M pages/colophon.fxg => pages/colophon.fxg +3 -0
@@ 3,6 3,9 @@
== about == 

you're looking at a <#https://en.wiktionary.org/wiki/beautiful#English beautiful> site, with <#https://caddyserver.com/ caddy> as its backend.

the site generator is a home-grown non-gmo <#https://github.com/m1kadev/www.m1kadev.nl/tree/main/kethel/ elixir app>. the internal markup language is <#https://github.com/m1kadev/fxg fxg>, made to be more legible than markdown. 

templating the final html is handled by <#https://mustache.github.io mustache>, a stateless templating library.

css, js, and html parsing are handled by lightningcss, uglifyjs, and html-minifier-next, respectively.

M styles/thoughts.css => styles/thoughts.css +5 -1
@@ 18,4 18,8 @@
.thought>small {
    text-align: right;
    display: block;
}
\ No newline at end of file
}

main hr:first-of-type {
    display: none;
}

A templates/inline_thought.thtml => templates/inline_thought.thtml +8 -0
@@ 0,0 1,8 @@
<hr>
<div class="thought">
    <h2> {{ &name }} </h2>
    <span>
        {{ thought }}
    </span>
    <small>-mika, {{ date }}</small>
</div>