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.
A list of raw text elements.
A list of void elements.
Link to this section Types
ast()
Specs
ast() :: {tag :: String.Chars.t(), attrs :: [{String.Chars.t(), String.Chars.t()}], ast()} | [ast()] | String.t()
chardata_list()
Specs
format(type)
Specs
format(type) :: {type, version()}
formats()
Specs
tag_list()
Specs
version()
Specs
version() :: {major :: non_neg_integer(), minor :: non_neg_integer()}
Link to this section Functions
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 validformats/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 atag_list/0
. By default this is set to the list of tags returned byvoid_elements/0
.:raw_text_elements
- To customise which elements are raw text elements (do not encode their content nor contain nested nodes). This takes atag_list/0
. By default this is set to the list of tags returned byraw_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 achardata_list/0
. By default this is set to the list of opening/closing tags returned bychardata/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-->"
chardata()
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" ?>
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 atag_list/0
. By default this is set to the list of tags returned byvoid_elements/0
.:raw_text_elements
- To customise which elements are raw text elements (do not encode their content nor contain nested nodes). This takes atag_list/0
. By default this is set to the list of tags returned byraw_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 achardata_list/0
. By default this is set to the list of opening/closing tags returned bychardata/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" }
raw_text_elements()
Specs
raw_text_elements() :: tag_list()
A list of raw text elements.
void_elements()
Specs
void_elements() :: tag_list()
A list of void elements.