aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2023-05-30 10:28:19 -0700
committerJohn MacFarlane <[email protected]>2023-06-06 11:10:19 -0700
commita32a7052154862710e14450458bf3a304eb95285 (patch)
treea2a998ddd34ad8754f962d7b01a72cea0eeebaba
parent9d79c9fcb75d7703d89aeb58357bd511c3acdbd0 (diff)
Add typst reader.
New module Text.Pandoc.Readers.Typst [API change].
-rw-r--r--MANUAL.txt1
-rw-r--r--cabal.project4
-rw-r--r--pandoc.cabal6
-rw-r--r--shell.nix1
-rw-r--r--src/Text/Pandoc/Format.hs1
-rw-r--r--src/Text/Pandoc/Readers.hs3
-rw-r--r--src/Text/Pandoc/Readers/Typst.hs48
-rw-r--r--test/epub/features.native4
8 files changed, 61 insertions, 7 deletions
diff --git a/MANUAL.txt b/MANUAL.txt
index 9906483f5..a83a59dd3 100644
--- a/MANUAL.txt
+++ b/MANUAL.txt
@@ -268,6 +268,7 @@ header when requesting a document from a URL:
- `textile` ([Textile])
- `tikiwiki` ([TikiWiki markup])
- `twiki` ([TWiki markup])
+ - `typst` ([typst])
- `vimwiki` ([Vimwiki])
- the path of a custom Lua reader, see [Custom readers and writers] below
:::
diff --git a/cabal.project b/cabal.project
index 6c0f3f09a..f5e662656 100644
--- a/cabal.project
+++ b/cabal.project
@@ -10,5 +10,5 @@ constraints: skylighting-format-blaze-html >= 0.1.1,
source-repository-package
type: git
- location: https://github.com/jgm/texmath
- tag: 6f22675a2fec126cfe8aed87af19aae4e55e5eb1
+ location: https://github.com/jgm/typst-hs
+ tag: 5b0ea38051645f271443f44f5de8ed0d3ec2bd04
diff --git a/pandoc.cabal b/pandoc.cabal
index 6210bfda8..d369a3c2d 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -515,7 +515,7 @@ library
syb >= 0.1 && < 0.8,
tagsoup >= 0.14.6 && < 0.15,
temporary >= 1.1 && < 1.4,
- texmath >= 0.12.7.1 && < 0.13,
+ texmath >= 0.12.8 && < 0.13,
text >= 1.1.1.0 && < 2.1,
text-conversions >= 0.3 && < 0.4,
time >= 1.5 && < 1.14,
@@ -524,7 +524,8 @@ library
yaml >= 0.11 && < 0.12,
zip-archive >= 0.4.3 && < 0.5,
zlib >= 0.5 && < 0.7,
- xml >= 1.3.12 && < 1.4
+ xml >= 1.3.12 && < 1.4,
+ typst >= 0.1 && < 0.2
if !os(windows)
build-depends: unix >= 2.4 && < 2.9
@@ -580,6 +581,7 @@ library
Text.Pandoc.Readers.Ipynb,
Text.Pandoc.Readers.CSV,
Text.Pandoc.Readers.RTF,
+ Text.Pandoc.Readers.Typst,
Text.Pandoc.Writers,
Text.Pandoc.Writers.Native,
Text.Pandoc.Writers.DocBook,
diff --git a/shell.nix b/shell.nix
index 51ff6c6c9..459520b52 100644
--- a/shell.nix
+++ b/shell.nix
@@ -100,7 +100,6 @@ let
nixPackages = [
pkgs.zlib
ghc
- pkgs.gdb
haskellPackages.ghcid
haskellPackages.haskell-language-server
haskellPackages.cabal2nix
diff --git a/src/Text/Pandoc/Format.hs b/src/Text/Pandoc/Format.hs
index ca04d4bc2..5f9459e3e 100644
--- a/src/Text/Pandoc/Format.hs
+++ b/src/Text/Pandoc/Format.hs
@@ -220,6 +220,7 @@ formatFromFilePath x =
".text" -> defFlavor "markdown"
".textile" -> defFlavor "textile"
".tsv" -> defFlavor "tsv"
+ ".typ" -> defFlavor "typst"
".txt" -> defFlavor "markdown"
".typ" -> defFlavor "typst"
".wiki" -> defFlavor "mediawiki"
diff --git a/src/Text/Pandoc/Readers.hs b/src/Text/Pandoc/Readers.hs
index 4c35de01a..378d9c2e0 100644
--- a/src/Text/Pandoc/Readers.hs
+++ b/src/Text/Pandoc/Readers.hs
@@ -60,6 +60,7 @@ module Text.Pandoc.Readers
, readEndNoteXML
, readRIS
, readRTF
+ , readTypst
-- * Miscellaneous
, getReader
, getDefaultExtensions
@@ -109,6 +110,7 @@ import Text.Pandoc.Readers.BibTeX
import Text.Pandoc.Readers.EndNote
import Text.Pandoc.Readers.RIS
import Text.Pandoc.Readers.RTF
+import Text.Pandoc.Readers.Typst
import qualified Text.Pandoc.UTF8 as UTF8
import Text.Pandoc.Sources (ToSources(..), sourcesToText)
@@ -160,6 +162,7 @@ readers = [("native" , TextReader readNative)
,("endnotexml" , TextReader readEndNoteXML)
,("ris" , TextReader readRIS)
,("rtf" , TextReader readRTF)
+ ,("typst" , TextReader readTypst)
]
-- | Retrieve reader, extensions based on format spec (format+extensions).
diff --git a/src/Text/Pandoc/Readers/Typst.hs b/src/Text/Pandoc/Readers/Typst.hs
new file mode 100644
index 000000000..2f1fbc976
--- /dev/null
+++ b/src/Text/Pandoc/Readers/Typst.hs
@@ -0,0 +1,48 @@
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE UndecidableInstances #-}
+{-# LANGUAGE OverloadedStrings #-}
+{- |
+ Module : Text.Pandoc.Readers.Typst
+ Copyright : Copyright (C) 2023 John MacFarlane
+ License : GNU GPL, version 2 or above
+
+ Maintainer : John MacFarlane <[email protected]>
+ Stability : alpha
+ Portability : portable
+
+Reads and evaluates a Typst document as a Pandoc AST.
+-}
+module Text.Pandoc.Readers.Typst
+ ( readTypst
+ )
+where
+
+import Text.Pandoc.Class
+import Text.Pandoc.Sources
+import Text.Pandoc.Options
+import Text.Pandoc.Definition
+import Typst ( parseTypst, evaluateTypst )
+import Typst.Pandoc (contentToPandoc)
+import qualified Data.Text as T
+import Text.Pandoc.Parsing (sourceName)
+import Text.Pandoc.Error (PandocError(..))
+import Text.Pandoc.Logging (LogMessage(..))
+import Control.Monad.Except (throwError)
+
+-- | Read Typst from an input string and return a Pandoc document.
+readTypst :: (PandocMonad m, ToSources a)
+ => ReaderOptions -> a -> m Pandoc
+readTypst _opts inp = do
+ let sources = toSources inp
+ let inputName = case sources of
+ Sources ((pos, _):_) -> sourceName pos
+ _ -> ""
+ case parseTypst inputName (sourcesToText sources) of
+ Left e -> throwError $ PandocParseError $ T.pack $ show e
+ Right parsed -> do
+ result <- 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
diff --git a/test/epub/features.native b/test/epub/features.native
index ef5f0d032..2a4580092 100644
--- a/test/epub/features.native
+++ b/test/epub/features.native
@@ -1281,7 +1281,7 @@
, Plain
[ Math
DisplayMath
- "\\begin{matrix}\n & {\\operatorname{cov}(\\mathcal{L})} & \\longrightarrow & {\\operatorname{non}(\\mathcal{K})} & \\longrightarrow & {\\operatorname{cof}(\\mathcal{K})} & \\longrightarrow & {\\operatorname{cof}(\\mathcal{L})} & \\longrightarrow & 2^{\\aleph_{0}} \\\\\n & \\uparrow & & \\uparrow & & \\uparrow & & \\uparrow & & \\\\\n & {\\mathfrak{b}} & \\longrightarrow & {\\mathfrak{d}} & & & & & & \\\\\n & \\uparrow & & \\uparrow & & & & & & \\\\\n\\aleph_{1} & \\longrightarrow & {\\operatorname{add}(\\mathcal{L})} & \\longrightarrow & {\\operatorname{add}(\\mathcal{K})} & \\longrightarrow & {\\operatorname{cov}(\\mathcal{K})} & \\longrightarrow & {\\operatorname{non}(\\mathcal{L})} & \\\\\n\\end{matrix}"
+ "\\begin{matrix}\n & {\\operatorname{cov}(\\mathcal{L})} & \\longrightarrow & {\\operatorname{non}(\\mathcal{K})} & \\longrightarrow & {\\operatorname{cof}(\\mathcal{K})} & \\longrightarrow & {\\operatorname{cof}(\\mathcal{L})} & \\longrightarrow & 2^{\\aleph_{0}} \\\\\n & \\uparrow & & \\uparrow & & \\uparrow & & \\uparrow & & \\\\\n & {\\mathfrak{b}} & \\longrightarrow & {\\mathfrak{d}} & & & & & & \\\\\n & \\uparrow & & \\uparrow & & & & & & \\\\\n\\aleph_{1} & \\longrightarrow & {\\operatorname{add}(\\mathcal{L})} & \\longrightarrow & {\\operatorname{add}(\\mathcal{K})} & \\longrightarrow & {\\operatorname{cov}(\\mathcal{K})} & \\longrightarrow & {\\operatorname{non}(\\mathcal{L})} & \n\\end{matrix}"
]
, Para
[ Str "The"
@@ -1352,7 +1352,7 @@
, Plain
[ Math
DisplayMath
- "{\1583(\1587)} = \\left\\{ \\begin{matrix}\n{\\sum\\limits_{\1646 = 1}^{\1589}\1587^{\1646}} & {\\text{\1573\1584\1575\1603\1575\1606}\1587 > 0} \\\\\n{\\int_{1}^{\1589}{\1587^{\1646}\1569\1587}} & {\\text{\1573\1584\1575\1603\1575\1606}\1587 \\in \1605} \\\\\n{{\1591\1575}\\pi} & {\\text{\1594\1610\1585\1584\1604\1603}\\left( \\text{\1605\1593}\\pi \\simeq 3,141 \\right)} \\\\\n\\end{matrix} \\right."
+ "{\1583(\1587)} = \\left\\{ \\begin{matrix}\n{\\sum\\limits_{\1646 = 1}^{\1589}\1587^{\1646}} & {\\text{\1573\1584\1575\1603\1575\1606}\1587 > 0} \\\\\n{\\int_{1}^{\1589}{\1587^{\1646}\1569\1587}} & {\\text{\1573\1584\1575\1603\1575\1606}\1587 \\in \1605} \\\\\n{{\1591\1575}\\pi} & {\\text{\1594\1610\1585\1584\1604\1603}\\left( \\text{\1605\1593}\\pi \\simeq 3,141 \\right)}\n\\end{matrix} \\right."
]
, Para
[ Str "The"