aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2023-06-06 11:31:23 -0700
committerJohn MacFarlane <[email protected]>2023-06-06 11:31:23 -0700
commitc7c3758dbd1f352b8110c958514ba044c7f032ee (patch)
treee2ad42b8492a2c29c522d168b7f94dfd2e0b4aea
parent0d7aae01f105df9ba0a831888b294794bbd88b45 (diff)
Integrate pandoc-specific things formerly in typst-hs.
-rw-r--r--src/Text/Pandoc/Readers/Typst.hs47
-rw-r--r--src/Text/Pandoc/Readers/Typst/Math.hs28
-rw-r--r--src/Text/Pandoc/Readers/Typst/Parsing.hs17
3 files changed, 34 insertions, 58 deletions
diff --git a/src/Text/Pandoc/Readers/Typst.hs b/src/Text/Pandoc/Readers/Typst.hs
index 2bbe2a751..31b689dd2 100644
--- a/src/Text/Pandoc/Readers/Typst.hs
+++ b/src/Text/Pandoc/Readers/Typst.hs
@@ -27,7 +27,6 @@ import Text.Pandoc.Options
import Text.Pandoc.Definition
import Typst ( parseTypst, evaluateTypst )
import Text.Pandoc.Error (PandocError(..))
-import Text.Pandoc.Logging (LogMessage(..))
import Control.Monad.Except (throwError)
import Control.Monad (MonadPlus (mplus), void)
import qualified Data.Foldable as F
@@ -59,37 +58,25 @@ readTypst _opts inp = do
_ -> ""
case parseTypst inputName (sourcesToText sources) of
Left e -> throwError $ PandocParseError $ T.pack $ show e
- Right parsed -> do
- result <- evaluateTypst readFileStrict inputName parsed >>=
+ Right parsed ->
+ evaluateTypst readFileStrict inputName parsed >>=
either (throwError . PandocParseError . T.pack . show) pure >>=
- contentToPandoc (report . IgnoredElement)
- case result of
- Left e -> throwError $ PandocParseError $ T.pack $ show e
- Right pdoc -> pure pdoc
+ runParserT pPandoc () inputName . F.toList >>=
+ either (throwError . PandocParseError . T.pack . show) pure
--- | Convert a sequence of content elements to a Pandoc document.
-contentToPandoc ::
- Monad m =>
- -- | Function to issue warnings
- (Text -> m ()) ->
- -- | Contents to convert
- Seq Content ->
- m (Either ParseError B.Pandoc)
-contentToPandoc warn' = runParserT pPandoc warn' "" . F.toList
-
-pPandoc :: Monad m => P m B.Pandoc
+pPandoc :: PandocMonad m => P m B.Pandoc
pPandoc = B.doc <$> pBlocks
-pBlocks :: Monad m => P m B.Blocks
+pBlocks :: PandocMonad m => P m B.Blocks
pBlocks = mconcat <$> many pBlock
-pBlock :: Monad m => P m B.Blocks
+pBlock :: PandocMonad m => P m B.Blocks
pBlock = pPara <|> pBlockElt
-pBlockElt :: Monad m => P m B.Blocks
+pBlockElt :: PandocMonad m => P m B.Blocks
pBlockElt = pTok isBlock >>= handleBlock
-pSpace :: Monad m => P m Content
+pSpace :: PandocMonad m => P m Content
pSpace = pTok
( \case
Txt t | T.all (== ' ') t -> True
@@ -97,7 +84,7 @@ pSpace = pTok
)
-pLab :: Monad m => P m Text
+pLab :: PandocMonad m => P m Text
pLab = try $ do
optional pSpace
Lab t <- pTok
@@ -107,7 +94,7 @@ pLab = try $ do
)
pure t
-handleBlock :: Monad m => Content -> P m B.Blocks
+handleBlock :: PandocMonad m => Content -> P m B.Blocks
handleBlock tok = do
-- check for following label
mbident <- option Nothing $ Just <$> pLab
@@ -320,11 +307,11 @@ handleBlock tok = do
warn ("Skipping unknown block element " <> tname)
pure mempty
-pPara :: Monad m => P m B.Blocks
+pPara :: PandocMonad m => P m B.Blocks
pPara =
B.para . B.trimInlines . mconcat <$> (many1 pInline <* optional pParBreak)
-pParBreak :: Monad m => P m ()
+pParBreak :: PandocMonad m => P m ()
pParBreak =
void $
pTok
@@ -383,7 +370,7 @@ isBlock (Elt name _ fields) =
"yaml" -> True
_ -> False
-pWithContents :: Monad m => P m a -> Seq Content -> P m a
+pWithContents :: PandocMonad m => P m a -> Seq Content -> P m a
pWithContents pa cs = do
inp <- getInput
setInput $ F.toList cs
@@ -391,13 +378,13 @@ pWithContents pa cs = do
setInput inp
pure res
-pInlines :: Monad m => P m B.Inlines
+pInlines :: PandocMonad m => P m B.Inlines
pInlines = mconcat <$> many pInline
-pInline :: Monad m => P m B.Inlines
+pInline :: PandocMonad m => P m B.Inlines
pInline = pTok isInline >>= handleInline
-handleInline :: Monad m => Content -> P m B.Inlines
+handleInline :: PandocMonad m => Content -> P m B.Inlines
handleInline tok =
case tok of
Txt t -> pure $ B.text t
diff --git a/src/Text/Pandoc/Readers/Typst/Math.hs b/src/Text/Pandoc/Readers/Typst/Math.hs
index db4273e14..b6b7f29b2 100644
--- a/src/Text/Pandoc/Readers/Typst/Math.hs
+++ b/src/Text/Pandoc/Readers/Typst/Math.hs
@@ -3,8 +3,7 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Text.Pandoc.Readers.Typst.Math
- ( contentToMath,
- pMathMany,
+ ( pMathMany
)
where
@@ -18,7 +17,8 @@ import qualified Data.Sequence as Seq
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Vector as V
-import Text.Parsec
+import Text.Pandoc.Parsing ( many )
+import Text.Pandoc.Class ( PandocMonad )
import Text.TeXMath.Types
( Alignment (..),
Exp (..),
@@ -33,16 +33,6 @@ import Typst.Types
-- import Debug.Trace
--- | Convert a sequence of content elements to a TeXMath expression.
-contentToMath ::
- Monad m =>
- -- | Function to issue warnings
- (Text -> m ()) ->
- -- | Contents to convert
- Seq Content ->
- m (Either ParseError [Exp])
-contentToMath warn' cs = runParserT (pMathMany cs) warn' "" mempty
-
withGroup :: [Exp] -> Exp
withGroup [x] = x
withGroup xs = EGrouped xs
@@ -50,7 +40,7 @@ withGroup xs = EGrouped xs
data AttachmentStyle = Limits | Scripts
deriving (Eq, Show)
-getAttachmentStyle :: Monad m => M.Map Identifier Val -> P m (Maybe AttachmentStyle)
+getAttachmentStyle :: PandocMonad m => M.Map Identifier Val -> P m (Maybe AttachmentStyle)
getAttachmentStyle fields = do
(base :: Seq Content) <- getField "base" fields
case base of
@@ -58,10 +48,10 @@ getAttachmentStyle fields = do
[Elt "scripts" _ _] -> pure $ Just Scripts
_ -> pure Nothing
-pMath :: Monad m => P m Exp
+pMath :: PandocMonad m => P m Exp
pMath = pTok (const True) >>= handleMath
-handleMath :: Monad m => Content -> P m Exp
+handleMath :: PandocMonad m => Content -> P m Exp
handleMath tok =
case tok of
Lab t -> do
@@ -329,7 +319,7 @@ handleMath tok =
warn ("Ignoring unsupported " <> name)
pMathGrouped body
-arrayDelims :: Monad m => M.Map Identifier Val -> P m (Text, Text)
+arrayDelims :: PandocMonad m => M.Map Identifier Val -> P m (Text, Text)
arrayDelims fields = do
(mbdelim :: Maybe Text) <- getField "delim" fields
pure $ case mbdelim of
@@ -340,7 +330,7 @@ arrayDelims fields = do
Just "||" -> ("\8741", "\8741")
_ -> ("(", ")")
-pMathMany :: Monad m => Seq Content -> P m [Exp]
+pMathMany :: PandocMonad m => Seq Content -> P m [Exp]
pMathMany cs = do
-- check for "alignpoint" and "linebreak" elements
-- and use an array structure for alignment
@@ -354,7 +344,7 @@ pMathMany cs = do
let cols = take numcols $ AlignRight : cycle [AlignLeft, AlignRight]
pure [EArray cols rows]
-pMathGrouped :: Monad m => Seq Content -> P m Exp
+pMathGrouped :: PandocMonad m => Seq Content -> P m Exp
pMathGrouped = fmap withGroup . pMathMany
splitOnLinebreaks :: Seq Content -> [Seq Content]
diff --git a/src/Text/Pandoc/Readers/Typst/Parsing.hs b/src/Text/Pandoc/Readers/Typst/Parsing.hs
index d06f35382..585cc34a0 100644
--- a/src/Text/Pandoc/Readers/Typst/Parsing.hs
+++ b/src/Text/Pandoc/Readers/Typst/Parsing.hs
@@ -20,14 +20,15 @@ import Data.Maybe (fromMaybe)
import Data.Sequence (Seq)
import Data.Text (Text)
import Text.Parsec
- ( ParsecT, getInput, getState, setInput, tokenPrim )
+ ( ParsecT, getInput, setInput, tokenPrim )
import Typst.Types
( Identifier, Content(Elt), FromVal(..), Val(VNone) )
+import Text.Pandoc.Class.PandocMonad ( PandocMonad, report )
+import Text.Pandoc.Logging (LogMessage(..))
+type P m a = ParsecT [Content] () m a
-type P m a = ParsecT [Content] (Text -> m ()) m a
-
-pTok :: Monad m => (Content -> Bool) -> P m Content
+pTok :: PandocMonad m => (Content -> Bool) -> P m Content
pTok f = tokenPrim show showPos match
where
showPos _oldpos (Elt _ (Just pos) _) _ = pos
@@ -35,12 +36,10 @@ pTok f = tokenPrim show showPos match
match x | f x = Just x
match _ = Nothing
-warn :: Monad m => Text -> P m ()
-warn msg = do
- warn' <- getState
- lift $ warn' msg
+warn :: PandocMonad m => Text -> P m ()
+warn msg = lift $ report $ IgnoredElement msg
-pWithContents :: Monad m => P m a -> Seq Content -> P m a
+pWithContents :: PandocMonad m => P m a -> Seq Content -> P m a
pWithContents pa cs = do
inp <- getInput
setInput $ F.toList cs