From 013fd1c6b68f2c061202d931f541aa4877ae543f Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Wed, 12 Jul 2017 13:58:47 +0200
Subject: [PATCH] Make sure \write18 is parsed as raw LaTeX.

The change is in the LaTeX reader's treatment of raw commands,
but it also affects the Markdown reader.
---
 src/Text/Pandoc/Readers/LaTeX.hs | 16 +++++++++-------
 test/command/3494.md             |  3 ++-
 test/command/3577.md             |  2 ++
 test/command/write18.md          | 14 ++++++++++++++
 test/latex-reader.native         |  2 +-
 5 files changed, 28 insertions(+), 9 deletions(-)
 create mode 100644 test/command/write18.md

diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index fde177f14..cd2c7c7f8 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -1049,14 +1049,12 @@ inlineCommand' :: PandocMonad m => LP m Inlines
 inlineCommand' = try $ do
   Tok _ (CtrlSeq name) cmd <- anyControlSeq
   guard $ name /= "begin" && name /= "end"
-  (star, rawstar) <- withRaw $ option "" ("*" <$ symbol '*' <* optional sp)
+  star <- option "" ("*" <$ symbol '*' <* optional sp)
   let name' = name <> star
   let names = ordNub [name', name] -- check non-starred as fallback
   let raw = do
        guard $ isInlineCommand name || not (isBlockCommand name)
-       (_, rawargs) <- withRaw
-               (skipangles *> skipopts *> option "" dimenarg *> many braced)
-       let rawcommand = T.unpack $ cmd <> untokenize (rawstar ++ rawargs)
+       rawcommand <- getRawCommand (cmd <> star)
        (guardEnabled Ext_raw_tex >> return (rawInline "latex" rawcommand))
          <|> ignore rawcommand
   lookupListDefault raw names inlineCommands
@@ -1353,7 +1351,11 @@ rawInlineOr name' fallback = do
 getRawCommand :: PandocMonad m => Text -> LP m String
 getRawCommand txt = do
   (_, rawargs) <- withRaw
-     (many (try (optional sp *> opt)) *>
+     ((if txt == "\\write"
+          then () <$ satisfyTok isWordTok -- digits
+          else return ()) *>
+      skipangles *>
+      skipopts *>
       option "" (try (optional sp *> dimenarg)) *>
       many braced)
   return $ T.unpack (txt <> untokenize rawargs)
@@ -1631,7 +1633,7 @@ blockCommand = try $ do
   let names = ordNub [name', name]
   let raw = do
         guard $ isBlockCommand name || not (isInlineCommand name)
-        rawBlock "latex" <$> getRawCommand txt
+        rawBlock "latex" <$> getRawCommand (txt <> star)
   lookupListDefault raw names blockCommands
 
 closing :: PandocMonad m => LP m Blocks
@@ -2128,8 +2130,8 @@ block = (mempty <$ spaces1)
     <|> environment
     <|> include
     <|> macroDef
-    <|> paragraph
     <|> blockCommand
+    <|> paragraph
     <|> grouped block
 
 blocks :: PandocMonad m => LP m Blocks
diff --git a/test/command/3494.md b/test/command/3494.md
index 7c480fde6..534041246 100644
--- a/test/command/3494.md
+++ b/test/command/3494.md
@@ -25,7 +25,8 @@
 <td style="text-align: left;">thank you</td>
 </tr>
 <tr class="odd">
-<td style="text-align: right;"><em>blah</em></td>
+<td style="text-align: right;">
+<p><em>blah</em></p></td>
 <td style="text-align: left;"><em>blah</em></td>
 <td style="text-align: left;"><em>blah</em></td>
 </tr>
diff --git a/test/command/3577.md b/test/command/3577.md
index dc88937e9..ca9dba97c 100644
--- a/test/command/3577.md
+++ b/test/command/3577.md
@@ -15,9 +15,11 @@
     \caption{Subfigure with Subfloat}
 \end{figure}
 ^D
+
 <figure>
 <img src="img1.jpg" alt="Caption 1" /><figcaption>Caption 1</figcaption>
 </figure>
+
 <figure>
 <img src="img2.jpg" alt="Caption 2" /><figcaption>Caption 2</figcaption>
 </figure>
diff --git a/test/command/write18.md b/test/command/write18.md
new file mode 100644
index 000000000..344dfc8cf
--- /dev/null
+++ b/test/command/write18.md
@@ -0,0 +1,14 @@
+Handle \write18{..} as raw tex:
+```
+% pandoc -t native
+\write18{git --version}
+^D
+[RawBlock (Format "latex") "\\write18{git --version}"]
+```
+
+```
+% pandoc -f latex+raw_tex -t native
+\write18{git --version}
+^D
+[RawBlock (Format "latex") "\\write18{git --version}"]
+```
diff --git a/test/latex-reader.native b/test/latex-reader.native
index 04be2538e..a62f2069e 100644
--- a/test/latex-reader.native
+++ b/test/latex-reader.native
@@ -261,7 +261,7 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "John",Spa
 ,Header 1 ("latex",[],[]) [Str "LaTeX"]
 ,BulletList
  [[Para [Cite [Citation {citationId = "smith.1899", citationPrefix = [], citationSuffix = [Str "22-23"], citationMode = NormalCitation, citationNoteNum = 0, citationHash = 0}] [RawInline (Format "latex") "\\cite[22-23]{smith.1899}"]]]
- ,[Para [RawInline (Format "latex") "\\doublespacing"]]
+ ,[RawBlock (Format "latex") "\\doublespacing"]
  ,[Para [Math InlineMath "2+2=4"]]
  ,[Para [Math InlineMath "x \\in y"]]
  ,[Para [Math InlineMath "\\alpha \\wedge \\omega"]]