Packages

Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as a command-line tool (run mix escript.build first). Output generation is pluggable.

Retired package: Deprecated - Earmark is no longer maintained. Migrate to a replacement, for example MDEx (https://hex.pm/packages/mdex).
Security advisory: This version has known vulnerabilities. View advisories

Current section

Files

Jump to
earmark lib earmark ast renderer table_renderer.ex
Raw

lib/earmark/ast/renderer/table_renderer.ex

defmodule Earmark.Ast.Renderer.TableRenderer do
@moduledoc false
alias Earmark.Ast.Inline
alias Earmark.Context
import Earmark.Ast.Emitter
def render_header(header, lnb, aligns, context) do
{th_ast, context1} =
header
|> Enum.zip(aligns)
|> Enum.map_reduce(context, &_render_col(&1, &2, lnb, "th"))
{emit("thead", emit("tr", th_ast)), context1}
end
def render_rows(rows, lnb, aligns, context) do
{rows1, context1} =
rows
|> Enum.zip(Stream.iterate(lnb, &(&1 + 1)))
|> Enum.map_reduce(context, &_render_row(&1, &2, aligns))
{[emit("tbody", rows1)], context1}
end
defp _render_cols(row, lnb, aligns, context, coltype \\ "td") do
row
|> Enum.zip(aligns)
|> Enum.map_reduce(context, &_render_col(&1, &2, lnb, coltype))
end
defp _render_col({col, align}, context, lnb, coltype) do
context1 = Inline.convert(col, lnb, Context.clear_value(context))
{emit(coltype, context1.value |> Enum.reverse, _align_to_style(align)), context1}
end
defp _render_row({row, lnb}, context, aligns) do
{ast, context1} = _render_cols(row, lnb, aligns, context)
{emit("tr", ast), context1}
end
defp _align_to_style(align)
defp _align_to_style(:left), do: [{"style", "text-align: left;"}]
defp _align_to_style(:right), do: [{"style", "text-align: right;"}]
defp _align_to_style(:center), do: [{"style", "text-align: center;"}]
defp _align_to_style(_), do: []
end