git.iko.soy/typeable/pandoc
on master help sign in
Added --email-obfuscation option.

+ Added writer option for email obfuscation. + Implemented email obfuscation options in HTML writer. + Added option to option parser. + Documented in README and pandoc man page. + Resolves Issue #97. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1523 788f1e2b-df1e-0410-8736-df70ead52e1b

commit2806aee9b210c338037176bff467b76a394b120f authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSat 24 Jan 2009, 19:58 (18 years ago) parent46a3b22
5 files changed+110−60
Main.hs+42−23 README+8 Text/Pandoc/Shared.hs+41−32 Text/Pandoc/Writers/HTML.hs+11−5 man/man1/pandoc.1.md+8
Main.hs+42−23
@@ -32,7 +32,7 @@ writers.
3232module Main where
3333import Text.Pandoc
3434import Text.Pandoc.ODT
35−import Text.Pandoc.Shared ( HTMLMathMethod (..), splitBy )
35+import Text.Pandoc.Shared ( HTMLMathMethod (..), splitBy, ObfuscationMethod (..) )
3636import Text.Pandoc.Highlighting ( languages )
3737import System.Environment ( getArgs, getProgName, getEnvironment )
3838import System.Exit ( exitWith, ExitCode (..) )
@@ -156,6 +156,7 @@ data Opt = Opt
156156 , optWrapText :: Bool -- ^ Wrap text
157157 , optSanitizeHTML :: Bool -- ^ Sanitize HTML
158158 , optPlugins :: [Pandoc -> IO Pandoc] -- ^ Plugins to apply
159+ , optEmailObfuscation :: ObfuscationMethod
159160#ifdef _CITEPROC
160161 , optBiblioFile :: String
161162 , optBiblioFormat :: String
@@ -191,6 +192,7 @@ defaultOpts = Opt
191192 , optWrapText = True
192193 , optSanitizeHTML = False
193194 , optPlugins = []
195+ , optEmailObfuscation = JavascriptObfuscation
194196#ifdef _CITEPROC
195197 , optBiblioFile = []
196198 , optBiblioFormat = []
@@ -301,6 +303,19 @@ options =
301303 (\opt -> return opt { optSanitizeHTML = True }))
302304 "" -- "Sanitize HTML"
303305
306+ , Option "" ["email-obfuscation"]
307+ (ReqArg
308+ (\arg opt -> do
309+ method <- case arg of
310+ "references" -> return ReferenceObfuscation
311+ "javascript" -> return JavascriptObfuscation
312+ "none" -> return NoObfuscation
313+ _ -> hPutStrLn stderr ("Error: Unknown obfuscation method: " ++ arg) >>
314+ exitWith (ExitFailure 6)
315+ return opt { optEmailObfuscation = method })
316+ "none|javascript|references")
317+ "" -- "Method for obfuscating email in HTML"
318+
304319 , Option "" ["toc", "table-of-contents"]
305320 (NoArg
306321 (\opt -> return opt { optTableOfContents = True }))
@@ -533,10 +548,11 @@ main = do
533548 , optWrapText = wrap
534549 , optSanitizeHTML = sanitize
535550 , optPlugins = plugins
551+ , optEmailObfuscation = obfuscationMethod
536552#ifdef _CITEPROC
537− , optBiblioFile = biblioFile
538− , optBiblioFormat = biblioFormat
539− , optCslFile = cslFile
553+ , optBiblioFile = biblioFile
554+ , optBiblioFormat = biblioFormat
555+ , optCslFile = cslFile
540556#endif
541557 } = opts
542558
@@ -614,25 +630,28 @@ main = do
614630 let header = (if (customHeader == "DEFAULT")
615631 then defaultHeader
616632 else customHeader) ++ csslink ++ includeHeader
617− let writerOptions = WriterOptions { writerStandalone = standalone',
618− writerHeader = header,
619− writerTitlePrefix = titlePrefix,
620− writerTabStop = tabStop,
621− writerTableOfContents = toc &&
622− (not strict) &&
623− writerName' /= "s5",
624− writerHTMLMathMethod = mathMethod,
625− writerS5 = (writerName' == "s5"),
626− writerIgnoreNotes = False,
627− writerIncremental = incremental,
628− writerNumberSections = numberSections,
629− writerIncludeBefore = includeBefore,
630− writerIncludeAfter = includeAfter,
631− writerStrictMarkdown = strict,
632− writerReferenceLinks = referenceLinks,
633− writerWrapText = wrap,
634− writerLiterateHaskell = "+lhs" `isSuffixOf` writerName' ||
635− lhsExtension [outputFile] }
633+ let writerOptions = WriterOptions { writerStandalone = standalone',
634+ writerHeader = header,
635+ writerTitlePrefix = titlePrefix,
636+ writerTabStop = tabStop,
637+ writerTableOfContents = toc &&
638+ (not strict) &&
639+ writerName' /= "s5",
640+ writerHTMLMathMethod = mathMethod,
641+ writerS5 = (writerName' == "s5"),
642+ writerIgnoreNotes = False,
643+ writerIncremental = incremental,
644+ writerNumberSections = numberSections,
645+ writerIncludeBefore = includeBefore,
646+ writerIncludeAfter = includeAfter,
647+ writerStrictMarkdown = strict,
648+ writerReferenceLinks = referenceLinks,
649+ writerWrapText = wrap,
650+ writerLiterateHaskell = "+lhs" `isSuffixOf` writerName' ||
651+ lhsExtension [outputFile],
652+ writerEmailObfuscation = if strict
653+ then ReferenceObfuscation
654+ else obfuscationMethod }
636655
637656 if isNonTextOutput writerName' && outputFile == "-"
638657 then do hPutStrLn stderr ("Error: Cannot write " ++ writerName ++ " output to stdout.\n" ++
README+8
@@ -383,6 +383,14 @@ For further documentation, see the `pandoc(1)` man page.
383383 are omitted. URIs in links and images are also checked against a
384384 whitelist of URI schemes.
385385
386+`--email-obfuscation`*=none|javascript|references*
387+: specifies a method for obfuscating `mailto:` links in HTML documents.
388+ *none* leaves `mailto:` links as they are. *javascript* obfuscates
389+ them using javascript. *references* obfuscates them by printing their
390+ letters as decimal or hexadecimal character references. If `--strict`
391+ is specified, *references* is used regardless of the presence
392+ of this option.
393+
386394`--dump-args`
387395: is intended to make it easier to create wrapper scripts that use
388396 Pandoc. It causes Pandoc to dump information about the arguments
Text/Pandoc/Shared.hs+41−32
@@ -99,6 +99,7 @@ module Text.Pandoc.Shared (
9999 isHeaderBlock,
100100 -- * Writer options
101101 HTMLMathMethod (..),
102+ ObfuscationMethod (..),
102103 WriterOptions (..),
103104 defaultWriterOptions,
104105 -- * File handling
@@ -889,45 +890,53 @@ data HTMLMathMethod = PlainMath
889890 | MimeTeX String -- url of mimetex.cgi
890891 deriving (Show, Read, Eq)
891892
893+-- | Methods for obfuscating email addresses in HTML.
894+data ObfuscationMethod = NoObfuscation
895+ | ReferenceObfuscation
896+ | JavascriptObfuscation
897+ deriving (Show, Read, Eq)
898+
892899-- | Options for writers
893900data WriterOptions = WriterOptions
894− { writerStandalone :: Bool -- ^ Include header and footer
895− , writerHeader :: String -- ^ Header for the document
896− , writerTitlePrefix :: String -- ^ Prefix for HTML titles
897− , writerTabStop :: Int -- ^ Tabstop for conversion btw spaces and tabs
898− , writerTableOfContents :: Bool -- ^ Include table of contents
899− , writerS5 :: Bool -- ^ We're writing S5
900− , writerHTMLMathMethod :: HTMLMathMethod -- ^ How to print math in HTML
901− , writerIgnoreNotes :: Bool -- ^ Ignore footnotes (used in making toc)
902− , writerIncremental :: Bool -- ^ Incremental S5 lists
903− , writerNumberSections :: Bool -- ^ Number sections in LaTeX
904− , writerIncludeBefore :: String -- ^ String to include before the body
905− , writerIncludeAfter :: String -- ^ String to include after the body
906− , writerStrictMarkdown :: Bool -- ^ Use strict markdown syntax
907− , writerReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
908− , writerWrapText :: Bool -- ^ Wrap text to line length
909− , writerLiterateHaskell :: Bool -- ^ Write as literate haskell
901+ { writerStandalone :: Bool -- ^ Include header and footer
902+ , writerHeader :: String -- ^ Header for the document
903+ , writerTitlePrefix :: String -- ^ Prefix for HTML titles
904+ , writerTabStop :: Int -- ^ Tabstop for conversion btw spaces and tabs
905+ , writerTableOfContents :: Bool -- ^ Include table of contents
906+ , writerS5 :: Bool -- ^ We're writing S5
907+ , writerHTMLMathMethod :: HTMLMathMethod -- ^ How to print math in HTML
908+ , writerIgnoreNotes :: Bool -- ^ Ignore footnotes (used in making toc)
909+ , writerIncremental :: Bool -- ^ Incremental S5 lists
910+ , writerNumberSections :: Bool -- ^ Number sections in LaTeX
911+ , writerIncludeBefore :: String -- ^ String to include before the body
912+ , writerIncludeAfter :: String -- ^ String to include after the body
913+ , writerStrictMarkdown :: Bool -- ^ Use strict markdown syntax
914+ , writerReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
915+ , writerWrapText :: Bool -- ^ Wrap text to line length
916+ , writerLiterateHaskell :: Bool -- ^ Write as literate haskell
917+ , writerEmailObfuscation :: ObfuscationMethod -- ^ How to obfuscate emails
910918 } deriving Show
911919
912920-- | Default writer options.
913921defaultWriterOptions :: WriterOptions
914922defaultWriterOptions =
915− WriterOptions { writerStandalone = False
916− , writerHeader = ""
917− , writerTitlePrefix = ""
918− , writerTabStop = 4
919− , writerTableOfContents = False
920− , writerS5 = False
921− , writerHTMLMathMethod = PlainMath
922− , writerIgnoreNotes = False
923− , writerIncremental = False
924− , writerNumberSections = False
925− , writerIncludeBefore = ""
926− , writerIncludeAfter = ""
927− , writerStrictMarkdown = False
928− , writerReferenceLinks = False
929− , writerWrapText = True
930− , writerLiterateHaskell = False
923+ WriterOptions { writerStandalone = False
924+ , writerHeader = ""
925+ , writerTitlePrefix = ""
926+ , writerTabStop = 4
927+ , writerTableOfContents = False
928+ , writerS5 = False
929+ , writerHTMLMathMethod = PlainMath
930+ , writerIgnoreNotes = False
931+ , writerIncremental = False
932+ , writerNumberSections = False
933+ , writerIncludeBefore = ""
934+ , writerIncludeAfter = ""
935+ , writerStrictMarkdown = False
936+ , writerReferenceLinks = False
937+ , writerWrapText = True
938+ , writerLiterateHaskell = False
939+ , writerEmailObfuscation = JavascriptObfuscation
931940 }
932941
933942--
Text/Pandoc/Writers/HTML.hs+11−5
@@ -193,10 +193,13 @@ parseMailto ('m':'a':'i':'l':'t':'o':':':addr) =
193193 in Just (name', domain)
194194parseMailto _ = Nothing
195195
196−-- | Obfuscate a "mailto:" link using Javascript.
196+-- | Obfuscate a "mailto:" link.
197197obfuscateLink :: WriterOptions -> String -> String -> Html
198+obfuscateLink opts txt s | writerEmailObfuscation opts == NoObfuscation =
199+ anchor ! [href s] << txt
198200obfuscateLink opts txt s =
199− let s' = map toLower s
201+ let meth = writerEmailObfuscation opts
202+ s' = map toLower s
200203 in case parseMailto s' of
201204 (Just (name', domain)) ->
202205 let domain' = substitute "." " dot " domain
@@ -206,17 +209,20 @@ obfuscateLink opts txt s =
206209 then ("'<code>'+e+'</code>'", name' ++ " at " ++ domain')
207210 else ("'" ++ txt ++ "'", txt ++ " (" ++ name' ++ " at " ++
208211 domain' ++ ")")
209− in if writerStrictMarkdown opts
210− then -- need to use primHtml or &'s are escaped to &amp; in URL
212+ in case meth of
213+ ReferenceObfuscation ->
214+ -- need to use primHtml or &'s are escaped to &amp; in URL
211215 primHtml $ "<a href=\"" ++ (obfuscateString s')
212216 ++ "\">" ++ (obfuscateString txt) ++ "</a>"
213− else (script ! [thetype "text/javascript"] $
217+ JavascriptObfuscation ->
218+ (script ! [thetype "text/javascript"] $
214219 primHtml ("\n<!--\nh='" ++
215220 obfuscateString domain ++ "';a='" ++ at' ++ "';n='" ++
216221 obfuscateString name' ++ "';e=n+a+h;\n" ++
217222 "document.write('<a h'+'ref'+'=\"ma'+'ilto'+':'+e+'\">'+" ++
218223 linkText ++ "+'<\\/'+'a'+'>');\n// -->\n")) +++
219224 noscript (primHtml $ obfuscateString altText)
225+ _ -> error $ "Unknown obfuscation method: " ++ show meth
220226 _ -> anchor ! [href s] $ primHtml txt -- malformed email
221227
222228-- | Obfuscate character as entity.
man/man1/pandoc.1.md+8
@@ -146,6 +146,14 @@ to Pandoc. Or use `html2markdown`(1), a wrapper around `pandoc`.
146146 are omitted. URIs in links and images are also checked against a
147147 whitelist of URI schemes.
148148
149+\--email-obfuscation=*none|javascript|references*
150+: Specify a method for obfuscating `mailto:` links in HTML documents.
151+ *none* leaves `mailto:` links as they are. *javascript* obfuscates
152+ them using javascript. *references* obfuscates them by printing their
153+ letters as decimal or hexadecimal character references.
154+ If `--strict` is specified, *references* is used regardless of the
155+ presence of this option.
156+
149157\--toc, \--table-of-contents
150158: Include an automatically generated table of contents (HTML, markdown,
151159 RTF) or an instruction to create one (LaTeX, reStructuredText).