Stable state. Starting to build out ugly frontend.

This commit is contained in:
Ryan Pandya 2022-09-06 14:54:15 -07:00
parent ad86f60b58
commit bd45b886ee
12 changed files with 127 additions and 28 deletions

View File

@ -0,0 +1,72 @@
defmodule AlchemistMarkdown do
def to_html(markdown \\ "", opts \\ [])
def to_html(markdown, _opts) do
markdown
|> hrs
|> divs
|> Earmark.as_html!(earmark_options())
|> HtmlSanitizeEx.html5()
|> smalls
|> bigs
end
# for now, we'll just replace H1 and H2 tags with H3s, but as the site grows and it becomes necessary, we'll add more restrictions to commenters.
def to_commenter_html(markdown) do
to_html(markdown)
|> h3_is_max
end
# Earmark doesn't support adding CSS classes to divs yet
def divs(text) do
matcher = ~r{(^|\n)::div((\.[\w-]*)*) ?(.*?)(\n):/div ?}s
matches = Regex.run(matcher, text)
case matches do
nil ->
text
[matched_part, _, classes, _, inner_md | _tail] ->
classname = classes |> String.split(".", trim: true) |> Enum.join(" ")
html = "<div class=#{classname}>#{to_html(inner_md)}</div>"
String.replace(text, matched_part, html)
end
end
def bigs(text) do
replace_unless_pre(text, ~r/\+\+(.+)\+\+/, "<big>\\1</big>")
end
def hrs(text) do
Regex.replace(~r{(^|\n)([-*])( *\2 *)+\2}s, text, "\\1<hr />")
end
def h3_is_max(text) do
text = Regex.replace(~r{<h1([^<]*>(.*)<\/)h1>}s, text, "<h3\\1h3>")
Regex.replace(~r{<h2([^<]*>(.*)<\/)h2>}s, text, "<h3\\1h3>")
end
# Replace the input text based on the regex and replacement text provided
# ... except leave everything inside <pre> blocks as is
def replace_unless_pre(text, rexp, replacement) do
Regex.split(~r|<pre[^<]*>.*<\/pre>|s, text, include_captures: true)
|> Enum.map(fn str ->
case String.starts_with?(str, "<pre") do
true -> str
_ -> Regex.replace(rexp, str, replacement)
end
end)
|> Enum.join("")
end
def smalls(text) do
replace_unless_pre(text, ~r/--(.+)--/, "<small>\\1</small>")
end
defp earmark_options() do
%Earmark.Options{
code_class_prefix: "lang-",
smartypants: false
}
end
end

View File

@ -34,8 +34,8 @@ defmodule LogsrvApi do
end end
end end
def read(page) do def read(entry) do
@repo.read(page) @repo.read(entry)
end end

View File

@ -1,12 +1,16 @@
defmodule LogsrvApi.Filesystem do defmodule LogsrvApi.Filesystem do
alias LogsrvApi.{Journal, Page} alias LogsrvApi.{Journal, Page}
def to_date(str) do def to_date(str) when is_bitstring(str) do
str str
|> String.replace(~r/\..*/,"") |> String.replace(~r/\..*/,"")
|> String.replace("_","-") |> String.replace("_","-")
|> Date.from_iso8601() |> Date.from_iso8601()
end end
def to_date(date) do
{:ok, date}
end
def dir do def dir do
Application.get_env(:logsrv_api, :dir) <> "/" Application.get_env(:logsrv_api, :dir) <> "/"
end end
@ -41,8 +45,14 @@ defmodule LogsrvApi.Filesystem do
def get!(Journal, date) do def get!(Journal, date) do
Enum.find(all(Journal), fn(entry) -> entry.date === date end) Enum.find(all(Journal), fn(entry) -> entry.date === date end)
end end
def get!(Page, title) do def get!(Page, filename) do
Enum.find(all(Page), fn(entry) -> entry.title === title end) Enum.find(all(Page), fn(entry) -> entry.filename === filename end)
end
def read(entry) do
entry.type
|> locate(entry.filename)
|> File.read!
end end
end end

View File

@ -7,13 +7,15 @@ defmodule LogsrvApi.Page do
|> String.replace(~r/\.md$/,"") |> String.replace(~r/\.md$/,"")
|> String.replace("%2F","/") |> String.replace("%2F","/")
date_modified = Page |> Filesystem.locate(fd) %{mtime: mtime} = (Page |> Filesystem.locate(fd) |> File.stat!)
date_modified = mtime |> NaiveDateTime.from_erl!() |> DateTime.from_naive!("Etc/UTC")
tags = [:fun] tags = [:fun]
%{ %{
type: Page,
title: title |> String.capitalize(), title: title |> String.capitalize(),
filename: fd, filename: fd,
date_modified: date_modified, date: date_modified,
tags: tags tags: tags
} }
end end

