Keep preformatting start/end lines when parsing
This commit is contained in:
parent
37864e9f11
commit
07abee79ed
|
@ -49,7 +49,9 @@ defmodule Gemini do
|
||||||
@type line ::
|
@type line ::
|
||||||
{:text, String.t()}
|
{:text, String.t()}
|
||||||
| {:link, URI.t(), String.t() | nil}
|
| {:link, URI.t(), String.t() | nil}
|
||||||
| {:preformatted, String.t(), String.t() | nil}
|
| {:preformatted_start, String.t() | nil}
|
||||||
|
| :preformatted_end
|
||||||
|
| {:preformatted, String.t()}
|
||||||
| {:heading, String.t(), 1 | 2 | 3}
|
| {:heading, String.t(), 1 | 2 | 3}
|
||||||
| {:list_item, String.t()}
|
| {:list_item, String.t()}
|
||||||
| {:quoted, String.t()}
|
| {:quoted, String.t()}
|
||||||
|
@ -62,19 +64,20 @@ defmodule Gemini do
|
||||||
{lines, _, _} =
|
{lines, _, _} =
|
||||||
doc
|
doc
|
||||||
|> String.split("\n")
|
|> String.split("\n")
|
||||||
|> Enum.reduce({[], false, nil}, fn line, {lines, in_preformatting, preformatting_alt} ->
|
|> Enum.reduce({[], false}, fn line, {lines, in_preformatting} ->
|
||||||
preformatting_toggle = match?("```" <> _, line)
|
preformatting_toggle = match?("```" <> _, line)
|
||||||
|
|
||||||
cond do
|
cond do
|
||||||
preformatting_toggle && in_preformatting ->
|
preformatting_toggle && in_preformatting ->
|
||||||
{lines, false, nil}
|
{[:preformatting_end | lines], false}
|
||||||
|
|
||||||
preformatting_toggle && !in_preformatting ->
|
preformatting_toggle && !in_preformatting ->
|
||||||
"```" <> alt = line
|
"```" <> alt = line
|
||||||
{lines, true, alt}
|
alt = if length(alt) == 0, do: nil, else: alt
|
||||||
|
{[{:preformatting_start, alt} | lines], true}
|
||||||
|
|
||||||
in_preformatting ->
|
in_preformatting ->
|
||||||
{[{:preformatted, line, preformatting_alt} | lines], true, preformatting_alt}
|
{[{:preformatted, line} | lines], true}
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
case line do
|
case line do
|
||||||
|
@ -85,25 +88,25 @@ defmodule Gemini do
|
||||||
[_, link, text] -> {link, text}
|
[_, link, text] -> {link, text}
|
||||||
end
|
end
|
||||||
|
|
||||||
{[{:link, URI.parse(link), text} | lines], false, nil}
|
{[{:link, URI.parse(link), text} | lines], false}
|
||||||
|
|
||||||
"###" <> rest ->
|
"###" <> rest ->
|
||||||
{[{:heading, String.trim(rest), 3} | lines], false, nil}
|
{[{:heading, String.trim(rest), 3} | lines], false}
|
||||||
|
|
||||||
"##" <> rest ->
|
"##" <> rest ->
|
||||||
{[{:heading, String.trim(rest), 2} | lines], false, nil}
|
{[{:heading, String.trim(rest), 2} | lines], false}
|
||||||
|
|
||||||
"#" <> rest ->
|
"#" <> rest ->
|
||||||
{[{:heading, String.trim(rest), 1} | lines], false, nil}
|
{[{:heading, String.trim(rest), 1} | lines], false}
|
||||||
|
|
||||||
"* " <> rest ->
|
"* " <> rest ->
|
||||||
{[{:list_item, String.trim(rest)} | lines], false, nil}
|
{[{:list_item, String.trim(rest)} | lines], false}
|
||||||
|
|
||||||
">" <> rest ->
|
">" <> rest ->
|
||||||
{[{:quoted, String.trim(rest)} | rest], false, nil}
|
{[{:quoted, String.trim(rest)} | rest], false}
|
||||||
|
|
||||||
line ->
|
line ->
|
||||||
{[{:text, line} | lines], false, nil}
|
{[{:text, line} | lines], false}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
Loading…
Reference in New Issue