zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

footnote_renderer.ex (1428B)


      1 defmodule EarmarkParser.Ast.Renderer.FootnoteRenderer do
      2   import EarmarkParser.Ast.Emitter
      3   alias EarmarkParser.{AstRenderer, Block, Context, Message}
      4   import Context, only: [clear_value: 1, prepend: 2]
      5 
      6   @moduledoc false
      7 
      8   @empty_set MapSet.new([])
      9 
     10   def render_defined_fns(%Block.FnList{blocks: footnotes}, context) do
     11     {elements, errors} = render_footnote_blocks(footnotes, context)
     12 
     13     ast =
     14       emit(
     15         "div",
     16         [
     17           emit("hr"),
     18           emit("ol", elements)
     19         ],
     20         class: "footnotes"
     21       )
     22 
     23     prepend(context, ast) |> Message.add_messages(errors)
     24   end
     25 
     26   defp _render_footnote_def(%Block.FnDef{blocks: blocks, id: id}, {ast, errors, context}=acc) do
     27     if MapSet.member?(context.referenced_footnote_ids, id) do
     28       context1 = AstRenderer.render(blocks, clear_value(context))
     29       a_attrs = %{title: "return to article", class: "reversefootnote", href: "#fnref:#{id}"}
     30       footnote_li_ast =
     31         emit("li", [emit("a", ["↩"], a_attrs) | context1.value],
     32          id: "fn:#{id}")
     33       {[footnote_li_ast|ast], MapSet.union(errors, context1.options.messages), context}
     34     else
     35       acc
     36     end
     37   end
     38 
     39 
     40   defp render_footnote_blocks(footnotes, context) do
     41     {elements, errors, _} =
     42       footnotes
     43       |> Enum.reduce({[], @empty_set, context}, &_render_footnote_def/2)
     44 
     45     {elements|>Enum.reverse, errors}
     46   end
     47 end
     48 #  SPDX-License-Identifier: Apache-2.0