View File

@ -1,17 +1,20 @@
defmodule LogsrvWeb.PostController do defmodule LogsrvWeb.PostController do
use LogsrvWeb, :controller use LogsrvWeb, :controller
import LogsrvApi
def index(conn, _params) do def index(conn, _params) do
pages = LogsrvApi.pages() render(conn, "index.html", pages: pages(), journals: journals())
journals = LogsrvApi.journals()
render(conn, "index.html", pages: pages, journals: journals)
end end
def show(conn, %{"id" => id}) do def show(conn, %{"id" => id}) do
pages = LogsrvApi.pages() post = id |> get_by_id
journals = LogsrvApi.journals() content = post |> read
post = LogsrvApi.get_by_id(id) render(conn, "post.html", post: post, pages: pages(), journals: journals(), content: content)
content = post |> LogsrvApi.read end
render(conn, "post.html", post: post, pages: pages, journals: journals, content: content)
def home(conn, _params) do
homepage = get_by_id("contents.md")
content = homepage |> read
render(conn, "post.html", post: homepage, pages: pages(), journals: journals(), content: content)
end end
end end

View File

@ -17,7 +17,9 @@ defmodule LogsrvWeb.Router do
scope "/", LogsrvWeb do scope "/", LogsrvWeb do
pipe_through :browser pipe_through :browser
get "/", PostController, :index get "/", PostController, :home
get "/index", PostController, :index
get "/post/:id", PostController, :show
end end
# Other scopes may use custom stacks. # Other scopes may use custom stacks.

View File

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en" data-theme="dark">
<head> <head>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
@ -7,19 +7,22 @@
<meta name="csrf-token" content={csrf_token_value()}> <meta name="csrf-token" content={csrf_token_value()}>
<%= live_title_tag assigns[:page_title] || "LogsrvWeb", suffix: " · Phoenix Framework" %> <%= live_title_tag assigns[:page_title] || "LogsrvWeb", suffix: " · Phoenix Framework" %>
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")}/> <link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")}/>
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/bear.css")}/>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js")}></script> <script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js")}></script>
</head> </head>
<body> <body><div id="root"><div class="theme-inner"><div id="app-container">
<header> <header>
<section class="container" style="flex-direction:row-reverse;"> <section class="container" style="flex-direction:row-reverse;">
<img style="padding:1em;border-radius:1em;" src={Routes.static_path(@conn, "/images/logseq-logo.png")} alt="Logo"/> <div><a href="/"><img style="padding:1em;border-radius:1em;" src={Routes.static_path(@conn, "/images/logseq-logo.png")} alt="Logo"/></a></div>
<div>
<ul> <ul>
<li> <b>Directory:</b> <%= LogsrvApi.Filesystem.dir %> </li> <li> <b>Directory:</b> <%= LogsrvApi.Filesystem.dir %> </li>
<li> <b>Journals:</b> <%= @pages |> length %> </li> <li> <b>Journals:</b> <%= @pages |> length %> </li>
<li> <b>Pages:</b> <%= @journals |> length %> </li> <li> <b>Pages:</b> <%= @journals |> length %> </li>
</ul> </ul></div>
<div> <a href="/pages"><h3>Pages</h3></a> </div><div> <a href="/journals"><h3>Journals</h3></a> </div>
</section> </section>
</header> </header>
<%= @inner_content %> <%= @inner_content %>
</body> </div></div></div> </body>
</html> </html>

View File

@ -4,5 +4,5 @@
</article> </article>
</section> </section>
<section class="row"> <section class="row">
<%= content(@post.filename) %> <%= raw @content |> AlchemistMarkdown.to_html |> cleanup %>
</section> </section>

View File

@ -6,9 +6,8 @@ defmodule LogsrvWeb.PostView do
"/post/#{post.filename}" "/post/#{post.filename}"
end end
def content(post) do def cleanup(str) do
IO.puts(post.filename) str
post.filename |> Journal.locate |> LogsrvApi.read
end end
end end

View File

@ -6,7 +6,8 @@ defmodule Logsrv.MixProject do
apps_path: "apps", apps_path: "apps",
version: "0.1.0", version: "0.1.0",
start_permanent: Mix.env() == :prod, start_permanent: Mix.env() == :prod,
deps: deps() deps: deps(),
apps: [:logsrv_api, :logsrv_web]
] ]
end end
@ -16,6 +17,9 @@ defmodule Logsrv.MixProject do
# #
# Run "mix help deps" for examples and options. # Run "mix help deps" for examples and options.
defp deps do defp deps do
[] [
{:earmark, "~> 1.4"},
{:html_sanitize_ex, "~> 1.3"}
]
end end
end end

View File

