git.iko.soy/typeable/pandoc
on master help sign in
Added writerTeXLigatures to WriterOptions, `--no-tex-ligatures` option.

This is useful for those who want to use advanced OpenType features with xelatex/lualatex.

commit5f889b93963dda851b3610dfd2ed74c17a3e4091 authorJohn MacFarlane <fiddlosopher@gmail.com> dateFri 11 May 2012, 22:58 (14 years ago) parent1aec24a
6 files changed+65−37
README+12−1 changelog+2 src/Text/Pandoc/Shared.hs+2 src/Text/Pandoc/Writers/ConTeXt.hs+14−10 src/Text/Pandoc/Writers/LaTeX.hs+23−24 src/pandoc.hs+12−2
README+12−1
@@ -213,7 +213,7 @@ Reader options
213213 abbreviations, such as "Mr." (Note: This option is significant only when
214214 the input format is `markdown` or `textile`. It is selected automatically
215215 when the input format is `textile` or the output format is `latex` or
216− `context`.)
216+ `context`, unless `--no-tex-ligatures` is used.)
217217
218218`--old-dashes`
219219: Selects the pandoc <= 1.8.2.1 behavior for parsing smart dashes: `-` before
@@ -358,6 +358,17 @@ Options affecting specific writers
358358: Number section headings in LaTeX, ConTeXt, or HTML output.
359359 By default, sections are not numbered.
360360
361+`--no-tex-ligatures`
362+: Do not convert quotation marks, apostrophes, and dashes to
363+ the TeX ligatures when writing LaTeX or ConTeXt. Instead, just
364+ use literal unicode characters. This is needed for using advanced
365+ OpenType features with XeLaTeX and LuaLaTeX. Note: normally
366+ `--smart` is selected automatically for LaTeX and ConTeXt
367+ output, but it must be specified explicitly if `--no-tex-ligatures`
368+ is selected. If you use literal curly quotes, dashes, and ellipses
369+ in your source, then you may want to use `--no-tex-ligatures`
370+ without `--smart`.
371+
361372`--listings`
362373: Use listings package for LaTeX code blocks
363374
changelog+2
@@ -43,6 +43,8 @@ pandoc (1.9.3)
4343
4444 * LaTeX writer:
4545
46+ + Added `--no-tex-ligatures` option to avoid replacing
47+ quotation marks and dashes with TeX ligatures.
4648 + Use `fixltx2e` package to provide '\textsubscript'.
4749 + Improve spacing around LaTeX block environments:
4850 quote, verbatim, itemize, description, enumerate.
src/Text/Pandoc/Shared.hs+2
@@ -520,6 +520,7 @@ data WriterOptions = WriterOptions
520520 , writerHighlight :: Bool -- ^ Highlight source code
521521 , writerHighlightStyle :: Style -- ^ Style to use for highlighting
522522 , writerSetextHeaders :: Bool -- ^ Use setext headers for levels 1-2 in markdown
523+ , writerTeXLigatures :: Bool -- ^ Use tex ligatures quotes, dashes in latex
523524 } deriving Show
524525
525526{-# DEPRECATED writerXeTeX "writerXeTeX no longer does anything" #-}
@@ -558,6 +559,7 @@ defaultWriterOptions =
558559 , writerHighlight = False
559560 , writerHighlightStyle = pygments
560561 , writerSetextHeaders = True
562+ , writerTeXLigatures = True
561563 }
562564
563565--
src/Text/Pandoc/Writers/ConTeXt.hs+14−10
@@ -86,8 +86,9 @@ pandocToConTeXt options (Pandoc (Meta title authors date) blocks) = do
8686
8787-- escape things as needed for ConTeXt
8888
89−escapeCharForConTeXt :: Char -> String
90−escapeCharForConTeXt ch =
89+escapeCharForConTeXt :: WriterOptions -> Char -> String
90+escapeCharForConTeXt opts ch =
91+ let ligatures = writerTeXLigatures opts in
9192 case ch of
9293 '{' -> "\\letteropenbrace{}"
9394 '}' -> "\\letterclosebrace{}"
@@ -105,15 +106,15 @@ escapeCharForConTeXt ch =
105106 ']' -> "{]}"
106107 '_' -> "\\letterunderscore{}"
107108 '\160' -> "~"
108− '\x2014' -> "---"
109− '\x2013' -> "--"
110− '\x2019' -> "'"
109+ '\x2014' | ligatures -> "---"
110+ '\x2013' | ligatures -> "--"
111+ '\x2019' | ligatures -> "'"
111112 '\x2026' -> "\\ldots{}"
112113 x -> [x]
113114
114115-- | Escape string for ConTeXt
115−stringToConTeXt :: String -> String
116−stringToConTeXt = concatMap escapeCharForConTeXt
116+stringToConTeXt :: WriterOptions -> String -> String
117+stringToConTeXt opts = concatMap (escapeCharForConTeXt opts)
117118
118119-- | Convert Elements to ConTeXt
119120elementToConTeXt :: WriterOptions -> Element -> State WriterState Doc
@@ -254,8 +255,9 @@ inlineToConTeXt (SmallCaps lst) = do
254255 return $ braces $ "\\sc " <> contents
255256inlineToConTeXt (Code _ str) | not ('{' `elem` str || '}' `elem` str) =
256257 return $ "\\type" <> braces (text str)
257−inlineToConTeXt (Code _ str) =
258− return $ "\\mono" <> braces (text $ stringToConTeXt str)
258+inlineToConTeXt (Code _ str) = do
259+ opts <- gets stOptions
260+ return $ "\\mono" <> braces (text $ stringToConTeXt opts str)
259261inlineToConTeXt (Quoted SingleQuote lst) = do
260262 contents <- inlineListToConTeXt lst
261263 return $ "\\quote" <> braces contents
@@ -263,7 +265,9 @@ inlineToConTeXt (Quoted DoubleQuote lst) = do
263265 contents <- inlineListToConTeXt lst
264266 return $ "\\quotation" <> braces contents
265267inlineToConTeXt (Cite _ lst) = inlineListToConTeXt lst
266−inlineToConTeXt (Str str) = return $ text $ stringToConTeXt str
268+inlineToConTeXt (Str str) = do
269+ opts <- gets stOptions
270+ return $ text $ stringToConTeXt opts str
267271inlineToConTeXt (Math InlineMath str) =
268272 return $ char '$' <> text str <> char '$'
269273inlineToConTeXt (Math DisplayMath str) =
src/Text/Pandoc/Writers/LaTeX.hs+23−24
@@ -174,9 +174,11 @@ elementToLaTeX opts (Sec level _ id' title' elements) = do
174174
175175-- escape things as needed for LaTeX
176176stringToLaTeX :: Bool -> String -> State WriterState String
177−stringToLaTeX _ [] = return ""
178−stringToLaTeX isUrl (x:xs) = do
177+stringToLaTeX _ [] = return ""
178+stringToLaTeX isUrl (x:xs) = do
179+ opts <- gets stOptions
179180 rest <- stringToLaTeX isUrl xs
181+ let ligatures = writerTeXLigatures opts
180182 when (x == '€') $
181183 modify $ \st -> st{ stUsesEuro = True }
182184 return $
@@ -201,13 +203,13 @@ stringToLaTeX isUrl (x:xs) = do
201203 '[' -> "{[}" ++ rest -- to avoid interpretation as
202204 ']' -> "{]}" ++ rest -- optional arguments
203205 '\160' -> "~" ++ rest
204− '\x2018' -> "`" ++ rest
205− '\x2019' -> "'" ++ rest
206− '\x201C' -> "``" ++ rest
207− '\x201D' -> "''" ++ rest
208206 '\x2026' -> "\\ldots{}" ++ rest
209− '\x2014' -> "---" ++ rest
210− '\x2013' -> "--" ++ rest
207+ '\x2018' | ligatures -> "`" ++ rest
208+ '\x2019' | ligatures -> "'" ++ rest
209+ '\x201C' | ligatures -> "``" ++ rest
210+ '\x201D' | ligatures -> "''" ++ rest
211+ '\x2014' | ligatures -> "---" ++ rest
212+ '\x2013' | ligatures -> "--" ++ rest
211213 _ -> x : rest
212214
213215-- | Puts contents into LaTeX command.
@@ -536,23 +538,11 @@ inlineToLaTeX (Code (_,classes,_) str) = do
536538 Just h -> modify (\st -> st{ stHighlighting = True }) >>
537539 return (text h)
538540 rawCode = liftM (text . (\s -> "\\texttt{" ++ s ++ "}"))
539− $ stringToLaTeX False str
540−inlineToLaTeX (Quoted SingleQuote lst) = do
541− contents <- inlineListToLaTeX lst
542− csquotes <- liftM stCsquotes get
543− if csquotes
544− then return $ "\\enquote" <> braces contents
545− else do
546− let s1 = if (not (null lst)) && (isQuoted (head lst))
547− then "\\,"
548− else empty
549− let s2 = if (not (null lst)) && (isQuoted (last lst))
550− then "\\,"
551− else empty
552− return $ char '`' <> s1 <> contents <> s2 <> char '\''
553−inlineToLaTeX (Quoted DoubleQuote lst) = do
541+ $ stringToLaTeX False str
542+inlineToLaTeX (Quoted qt lst) = do
554543 contents <- inlineListToLaTeX lst
555544 csquotes <- liftM stCsquotes get
545+ opts <- gets stOptions
556546 if csquotes
557547 then return $ "\\enquote" <> braces contents
558548 else do
@@ -562,7 +552,16 @@ inlineToLaTeX (Quoted DoubleQuote lst) = do
562552 let s2 = if (not (null lst)) && (isQuoted (last lst))
563553 then "\\,"
564554 else empty
565− return $ "``" <> s1 <> contents <> s2 <> "''"
555+ let inner = s1 <> contents <> s2
556+ return $ case qt of
557+ DoubleQuote ->
558+ if writerTeXLigatures opts
559+ then text "``" <> inner <> text "''"
560+ else char '\x201C' <> inner <> char '\x201D'
561+ SingleQuote ->
562+ if writerTeXLigatures opts
563+ then char '`' <> inner <> char '\''
564+ else char '\x2018' <> inner <> char '\x2019'
566565inlineToLaTeX (Str str) = liftM text $ stringToLaTeX False str
567566inlineToLaTeX (Math InlineMath str) = return $ char '$' <> text str <> char '$'
568567inlineToLaTeX (Math DisplayMath str) = return $ "\\[" <> text str <> "\\]"
src/pandoc.hs+12−2
@@ -135,6 +135,7 @@ data Opt = Opt
135135 , optSlideLevel :: Maybe Int -- ^ Header level that creates slides
136136 , optSetextHeaders :: Bool -- ^ Use atx headers for markdown level 1-2
137137 , optAscii :: Bool -- ^ Use ascii characters only in html
138+ , optTeXLigatures :: Bool -- ^ Use TeX ligatures for quotes/dashes
138139 }
139140
140141-- | Defaults for command-line options.
@@ -187,6 +188,7 @@ defaultOpts = Opt
187188 , optSlideLevel = Nothing
188189 , optSetextHeaders = True
189190 , optAscii = False
191+ , optTeXLigatures = True
190192 }
191193
192194-- | A list of functions, each transforming the options data structure
@@ -438,6 +440,11 @@ options =
438440 (\opt -> return opt { optNumberSections = True }))
439441 "" -- "Number sections in LaTeX"
440442
443+ , Option "" ["no-tex-ligatures"]
444+ (NoArg
445+ (\opt -> return opt { optTeXLigatures = False }))
446+ "" -- "Don't use tex ligatures for quotes, dashes"
447+
441448 , Option "" ["listings"]
442449 (NoArg
443450 (\opt -> return opt { optListings = True }))
@@ -804,6 +811,7 @@ main = do
804811 , optSlideLevel = slideLevel
805812 , optSetextHeaders = setextHeaders
806813 , optAscii = ascii
814+ , optTeXLigatures = texLigatures
807815 } = opts
808816
809817 when dumpArgs $
@@ -918,7 +926,8 @@ main = do
918926 lhsExtension sources,
919927 stateStandalone = standalone',
920928 stateCitations = map CSL.refId refs,
921− stateSmart = smart || laTeXOutput || writerName' == "context",
929+ stateSmart = smart || (texLigatures &&
930+ (laTeXOutput || writerName' == "context")),
922931 stateOldDashes = oldDashes,
923932 stateColumns = columns,
924933 stateStrict = strict,
@@ -961,7 +970,8 @@ main = do
961970 writerSlideLevel = slideLevel,
962971 writerHighlight = highlight,
963972 writerHighlightStyle = highlightStyle,
964− writerSetextHeaders = setextHeaders
973+ writerSetextHeaders = setextHeaders,
974+ writerTeXLigatures = texLigatures
965975 }
966976
967977 when (writerName' `elem` nonTextFormats&& outputFile == "-") $