git.iko.soy/typeable/pandoc
on master help sign in
Make the `smart` extension affect the Markdown writer.
412ed3fJohn MacFarlane
MANUAL.txt+12−11
@@ -3383,17 +3383,18 @@ example, `markdown+hard_line_breaks` is Markdown with hard line breaks.
3383
3383
3384#### Extension: `smart` ####
3384#### Extension: `smart` ####
3385
3385
3386−Produce typographically correct output, converting straight
3387−quotes to curly quotes, `---` to em-dashes, `--` to en-dashes,
3386+Interpret straight quotes as curly quotes, `---` as em-dashes,
3388−and `...` to ellipses. Nonbreaking spaces are inserted after
3387+`--` as en-dashes, and `...` as ellipses. Nonbreaking spaces are
3389−certain abbreviations, such as "Mr." (Note: This option is
3388+inserted after certain abbreviations, such as "Mr."
3389+
3390+Notes:
3391+
3390−selected automatically when the output format is `latex` or
3392+ * This extension option is selected automatically when the
3393+ output format is `latex` or `context`, unless
3391−`context`, unless `--no-tex-ligatures` is used. It has no
3394+ `--no-tex-ligatures` is used. It has no effect for `latex` input.
3392−effect for `latex` input.)
3393−
3394−Note: if your LaTeX template or any included header file call
3395+ * If your LaTeX template or any included header file call
3395−for the [`csquotes`] package, pandoc will detect this
3396+ for the [`csquotes`] package, pandoc will detect this
3396−automatically and use `\enquote{...}` for quoted text.
3397+ automatically and use `\enquote{...}` for quoted text.
3397
3398
3398#### Extension: `old_dashes` ####
3399#### Extension: `old_dashes` ####
3399
3400
src/Text/Pandoc/Writers/Markdown.hs+29−6
@@ -280,7 +280,10 @@ escapeString opts = escapeStringUsing markdownEscapes
280 (if isEnabled Ext_tex_math_dollars opts
280 (if isEnabled Ext_tex_math_dollars opts
281 then ('$':)
281 then ('$':)
282 else id) $
282 else id) $
283− "\\`*_[]#"
283+ "\\`*_[]#" ++
284+ if isEnabled Ext_smart opts
285+ then "\"'"
286+ else ""
284
287
285-- | Construct table of contents from list of header blocks.
288-- | Construct table of contents from list of header blocks.
286tableOfContents :: PandocMonad m => WriterOptions -> [Block] -> m Doc
289tableOfContents :: PandocMonad m => WriterOptions -> [Block] -> m Doc
@@ -949,10 +952,14 @@ inlineToMarkdown opts (SmallCaps lst) = do
949 else inlineListToMarkdown opts $ capitalize lst
952 else inlineListToMarkdown opts $ capitalize lst
950inlineToMarkdown opts (Quoted SingleQuote lst) = do
953inlineToMarkdown opts (Quoted SingleQuote lst) = do
951 contents <- inlineListToMarkdown opts lst
954 contents <- inlineListToMarkdown opts lst
955+ return $ if isEnabled Ext_smart opts
956+ then "'" <> contents <> "'"
952− return $ "‘" <> contents <> "’"
957+ else "‘" <> contents <> "’"
953inlineToMarkdown opts (Quoted DoubleQuote lst) = do
958inlineToMarkdown opts (Quoted DoubleQuote lst) = do
954 contents <- inlineListToMarkdown opts lst
959 contents <- inlineListToMarkdown opts lst
960+ return $ if isEnabled Ext_smart opts
961+ then "\"" <> contents <> "\""
955− return $ "“" <> contents <> "”"
962+ else "“" <> contents <> "”"
956inlineToMarkdown opts (Code attr str) = do
963inlineToMarkdown opts (Code attr str) = do
957 let tickGroups = filter (\s -> '`' `elem` s) $ group str
964 let tickGroups = filter (\s -> '`' `elem` s) $ group str
958 let longest = if null tickGroups
965 let longest = if null tickGroups
@@ -969,9 +976,13 @@ inlineToMarkdown opts (Code attr str) = do
969 else return $ text (marker ++ spacer ++ str ++ spacer ++ marker) <> attrs
976 else return $ text (marker ++ spacer ++ str ++ spacer ++ marker) <> attrs
970inlineToMarkdown opts (Str str) = do
977inlineToMarkdown opts (Str str) = do
971 isPlain <- asks envPlain
978 isPlain <- asks envPlain
979+ let str' = (if isEnabled Ext_smart opts
980+ then unsmartify opts
981+ else id) $
972− if isPlain
982+ if isPlain
973− then return $ text str
983+ then str
974− else return $ text $ escapeString opts str
984+ else escapeString opts str
985+ return $ text str'
975inlineToMarkdown opts (Math InlineMath str) =
986inlineToMarkdown opts (Math InlineMath str) =
976 case writerHTMLMathMethod opts of
987 case writerHTMLMathMethod opts of
977 WebTeX url ->
988 WebTeX url ->
@@ -1126,3 +1137,15 @@ makeMathPlainer = walk go
1126 go (Emph xs) = Span nullAttr xs
1137 go (Emph xs) = Span nullAttr xs
1127 go x = x
1138 go x = x
1128
1139
1140+unsmartify :: WriterOptions -> String -> String
1141+unsmartify opts ('\8217':xs) = '\'' : unsmartify opts xs
1142+unsmartify opts ('\8230':xs) = "..." ++ unsmartify opts xs
1143+unsmartify opts ('\8211':xs)
1144+ | isEnabled Ext_old_dashes opts = '-' : unsmartify opts xs
1145+ | otherwise = "--" ++ unsmartify opts xs
1146+unsmartify opts ('\8212':xs)
1147+ | isEnabled Ext_old_dashes opts = "--" ++ unsmartify opts xs
1148+ | otherwise = "---" ++ unsmartify opts xs
1149+unsmartify opts (x:xs) = x : unsmartify opts xs
1150+unsmartify _ [] = []
1151+