git.iko.soy/typeable/pandoc
on master help sign in
Made javascript obfuscation of emails even more obfuscatory,

by combining it with entity obfuscation. git-svn-id: https://pandoc.googlecode.com/svn/trunk@254 788f1e2b-df1e-0410-8736-df70ead52e1b

commitb98edf2c745d6d2a1b5a20c2b50c262d9b4e85f2 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateWed 20 Dec 2006, 18:16 (20 years ago) parent19a81cb
3 files changed+51−14
src/Text/Pandoc/Writers/HTML.hs+27−10 tests/writer.html+12−2 tests/writer.smart.html+12−2
src/Text/Pandoc/Writers/HTML.hs+27−10
@@ -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
tests/writer.html+12−2
@@ -415,7 +415,12 @@ Cat &amp; 1 \\ \hline
415415<p><a href="/url/" title="title preceded by a tab">URL and title</a>.</p>
416416<p><a href="/url/" title="title with &quot;quotes&quot; in it">URL and title</a></p>
417417<p><a href="/url/" title="title with single quotes">URL and title</a></p>
418−<p><script type="text/javascript">h='nowhere.net';n='nobody';e=n+'@'+h;document.write('<a href="mailto:'+e+'">'+'Email link'+'<\/a>');</script><noscript>Email link (nobody [at] nowhere.net)</noscript></p>
418+<p><script type="text/javascript">
419+<!--
420+h='&#110;&#x6f;&#x77;&#104;&#x65;&#114;&#x65;&#46;&#110;&#x65;&#116;';a='&#64;';n='&#110;&#x6f;&#98;&#x6f;&#100;&#x79;';e=n+a+h;
421+document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'">'+'Email link'+'<\/'+'a'+'>');
422+// -->
423+</script><noscript>&#x45;&#x6d;&#x61;&#x69;&#108;&#32;&#108;&#x69;&#110;&#x6b;&#32;&#40;&#110;&#x6f;&#98;&#x6f;&#100;&#x79;&#32;&#x61;&#116;&#32;&#110;&#x6f;&#x77;&#104;&#x65;&#114;&#x65;&#32;&#100;&#x6f;&#116;&#32;&#110;&#x65;&#116;&#x29;</noscript></p>
419424<p><a href="">Empty</a>.</p>
420425<a id="Reference"></a>
421426<h2>Reference</h2>
@@ -446,7 +451,12 @@ Cat &amp; 1 \\ \hline
446451<li><a href="http://example.com/">http://example.com/</a></li>
447452<li>It should.</li>
448453</ul>
449−<p>An e-mail address: <script type="text/javascript">h='nowhere.net';n='nobody';e=n+'@'+h;document.write('<a href="mailto:'+e+'">'+e+'<\/a>');</script><noscript>nobody [at] nowhere.net</noscript></p>
454+<p>An e-mail address: <script type="text/javascript">
455+<!--
456+h='&#110;&#x6f;&#x77;&#104;&#x65;&#114;&#x65;&#46;&#110;&#x65;&#116;';a='&#64;';n='&#110;&#x6f;&#98;&#x6f;&#100;&#x79;';e=n+a+h;
457+document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'">'+e+'<\/'+'a'+'>');
458+// -->
459+</script><noscript>&#110;&#x6f;&#98;&#x6f;&#100;&#x79;&#32;&#x61;&#116;&#32;&#110;&#x6f;&#x77;&#104;&#x65;&#114;&#x65;&#32;&#100;&#x6f;&#116;&#32;&#110;&#x65;&#116;</noscript></p>
450460<blockquote>
451461<p>Blockquoted: <a href="http://example.com/">http://example.com/</a></p>
452462</blockquote>
tests/writer.smart.html+12−2
@@ -415,7 +415,12 @@ Cat &amp; 1 \\ \hline
415415<p><a href="/url/" title="title preceded by a tab">URL and title</a>.</p>
416416<p><a href="/url/" title="title with &quot;quotes&quot; in it">URL and title</a></p>
417417<p><a href="/url/" title="title with single quotes">URL and title</a></p>
418−<p><script type="text/javascript">h='nowhere.net';n='nobody';e=n+'@'+h;document.write('<a href="mailto:'+e+'">'+'Email link'+'<\/a>');</script><noscript>Email link (nobody [at] nowhere.net)</noscript></p>
418+<p><script type="text/javascript">
419+<!--
420+h='&#110;&#x6f;&#x77;&#104;&#x65;&#114;&#x65;&#46;&#110;&#x65;&#116;';a='&#64;';n='&#110;&#x6f;&#98;&#x6f;&#100;&#x79;';e=n+a+h;
421+document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'">'+'Email link'+'<\/'+'a'+'>');
422+// -->
423+</script><noscript>&#x45;&#x6d;&#x61;&#x69;&#108;&#32;&#108;&#x69;&#110;&#x6b;&#32;&#40;&#110;&#x6f;&#98;&#x6f;&#100;&#x79;&#32;&#x61;&#116;&#32;&#110;&#x6f;&#x77;&#104;&#x65;&#114;&#x65;&#32;&#100;&#x6f;&#116;&#32;&#110;&#x65;&#116;&#x29;</noscript></p>
419424<p><a href="">Empty</a>.</p>
420425<a id="Reference"></a>
421426<h2>Reference</h2>
@@ -446,7 +451,12 @@ Cat &amp; 1 \\ \hline
446451<li><a href="http://example.com/">http://example.com/</a></li>
447452<li>It should.</li>
448453</ul>
449−<p>An e-mail address: <script type="text/javascript">h='nowhere.net';n='nobody';e=n+'@'+h;document.write('<a href="mailto:'+e+'">'+e+'<\/a>');</script><noscript>nobody [at] nowhere.net</noscript></p>
454+<p>An e-mail address: <script type="text/javascript">
455+<!--
456+h='&#110;&#x6f;&#x77;&#104;&#x65;&#114;&#x65;&#46;&#110;&#x65;&#116;';a='&#64;';n='&#110;&#x6f;&#98;&#x6f;&#100;&#x79;';e=n+a+h;
457+document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'">'+e+'<\/'+'a'+'>');
458+// -->
459+</script><noscript>&#110;&#x6f;&#98;&#x6f;&#100;&#x79;&#32;&#x61;&#116;&#32;&#110;&#x6f;&#x77;&#104;&#x65;&#114;&#x65;&#32;&#100;&#x6f;&#116;&#32;&#110;&#x65;&#116;</noscript></p>
450460<blockquote>
451461<p>Blockquoted: <a href="http://example.com/">http://example.com/</a></p>
452462</blockquote>