packages feed

typst 0.7 → 0.8

raw patch · 39 files changed

+123/−68 lines, 39 filesdep ~typst-symbols

Dependency ranges changed: typst-symbols

Files

CHANGELOG.md view
@@ -1,5 +1,28 @@ # Revision history for typst-hs +## 0.8++  * Allow `json`, `toml`, `xml` to take either file path or bytes.++  * Allow `read` to return bytes if encoding is 'none'.++  * `bibliography`, `image`: change parameter name to `source` and allow bytes.++  * Add 'bytes' as a type name and constructor.++  * Add VBytes constructor for Val and TBytes for ValType. [API change]++  * Allow values of arguments type to be added together.++  * Support `calc.norm`.++  * Math: add `lcm` operator.++  * Require typst-symbols >= 0.1.8.1 (#67), giving us typst 0.13 symbols.++  * Add "dictionary" as name of TDict type (#65).++ ## 0.7    * Fix problems with module loading paths (#62).
src/Typst/Constructors.hs view
@@ -14,12 +14,14 @@ import qualified Data.Map.Ordered as OM import qualified Data.Map as M import Data.Time (fromGregorian, secondsToDiffTime)-import Data.Maybe (fromMaybe)+import Data.Maybe (fromMaybe, mapMaybe)+import qualified Data.ByteString as B import Typst.Types import Typst.Util (makeFunction, makeFunctionWithScope, namedArg, nthArg, allArgs) import qualified Data.Set as Set import Data.Text (Text) import qualified Data.Text as T+import qualified Data.Text.Encoding as TE import Typst.Regex (makeRE) import Data.List (genericTake) import Control.Monad.Reader (asks)@@ -97,6 +99,14 @@       case a of         VModule _ m -> pure $ VDict $ OM.fromList $ M.toList m         _ -> fail "dictionary constructor requires a module as argument"+    TBytes -> Just $ makeFunction $ do+      x <- nthArg 1+      let extractWord8 (VInteger w) = Just $ fromIntegral w+          extractWord8 _ = Nothing+      case x of+        VString s -> pure $ VBytes $ TE.encodeUtf8 s+        VArray xs -> pure $ VBytes $ B.pack $ mapMaybe extractWord8 $ V.toList xs+        _ -> fail "bytes constructor requires a string or array as argument"     TArguments -> Nothing     -- TODO https://typst.app/docs/reference/foundations/arguments/     TSelector -> Nothing
src/Typst/Evaluate.hs view
@@ -690,6 +690,8 @@       case (v1, v2) of         (VAlignment x1 y1, VAlignment x2 y2) ->           pure $ VAlignment (x1 `mplus` x2) (y1 `mplus` y2)+        (VArguments xs, VArguments ys) ->+          pure $ VArguments (xs <> ys)         _ -> case maybePlus v1 v2 of           Nothing -> fail $ "Can't + " <> show v1 <> " and " <> show v2           Just v -> pure v
src/Typst/Module/Calc.hs view
@@ -7,7 +7,7 @@ where  import qualified Data.Map as M-import Data.Maybe (fromMaybe)+import Data.Maybe (fromMaybe, mapMaybe) import Typst.Types import Typst.Util @@ -112,6 +112,17 @@           case vs of             [] -> fail "min requires one or more argument"             _ : _ -> pure $ minimum vs+      ),+      ( "norm",+        makeFunction $ do+          p <- namedArg "p" (2.0 :: Double)+          let extractFloat (VFloat x) = Just x+              extractFloat (VInteger x) = Just (fromIntegral x)+              extractFloat _ = Nothing+          vs <- mapMaybe extractFloat <$> allArgs+          case vs of+            [] -> fail "norm requires one or more argument"+            _ : _ -> pure $ VFloat $ (sum (map ((**p) . abs) vs))**(1.0/p)       ),       ( "odd",         makeFunction $ do
src/Typst/Module/Math.hs view
@@ -188,6 +188,7 @@           "hom",           "mod",           "ker",+          "lcm",           "lg",           "ln",           "log",
src/Typst/Module/Standard.hs view
@@ -17,7 +17,7 @@ import Data.Version (showVersion) import Control.Applicative ((<|>)) import Control.Monad (mplus, unless)-import Control.Monad.Reader (lift)+import Control.Monad.Reader (lift, ReaderT) import qualified Data.Aeson as Aeson import qualified Data.ByteString.Lazy as BL import qualified Data.Csv as Csv@@ -222,7 +222,7 @@ visualize =   [ makeElement Nothing "circle" [("body", One (TContent :|: TNone))],     makeElement Nothing "ellipse" [("body", One (TContent :|: TNone))],-    makeElement Nothing "image" [("path", One TString)],+    makeElement Nothing "image" [("source", One (TString :|: TBytes))],     makeElement Nothing "line" [],     makeElement Nothing "path" [("vertices", Many TArray)],     makeElement Nothing "polygon" [("vertices", Many TArray)],@@ -232,7 +232,7 @@  meta :: [(Identifier, Val)] meta =-  [ makeElement Nothing "bibliography" [("path", One (TString :|: TArray))],+  [ makeElement Nothing "bibliography" [("source", One (TString :|: TArray :|: TBytes))],     makeElement Nothing "cite" [("key", One TLabel)],     makeElement Nothing "document" [],     makeElementWithScope@@ -290,6 +290,7 @@   [ ("array", VType TArray)   , ("bool", VType TBoolean)   , ("content", VType TContent)+  , ("dictionary", VType TDict)   , ("int", VType TInteger)   , ("float", VType TFloat)   , ("regex", VType TRegex)@@ -300,6 +301,7 @@   , ("str", VType TString)   , ("label", VType TLabel)   , ("version", VType TVersion)+  , ("bytes", VType TBytes)   ]  colors :: [(Identifier, Val)]@@ -532,16 +534,14 @@     ),     ( "json",       makeFunction $ do-        fp <- nthArg 1-        bs <- lift $ loadFileLazyBytes fp+        bs <- getFileOrBytes         case Aeson.eitherDecode bs of           Left e -> fail e           Right (v :: Val) -> pure v     ),     ( "yaml",       makeFunction $ do-        fp <- nthArg 1-        bs <- lift $ loadFileLazyBytes fp+        bs <- getFileOrBytes         case Yaml.decodeEither' (BL.toStrict bs) of           Left e -> fail $ show e           Right (v :: Val) -> pure v@@ -549,21 +549,23 @@     ( "read",       makeFunction $ do         fp <- nthArg 1-        t <- lift $ loadFileText fp-        pure $ VString t+        enc <- namedArg "encoding" (VString "utf-8")+        case enc of+          VNone -> do bs <- lift $ loadFileLazyBytes fp+                      pure $ VBytes $ BL.toStrict bs+          _ -> do t <- lift $ loadFileText fp+                  pure $ VString t     ),     ( "toml",       makeFunction $ do-        fp <- nthArg 1-        t <- lift $ loadFileText fp-        case Toml.decode t of+        bs <- getFileOrBytes+        case Toml.decode (TE.decodeUtf8 $ BL.toStrict bs) of           Toml.Failure e -> fail (unlines ("toml errors:" : e))           Toml.Success _ v -> pure v     ),     ( "xml",       makeFunction $ do-        fp <- nthArg 1-        bs <- lift $ loadFileLazyBytes fp+        bs <- getFileOrBytes         case XML.parseLBS XML.def bs of           Left e -> fail $ show e           Right doc ->@@ -612,3 +614,11 @@                  , evalMathIdentifiers = [(BlockScope, mathModule <> symModule)]                  , evalStandardIdentifiers = [(BlockScope, standardModule)]                  }++getFileOrBytes :: Monad m => ReaderT Arguments (MP m) BL.ByteString+getFileOrBytes = do+  v <- nthArg 1+  case v of+    VString fp -> lift $ loadFileLazyBytes (T.unpack fp)+    VBytes bs -> pure $ BL.fromStrict bs+    _ -> fail "expecting file path or bytes"
src/Typst/Types.hs view
@@ -68,6 +68,7 @@ import Data.String import Data.Text (Text) import qualified Data.Text as T+import Data.ByteString (ByteString) import Data.Vector (Vector) import qualified Data.Vector as V import Text.Parsec@@ -144,7 +145,7 @@   | VModule Identifier (M.Map Identifier Val)   | VStyles -- just a placeholder for now   | VVersion [Integer]-  -- Types typically have constructors+  | VBytes ByteString   | VType !ValType   deriving (Show, Eq, Typeable) @@ -205,6 +206,7 @@   | TCounter   | TLocation   | TVersion+  | TBytes   | TType   | TAny   | ValType :|: ValType@@ -241,6 +243,7 @@     VSelector {} -> TSelector     VStyles {} -> TStyles     VVersion {} -> TVersion+    VBytes {} -> TBytes     VType {} -> TType  hasType :: ValType -> Val -> Bool@@ -923,6 +926,7 @@     VSelector _ -> mempty     VStyles -> mempty     VVersion xs -> text $ T.intercalate "." (map (T.pack . show) xs)+    VBytes bs -> text $ "bytes(" <> T.pack (show (BS.length bs)) <> ")"     VType ty -> text $ prettyType ty  prettyType :: ValType -> Text
test/typ/coma-00.out view
@@ -409,6 +409,6 @@                  text(body: [ zu einem Blatt. Die Höhe des Baumes ist die Höhe der Wurzel.]),                   parbreak(),                   align(alignment: center, -                       body: image(path: "/graph.png", +                       body: image(source: "/graph.png",                                     width: 75%)),                   parbreak() })
test/typ/compiler/show-text-08.out view
@@ -67,6 +67,6 @@ ]),                   parbreak(),                   text(body: [The ]), -                 image(path: "/assets/files/graph.png"), +                 image(source: "/assets/files/graph.png"),                   text(body: [ has nodes.]),                   parbreak() })
test/typ/layout/grid-3-01.out view
@@ -108,7 +108,7 @@                  text(body: [ ]),                   grid(children: (align(alignment: top, -                                       body: image(path: "/assets/files/rhino.png")), +                                       body: image(source: "/assets/files/rhino.png")),                                   align(alignment: top,                                         body: rect(body: align(alignment: right,                                                                body: text(body: [LoL])), 
test/typ/layout/pad-02.out view
@@ -87,7 +87,7 @@                        body: text(body: [Before])),                   text(body: [ ]), -                 pad(body: image(path: "/assets/files/tiger.jpg"), +                 pad(body: image(source: "/assets/files/tiger.jpg"),                       rest: 10.0pt),                   text(body: [ ]), 
test/typ/layout/par-bidi-06.out view
@@ -70,7 +70,7 @@ קרנפיםRh],                        lang: "he"),                   box(body: image(height: 11.0pt, -                                 path: "/assets/files/rhino.png")), +                                 source: "/assets/files/rhino.png")),                   text(body: [inoחיים],                        lang: "he"),                   parbreak() })
test/typ/layout/par-indent-00.out view
@@ -223,12 +223,12 @@                  text(body: [But the second one does.]),                   parbreak(),                   box(body: image(height: 6.0pt, -                                 path: "/assets/files/tiger.jpg")), +                                 source: "/assets/files/tiger.jpg")),                   text(body: [ starts a paragraph, also with indent.]),                   parbreak(),                   align(alignment: center, -                       body: image(path: "/assets/files/rhino.png", +                       body: image(source: "/assets/files/rhino.png",                                     width: 1.0cm)),                   parbreak(),                   heading(body: text(body: [Headings]), 
test/typ/layout/place-00.out view
@@ -197,7 +197,7 @@                  heading(body: text(body: [Placement]),                           level: 1),                   place(alignment: right, -                       body: image(path: "/assets/files/tiger.jpg", +                       body: image(source: "/assets/files/tiger.jpg",                                     width: 1.8cm)),                   text(body: [ Hi there. This is ]), 
test/typ/layout/place-background-00.out view
@@ -110,7 +110,7 @@                       fill: rgb(100%,100%,100%,100%)),                   place(body: image(fit: "cover",                                     height: 20.0pt + 100%, -                                   path: "/tiger.jpg", +                                   source: "/tiger.jpg",                                     width: 20.0pt + 100%),                         dx: -10.0pt,                         dy: -10.0pt), 
test/typ/layout/transform-01.out view
@@ -78,6 +78,6 @@ ]),                   align(alignment: Axes(center, horizon),                         body: rotate(angle: 20.0deg, -                                    body: scale(body: image(path: "/assets/files/tiger.jpg"), +                                    body: scale(body: image(source: "/assets/files/tiger.jpg"),                                                  factor: 70%))),                   parbreak() })
test/typ/layout/transform-02.out view
@@ -62,7 +62,7 @@ document(body: { text(body: [ ]),                   rotate(angle: 10.0deg, -                        body: image(path: "/assets/files/tiger.jpg", +                        body: image(source: "/assets/files/tiger.jpg",                                      width: 50%),                          origin: Axes(left, top)),                   parbreak() })
test/typ/math/content-00.out view
@@ -109,7 +109,7 @@                                        text(body: [+]),                                         math.frac(denom: text(body: [2]),                                                   num: move(body: image(height: 1.0em, -                                                                       path: "/assets/files/monkey.svg"), +                                                                       source: "/assets/files/monkey.svg"),                                                             dy: 0.2em)) },                                 numbering: none),                   parbreak() })
test/typ/math/syntax-01.out view
@@ -49,10 +49,7 @@            (Ident (Identifier "underline"))            [ BlockArg                [ Text "f"-               , Code-                   "typ/math/syntax-01.typ"-                   ( line 3 , column 14 )-                   (Ident (Identifier "prime"))+               , Text "'"                , Text ":"                , Code                    "typ/math/syntax-01.typ"@@ -73,10 +70,7 @@     , Code         "typ/math/syntax-01.typ"         ( line 4 , column 5 )-        (FieldAccess-           (Ident (Identifier "r"))-           (FieldAccess-              (Ident (Identifier "bar")) (Ident (Identifier "arrow"))))+        (Ident (Identifier "mapsto"))     , Code         "typ/math/syntax-01.typ"         ( line 4 , column 9 )@@ -150,7 +144,7 @@ ]),                   math.equation(block: true,                                 body: { math.underline(body: { text(body: [f]), -                                                              text(body: [′]), +                                                              text(body: [']),                                                                text(body: [:]),                                                                text(body: [ℕ]),                                                                text(body: [→]), 
test/typ/meta/bibliography-01.out view
@@ -101,5 +101,5 @@                      target: <distress>),                   text(body: [. ]), -                 bibliography(path: "/works.bib"), +                 bibliography(source: "/works.bib"),                   parbreak() })
test/typ/meta/bibliography-02.out view
@@ -142,7 +142,7 @@ ]),                   text(body: [ ]), -                 bibliography(path: "/works.bib", +                 bibliography(source: "/works.bib",                                style: "chicago-author-date",                                title: text(body: [Works to be cited])),                   text(body: [
test/typ/meta/bibliography-04.out view
@@ -115,6 +115,6 @@                  cite(key: <keshav2007read>),                   text(body: [ ]), -                 bibliography(path: ("/works.bib", -                                     "/works_too.bib")), +                 bibliography(source: ("/works.bib", +                                       "/works_too.bib")),                   parbreak() })
test/typ/meta/bibliography-ordering-00.out view
@@ -130,5 +130,5 @@                  ref(supplement: auto,                       target: <restful>),                   parbreak(), -                 bibliography(path: "/works.bib"), +                 bibliography(source: "/works.bib"),                   parbreak() })
test/typ/meta/cite-footnote-00.out view
@@ -78,6 +78,6 @@                  pagebreak(),                   text(body: [ ]), -                 bibliography(path: "/works.bib", +                 bibliography(source: "/works.bib",                                style: "chicago-notes"),                   parbreak() })
test/typ/meta/figure-00.out view
@@ -202,7 +202,7 @@                  <tab-basic>,                   parbreak(),                   figure(body: pad(body: image(height: 2.0cm, -                                              path: "/assets/files/cylinder.svg"), +                                              source: "/assets/files/cylinder.svg"),                                    y: -6.0pt),                          caption: text(body: [The basic shapes.]),                          numbering: "I"), 
test/typ/meta/figure-01.out view
@@ -70,7 +70,7 @@ --- evaluated --- document(body: { parbreak(),                   figure(body: table(children: (text(body: [Second cylinder]), -                                               image(path: "/assets/files/cylinder.svg")), +                                               image(source: "/assets/files/cylinder.svg")),                                      columns: 2),                          caption: "A table containing images."),                   text(body: [ ]), 
test/typ/meta/footnote-container-00.out view
@@ -141,7 +141,7 @@                                      dest: "https://typst.app/docs")),                   text(body: [! ]), -                 figure(body: image(path: "/assets/files/graph.png", +                 figure(body: image(source: "/assets/files/graph.png",                                      width: 70%),                          caption: { text(body: [ A graph ]), 
@@ -86,7 +86,7 @@                  link(body: block(body: { text(body: [ My cool rhino ]), -                                          box(body: move(body: image(path: "/assets/files/rhino.png", +                                          box(body: move(body: image(source: "/assets/files/rhino.png",                                                                       width: 1.0cm),                                                           dx: 10.0pt)),                                            parbreak() }), 
test/typ/meta/query-figure-00.out view
@@ -243,7 +243,7 @@                          level: 1),                   locate(func: ),                   parbreak(), -                 figure(body: image(path: "/assets/files/glacier.jpg"), +                 figure(body: image(source: "/assets/files/glacier.jpg"),                          caption: text(body: [Glacier melting]),                          numbering: "I"),                   parbreak(), @@ -253,7 +253,7 @@                         numbering: "I",                          supplement: "Figure"),                   parbreak(), -                 figure(body: image(path: "/assets/files/tiger.jpg"), +                 figure(body: image(source: "/assets/files/tiger.jpg"),                          caption: text(body: [Tiger world]),                          numbering: "I"),                   parbreak() })
test/typ/text/baseline-00.out view
@@ -187,7 +187,7 @@                  text(body: [— Hey ]),                   box(baseline: 40%, -                     body: image(path: "/assets/files/tiger.jpg", +                     body: image(source: "/assets/files/tiger.jpg",                                   width: 1.5cm)),                   text(body: [ there!]),                   parbreak() })
test/typ/text/linebreak-obj-00.out view
@@ -93,5 +93,5 @@                  text(body: [, which is the authoritative source.]),                   parbreak(), -                 bibliography(path: "/works.bib"), +                 bibliography(source: "/works.bib"),                   parbreak() })
test/typ/visualize/image-00.out view
@@ -70,9 +70,9 @@ ]),                   text(body: [ ]), -                 image(path: "/assets/files/rhino.png"), +                 image(source: "/assets/files/rhino.png"),                   parbreak(),                   text(body: [ ]), -                 image(path: "/assets/files/tiger.jpg"), +                 image(source: "/assets/files/tiger.jpg"),                   parbreak() })
test/typ/visualize/image-01.out view
@@ -103,20 +103,20 @@ ]),                   text(body: [ ]), -                 box(body: image(path: "/assets/files/rhino.png", +                 box(body: image(source: "/assets/files/rhino.png",                                   width: 30.0pt)),                   text(body: [ ]),                   box(body: image(height: 30.0pt, -                                 path: "/assets/files/rhino.png")), +                                 source: "/assets/files/rhino.png")),                   parbreak(),                   image(fit: "stretch",                         height: 20.0pt, -                       path: "/assets/files/monkey.svg", +                       source: "/assets/files/monkey.svg",                         width: 100%),                   parbreak(),                   align(alignment: Axes(right, bottom),                         body: image(alt: "A tiger", -                                   path: "/assets/files/tiger.jpg", +                                   source: "/assets/files/tiger.jpg",                                     width: 40.0pt)),                   parbreak() })
test/typ/visualize/image-02.out view
@@ -97,15 +97,15 @@ ]),                   grid(children: (image(fit: "contain",                                         height: 100%, -                                       path: "/assets/files/tiger.jpg", +                                       source: "/assets/files/tiger.jpg",                                         width: 100%),                                   image(fit: "cover",                                         height: 100%, -                                       path: "/assets/files/tiger.jpg", +                                       source: "/assets/files/tiger.jpg",                                         width: 100%),                                   image(fit: "stretch",                                         height: 100%, -                                       path: "/assets/files/monkey.svg", +                                       source: "/assets/files/monkey.svg",                                         width: 100%)),                        columns: (1.0fr,                                  1.0fr, 
test/typ/visualize/image-03.out view
@@ -63,5 +63,5 @@                  text(body: [ Stuff ]), -                 image(path: "/assets/files/rhino.png"), +                 image(source: "/assets/files/rhino.png"),                   parbreak() })
test/typ/visualize/image-04.out view
@@ -64,7 +64,7 @@ ]),                   text(body: [A ]),                   box(body: image(height: 1.0cm, -                                 path: "/assets/files/tiger.jpg", +                                 source: "/assets/files/tiger.jpg",                                   width: 80%)),                   text(body: [ B]),                   parbreak() })
test/typ/visualize/image-05.out view
@@ -51,5 +51,5 @@ --- evaluated --- document(body: { text(body: [ ]), -                 image(path: "/assets/files/pattern.svg"), +                 image(source: "/assets/files/pattern.svg"),                   parbreak() })
test/typ/visualize/svg-text-00.out view
@@ -67,6 +67,6 @@ document(body: { text(body: [ ]),                   parbreak(), -                 figure(body: image(path: "/assets/files/diagram.svg"), +                 figure(body: image(source: "/assets/files/diagram.svg"),                          caption: text(body: [A textful diagram])),                   parbreak() })
typst.cabal view
@@ -1,10 +1,10 @@ cabal-version:      2.4 name:               typst-version:            0.7+version:            0.8 synopsis:           Parsing and evaluating typst syntax. description:        A library for parsing and evaluating typst syntax.                     Typst (<https://typst.app>) is a document layout and-                    formatting language. This library targets typst 0.10+                    formatting language. This library targets typst 0.13                     and currently offers only partial support. license:            BSD-3-Clause license-file:       LICENSE@@ -62,7 +62,7 @@      -- other-extensions:     build-depends:    base >= 4.14 && <= 5,-                      typst-symbols >= 0.1.7 && < 0.1.8,+                      typst-symbols >= 0.1.8.1 && < 0.1.9,                       mtl,                       vector,                       parsec,