Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start on rendering blocks #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

.elixir_ls
6 changes: 5 additions & 1 deletion lib/fragment/image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ end

defimpl Prismic.Fragment, for: Prismic.Fragment.Image do
# TODO
def as_html(_, _, _), do: ""
def as_html(img, _link_resolver, _html_serializer) do
main = img.main
%{url: url, height: h, width: w} = main
~s[<img src="#{url}" height="#{h}" width="#{w}" />]
end
end
39 changes: 38 additions & 1 deletion lib/fragment/structured_text/block/text/paragraph.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,42 @@ defmodule Block.Text.Paragraph do
end

defimpl Block, for: Block.Text.Paragraph do
def as_html(_, _link_resolver, _html_serializer), do: ""
alias Prismic.Fragment.StructuredText.Span

def as_html(para, _link_resolver, _html_serializer) do
~s(<p>#{process_spans(para)}</p>)
end

# TODO: All this should move out (unless paragraph is the only one with spans?)
defp process_spans(%{text: text, spans: []}), do: text

defp process_spans(%{text: text, spans: spans}) do
text_stream = String.codepoints(text)
do_process_spans(text_stream, 0, spans, [], [])
end

defp do_process_spans([], _pos, _spans, applied_spans, acc) do
[acc | Enum.map(applied_spans, &Span.close_tag/1)]
end

# TODO: Not sure how efficient this is, since it is one character at a time
# Could do something smarter by checking the next opening and chunk characters
# from there. Uses an IO list so we aren't creating too many intermediate strings
defp do_process_spans([h | t], pos, spans, applied_spans, acc) do
{ending_here, still_to_end} = Enum.split_with(applied_spans, &match?(%{end: ^pos}, &1))
starting_here = Enum.filter(spans, &match?(%{start: ^pos}, &1))

ending_tags = Enum.map(ending_here, &Span.close_tag/1)
starting_tags = Enum.map(starting_here, &Span.open_tag/1)

# TODO: h should be html encoded
acc = [acc | [ending_tags, starting_tags, h]]

applied_spans =
starting_here
|> Enum.reverse()
|> Enum.concat(still_to_end)

do_process_spans(t, pos + 1, spans, applied_spans, acc)
end
end
2 changes: 2 additions & 0 deletions lib/fragment/structured_text/span.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
defprotocol Prismic.Fragment.StructuredText.Span do
def serialize(span, link_resolver)
def open_tag(span)
def close_tag(span)
end
2 changes: 2 additions & 0 deletions lib/fragment/structured_text/span/em.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ end
defimpl Span, for: Span.Em do
# TODO
def serialize(_, _link_resolver), do: ""
def open_tag(_span), do: "<i>"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe "em" tag would be more appropriate her, see ruby implentation

def close_tag(_span), do: "</i>"
end
9 changes: 6 additions & 3 deletions lib/fragment/structured_text/span/hyperlink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ defmodule Span.Hyperlink do
defstruct [:start, :end, :link]

@type t :: %__MODULE__{
start: Integer.t(),
end: Integer.t(),
link: WebLink.t() | DocumentLink.t() | ImageLink.t()}
start: Integer.t(),
end: Integer.t(),
link: WebLink.t() | DocumentLink.t() | ImageLink.t()
}
end

defimpl Span, for: Span.Hyperlink do
# TODO
def serialize(_, _link_resolver), do: ""
def open_tag(%Span.Hyperlink{link: link}), do: ~s(<a href="#{link}">)
def close_tag(_span), do: "</a>"
end
2 changes: 2 additions & 0 deletions lib/fragment/structured_text/span/label.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ end
defimpl Span, for: Span.Label do
# TODO
def serialize(_, _link_resolver), do: ""
def open_tag(_span), do: "<label>"
def close_tag(_span), do: "</label>"
end
2 changes: 2 additions & 0 deletions lib/fragment/structured_text/span/strong.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ end
defimpl Span, for: Span.Strong do
# TODO
def serialize(_, _link_resolver), do: ""
def open_tag(_span), do: "<b>"
def close_tag(_span), do: "</b>"
end
107 changes: 107 additions & 0 deletions test/frag_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
defmodule Prismic.FragText do
use ExUnit.Case
alias Prismic.Fragment.StructuredText.Block.Text.Paragraph
alias Prismic.Fragment.StructuredText.Span.{Em, Strong, Hyperlink}

describe "structured text render" do
defp render_para(spans, text \\ "abc") do
st = %Prismic.Fragment.StructuredText{
blocks: [
%Paragraph{
label: nil,
spans: spans,
text: text
}
]
}

Prismic.Fragment.as_html(st, nil)
end

test "renders em" do
spans = [
%Em{end: 3, start: 0}
]

assert ~s(<p><i>abc</i></p>) == render_para(spans)
end

test "renders strong" do
spans = [
%Strong{end: 3, start: 0}
]

assert ~s(<p><b>abc</b></p>) == render_para(spans)
end

test "renders link" do
spans = [
%Hyperlink{end: 3, start: 0, link: "somelink"}
]

assert ~s(<p><a href="somelink">abc</a></p>) == render_para(spans)
end

test "renders multiple" do
spans = [
%Em{end: 3, start: 0},
%Em{end: 6, start: 4}
]

text = "abcdef"
assert ~s(<p><i>abc</i>d<i>ef</i></p>) == render_para(spans, text)
end

test "handles nesting" do
spans = [
%Em{end: 6, start: 0},
%Strong{end: 5, start: 3}
]

text = "abcdef"
assert ~s(<p><i>abc<b>de</b>f</i></p>) == render_para(spans, text)
end

test "ignores starts longer than input" do
spans = [
%Em{end: 6, start: 999}
]

assert ~s(<p>abc</p>) == render_para(spans)
end

test "closes tag if end greater than input" do
spans = [
%Em{end: 999, start: 0}
]

assert ~s(<p><i>abc</i></p>) == render_para(spans)
end

test "can do single characters" do
spans = [
%Em{end: 2, start: 1}
]

assert ~s(<p>a<i>b</i>c</p>) == render_para(spans)
end

test "can do middle" do
spans = [
%Em{end: 4, start: 2}
]

text = "abcdef"
assert ~s(<p>ab<i>cd</i>ef</p>) == render_para(spans, text)
end

test "leaves tags balanced" do
spans = [
%Em{end: 3, start: 0},
%Strong{end: 3, start: 0}
]

assert ~s(<p><i><b>abc</b></i></p>) == render_para(spans)
end
end
end