summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2022-02-19 11:45:35 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2022-02-19 11:55:46 -0800
commit6fe8014a2cc1d732dd11c0fc386d7c8449048c90 (patch)
treed7a83366ca74976d2f4690f570298339fcf2024d
parenta3117bc1420d8f3f8db39957c62bc3af8a3aef1f (diff)
LaTeX reader: Handle `\label` and `\ref` for footnotes.
Closes #7930.
-rw-r--r--src/Text/Pandoc/Readers/LaTeX.hs18
-rw-r--r--src/Text/Pandoc/Readers/LaTeX/Parsing.hs2
-rw-r--r--test/command/7930.md98
3 files changed, 117 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index e2b283cf2..32fae9021 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -383,7 +383,7 @@ inlineCommands = M.unions
, ("MakeTextLowercase", makeLowercase <$> tok)
, ("lowercase", makeLowercase <$> tok)
, ("thanks", skipopts >> note <$> grouped block)
- , ("footnote", skipopts >> note <$> grouped block)
+ , ("footnote", skipopts >> footnote)
, ("passthrough", tok) -- \passthrough macro used by latex writer
-- for listings
, ("includegraphics", do options <- option [] keyvals
@@ -431,6 +431,22 @@ today =
text . T.pack . showGregorian . localDay . zonedTimeToLocalTime
<$> getZonedTime
+footnote :: PandocMonad m => LP m Inlines
+footnote = do
+ updateState $ \st -> st{ sLastNoteNum = sLastNoteNum st + 1 }
+ contents <- grouped block >>= walkM resolveNoteLabel
+ return $ note contents
+
+resolveNoteLabel :: PandocMonad m => Inline -> LP m Inline
+resolveNoteLabel (Span (_,cls,kvs) _)
+ | Just lab <- lookup "label" kvs = do
+ updateState $ \st -> st{
+ sLabels = M.insert lab (toList . text . tshow $ sLastNoteNum st)
+ $ sLabels st }
+ return $ Span (lab,cls,kvs) []
+resolveNoteLabel il = return il
+
+
lettrine :: PandocMonad m => LP m Inlines
lettrine = do
optional rawopt
diff --git a/src/Text/Pandoc/Readers/LaTeX/Parsing.hs b/src/Text/Pandoc/Readers/LaTeX/Parsing.hs
index 4807c817f..431da1a1f 100644
--- a/src/Text/Pandoc/Readers/LaTeX/Parsing.hs
+++ b/src/Text/Pandoc/Readers/LaTeX/Parsing.hs
@@ -162,6 +162,7 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
, sLastHeaderNum :: DottedNum
, sLastFigureNum :: DottedNum
, sLastTableNum :: DottedNum
+ , sLastNoteNum :: Int
, sTheoremMap :: M.Map Text TheoremSpec
, sLastTheoremStyle :: TheoremStyle
, sLastLabel :: Maybe Text
@@ -189,6 +190,7 @@ defaultLaTeXState = LaTeXState{ sOptions = def
, sLastHeaderNum = DottedNum []
, sLastFigureNum = DottedNum []
, sLastTableNum = DottedNum []
+ , sLastNoteNum = 0
, sTheoremMap = M.empty
, sLastTheoremStyle = PlainStyle
, sLastLabel = Nothing
diff --git a/test/command/7930.md b/test/command/7930.md
new file mode 100644
index 000000000..5e3625419
--- /dev/null
+++ b/test/command/7930.md
@@ -0,0 +1,98 @@
+```
+% pandoc -f latex -t native
+We discuss foobar in notes \ref{note:X} and \ref{note:Y}.
+
+Foo.\footnote{\label{note:X}A note. See also note~\ref{note:Y}.}
+
+Bar.\footnote{\label{note:Y}Another note. See also
+note~\ref{note:X}}
+^D
+[ Para
+ [ Str "We"
+ , Space
+ , Str "discuss"
+ , Space
+ , Str "foobar"
+ , Space
+ , Str "in"
+ , Space
+ , Str "notes"
+ , Space
+ , Link
+ ( ""
+ , []
+ , [ ( "reference-type" , "ref" )
+ , ( "reference" , "note:X" )
+ ]
+ )
+ [ Str "1" ]
+ ( "#note:X" , "" )
+ , Space
+ , Str "and"
+ , Space
+ , Link
+ ( ""
+ , []
+ , [ ( "reference-type" , "ref" )
+ , ( "reference" , "note:Y" )
+ ]
+ )
+ [ Str "2" ]
+ ( "#note:Y" , "" )
+ , Str "."
+ ]
+, Para
+ [ Str "Foo."
+ , Note
+ [ Para
+ [ Span ( "note:X" , [] , [ ( "label" , "note:X" ) ] ) []
+ , Str "A"
+ , Space
+ , Str "note."
+ , Space
+ , Str "See"
+ , Space
+ , Str "also"
+ , Space
+ , Str "note\160"
+ , Link
+ ( ""
+ , []
+ , [ ( "reference-type" , "ref" )
+ , ( "reference" , "note:Y" )
+ ]
+ )
+ [ Str "2" ]
+ ( "#note:Y" , "" )
+ , Str "."
+ ]
+ ]
+ ]
+, Para
+ [ Str "Bar."
+ , Note
+ [ Para
+ [ Span ( "note:Y" , [] , [ ( "label" , "note:Y" ) ] ) []
+ , Str "Another"
+ , Space
+ , Str "note."
+ , Space
+ , Str "See"
+ , Space
+ , Str "also"
+ , SoftBreak
+ , Str "note\160"
+ , Link
+ ( ""
+ , []
+ , [ ( "reference-type" , "ref" )
+ , ( "reference" , "note:X" )
+ ]
+ )
+ [ Str "1" ]
+ ( "#note:X" , "" )
+ ]
+ ]
+ ]
+]
+```