@@ -15,9 +15,9 @@ module Text.Pandoc.Writers.HTML (
1515import Text.Pandoc.Definition
1616import Text.Pandoc.Shared
1717import Text.Html ( stringToHtmlString )
18−import Text.Regex ( mkRegex )
18+import Text.Regex ( mkRegex, matchRegex )
1919import Numeric ( showHex )
20−import Char ( ord )
20+import Data.Char ( ord, toLower )
2121import Data.List ( isPrefixOf, partition )
2222
2323-- | Convert Pandoc document to string in HTML format.
@@ -59,14 +59,27 @@ footnoteSection options notes =
5959-- | Obfuscate a "mailto:" link using Javascript.
6060obfuscateLink :: WriterOptions -> [Inline] -> String -> String
6161obfuscateLink options text src =
62− let text' = inlineListToHtml options text in
63− let linkText = if src == ("mailto:" ++ text')
64− then "e"
65− else "'" ++ text' ++ "'"
66− altText = if src == ("mailto:" ++ text')
67− then "\\1 [at] \\2"
68− else text' ++ " (\\1 [at] \\2)" in
69− gsub "mailto:([^@]*)@(.*)" ("<script type=\"text/javascript\">h='\\2';n='\\1';e=n+'@'+h;document.write('<a href=\"mailto:'+e+'\">'+" ++ linkText ++ "+'<\\/a>');</script><noscript>" ++ altText ++ "</noscript>") src
62+ let emailRegex = mkRegex "mailto:*([^@]*)@(.*)"
63+ text' = inlineListToHtml options text
64+ src' = map toLower src in
65+ case (matchRegex emailRegex src') of
66+ (Just [name, domain]) ->
67+ let domain' = gsub "\\." " dot " domain
68+ at' = obfuscateChar '@' in
69+ let linkText = if src' == ("mailto:" ++ text')
70+ then "e"
71+ else "'" ++ text' ++ "'"
72+ altText = if src' == ("mailto:" ++ text')
73+ then name ++ " at " ++ domain'
74+ else text' ++ " (" ++ name ++ " at " ++
75+ domain' ++ ")" in
76+ "<script type=\"text/javascript\">\n<!--\nh='" ++
77+ obfuscateString domain ++ "';a='" ++ at' ++ "';n='" ++
78+ obfuscateString name ++ "';e=n+a+h;\n" ++
79+ "document.write('<a h'+'ref'+'=\"ma'+'ilto'+':'+e+'\">'+" ++
80+ linkText ++ "+'<\\/'+'a'+'>');\n// -->\n</script><noscript>" ++
81+ obfuscateString altText ++ "</noscript>"
82+ _ -> "<a href=\"" ++ src ++ "\">" ++ text' ++ "</a>" -- malformed email
7083
7184-- | Obfuscate character as entity.
7285obfuscateChar :: Char -> String
@@ -75,6 +88,10 @@ obfuscateChar char =
7588 let numstr = if even num then (show num) else ("x" ++ (showHex num "")) in
7689 "&#" ++ numstr ++ ";"
7790
91+-- | Obfuscate string using entities.
92+obfuscateString :: String -> String
93+obfuscateString = concatMap obfuscateChar
94+
7895-- | Escape string, preserving character entities and quote.
7996stringToHtml :: String -> String
8097stringToHtml str = escapePreservingRegex stringToHtmlString