Add some specs and documentation, format

This commit is contained in:
byte[] 2021-09-29 22:09:21 -04:00
parent 5f7fe354a1
commit 983d606013
11 changed files with 47 additions and 21 deletions

View file

@ -1,3 +1,8 @@
defmodule Camo.Image do defmodule Camo.Image do
@doc """
Convert a potentially untrusted external image URL into a trusted one
loaded through a gocamo proxy (specified by the environment).
"""
@spec image_url(String.t()) :: String.t()
def image_url(input), do: Philomena.Native.camo_image_url(input) def image_url(input), do: Philomena.Native.camo_image_url(input)
end end

View file

@ -1,12 +1,26 @@
defmodule Philomena.Markdown do defmodule Philomena.Markdown do
@markdown_chars ~r/[\*_\[\]\(\)\^`\%\\~<>#\|]/ @markdown_chars ~r/[\*_\[\]\(\)\^`\%\\~<>#\|]/
# When your NIF is loaded, it will override this function. @doc """
Converts user-input Markdown to HTML, with the specified map of image
replacements (which converts ">>1234p" syntax to an embedded image).
"""
@spec to_html(String.t(), %{String.t() => String.t()}) :: String.t()
def to_html(text, replacements), do: Philomena.Native.markdown_to_html(text, replacements) def to_html(text, replacements), do: Philomena.Native.markdown_to_html(text, replacements)
@doc """
Converts trusted-input Markdown to HTML, with the specified map of image
replacements (which converts ">>1234p" syntax to an embedded image). This
function does not strip any raw HTML embedded in the document.
"""
@spec to_html_unsafe(String.t(), %{String.t() => String.t()}) :: String.t()
def to_html_unsafe(text, replacements), def to_html_unsafe(text, replacements),
do: Philomena.Native.markdown_to_html_unsafe(text, replacements) do: Philomena.Native.markdown_to_html_unsafe(text, replacements)
@doc """
Escapes special characters in text which is to be rendered as Markdown.
"""
@spec escape(String.t()) :: String.t()
def escape(text) do def escape(text) do
@markdown_chars @markdown_chars
|> Regex.replace(text, fn m -> |> Regex.replace(text, fn m ->

View file

@ -1,10 +1,14 @@
defmodule Philomena.Native do defmodule Philomena.Native do
@moduledoc false
use Rustler, otp_app: :philomena use Rustler, otp_app: :philomena
# Markdown @spec markdown_to_html(String.t(), %{String.t() => String.t()}) :: String.t()
def markdown_to_html(_text, _replacements), do: :erlang.nif_error(:nif_not_loaded) def markdown_to_html(_text, _replacements), do: :erlang.nif_error(:nif_not_loaded)
@spec markdown_to_html_unsafe(String.t(), %{String.t() => String.t()}) :: String.t()
def markdown_to_html_unsafe(_text, _replacements), do: :erlang.nif_error(:nif_not_loaded) def markdown_to_html_unsafe(_text, _replacements), do: :erlang.nif_error(:nif_not_loaded)
# Camo @spec camo_image_url(String.t()) :: String.t()
def camo_image_url(_uri), do: :erlang.nif_error(:nif_not_loaded) def camo_image_url(_uri), do: :erlang.nif_error(:nif_not_loaded)
end end

View file

@ -22,7 +22,9 @@ defmodule Philomena.Scrapers.Tumblr do
[post_id] = Regex.run(@url_regex, url, capture: :all_but_first) [post_id] = Regex.run(@url_regex, url, capture: :all_but_first)
api_url = api_url =
"https://api.tumblr.com/v2/blog/#{uri.host}/posts/photo?id=#{post_id}&api_key=#{tumblr_api_key()}" "https://api.tumblr.com/v2/blog/#{uri.host}/posts/photo?id=#{post_id}&api_key=#{
tumblr_api_key()
}"
Philomena.Http.get(api_url) Philomena.Http.get(api_url)
|> json!() |> json!()

View file

@ -34,8 +34,7 @@ defmodule PhilomenaWeb.Image.DescriptionController do
Images.reindex_image(image) Images.reindex_image(image)
body = body = MarkdownRenderer.render_one(%{body: image.description}, conn)
MarkdownRenderer.render_one(%{body: image.description}, conn)
conn conn
|> put_view(PhilomenaWeb.ImageView) |> put_view(PhilomenaWeb.ImageView)

View file

@ -134,11 +134,9 @@ defmodule PhilomenaWeb.ProfileController do
|> MarkdownRenderer.render_collection(conn) |> MarkdownRenderer.render_collection(conn)
|> Enum.zip(recent_comments) |> Enum.zip(recent_comments)
about_me = about_me = MarkdownRenderer.render_one(%{body: user.description || ""}, conn)
MarkdownRenderer.render_one(%{body: user.description || ""}, conn)
scratchpad = scratchpad = MarkdownRenderer.render_one(%{body: user.scratchpad || ""}, conn)
MarkdownRenderer.render_one(%{body: user.scratchpad || ""}, conn)
commission_information = commission_info(user.commission, conn) commission_information = commission_info(user.commission, conn)

View file

@ -61,8 +61,7 @@ defmodule PhilomenaWeb.TagController do
interactions = Interactions.user_interactions(images, user) interactions = Interactions.user_interactions(images, user)
body = body = MarkdownRenderer.render_one(%{body: tag.description || ""}, conn)
MarkdownRenderer.render_one(%{body: tag.description || ""}, conn)
dnp_bodies = dnp_bodies =
MarkdownRenderer.render_collection( MarkdownRenderer.render_collection(

View file

@ -139,8 +139,7 @@ defmodule PhilomenaWeb.ImageLoader do
dnp_entries = Enum.zip(dnp_bodies, tag.dnp_entries) dnp_entries = Enum.zip(dnp_bodies, tag.dnp_entries)
description = description = MarkdownRenderer.render_one(%{body: tag.description || ""}, conn)
MarkdownRenderer.render_one(%{body: tag.description || ""}, conn)
[{tag, description, dnp_entries}] [{tag, description, dnp_entries}]
end end

View file

@ -1,4 +1,4 @@
# NIF for Elixir.Philomena.Markdown # NIF for Elixir.Philomena.Native
## To build the NIF module: ## To build the NIF module:
@ -9,8 +9,8 @@
## To load the NIF: ## To load the NIF:
```elixir ```elixir
defmodule Philomena.Markdown do defmodule Philomena.Native do
use Rustler, otp_app: <otp-app>, crate: "philomena_markdown" use Rustler, otp_app: <otp-app>, crate: "philomena"
# When your NIF is loaded, it will override this function. # When your NIF is loaded, it will override this function.
def add(_a, _b), do: :erlang.nif_error(:nif_not_loaded) def add(_a, _b), do: :erlang.nif_error(:nif_not_loaded)

View file

@ -92,8 +92,14 @@ defmodule Philomena.Repo.Migrations.RenameBodyFields do
execute("update images set description='' where description is null;") execute("update images set description='' where description is null;")
execute("update tags set description='' where description is null;") execute("update tags set description='' where description is null;")
execute("alter table images alter column description set default ''::character varying, alter column description set not null;")
execute("alter table tags alter column description set default ''::character varying, alter column description set not null;") execute(
"alter table images alter column description set default ''::character varying, alter column description set not null;"
)
execute(
"alter table tags alter column description set default ''::character varying, alter column description set not null;"
)
# Unneeded columns # Unneeded columns
alter table("badges") do alter table("badges") do