File: footnote_helpers.ex

package info (click to toggle)
elixir-earmark-parser 1.4.44-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,148 kB
  • sloc: makefile: 9
file content (66 lines) | stat: -rw-r--r-- 1,359 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
defmodule Support.FootnoteHelpers do
  import EarmarkAstDsl
  @moduledoc false

  def fn_ast(markdown) do
    {:ok, ast, []} = EarmarkParser.as_ast(markdown, footnotes: true)
    ast
  end

  def footnote_def(number, content)

  def footnote_def(number, content) when is_tuple(content) do
    footnote_def(number, [content])
  end

  def footnote_def(number, content) do
    tag("li", [reverse_footnote(number) | content], id: "fn:#{number}")
  end

  def footnote(number) do
    a("#{number}",
      title: "see footnote",
      href: "#fn:#{number}",
      class: "footnote",
      id: "fnref:#{number}"
    )
  end

  def footnotes(content) do
    tag("div", [tag("hr"), tag("ol", content)], class: "footnotes")
  end

  defp reverse_footnote(number) do
    a("↩",
      class: "reversefootnote",
      href: "#fnref:#{number}",
      title: "return to article"
    )
  end

  def has_verbatim?(ast) do
    case ast do
      [
        _,
        {"div", [{"class", "footnotes"}],
         [
           _,
           {"ol", [],
            [
              {"li", [_],
               [
                 {"a", [_, _, {"href", _}], ["↩"], %{verbatim: true}},
                 _
               ], %{}}
            ], %{}}
         ], %{}}
      ] ->
        true

      _ ->
        false
    end
  end
end

#  SPDX-License-Identifier: Apache-2.0