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

Currently supported only in HTML writer.

commit8f408e4b7b24bfcf0f6a48522a2d77b0b878d34d authorJohn MacFarlane <jgm@berkeley.edu> dateSun 6 Feb 2011, 09:27 (16 years ago) parent9b38b69
4 files changed+38−19
README+5 src/Text/Pandoc/Shared.hs+2 src/Text/Pandoc/Writers/HTML.hs+20−17 src/pandoc.hs+11−2
README+5
@@ -292,6 +292,11 @@ Options
292292`--columns`=*NUMBER*
293293: Specify length of lines in characters (for text wrapping).
294294
295+`--ascii`
296+: Use only ascii characters in output. Currently supported only
297+ for HTML output (which uses numerical entities instead of
298+ UTF-8 when this option is selected).
299+
295300`--email-obfuscation=`*none|javascript|references*
296301: Specify a method for obfuscating `mailto:` links in HTML documents.
297302 *none* leaves `mailto:` links as they are. *javascript* obfuscates
src/Text/Pandoc/Shared.hs+2
@@ -490,6 +490,7 @@ data WriterOptions = WriterOptions
490490 , writerHtml5 :: Bool -- ^ Produce HTML5
491491 , writerChapters :: Bool -- ^ Use "chapter" for top-level sects
492492 , writerListings :: Bool -- ^ Use listings package for code
493+ , writerAscii :: Bool -- ^ Avoid non-ascii characters
493494 } deriving Show
494495
495496-- | Default writer options.
@@ -522,6 +523,7 @@ defaultWriterOptions =
522523 , writerHtml5 = False
523524 , writerChapters = False
524525 , writerListings = False
526+ , writerAscii = False
525527 }
526528
527529--
src/Text/Pandoc/Writers/HTML.hs+20−17
@@ -43,6 +43,7 @@ import Data.List ( isPrefixOf, intersperse )
4343import Data.Maybe ( catMaybes )
4444import Control.Monad.State
4545import Text.XHtml.Transitional hiding ( stringToHtml, unordList, ordList )
46+import qualified Text.XHtml.Transitional as XHtml
4647import Text.TeXMath
4748import Text.XML.Light.Output
4849
@@ -60,8 +61,10 @@ defaultWriterState = WriterState {stNotes= [], stMath = False, stHighlighting =
6061
6162-- | Modified version of Text.XHtml's stringToHtml.
6263-- Use unicode characters wherever possible.
63−stringToHtml :: String -> Html
64−stringToHtml = primHtml . escapeStringForXML
64+stringToHtml :: WriterOptions -> String -> Html
65+stringToHtml opts = if writerAscii opts
66+ then XHtml.stringToHtml
67+ else primHtml . escapeStringForXML
6568
6669-- | Hard linebreak.
6770nl :: WriterOptions -> Html
@@ -219,7 +222,7 @@ elementToListItem _ (Blk _) = return Nothing
219222elementToListItem opts (Sec _ num id' headerText subsecs) = do
220223 let sectnum = if writerNumberSections opts
221224 then (thespan ! [theclass "toc-section-number"] << showSecNum num) +++
222− stringToHtml " "
225+ stringToHtml opts" "
223226 else noHtml
224227 txt <- liftM (sectnum +++) $ inlineListToHtml opts headerText
225228 subHeads <- mapM (elementToListItem opts) subsecs >>= return . catMaybes
@@ -301,7 +304,7 @@ obfuscateLink opts txt s =
301304 linkText ++ "+'<\\/'+'a'+'>');\n// -->\n")) +++
302305 noscript (primHtml $ obfuscateString altText)
303306 _ -> error $ "Unknown obfuscation method: " ++ show meth
304− _ -> anchor ! [href s] $ stringToHtml txt -- malformed email
307+ _ -> anchor ! [href s] $ stringToHtml opts txt -- malformed email
305308
306309-- | Obfuscate character as entity.
307310obfuscateChar :: Char -> String
@@ -353,7 +356,7 @@ blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
353356 else unlines . lines
354357 in return $ pre ! attrs $ thecode <<
355358 (replicate (length leadingBreaks) br +++
356− [stringToHtml $ addBird rawCode'])
359+ [stringToHtml opts $ addBird rawCode'])
357360 Right h -> modify (\st -> st{ stHighlighting = True }) >>
358361 return h
359362blockToHtml opts (BlockQuote blocks) =
@@ -379,7 +382,7 @@ blockToHtml opts (Header level lst) = do
379382 secnum <- liftM stSecNum get
380383 let contents' = if writerNumberSections opts
381384 then (thespan ! [theclass "header-section-number"] << showSecNum secnum) +++
382− stringToHtml " " +++ contents
385+ stringToHtml opts " " +++ contents
383386 else contents
384387 let contents'' = if writerTableOfContents opts
385388 then anchor ! [href $ "#" ++ writerIdentifierPrefix opts ++ "TOC"] $ contents'
@@ -512,19 +515,19 @@ inlineListToHtml opts lst =
512515inlineToHtml :: WriterOptions -> Inline -> State WriterState Html
513516inlineToHtml opts inline =
514517 case inline of
515− (Str str) -> return $ stringToHtml str
516− (Space) -> return $ stringToHtml " "
518+ (Str str) -> return $ stringToHtml opts str
519+ (Space) -> return $ stringToHtml opts " "
517520 (LineBreak) -> return br
518− (EmDash) -> return $ stringToHtml "—"
519− (EnDash) -> return $ stringToHtml "–"
520− (Ellipses) -> return $ stringToHtml "…"
521− (Apostrophe) -> return $ stringToHtml "’"
521+ (EmDash) -> return $ stringToHtml opts "—"
522+ (EnDash) -> return $ stringToHtml opts "–"
523+ (Ellipses) -> return $ stringToHtml opts "…"
524+ (Apostrophe) -> return $ stringToHtml opts "’"
522525 (Emph lst) -> inlineListToHtml opts lst >>= return . emphasize
523526 (Strong lst) -> inlineListToHtml opts lst >>= return . strong
524527 (Code attr str) -> case highlightHtml True attr str of
525528 Left _ -> return
526529 $ thecode ! (attrsToHtml opts attr)
527− $ stringToHtml str
530+ $ stringToHtml opts str
528531 Right h -> return h
529532 (Strikeout lst) -> inlineListToHtml opts lst >>=
530533 return . (thespan ! [thestyle "text-decoration: line-through;"])
@@ -534,10 +537,10 @@ inlineToHtml opts inline =
534537 (Subscript lst) -> inlineListToHtml opts lst >>= return . sub
535538 (Quoted quoteType lst) ->
536539 let (leftQuote, rightQuote) = case quoteType of
537− SingleQuote -> (stringToHtml "‘",
538− stringToHtml "’")
539− DoubleQuote -> (stringToHtml "“",
540− stringToHtml "”")
540+ SingleQuote -> (stringToHtml opts "‘",
541+ stringToHtml opts "’")
542+ DoubleQuote -> (stringToHtml opts "“",
543+ stringToHtml opts "”")
541544 in do contents <- inlineListToHtml opts lst
542545 return $ leftQuote +++ contents +++ rightQuote
543546 (Math t str) -> modify (\st -> st {stMath = True}) >>
src/pandoc.hs+11−2
@@ -123,6 +123,7 @@ data Opt = Opt
123123 , optBibliography :: [String]
124124 , optCslFile :: FilePath
125125 , optListings :: Bool -- ^ Use listings package for code blocks
126+ , optAscii :: Bool -- ^ Avoid using nonascii characters
126127 }
127128
128129-- | Defaults for command-line options.
@@ -166,6 +167,7 @@ defaultOpts = Opt
166167 , optBibliography = []
167168 , optCslFile = ""
168169 , optListings = False
170+ , optAscii = False
169171 }
170172
171173-- | A list of functions, each transforming the options data structure
@@ -347,6 +349,11 @@ options =
347349 "NUMBER")
348350 "" -- "Length of line in characters"
349351
352+ , Option "" ["ascii"]
353+ (NoArg
354+ (\opt -> return opt { optAscii = True }))
355+ "" -- "Avoid using non-ascii characters in output"
356+
350357 , Option "" ["email-obfuscation"]
351358 (ReqArg
352359 (\arg opt -> do
@@ -681,7 +688,8 @@ main = do
681688 , optBibliography = reffiles
682689 , optCslFile = cslfile
683690 , optCiteMethod = citeMethod
684− , optListings = listings
691+ , optListings = listings
692+ , optAscii = ascii
685693 } = opts
686694
687695 when dumpArgs $
@@ -803,7 +811,8 @@ main = do
803811 writerHtml5 = html5 &&
804812 "html" `isPrefixOf` writerName',
805813 writerChapters = chapters,
806− writerListings = listings }
814+ writerListings = listings,
815+ writerAscii = ascii }
807816
808817 when (isNonTextOutput writerName' && outputFile == "-") $
809818 do UTF8.hPutStrLn stderr ("Error: Cannot write " ++ writerName ++ " output to stdout.\n" ++