All the things I wanted to work, work.

This commit is contained in:
Ryan Pandya 2022-09-19 00:18:57 +05:30
parent 71b3edf2db
commit ce87f3623a
6 changed files with 76 additions and 15 deletions

View File

@ -90,7 +90,7 @@ defmodule LogsrvApi do
end
def forward_links(post) do
post.markdown
to_post = post.markdown
|> String.split(~r/[\[\]]/, trim: true)
|> Enum.map(&(
if(String.match?(post.markdown, Regex.compile!("\\[\\[" <> &1 <> "\\]\\]"))) do
@ -105,10 +105,30 @@ defmodule LogsrvApi do
end )
|> Enum.map(fn(str) ->
{title, true} = str
case title |> to_slug |> @repo.get_by_slug do
to_link = case title |> to_slug |> @repo.get_by_slug do
{:ok, page} -> page
{:no_such_page, slug} -> %{LogsrvApi.Page.empty | slug: slug, public: post.public, title: slug |> to_title}
end
context = post.markdown |> String.split("\n") |> Enum.find(&(&1 |> String.contains?(title)))
{to_link, context}
end)
end
def forward_pages(post) do
post |> forward_links
|> Enum.map(fn(tuple) ->
{to_link, context} = tuple
to_link.slug
end)
end
def forward_contexts(post) do
post |> forward_links
|> Enum.map(fn(tuple) ->
{to_link, context} = tuple
context
end)
end

View File

@ -2,7 +2,7 @@ defmodule LogsrvApi.Link do
use GenServer
alias LogsrvApi.{Filesystem,Page,Journal,Link}
defstruct from: nil, to: nil
defstruct from: nil, to: nil, context: ""
def start_link(_opts) do
GenServer.start_link(__MODULE__, :ok, [name: __MODULE__])
@ -14,7 +14,12 @@ defmodule LogsrvApi.Link do
post
|> LogsrvApi.forward_links
|> Enum.map(fn(fwd_link) ->
%Link{from: post, to: fwd_link}
{to_post, context} = fwd_link
%Link{
from: post,
to: to_post,
context: context
}
end)
end) |> List.flatten
{:ok, links}
@ -40,7 +45,8 @@ defmodule LogsrvApi.Link do
#GenServer.call(__MODULE__, {:get_links_to, post})
end
def get_links_to!(post), do: get_links_to(post) |> LogsrvApi.just
def to(post), do: get_links_to(post) |> LogsrvApi.just
def from(post), do: get_links_from(post) |> LogsrvApi.just
def handle_call({:links}, _from, links) do
{:reply, {:ok, links}, links}
@ -67,6 +73,6 @@ defimpl Inspect, for: LogsrvApi.Link do
else
""
end
"%#{is_broken}Link{'#{link.from.title}' => '#{link.to.title}'}"
"%#{is_broken}Link{'#{link.from.title}' => '#{link.to.title}' @ \"#{link.context}\"}"
end
end

View File

@ -19,7 +19,7 @@ defmodule LogsrvWeb.PostController do
case status do
:ok ->
render(conn, "post.html", post: post, backlinks: post |> Link.get_links_to!)
render(conn, "post.html", post: post, backlinks: post |> Link.to)
_ ->
not_found(conn)
@ -28,7 +28,7 @@ defmodule LogsrvWeb.PostController do
def home(conn, _params) do
# render(conn, "home.html")
show(conn, %{"id" => "contents"})
show(conn, %{"id" => "home"})
end
def not_found(conn) do

View File

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<html lang="en" data-theme="lemonade">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
@ -12,7 +12,7 @@
<body class="bg-base-200 w-full h-screen">
<div class="navbar sticky top-0 z-40 bg-neutral text-neutral-content border-b-4 border-primary"><div class="w-2/3 mx-auto">
<div class="flex-1">
<div class="btn btn-ghost text-2xl normal-case"><a href="/">Ryan's Blog</a></div></div>
<div class="btn btn-ghost text-2xl normal-case hover:bg-neutral hover:text-primary"><a href="/">Ryan's Blog</a></div></div>
<div class="flex-none">
<div class="btn btn-primary text-xl normal-case"><a>Admin</a></div></div>
</div></div>

View File

@ -1,16 +1,25 @@
<div class="prose pb-10 border-b-2 border-neutral">
<h1><%= @post.title %></h1>
<%= raw(@post.html) %>
<%= parse_links(@post) %>
</div>
<div class="prose mt-10">
<div class="prose mt-5">
<h3> Backlinks </h3>
<ul>
<%= for link <- @backlinks do %>
<li><%= link.from.title %></li>
<li class="text-info-content">
<div class="font-semibold">
<a class="no-underline hover:text-neutral-content" href={link.from.slug}>
<%= link.from.title %>
</a>
</div>
<div class="text-xs">
<%= parse_links(link.from, link.context) %>
</div>
</li>
<% end %>
</ul>
<%= if @backlinks |> length == 0 do %>
No backlinks.
No pages link to this one.
<% end %>
</div>

View File

@ -3,11 +3,37 @@ defmodule LogsrvWeb.PostView do
import LogsrvApi.Journal
def post_path(post) do
"/post/#{post.slug}"
post.slug
end
def cleanup(str) do
str
end
def parse_links(post) do
parse_links(post, post.html)
end
def parse_links(post, content) do
output = content
|> String.split(~r/[\[\]]/, trim: true)
|> Enum.map(fn(str) ->
if String.match?(
" " <> (post |> LogsrvApi.forward_pages |> Enum.join(" ")) <> " ",
" #{str |> LogsrvApi.to_slug} " |> Regex.compile!
) do
slug = str |> LogsrvApi.to_slug
{status, page} = slug |> LogsrvApi.Filesystem.get_by_slug
{no_exist, href} = case status do
:ok -> {"", slug}
:no_such_page -> {"bg-error", "javascript:;"}
end
"<a class='#{no_exist}' href='#{href}'>#{str}</a>"
else
str
end
end)
raw(output |> List.to_string)
end
end