HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Moduletagsoup-0.14.8Haskell2010

Text.HTML.TagSoup

This module is for working with HTML/XML. It deals with both well-formed XML and malformed HTML from the web. It features:

  • A lazy parser, based on the HTML 5 specification - see parseTags.

  • A renderer that can write out HTML/XML - see renderTags.

  • Utilities for extracting information from a document - see ~==, sections and partitions.

The standard practice is to parse a String to [Tag String] using parseTags, then operate upon it to extract the necessary information.

Contents
  1. Data structures and parsing
  2. Tag identification
  3. Extraction
  4. Utility
  5. Combinators
Synopsis
  • Packagetagsoup-0.14.8
  • Exports34
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTagSoup.hs

Data structures and parsing

datadata Tag str

A single HTML element. A whole document is represented by a list of Tag. There is no requirement for TagOpen and TagClose to match.

Constructors

Instances6Functor, Eq, Data, Ord, Show, TagRep
  • Functor TagDefined in tagsoup-0.14.8 · Text.HTML.TagSoup.Type
  • Eq str => Eq (Tag str)Defined in tagsoup-0.14.8 · Text.HTML.TagSoup.Type
  • Data str => Data (Tag str)Defined in tagsoup-0.14.8 · Text.HTML.TagSoup.Type
  • Ord str => Ord (Tag str)Defined in tagsoup-0.14.8 · Text.HTML.TagSoup.Type
  • Show str => Show (Tag str)Defined in tagsoup-0.14.8 · Text.HTML.TagSoup.Type
  • StringLike str => TagRep (Tag str)Defined in tagsoup-0.14.8 · Text.HTML.TagSoup
typetype Row = Int

The row/line of a position, starting at 1

typetype Attribute str = (str, str)

An HTML attribute id="name" generates ("id","name")

valueparseTags :: StringLike str => str -> [Tag str]

Parse a string to a list of tags, using an HTML 5 compliant parser.

parseTags "<hello>my&amp;</world>" == [TagOpen "hello" [],TagText "my&",TagClose "world"]
valueparseTagsOptions :: StringLike str => ParseOptions str -> str -> [Tag str]

Parse a string to a list of tags, using settings supplied by the ParseOptions parameter, eg. to output position information:

parseTagsOptions parseOptions{optTagPosition = True} "<hello>my&amp;</world>" ==
   [TagPosition 1 1,TagOpen "hello" [],TagPosition 1 8,TagText "my&",TagPosition 1 15,TagClose "world"]
datadata ParseOptions str

These options control how parseTags works. The ParseOptions type is usually generated by one of parseOptions, parseOptionsFast or parseOptionsEntities, then selected fields may be overriden.

The options optTagPosition and optTagWarning specify whether to generate TagPosition or TagWarning elements respectively. Usually these options should be set to False to simplify future stages, unless you rely on position information or want to give malformed HTML messages to the end user.

The options optEntityData and optEntityAttrib control how entities, for example &nbsp; are handled. Both take a string, and a boolean, where True indicates that the entity ended with a semi-colon ;. Inside normal text optEntityData will be called, and the results will be inserted in the tag stream. Inside a tag attribute optEntityAttrib will be called, and the first component of the result will be used in the attribute, and the second component will be appended after the TagOpen value (usually the second component is []). As an example, to not decode any entities, pass:

parseOptions
    {optEntityData=\(str,b) -> [TagText $ "&" ++ str ++ [';' | b]]
    ,optEntityAttrib\(str,b) -> ("&" ++ str ++ [';' | b], [])

Constructors

valuerenderTags :: StringLike str => [Tag str] -> str

Show a list of tags, as they might have been parsed, using the default settings given in RenderOptions.

renderTags [TagOpen "hello" [],TagText "my&",TagClose "world"] == "<hello>my&amp;</world>"
datadata RenderOptions str

These options control how renderTags works.

The strange quirk of only minimizing <br> tags is due to Internet Explorer treating <br></br> as <br><br>.

Constructors

  • RenderOptions
    • optEscape :: str -> str

      Escape a piece of text (default = escape the four characters &"<>)

    • optMinimize :: str -> Bool

      Minimise <b></b> -> <b/> (default = minimise only <br> tags)

    • optRawTag :: str -> Bool

      Should a tag be output with no escaping (default = true only for script)

Tag identification

Extraction

valuefromAttrib :: (Show str, Eq str, StringLike str) => str -> Tag str -> str

Extract an attribute, crashes if not a TagOpen. Returns "" if no attribute present.

Warning: does not distinquish between missing attribute and present attribute with value "".

Utility

valuesections :: (a -> Bool) -> [a] -> [[a]]

This function takes a list, and returns all suffixes whose first item matches the predicate.

valuepartitions :: (a -> Bool) -> [a] -> [[a]]

This function is similar to sections, but splits the list so no element appears in any two partitions.

Combinators

value(~==) :: (StringLike str, TagRep t) => Tag str -> t -> Bool

Performs an inexact match, the first item should be the thing to match. If the second item is a blank string, that is considered to match anything. For example:

(TagText "test" ~== TagText ""    ) == True
(TagText "test" ~== TagText "test") == True
(TagText "test" ~== TagText "soup") == False

For TagOpen missing attributes on the right are allowed.