From d0e70cbc29ac856b92914111ca46842aafe4b961 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 28 Jan 2012 09:30:31 -0800 Subject: XML: Added toEntities. --- src/Text/Pandoc/XML.hs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Text/Pandoc/XML.hs b/src/Text/Pandoc/XML.hs index e21525018..1532e790b 100644 --- a/src/Text/Pandoc/XML.hs +++ b/src/Text/Pandoc/XML.hs @@ -33,9 +33,11 @@ module Text.Pandoc.XML ( stripTags, inTags, selfClosingTag, inTagsSimple, - inTagsIndented ) where + inTagsIndented, + toEntities ) where import Text.Pandoc.Pretty +import Data.Char (ord, isAscii) -- | Remove everything between <...> stripTags :: String -> String @@ -89,3 +91,10 @@ inTagsSimple tagType = inTags False tagType [] -- | Put the supplied contents in indented block btw start and end tags. inTagsIndented :: String -> Doc -> Doc inTagsIndented tagType = inTags True tagType [] + +-- | Escape all non-ascii characters using numerical entities. +toEntities :: String -> String +toEntities [] = "" +toEntities (c:cs) + | isAscii c = c : toEntities cs + | otherwise = "&#" ++ show (ord c) ++ ";" ++ toEntities cs -- cgit v1.2.3