From b185560a8ec1fd8a0b578de4d94ea72a3d3e2943 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sat, 4 Sep 2021 10:02:38 -0700
Subject: [PATCH] RTF reader: better handling of `\*` and bookmarks.

We now ensure that groups starting with `\*` never cause
text to be added to the document.

In addition, bookmarks now create a span between the start
and end of the bookmark, rather than an empty span.
---
 src/Text/Pandoc/Readers/RTF.hs | 16 ++++++++--------
 test/rtf/bookmark.native       |  2 +-
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/Text/Pandoc/Readers/RTF.hs b/src/Text/Pandoc/Readers/RTF.hs
index 26a132758..4eeab1312 100644
--- a/src/Text/Pandoc/Readers/RTF.hs
+++ b/src/Text/Pandoc/Readers/RTF.hs
@@ -395,15 +395,15 @@ isUnderline _ = False
 processTok :: PandocMonad m => Blocks -> Tok -> RTFParser m Blocks
 processTok bs (Tok pos tok') = do
   setPosition pos
-  -- ignore \* at beginning of group:
-  let tok'' = case tok' of
-                Grouped (Tok _ (ControlSymbol '*') : toks) -> Grouped toks
-                _ -> tok'
-  case tok'' of
+  case tok' of
     HexVal{} -> return ()
     UnformattedText{} -> return ()
     _ -> updateState $ \s -> s{ sEatChars = 0 }
-  case tok'' of
+  case tok' of
+    Grouped (Tok _ (ControlSymbol '*') : toks) ->
+      bs <$ (do oldTextContent <- sTextContent <$> getState
+                processTok mempty (Tok pos (Grouped toks))
+                updateState $ \st -> st{ sTextContent = oldTextContent })
     Grouped (Tok _ (ControlWord "fonttbl" _) : toks) -> inGroup $ do
       updateState $ \s -> s{ sFontTable = processFontTable toks }
       pure bs
@@ -440,10 +440,10 @@ processTok bs (Tok pos tok') = do
       -- TODO ideally we'd put the span around bkmkstart/end, but this
       -- is good for now:
       modifyGroup (\g -> g{ gAnchor = Just $ T.strip t })
-      addText ""
+      pure bs
+    Grouped (Tok _ (ControlWord "bkmkend" _) : _) -> do
       modifyGroup (\g -> g{ gAnchor = Nothing })
       pure bs
-    Grouped (Tok _ (ControlWord "bkmkend" _) : _) -> pure bs -- TODO
     Grouped (Tok _ (ControlWord f _) : _) | isHeaderFooter f -> pure bs
     Grouped (Tok _ (ControlWord "footnote" _) : toks) -> do
       noteBs <- inGroup $ processDestinationToks toks
diff --git a/test/rtf/bookmark.native b/test/rtf/bookmark.native
index bc5b67afc..f142f1c5c 100644
--- a/test/rtf/bookmark.native
+++ b/test/rtf/bookmark.native
@@ -1,3 +1,3 @@
 Pandoc (Meta {unMeta = fromList []})
-[Para [Span ("bookmark_1",[],[]) [],Str "Bookmark_1"]
+[Para [Span ("bookmark_1",[],[]) [Str "Bookmark_1"]]
 ,Para [Link ("",[],[]) [Str "click",Space,Str "me"] ("#bookmark_1","")]]