diff options
author | John MacFarlane <jgm@berkeley.edu> | 2017-01-15 11:56:26 +0100 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-01-25 17:07:42 +0100 |
commit | 412ed3f1321a49d3c3b2119ebd28705376bbd551 (patch) | |
tree | 99d0c1e5bf82bbddafdd5e3763b12a203e9f2510 | |
parent | 5bf912577092fd1fd8874ccc89370396f22b5388 (diff) | |
download | pandoc-412ed3f1321a49d3c3b2119ebd28705376bbd551.tar.gz |
Make the `smart` extension affect the Markdown writer.
Thus, to "unsmartify" something that has been parsed as
smart by pandoc, you can use `-t markdown+smart`, and
straight quotes will be produced instead of curly quotes,
etc.
Example:
% pandoc -f latex -t markdown+smart
``hi''---ok
^D
"hi"---ok
-rw-r--r-- | MANUAL.txt | 23 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers/Markdown.hs | 35 |
2 files changed, 41 insertions, 17 deletions
diff --git a/MANUAL.txt b/MANUAL.txt index 236deeed4..ec3499513 100644 --- a/MANUAL.txt +++ b/MANUAL.txt @@ -3383,17 +3383,18 @@ example, `markdown+hard_line_breaks` is Markdown with hard line breaks. #### Extension: `smart` #### -Produce typographically correct output, converting straight -quotes to curly quotes, `---` to em-dashes, `--` to en-dashes, -and `...` to ellipses. Nonbreaking spaces are inserted after -certain abbreviations, such as "Mr." (Note: This option is -selected automatically when the output format is `latex` or -`context`, unless `--no-tex-ligatures` is used. It has no -effect for `latex` input.) - -Note: if your LaTeX template or any included header file call -for the [`csquotes`] package, pandoc will detect this -automatically and use `\enquote{...}` for quoted text. +Interpret straight quotes as curly quotes, `---` as em-dashes, +`--` as en-dashes, and `...` as ellipses. Nonbreaking spaces are +inserted after certain abbreviations, such as "Mr." + +Notes: + + * This extension option is selected automatically when the + output format is `latex` or `context`, unless + `--no-tex-ligatures` is used. It has no effect for `latex` input. + * If your LaTeX template or any included header file call + for the [`csquotes`] package, pandoc will detect this + automatically and use `\enquote{...}` for quoted text. #### Extension: `old_dashes` #### diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index 6a5a1130e..9ef968fc6 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -280,7 +280,10 @@ escapeString opts = escapeStringUsing markdownEscapes (if isEnabled Ext_tex_math_dollars opts then ('$':) else id) $ - "\\`*_[]#" + "\\`*_[]#" ++ + if isEnabled Ext_smart opts + then "\"'" + else "" -- | Construct table of contents from list of header blocks. tableOfContents :: PandocMonad m => WriterOptions -> [Block] -> m Doc @@ -949,10 +952,14 @@ inlineToMarkdown opts (SmallCaps lst) = do else inlineListToMarkdown opts $ capitalize lst inlineToMarkdown opts (Quoted SingleQuote lst) = do contents <- inlineListToMarkdown opts lst - return $ "‘" <> contents <> "’" + return $ if isEnabled Ext_smart opts + then "'" <> contents <> "'" + else "‘" <> contents <> "’" inlineToMarkdown opts (Quoted DoubleQuote lst) = do contents <- inlineListToMarkdown opts lst - return $ "“" <> contents <> "”" + return $ if isEnabled Ext_smart opts + then "\"" <> contents <> "\"" + else "“" <> contents <> "”" inlineToMarkdown opts (Code attr str) = do let tickGroups = filter (\s -> '`' `elem` s) $ group str let longest = if null tickGroups @@ -969,9 +976,13 @@ inlineToMarkdown opts (Code attr str) = do else return $ text (marker ++ spacer ++ str ++ spacer ++ marker) <> attrs inlineToMarkdown opts (Str str) = do isPlain <- asks envPlain - if isPlain - then return $ text str - else return $ text $ escapeString opts str + let str' = (if isEnabled Ext_smart opts + then unsmartify opts + else id) $ + if isPlain + then str + else escapeString opts str + return $ text str' inlineToMarkdown opts (Math InlineMath str) = case writerHTMLMathMethod opts of WebTeX url -> @@ -1126,3 +1137,15 @@ makeMathPlainer = walk go go (Emph xs) = Span nullAttr xs go x = x +unsmartify :: WriterOptions -> String -> String +unsmartify opts ('\8217':xs) = '\'' : unsmartify opts xs +unsmartify opts ('\8230':xs) = "..." ++ unsmartify opts xs +unsmartify opts ('\8211':xs) + | isEnabled Ext_old_dashes opts = '-' : unsmartify opts xs + | otherwise = "--" ++ unsmartify opts xs +unsmartify opts ('\8212':xs) + | isEnabled Ext_old_dashes opts = "--" ++ unsmartify opts xs + | otherwise = "---" ++ unsmartify opts xs +unsmartify opts (x:xs) = x : unsmartify opts xs +unsmartify _ [] = [] + |