HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

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.

  • 6 types
  • 1 class
  • 27 values
  • Packagetagsoup-0.14.8
  • Exports34
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTagSoup.hs

Data structures and parsing

16 declarations
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 Column = Int
#

The column of a position, starting at 1

typetype Attribute str = (str, str)
#

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

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   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

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 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)

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>"
valuerenderTagsOptions :: StringLike str => RenderOptions str -> [Tag str] -> str
#

Show a list of tags using settings supplied by the RenderOptions parameter, eg. to avoid escaping any characters one could do:

renderTagsOptions renderOptions{optEscape = id} [TagText "my&"] == "my&"

Tag identification

8 declarations

Extraction

5 declarations
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 "".

valueinnerText :: StringLike str => [Tag str] -> str
#

Extract all text content from tags (similar to Verbatim found in HaXml)

Utility

2 declarations
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

3 declarations
classclass TagRep a where
#

Define a class to allow String's or Tag str's to be used as matches

Methods

Instances2TagRep
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.