simple_markdown v0.8.1 SimpleMarkdown.Renderer.HTML.Utilities

Convenient functions for working with HTML.

Link to this section Summary

Functions

Convert the HTML AST to HTML.

A list of any special nodes that will be treated as raw character data.

Convert the HTML to HTML AST.

Link to this section Types

Specs

ast() ::
  {tag :: String.Chars.t(), attrs :: [{String.Chars.t(), String.Chars.t()}],
   ast()}
  | [ast()]
  | String.t()
Link to this type

chardata_list()

Specs

chardata_list() :: [{String.t(), String.t()}]
Link to this type

format(type)

Specs

format(type) :: {type, version()}

Specs

formats() :: format(:html) | format(:xhtml)

Specs

tag_list() :: [atom() | String.t()]

Specs

version() :: {major :: non_neg_integer(), minor :: non_neg_integer()}

Link to this section Functions

Link to this function

ast_to_html(ast, opts \\ [])

Specs

ast_to_html(ast(), keyword()) :: IO.chardata()

Convert the HTML AST to HTML.

The conversion behaviour can be modified by setting the opts parameter with any of the following:

  • :format - To control the HTML format. This takes one of the valid formats/0. By default this is set to generate HTML5 code ({ :html, { 5, 0 } }).
  • :void_elements - To customise which elements are void elements (do not contain content). This takes a tag_list/0. By default this is set to the list of tags returned by void_elements/0.
  • :raw_text_elements - To customise which elements are raw text elements (do not encode their content nor contain nested nodes). This takes a tag_list/0. By default this is set to the list of tags returned by raw_text_elements/0.
  • :include_chardata - To control whether nodes that match :chardata should be included in the HTML or not. By default this is set to false.
  • :chardata - To customise which elements are considered to be character data (special cases that do not encode their content nor contain nested nodes). This takes a chardata_list/0. By default this is set to the list of opening/closing tags returned by chardata/0.

Example

iex> SimpleMarkdown.Renderer.HTML.Utilities.ast_to_html({ :p, [], "hello" }) |> IO.chardata_to_string
"<p>hello</p>"

iex> SimpleMarkdown.Renderer.HTML.Utilities.ast_to_html({ "!--", [], "hello" }, include_chardata: true) |> IO.chardata_to_string
"<!--hello-->"

A list of any special nodes that will be treated as raw character data.

Currently this includes comments, character data, DTD (document type definitions), PI (processing instructons).

Examples of currently supported nodes and how they're represented in the AST:

{ "!--", [], " comment " } #<!-- comment -->
{ "![CDATA[", [], "foo" } #<![CDATA[foo]]>
{ "!DOCTYPE", [], " html" } #<!DOCTYPE html>
{ "?", [], "xml version="1.0" encoding="UTF-8" " } #<?xml version="1.0" encoding="UTF-8" ?>
Link to this function

html_to_ast(html, opts \\ [])

Specs

html_to_ast(IO.chardata(), keyword()) :: ast()

Convert the HTML to HTML AST.

The parsing behaviour can be modified by setting the opts parameter with any of the following:

  • :void_elements - To customise which elements are void elements (do not contain content). This takes a tag_list/0. By default this is set to the list of tags returned by void_elements/0.
  • :raw_text_elements - To customise which elements are raw text elements (do not encode their content nor contain nested nodes). This takes a tag_list/0. By default this is set to the list of tags returned by raw_text_elements/0.
  • :include_chardata - To control whether nodes that match :chardata should be included in the AST or not. By default this is set to false.
  • :chardata - To customise which elements are considered to be character data (special cases that do not encode their content nor contain nested nodes). This takes a chardata_list/0. By default this is set to the list of opening/closing tags returned by chardata/0.

Example

iex> SimpleMarkdown.Renderer.HTML.Utilities.html_to_ast("<p>hello</p>")
{ "p", [], "hello" }

iex> SimpleMarkdown.Renderer.HTML.Utilities.html_to_ast("<!--hello-->", include_chardata: true)
{ "!--", [], "hello" }
Link to this function

raw_text_elements()

Specs

raw_text_elements() :: tag_list()

A list of raw text elements.

Link to this function

void_elements()

Specs

void_elements() :: tag_list()

A list of void elements.