git.iko.soy/typeable/pandoc
on master help sign in
Made javascript obfuscation of emails even more obfuscatory,
b98edf2fiddlosopher
src/Text/Pandoc/Writers/HTML.hs+27−10
@@ -15,9 +15,9 @@ module Text.Pandoc.Writers.HTML (
15import Text.Pandoc.Definition
15import Text.Pandoc.Definition
16import Text.Pandoc.Shared
16import Text.Pandoc.Shared
17import Text.Html ( stringToHtmlString )
17import Text.Html ( stringToHtmlString )
18−import Text.Regex ( mkRegex )
18+import Text.Regex ( mkRegex, matchRegex )
19import Numeric ( showHex )
19import Numeric ( showHex )
20−import Char ( ord )
20+import Data.Char ( ord, toLower )
21import Data.List ( isPrefixOf, partition )
21import Data.List ( isPrefixOf, partition )
22
22
23-- | Convert Pandoc document to string in HTML format.
23-- | Convert Pandoc document to string in HTML format.
@@ -59,14 +59,27 @@ footnoteSection options notes =
59-- | Obfuscate a "mailto:" link using Javascript.
59-- | Obfuscate a "mailto:" link using Javascript.
60obfuscateLink :: WriterOptions -> [Inline] -> String -> String
60obfuscateLink :: WriterOptions -> [Inline] -> String -> String
61obfuscateLink options text src =
61obfuscateLink options text src =
62+ let emailRegex = mkRegex "mailto:*([^@]*)@(.*)"
62− let text' = inlineListToHtml options text in
63+ text' = inlineListToHtml options text
63− let linkText = if src == ("mailto:" ++ text')
64+ src' = map toLower src in
64− then "e"
65+ case (matchRegex emailRegex src') of
65− else "'" ++ text' ++ "'"
66+ (Just [name, domain]) ->
66− altText = if src == ("mailto:" ++ text')
67+ let domain' = gsub "\\." " dot " domain
67− then "\\1 [at] \\2"
68+ at' = obfuscateChar '@' in
68− else text' ++ " (\\1 [at] \\2)" in
69+ let linkText = if src' == ("mailto:" ++ text')
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
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
70
83
71-- | Obfuscate character as entity.
84-- | Obfuscate character as entity.
72obfuscateChar :: Char -> String
85obfuscateChar :: Char -> String
@@ -75,6 +88,10 @@ obfuscateChar char =
75 let numstr = if even num then (show num) else ("x" ++ (showHex num "")) in
88 let numstr = if even num then (show num) else ("x" ++ (showHex num "")) in
76 "&#" ++ numstr ++ ";"
89 "&#" ++ numstr ++ ";"
77
90
91+-- | Obfuscate string using entities.
92+obfuscateString :: String -> String
93+obfuscateString = concatMap obfuscateChar
94+
78-- | Escape string, preserving character entities and quote.
95-- | Escape string, preserving character entities and quote.
79stringToHtml :: String -> String
96stringToHtml :: String -> String
80stringToHtml str = escapePreservingRegex stringToHtmlString
97stringToHtml str = escapePreservingRegex stringToHtmlString
tests/writer.html+12−2
@@ -415,7 +415,12 @@ Cat &amp; 1 \\ \hline
415<p><a href="/url/" title="title preceded by a tab">URL and title</a>.</p>
415<p><a href="/url/" title="title preceded by a tab">URL and title</a>.</p>
416<p><a href="/url/" title="title with &quot;quotes&quot; in it">URL and title</a></p>
416<p><a href="/url/" title="title with &quot;quotes&quot; in it">URL and title</a></p>
417<p><a href="/url/" title="title with single quotes">URL and title</a></p>
417<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>
419<p><a href="">Empty</a>.</p>
424<p><a href="">Empty</a>.</p>
420<a id="Reference"></a>
425<a id="Reference"></a>
421<h2>Reference</h2>
426<h2>Reference</h2>
@@ -446,7 +451,12 @@ Cat &amp; 1 \\ \hline
446<li><a href="http://example.com/">http://example.com/</a></li>
451<li><a href="http://example.com/">http://example.com/</a></li>
447<li>It should.</li>
452<li>It should.</li>
448</ul>
453</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>
450<blockquote>
460<blockquote>
451<p>Blockquoted: <a href="http://example.com/">http://example.com/</a></p>
461<p>Blockquoted: <a href="http://example.com/">http://example.com/</a></p>
452</blockquote>
462</blockquote>
tests/writer.smart.html+12−2
@@ -415,7 +415,12 @@ Cat &amp; 1 \\ \hline
415<p><a href="/url/" title="title preceded by a tab">URL and title</a>.</p>
415<p><a href="/url/" title="title preceded by a tab">URL and title</a>.</p>
416<p><a href="/url/" title="title with &quot;quotes&quot; in it">URL and title</a></p>
416<p><a href="/url/" title="title with &quot;quotes&quot; in it">URL and title</a></p>
417<p><a href="/url/" title="title with single quotes">URL and title</a></p>
417<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>
419<p><a href="">Empty</a>.</p>
424<p><a href="">Empty</a>.</p>
420<a id="Reference"></a>
425<a id="Reference"></a>
421<h2>Reference</h2>
426<h2>Reference</h2>
@@ -446,7 +451,12 @@ Cat &amp; 1 \\ \hline
446<li><a href="http://example.com/">http://example.com/</a></li>
451<li><a href="http://example.com/">http://example.com/</a></li>
447<li>It should.</li>
452<li>It should.</li>
448</ul>
453</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>
450<blockquote>
460<blockquote>
451<p>Blockquoted: <a href="http://example.com/">http://example.com/</a></p>
461<p>Blockquoted: <a href="http://example.com/">http://example.com/</a></p>
452</blockquote>
462</blockquote>