summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2023-12-10 12:56:59 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2023-12-10 12:56:59 -0800
commit42091d1e13f2c17f477396278b78d3addcf01837 (patch)
tree850b24064ef6e59bf44c58fc8c3b0512d357d208
parentb0e8e99c831c54e1f46d3f5c78ebcc547279e15d (diff)
Typst reader: parse metadata from document element.
-rw-r--r--cabal.project2
-rw-r--r--pandoc.cabal2
-rw-r--r--src/Text/Pandoc/Readers/Typst.hs41
-rw-r--r--src/Text/Pandoc/Readers/Typst/Parsing.hs17
-rw-r--r--stack.yaml2
5 files changed, 44 insertions, 20 deletions
diff --git a/cabal.project b/cabal.project
index 07515e51c..e6c89d0e9 100644
--- a/cabal.project
+++ b/cabal.project
@@ -28,4 +28,4 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/jgm/typst-hs
- tag: 94747f802831b05b5c28bba0bd7a7a30217bda8d
+ tag: c909bd7f012044834ef06fbeda4626dcaee90abd
diff --git a/pandoc.cabal b/pandoc.cabal
index 21f9519b1..433804da5 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -522,7 +522,7 @@ library
zip-archive >= 0.4.3 && < 0.5,
zlib >= 0.5 && < 0.7,
xml >= 1.3.12 && < 1.4,
- typst >= 0.3.2.1 && < 0.3.3,
+ typst >= 0.4 && < 0.4.1,
vector >= 0.12 && < 0.14
if !os(windows)
diff --git a/src/Text/Pandoc/Readers/Typst.hs b/src/Text/Pandoc/Readers/Typst.hs
index 515e33aa6..3b9d5b7cb 100644
--- a/src/Text/Pandoc/Readers/Typst.hs
+++ b/src/Text/Pandoc/Readers/Typst.hs
@@ -72,10 +72,10 @@ readTypst _opts inp = do
res <- evaluateTypst ops inputName parsed
case res of
Left e -> throwError $ PandocParseError $ tshow e
- Right cs -> do
- let labs = findLabels cs
+ Right content -> do
+ let labs = findLabels [content]
runParserT pPandoc defaultPState{ sLabels = labs }
- inputName (F.toList cs) >>=
+ inputName [content] >>=
either (throwError . PandocParseError . T.pack . show) pure
pBlockElt :: PandocMonad m => P m B.Blocks
@@ -128,7 +128,36 @@ pInline = try $ do
Just handler -> handler Nothing fields
pPandoc :: PandocMonad m => P m B.Pandoc
-pPandoc = B.doc <$> pBlocks
+pPandoc = do
+ Elt "document" _ fields <- pTok isDocument
+ bs <- getField "body" fields >>= pWithContents pBlocks
+ title <- (getField "title" fields >>= pWithContents pInlines) <|>
+ pure mempty
+ authors <- (getField "author" fields >>=
+ mapM (pWithContents pInlines) . V.toList) <|>
+ ((:[]) <$> (getField "author" fields >>=
+ (\x -> guard (not (null x)) *>
+ pWithContents pInlines x))) <|>
+ pure []
+ date <- (getField "date" fields >>= pWithContents pInlines) <|>
+ pure mempty
+ keywords <- (getField "keywords" fields >>=
+ mapM (pWithContents pInlines) . V.toList)
+ <|> pure []
+ pure $
+ (if title == mempty
+ then id
+ else B.setMeta "title" title) .
+ (if null authors
+ then id
+ else B.setMeta "author" authors) .
+ (if null date
+ then id
+ else B.setMeta "date" date) .
+ (if null keywords
+ then id
+ else B.setMeta "keywords" keywords) $
+ B.doc bs
pBlocks :: PandocMonad m => P m B.Blocks
pBlocks = mconcat <$> many pBlock
@@ -152,6 +181,10 @@ pLab = try $ do
)
pure t
+isDocument :: Content -> Bool
+isDocument (Elt "document" _ _) = True
+isDocument _ = False
+
isBlock :: Content -> Bool
isBlock (Elt "raw" _ fields) = M.lookup "block" fields == Just (VBoolean True)
isBlock (Elt name _ _) = name `Set.member` blockKeys
diff --git a/src/Text/Pandoc/Readers/Typst/Parsing.hs b/src/Text/Pandoc/Readers/Typst/Parsing.hs
index 61279e178..b7b725c6f 100644
--- a/src/Text/Pandoc/Readers/Typst/Parsing.hs
+++ b/src/Text/Pandoc/Readers/Typst/Parsing.hs
@@ -14,7 +14,6 @@ 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
@@ -29,22 +28,14 @@ import Typst.Types
import Text.Pandoc.Class.PandocMonad ( PandocMonad, report )
import Text.Pandoc.Logging (LogMessage(..))
-data PState = PState
- { sLabels :: [Text]
- , sTitle :: B.Inlines
- , sAuthors :: [B.Inlines]
- , sDate :: B.Inlines
- , sKeywords :: [Text] }
- deriving (Show)
+newtype PState = PState
+ { sLabels :: [Text]}
+ deriving (Show)
defaultPState :: PState
defaultPState =
PState
- { sLabels = []
- , sTitle = mempty
- , sAuthors = []
- , sDate = mempty
- , sKeywords = [] }
+ { sLabels = [] }
type P m a = ParsecT [Content] PState m a
-- state tracks a list of labels in the document
diff --git a/stack.yaml b/stack.yaml
index 70c3b0f1d..abde05fee 100644
--- a/stack.yaml
+++ b/stack.yaml
@@ -27,7 +27,7 @@ extra-deps:
- tls-1.9.0
- commonmark-0.2.4
- git: https://github.com/jgm/typst-hs
- commit: 94747f802831b05b5c28bba0bd7a7a30217bda8d
+ commit: c909bd7f012044834ef06fbeda4626dcaee90abd
- git: https://github.com/jgm/commonmark-hs
subdirs: [commonmark-extensions, commonmark-pandoc]
commit: 0c4807c92b94b1b5ac0f0845daac276ab9d3495f