table_renderer.ex (1495B)
1 defmodule EarmarkParser.Ast.Renderer.TableRenderer do 2 @moduledoc false 3 4 alias EarmarkParser.Ast.Inline 5 alias EarmarkParser.Context 6 7 import EarmarkParser.Ast.Emitter 8 9 def render_header(header, lnb, aligns, context) do 10 {th_ast, context1} = 11 header 12 |> Enum.zip(aligns) 13 |> Enum.map_reduce(context, &_render_col(&1, &2, lnb, "th")) 14 {emit("thead", emit("tr", th_ast)), context1} 15 end 16 17 def render_rows(rows, lnb, aligns, context) do 18 {rows1, context1} = 19 rows 20 |> Enum.zip(Stream.iterate(lnb, &(&1 + 1))) 21 |> Enum.map_reduce(context, &_render_row(&1, &2, aligns)) 22 {[emit("tbody", rows1)], context1} 23 end 24 25 26 defp _render_cols(row, lnb, aligns, context, coltype \\ "td") do 27 row 28 |> Enum.zip(aligns) 29 |> Enum.map_reduce(context, &_render_col(&1, &2, lnb, coltype)) 30 end 31 32 defp _render_col({col, align}, context, lnb, coltype) do 33 context1 = Inline.convert(col, lnb, Context.clear_value(context)) 34 {emit(coltype, context1.value |> Enum.reverse, _align_to_style(align)), context1} 35 end 36 37 defp _render_row({row, lnb}, context, aligns) do 38 {ast, context1} = _render_cols(row, lnb, aligns, context) 39 {emit("tr", ast), context1} 40 end 41 42 defp _align_to_style(align) 43 defp _align_to_style(:left), do: [{"style", "text-align: left;"}] 44 defp _align_to_style(:right), do: [{"style", "text-align: right;"}] 45 defp _align_to_style(:center), do: [{"style", "text-align: center;"}] 46 end 47 # SPDX-License-Identifier: Apache-2.0