@ -3,13 +3,17 @@
"cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"}, "cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"},
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
"earmark": {:hex, :earmark, "1.4.27", "b413b0379043df51475a9b22ce344e8a58a117516c735b8871e6cdd5ed0f0153", [:mix], [{:earmark_parser, "~> 1.4.26", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "579ebe2eaf4c7e040815a73a268036bcd96e6aab8ad2b1fcd979aaeb1ea47e15"},
"earmark_parser": {:hex, :earmark_parser, "1.4.26", "f4291134583f373c7d8755566122908eb9662df4c4b63caa66a0eabe06569b0a", [:mix], [], "hexpm", "48d460899f8a0c52c5470676611c01f64f3337bad0b26ddab43648428d94aabc"},
"esbuild": {:hex, :esbuild, "0.5.0", "d5bb08ff049d7880ee3609ed5c4b864bd2f46445ea40b16b4acead724fb4c4a3", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "f183a0b332d963c4cfaf585477695ea59eef9a6f2204fdd0efa00e099694ffe5"}, "esbuild": {:hex, :esbuild, "0.5.0", "d5bb08ff049d7880ee3609ed5c4b864bd2f46445ea40b16b4acead724fb4c4a3", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "f183a0b332d963c4cfaf585477695ea59eef9a6f2204fdd0efa00e099694ffe5"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"floki": {:hex, :floki, "0.33.1", "f20f1eb471e726342b45ccb68edb9486729e7df94da403936ea94a794f072781", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "461035fd125f13fdf30f243c85a0b1e50afbec876cbf1ceefe6fddd2e6d712c6"}, "floki": {:hex, :floki, "0.33.1", "f20f1eb471e726342b45ccb68edb9486729e7df94da403936ea94a794f072781", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "461035fd125f13fdf30f243c85a0b1e50afbec876cbf1ceefe6fddd2e6d712c6"},
"gettext": {:hex, :gettext, "0.20.0", "75ad71de05f2ef56991dbae224d35c68b098dd0e26918def5bb45591d5c8d429", [:mix], [], "hexpm", "1c03b177435e93a47441d7f681a7040bd2a816ece9e2666d1c9001035121eb3d"}, "gettext": {:hex, :gettext, "0.20.0", "75ad71de05f2ef56991dbae224d35c68b098dd0e26918def5bb45591d5c8d429", [:mix], [], "hexpm", "1c03b177435e93a47441d7f681a7040bd2a816ece9e2666d1c9001035121eb3d"},
"html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"}, "html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"},
"html_sanitize_ex": {:hex, :html_sanitize_ex, "1.4.2", "c479398b6de798c03eb5d04a0a9a9159d73508f83f6590a00b8eacba3619cf4c", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm", "aef6c28585d06a9109ad591507e508854c5559561f950bbaea773900dd369b0e"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"}, "jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"}, "mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
"mochiweb": {:hex, :mochiweb, "2.22.0", "f104d6747c01a330c38613561977e565b788b9170055c5241ac9dd6e4617cba5", [:rebar3], [], "hexpm", "cbbd1fd315d283c576d1c8a13e0738f6dafb63dc840611249608697502a07655"},
"phoenix": {:hex, :phoenix, "1.6.11", "29f3c0fd12fa1fc4d4b05e341578e55bc78d96ea83a022587a7e276884d397e4", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1664e34f80c25ea4918fbadd957f491225ef601c0e00b4e644b1a772864bfbc2"}, "phoenix": {:hex, :phoenix, "1.6.11", "29f3c0fd12fa1fc4d4b05e341578e55bc78d96ea83a022587a7e276884d397e4", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1664e34f80c25ea4918fbadd957f491225ef601c0e00b4e644b1a772864bfbc2"},
"phoenix_html": {:hex, :phoenix_html, "3.2.0", "1c1219d4b6cb22ac72f12f73dc5fad6c7563104d083f711c3fcd8551a1f4ae11", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "36ec97ba56d25c0136ef1992c37957e4246b649d620958a1f9fa86165f8bc54f"}, "phoenix_html": {:hex, :phoenix_html, "3.2.0", "1c1219d4b6cb22ac72f12f73dc5fad6c7563104d083f711c3fcd8551a1f4ae11", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "36ec97ba56d25c0136ef1992c37957e4246b649d620958a1f9fa86165f8bc54f"},
"phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.6.5", "1495bb014be12c9a9252eca04b9af54246f6b5c1e4cd1f30210cd00ec540cf8e", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.3", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.17.7", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "ef4fa50dd78364409039c99cf6f98ab5209b4c5f8796c17f4db118324f0db852"}, "phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.6.5", "1495bb014be12c9a9252eca04b9af54246f6b5c1e4cd1f30210cd00ec540cf8e", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.3", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.17.7", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "ef4fa50dd78364409039c99cf6f98ab5209b4c5f8796c17f4db118324f0db852"},