markup-parse
Safe HaskellNone
LanguageGHC2024

Data.Markup.Parser

Description

Parsing and tree operations for markup.

Synopsis

high-level parsers

markup :: Standard -> ByteString -> Warn Markup Source #

Convert bytestrings to Markup

Two-phase pipeline: lexical (tokenize) then semantic (gather)

>>> markup Html "<foo><br></foo><baz"
These [MarkupParser (ParserLeftover "<baz")] (Markup {elements = [Node {rootLabel = OpenTag StartTag "foo" [], subForest = [Node {rootLabel = OpenTag StartTag "br" [], subForest = []}]}]})

markup_ :: Standard -> ByteString -> Markup Source #

markup but errors on warnings.

tokenize :: Standard -> ByteString -> Warn [Token] Source #

Parse a bytestring into tokens

>>> tokenize Html "<foo>content</foo>"
That [OpenTag StartTag "foo" [],Content "content",EndTag "foo"]

tokenize_ :: Standard -> ByteString -> [Token] Source #

tokenize but errors on warnings.

tokenP :: Standard -> Parser Identity ByteString Char Token Source #

A Token parser.

>>> runMarkupParser (tokenP Html) "<foo>content</foo>"
("content</foo>",These () (OpenTag StartTag "foo" []))

token-stream tree builder

gather :: Standard -> TokenParser [MarkupWarning] Markup Source #

Gather together token trees from a token list, placing child elements in nodes and removing EndTags.

gather_ :: Standard -> [Token] -> Markup Source #

gather but errors on warnings.

degather :: Standard -> Markup -> Warn [Token] Source #

Convert a markup into a token list, adding end tags.

degather_ :: Standard -> Markup -> [Token] Source #

degather but errors on warning

normalisation & well-formedness

normalize :: Markup -> Markup Source #

Concatenate sequential content and normalize attributes; unwording class values and removing duplicate attributes (taking last).

normContent :: Markup -> Markup Source #

Normalise Content in Markup, concatenating adjacent Content, and removing mempty Content.

wellFormed :: Standard -> Markup -> [MarkupWarning] Source #

Check for well-formedness and return warnings encountered.

>>> wellFormed Html $ Markup [Node (Comment "") [], Node (EndTag "foo") [], Node (OpenTag EmptyElemTag "foo" []) [Node (Content "bar") []], Node (OpenTag EmptyElemTag "foo" []) []]
[EmptyContent,EndTagInTree,LeafWithChildren,BadEmptyElemTag]

isWellFormed :: Standard -> Markup -> Bool Source #

Are the trees in the markup well-formed?

element construction

element :: NameTag -> [Attr] -> Markup -> Markup Source #

Create Markup from a name tag and attributes that wraps some other markup.

>>> element "div" [] (element_ "br" [])
Markup {elements = [Node {rootLabel = OpenTag StartTag "div" [], subForest = [Node {rootLabel = OpenTag StartTag "br" [], subForest = []}]}]}

element_ :: NameTag -> [Attr] -> Markup Source #

Create Markup from a name tag and attributes that doesn't wrap some other markup. The OpenTagType used is StartTag. Use emptyElem if you want to create EmptyElemTag based markup.

>>> (element_ "br" [])
Markup {elements = [Node {rootLabel = OpenTag StartTag "br" [], subForest = []}]}

emptyElem :: NameTag -> [Attr] -> Markup Source #

Create Markup from a name tag and attributes using EmptyElemTag, that doesn't wrap some other markup. No checks are made on whether this creates well-formed markup.

>>> emptyElem "br" []
Markup {elements = [Node {rootLabel = OpenTag EmptyElemTag "br" [], subForest = []}]}

elementc :: NameTag -> [Attr] -> ByteString -> Markup Source #

Create Markup from a name tag and attributes that wraps some Content. No escaping is performed.

>>> elementc "div" [] "content"
Markup {elements = [Node {rootLabel = OpenTag StartTag "div" [], subForest = [Node {rootLabel = Content "content", subForest = []}]}]}

contentRaw :: ByteString -> Markup Source #

Create a Markup element from a bytestring, not escaping the usual characters.

>>> contentRaw "<content>"
Markup {elements = [Node {rootLabel = Content "<content>", subForest = []}]}

addAttrs :: [Attr] -> Token -> Maybe Token Source #

Append attributes to an existing Token attribute list. Returns Nothing for tokens that do not have attributes.

token parser helpers

runMarkupParser :: Parser Identity ByteString Char a -> ByteString -> (ByteString, These () a) Source #

Run a parser and return the remaining input and result as a tuple

runParserWarn :: Parser Identity ByteString Char a -> ByteString -> These ParserWarning a Source #

Run parser, returning leftovers and errors as ParserWarnings.

>>> runParserWarn ws " "
That ' '
>>> runParserWarn ws "x"
This ParserUncaught
>>> runParserWarn ws " x"
These (ParserLeftover "x") ' '

attrsP :: Standard -> Parser Identity ByteString Char [Attr] Source #

Parse an attribute. | Parse attributes list.

ws :: Parser Identity ByteString Char Char Source #

Alias for single whitespace (backward compat with mpar)

ws_ :: Parser Identity ByteString Char () Source #

Alias for skip whitespace (backward compat with mpar)

constants

doctypeHtml :: Markup Source #

Standard Html Doctype

doctypeXml :: Markup Source #

Standard Xml Doctype

selfClosers :: [NameTag] Source #

Html tags that self-close

internal token parsers (exported for reuse / testing)

bs :: Parser Identity ByteString Char a -> Parser Identity ByteString Char ByteString Source #

Matched span as a ByteString (capturedBS — flatparse specialty).

eq_ :: Parser Identity ByteString Char () Source #

equals sign with optional whitespace

wrappedQ :: Parser Identity ByteString Char ByteString Source #

quoted string: single or double quoted