@@ -1,72 +0,0 @@
1−{-
2−Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>
3−
4−This program is free software; you can redistribute it and/or modify
5−it under the terms of the GNU General Public License as published by
6−the Free Software Foundation; either version 2 of the License, or
7−(at your option) any later version.
8−
9−This program is distributed in the hope that it will be useful,
10−but WITHOUT ANY WARRANTY; without even the implied warranty of
11−MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12−GNU General Public License for more details.
13−
14−You should have received a copy of the GNU General Public License
15−along with this program; if not, write to the Free Software
16−Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17−-}
18−
19−{- |
20− Module : Text.Pandoc.CharacterReferences
21− Copyright : Copyright (C) 2006-2010 John MacFarlane
22− License : GNU GPL, version 2 or above
23−
24− Maintainer : John MacFarlane <jgm@berkeley.edu>
25− Stability : alpha
26− Portability : portable
27−
28−Functions for parsing character references.
29−-}
30−module Text.Pandoc.CharacterReferences (
31− characterReference,
32− decodeCharacterReferences,
33− ) where
34−import Text.ParserCombinators.Parsec
35−import Text.HTML.TagSoup.Entity ( lookupNamedEntity, lookupNumericEntity )
36−import Data.Maybe ( fromMaybe )
37−
38−-- | Parse character entity.
39−characterReference :: GenParser Char st Char
40−characterReference = try $ do
41− char '&'
42− character <- numRef <|> entity
43− char ';'
44− return character
45−
46−numRef :: GenParser Char st Char
47−numRef = do
48− char '#'
49− num <- hexNum <|> decNum
50− return $ fromMaybe '?' $ lookupNumericEntity num
51−
52−hexNum :: GenParser Char st [Char]
53−hexNum = do
54− x <- oneOf "Xx"
55− num <- many1 hexDigit
56− return (x:num)
57−
58−decNum :: GenParser Char st [Char]
59−decNum = many1 digit
60−
61−entity :: GenParser Char st Char
62−entity = do
63− body <- many1 alphaNum
64− return $ fromMaybe '?' $ lookupNamedEntity body
65−
66−-- | Convert entities in a string to characters.
67−decodeCharacterReferences :: String -> String
68−decodeCharacterReferences str =
69− case parse (many (characterReference <|> anyChar)) str str of
70− Left err -> error $ "\nError: " ++ show err
71− Right result -> result
72−