summaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2023-12-10 11:50:25 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2023-12-10 11:50:25 -0800
commitb0e8e99c831c54e1f46d3f5c78ebcc547279e15d (patch)
tree10a69c96604c02a309d099cfc2046a46f1d0550b /src/Text
parent8c90b46d8b5a9dfbbff4da8c2999c1b4b5ca4b51 (diff)
Typst reader: add state fields for metadata.
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Readers/Typst.hs12
-rw-r--r--src/Text/Pandoc/Readers/Typst/Parsing.hs22
2 files changed, 28 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Readers/Typst.hs b/src/Text/Pandoc/Readers/Typst.hs
index 5e20fe9c6..515e33aa6 100644
--- a/src/Text/Pandoc/Readers/Typst.hs
+++ b/src/Text/Pandoc/Readers/Typst.hs
@@ -46,7 +46,8 @@ import Text.Parsec
import Text.TeXMath (writeTeX)
import Text.TeXMath.Shared (getSpaceChars)
import Text.Pandoc.Readers.Typst.Math (pMathMany)
-import Text.Pandoc.Readers.Typst.Parsing (pTok, ignored, chunks, getField, P)
+import Text.Pandoc.Readers.Typst.Parsing (pTok, ignored, chunks, getField, P,
+ PState(..), defaultPState)
import Typst.Methods (formatNumber, applyPureFunction)
import Typst.Types
@@ -73,8 +74,9 @@ readTypst _opts inp = do
Left e -> throwError $ PandocParseError $ tshow e
Right cs -> do
let labs = findLabels cs
- runParserT pPandoc labs inputName (F.toList cs) >>=
- either (throwError . PandocParseError . T.pack . show) pure
+ runParserT pPandoc defaultPState{ sLabels = labs }
+ inputName (F.toList cs) >>=
+ either (throwError . PandocParseError . T.pack . show) pure
pBlockElt :: PandocMonad m => P m B.Blocks
pBlockElt = try $ do
@@ -102,7 +104,7 @@ pInline = try $ do
, tname /= "math.equation" ->
B.math . writeTeX <$> pMathMany (Seq.singleton res)
Elt name@(Identifier tname) pos fields -> do
- labs <- getState
+ labs <- sLabels <$> getState
labelTarget <- (do VLabel t <- getField "target" fields
True <$ guard (t `elem` labs))
<|> pure False
@@ -521,7 +523,7 @@ inlineHandlers = M.fromList
alignment <- getField "alignment" fields
B.spanWith ("", [], [("align", repr alignment)])
<$> (getField "body" fields >>= pWithContents pInlines))
- ,("sys.version", \_ fields -> pure $ B.text "typst-hs")
+ ,("sys.version", \_ _ -> pure $ B.text "typst-hs")
,("math.equation", \_ fields -> do
body <- getField "body" fields
display <- getField "block" fields
diff --git a/src/Text/Pandoc/Readers/Typst/Parsing.hs b/src/Text/Pandoc/Readers/Typst/Parsing.hs
index 32c26d512..61279e178 100644
--- a/src/Text/Pandoc/Readers/Typst/Parsing.hs
+++ b/src/Text/Pandoc/Readers/Typst/Parsing.hs
@@ -5,6 +5,8 @@
module Text.Pandoc.Readers.Typst.Parsing
( P,
+ PState(..),
+ defaultPState,
pTok,
pWithContents,
ignored,
@@ -12,6 +14,7 @@ module Text.Pandoc.Readers.Typst.Parsing
chunks,
)
where
+import qualified Text.Pandoc.Builder as B
import Control.Monad (MonadPlus)
import Control.Monad.Reader (lift)
import qualified Data.Foldable as F
@@ -26,7 +29,24 @@ import Typst.Types
import Text.Pandoc.Class.PandocMonad ( PandocMonad, report )
import Text.Pandoc.Logging (LogMessage(..))
-type P m a = ParsecT [Content] [Text] m a
+data PState = PState
+ { sLabels :: [Text]
+ , sTitle :: B.Inlines
+ , sAuthors :: [B.Inlines]
+ , sDate :: B.Inlines
+ , sKeywords :: [Text] }
+ deriving (Show)
+
+defaultPState :: PState
+defaultPState =
+ PState
+ { sLabels = []
+ , sTitle = mempty
+ , sAuthors = []
+ , sDate = mempty
+ , sKeywords = [] }
+
+type P m a = ParsecT [Content] PState m a
-- state tracks a list of labels in the document
pTok :: PandocMonad m => (Content -> Bool) -> P m Content