git.iko.soy/typeable/pandoc
on master help sign in
RST writer: don't backslash-escape word-internal punctuation.

Closes #3978.

commitcba18c19a69d05ecd3e617bcbd74780482bffd7e authorJohn MacFarlane <jgm@berkeley.edu> dateMon 16 Oct 2017, 20:36 (9 years ago) parentd8804f4
3 files changed+25−16
src/Text/Pandoc/Writers/RST.hs+18−15 test/command/3978.md+6 test/writer.rst+1−1
src/Text/Pandoc/Writers/RST.hs+18−15
@@ -169,21 +169,24 @@ pictToRST (label, (attr, src, _, mbtarget)) = do
169169
170170-- | Escape special characters for RST.
171171escapeString :: WriterOptions -> String -> String
172−escapeString _ [] = []
173−escapeString opts (c:cs) =
174− case c of
175− _ | c `elem` ['\\','`','*','_','|'] -> '\\':c:escapeString opts cs
176− '\'' | isEnabled Ext_smart opts -> '\\':'\'':escapeString opts cs
177− '"' | isEnabled Ext_smart opts -> '\\':'"':escapeString opts cs
178− '-' | isEnabled Ext_smart opts ->
179− case cs of
180− '-':_ -> '\\':'-':escapeString opts cs
181− _ -> '-':escapeString opts cs
182− '.' | isEnabled Ext_smart opts ->
183− case cs of
184− '.':'.':rest -> '\\':'.':'.':'.':escapeString opts rest
185− _ -> '.':escapeString opts cs
186− _ -> c : escapeString opts cs
172+escapeString = escapeString' True
173+ where
174+ escapeString' _ _ [] = []
175+ escapeString' firstChar opts (c:cs) =
176+ case c of
177+ _ | c `elem` ['\\','`','*','_','|'] &&
178+ (firstChar || null cs) -> '\\':c:escapeString' False opts cs
179+ '\'' | isEnabled Ext_smart opts -> '\\':'\'':escapeString' False opts cs
180+ '"' | isEnabled Ext_smart opts -> '\\':'"':escapeString' False opts cs
181+ '-' | isEnabled Ext_smart opts ->
182+ case cs of
183+ '-':_ -> '\\':'-':escapeString' False opts cs
184+ _ -> '-':escapeString' False opts cs
185+ '.' | isEnabled Ext_smart opts ->
186+ case cs of
187+ '.':'.':rest -> '\\':'.':'.':'.':escapeString' False opts rest
188+ _ -> '.':escapeString' False opts cs
189+ _ -> c : escapeString' False opts cs
187190
188191titleToRST :: PandocMonad m => [Inline] -> [Inline] -> RST m Doc
189192titleToRST [] _ = return empty
test/command/3978.mdnew file+6
@@ -0,0 +1,6 @@
1+```
2+% pandoc -t rst
3+foo_bar*baz
4+^D
5+foo_bar*baz
6+```
test/writer.rst+1−1
@@ -772,7 +772,7 @@ Just a `URL </url/>`__.
772772
773773`URL and title </url/>`__
774774
775−`with\_underscore </url/with_underscore>`__
775+`with_underscore </url/with_underscore>`__
776776
777777`Email link <mailto:nobody@nowhere.net>`__
778778