Haskell

Haskell.hs

Copyright © 2007 Dave Bayer. Subject to a BSD-style license.

This module is part of the Annote project.

module Haskell (doHaskell) where

Haskell provides a language-specific filter for the language Haskell.


Regex provides regular expression matching. It is a wrapper around Text.Regex.

import Regex (mkRegex,matchRegex,Sub,mkSub,doSub)

Split divides an input file into code, documentation, and external documentation.

import Split (Split(..))

Filter processes the Code and Doc lines of split input text.

import Filter (Filt,DocSubs,filterLine,docSubs,process)

notSymbol

notSymbol is a pattern matching any character that cannot be used to form a Haskell operator symbol.

notSymbol :: String
notSymbol = "[^-!#$%&*+./<=>?@\\^|~]"

isolated

isolated matches the string s if it is not part of a longer Haskell operator symbol.

isolated :: String → String
isolated s = "(^|" ++ notSymbol ++ ")" ++ s ++ "($|" ++ notSymbol ++ ")"

codeSubs

codeSubs is a mapping of regular expression substitutions to apply to Haskell source code. We rewrite =>, <=, ->, <- arrows.

codeSubs :: [Sub]
codeSubs = map mkSub
    [ (isolated "⇒", "\\1&rArr;\\2")
    , (isolated "⇐", "\\1&lArr;\\2")
    , (isolated "→", "\\1&rarr;\\2")
    , (isolated "←", "\\1&larr;\\2")
    ]

libraries

libraries is the location of GHC Haskell web pages documenting libraries.

libraries :: String
libraries = "http://www.haskell.org/ghc/docs/latest/html/libraries/"

haskellDocSubs

haskellDocSubs is a mapping of regular expression substitutions to apply to Haskell documentation.

haskellDocSubs :: DocSubs
haskellDocSubs = 
    [ ("lib", "([^` \t]+)[ \t]+`([^` \t]+)`",
        "[`\\3`](" ++ libraries ++ "\\2/\\3.html \"\\3\")")
    ]

undotURL

undotURL converts . to - in the name of a Haskell standard library module.

undotURL :: String → String
undotURL line =
    let regex = mkRegex $ "(^.*" ++ libraries ++ ")(.*)(.html .*$)"
        sub   = mkSub ("\\.", "-")
    in  case matchRegex regex line of
        Just [x,y,z] → x ++ (doSub y sub) ++ z
        _ → line

codeFilter, docFilter

codeFilter, docFilter specialize filterLine to code and documentation, using codeSubs and docSubs respectively. For documentation, we also apply undotURL to clean up library module references.

codeFilter, docFilter :: Filt
codeFilter = filterLine codeSubs
docFilter = undotURL . (filterLine $ docSubs haskellDocSubs)

doHaskell

doHaskell is the exported function for this module. It provides a language-specific filter for the language Haskell.

doHaskell :: [Split] → [Split]
doHaskell = process codeFilter docFilter