-
Notifications
You must be signed in to change notification settings - Fork 21
popdoc: parse iex-popcorn blocks in markdown (3/5) #697
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
base: popdoc-wasm-iex
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,12 @@ defmodule Popdoc.Markdown do | |
| @popcorn_code_class "elixir-popcorn" | ||
| @eval_class "popcorn-eval" | ||
|
|
||
| @iex_code_class "iex-popcorn" | ||
| @iex_class "popcorn-iex" | ||
|
|
||
| @iex_prompt_re ~r/^iex(?:\(\d+\))?> ?/ | ||
| @cont_prompt_re ~r/^\.\.\.(?:\(\d+\))?> ?/ | ||
|
|
||
| def available? do | ||
| case earmark_adapter() do | ||
| {:ok, module} -> apply(module, :available?, []) | ||
|
|
@@ -26,12 +32,27 @@ defmodule Popdoc.Markdown do | |
| defp walk(list) when is_list(list), do: walk_list(list, []) | ||
|
|
||
| defp walk({:pre, pre_attrs, [{:code, code_attrs, code_children, code_meta}], pre_meta}) do | ||
| if popcorn_code_block?(code_attrs) do | ||
| {:pre, add_class(pre_attrs, @eval_class), | ||
| [{:code, normalize_code_attrs(code_attrs), walk(code_children), code_meta}], pre_meta} | ||
| else | ||
| {:pre, pre_attrs, [{:code, code_attrs, walk(code_children), code_meta}], pre_meta} | ||
| end | ||
| {pre_attrs, code_attrs} = | ||
| cond do | ||
| popcorn_code_block?(code_attrs) -> | ||
| {add_class(pre_attrs, @eval_class), normalize_code_attrs(code_attrs)} | ||
|
|
||
| iex_code_block?(code_attrs) -> | ||
| commands_json = | ||
| code_children |> extract_text() |> parse_iex_commands() |> commands_to_json() | ||
|
|
||
| new_pre_attrs = | ||
| pre_attrs | ||
| |> add_class(@iex_class) | ||
| |> Keyword.put(:"data-popcorn-iex-commands", commands_json) | ||
|
|
||
| {new_pre_attrs, normalize_code_attrs(code_attrs)} | ||
|
|
||
| true -> | ||
| {pre_attrs, code_attrs} | ||
| end | ||
|
|
||
| {:pre, pre_attrs, [{:code, code_attrs, walk(code_children), code_meta}], pre_meta} | ||
| end | ||
|
|
||
| defp walk({tag, attrs, children, meta}), do: {tag, attrs, walk(children), meta} | ||
|
|
@@ -46,12 +67,19 @@ defmodule Popdoc.Markdown do | |
| |> Enum.member?(@popcorn_code_class) | ||
| end | ||
|
|
||
| defp iex_code_block?(attrs) do | ||
| attrs | ||
| |> class_tokens() | ||
| |> Enum.member?(@iex_code_class) | ||
| end | ||
|
|
||
| defp normalize_code_attrs(attrs) do | ||
| classes = | ||
| attrs | ||
| |> class_tokens() | ||
| |> Enum.map(fn | ||
| @popcorn_code_class -> "elixir" | ||
| @iex_code_class -> "elixir" | ||
| class -> class | ||
| end) | ||
| |> Enum.uniq() | ||
|
|
@@ -60,6 +88,54 @@ defmodule Popdoc.Markdown do | |
| Keyword.put(attrs, :class, classes) | ||
| end | ||
|
|
||
| defp extract_text(children) when is_list(children) do | ||
| Enum.map_join(children, "", fn | ||
| text when is_binary(text) -> text | ||
| _ -> "" | ||
| end) | ||
| end | ||
|
|
||
| defp parse_iex_commands(text) do | ||
| {commands, current} = | ||
| text | ||
| |> String.split("\n") | ||
| |> Enum.reduce({[], nil}, fn line, {commands, current} -> | ||
| trimmed = String.trim_leading(line) | ||
|
|
||
| cond do | ||
| code = strip_prompt(trimmed, @iex_prompt_re) -> | ||
| acc = if current != nil, do: [current | commands], else: commands | ||
| {acc, code} | ||
|
|
||
| (cont = strip_prompt(trimmed, @cont_prompt_re)) && current != nil -> | ||
| {commands, current <> "\n" <> cont} | ||
|
|
||
| true -> | ||
| {commands, current} | ||
| end | ||
|
Comment on lines
+105
to
+115
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to change, assignments in cond, we use |
||
| end) | ||
|
|
||
| final = if current != nil, do: [current | commands], else: commands | ||
| Enum.reverse(final) | ||
| end | ||
|
|
||
| defp strip_prompt(line, prompt_re) do | ||
| case Regex.run(prompt_re, line) do | ||
| [prompt] -> String.replace_prefix(line, prompt, "") | ||
| nil -> nil | ||
| end | ||
| end | ||
|
Comment on lines
+122
to
+127
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like it could be |
||
|
|
||
| # ExDoc.Utils.to_json escapes every control character; a raw byte < 0x20 | ||
| # in a doc line would otherwise make JSON.parse throw and silently disable | ||
| # the block. ExDoc is guaranteed loaded here: to_ast/2 already requires its | ||
| # Earmark adapter. | ||
| defp commands_to_json(commands) do | ||
| Module.concat([ExDoc, Utils]) | ||
| |> apply(:to_json, [commands]) | ||
|
Comment on lines
+134
to
+135
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we depend on ExDoc? |
||
| |> IO.iodata_to_binary() | ||
| end | ||
|
|
||
| defp class_tokens(attrs) do | ||
| attrs | ||
| |> Keyword.get(:class, "") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like it mixes a lot of abstraction. What about:
type(code_attrs) -> :iex | :code_block | :otheradd_data(attrs, key, value)normalize_code_attrsafter the cond/case block (maybe with rename todedup())