summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2023-03-26 13:37:08 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2023-03-26 21:32:31 -0700
commit4780b4760d26e39597ab6868b0ddca1e64b609f6 (patch)
tree40e2b1cafad0fb2d2e5c4b1f69459a52a3066fce
parentcd6b3bde622c93eea763e981692b4bc53dd5e5df (diff)
More Typst writer improvements.
+ Update manual with variables for Typst. + Split `lang` metadata into separate `lang` and `region` for Typst. + Fix label positioning in Divs. + Fix text customizations (lang, region, font, size) so they actually work. + Update tests and man page.
-rw-r--r--MANUAL.txt21
-rw-r--r--data/templates/default.typst6
-rw-r--r--data/templates/template.typst22
-rw-r--r--man/pandoc.123
-rw-r--r--src/Text/Pandoc/Writers/Typst.hs14
-rw-r--r--test/writer.typst22
6 files changed, 75 insertions, 33 deletions
diff --git a/MANUAL.txt b/MANUAL.txt
index 13c72c9f3..78655e252 100644
--- a/MANUAL.txt
+++ b/MANUAL.txt
@@ -3145,6 +3145,27 @@ The `--css` option also affects the output.
`section`
: section number in man pages
+### Variables for Typst
+
+`margin`
+: A dictionary with the fields defined in the Typst documentation:
+ `x`, `y`, `top`, `bottom`, `left`, `right`.
+
+`papersize`
+: Paper size: `a4`, `us-letter`, etc.
+
+`mainfont`
+: Name of system font to use for the main font.
+
+`fontsize`
+: Font size (e.g., `12pt`).
+
+`section-numbering`
+: Schema to use for numbering sections, e.g. `1.A.1`.
+
+`columns`
+: Number of columns for body text.
+
### Variables for ms
`fontfamily`
diff --git a/data/templates/default.typst b/data/templates/default.typst
index 11e7b1211..43674bc32 100644
--- a/data/templates/default.typst
+++ b/data/templates/default.typst
@@ -28,6 +28,12 @@ $endif$
$if(date)$
date: [$date$],
$endif$
+$if(lang)$
+ lang: "$lang$",
+$endif$
+$if(region)$
+ region: "$region$",
+$endif$
$if(abstract)$
abstract: [$abstract$],
$endif$
diff --git a/data/templates/template.typst b/data/templates/template.typst
index 29edca969..009968ba3 100644
--- a/data/templates/template.typst
+++ b/data/templates/template.typst
@@ -6,8 +6,9 @@
cols: 1,
margin: (x: 1.25in, y: 1.25in),
paper: "us-letter",
- lang: none,
- font: none,
+ lang: "en",
+ region: "US",
+ font: (),
fontsize: 11pt,
sectionnumbering: none,
doc,
@@ -18,18 +19,11 @@
numbering: "1",
)
set par(justify: true)
- if lang != none {
- set text(lang: lang)
- }
- if font != none {
- set text(font: font)
- }
- if fontsize != none {
- set text(size: fontsize)
- }
- if sectionnumbering != none {
- set heading(numbering: sectionnumbering)
- }
+ set text(lang: lang,
+ region: region,
+ font: font,
+ size: fontsize)
+ set heading(numbering: sectionnumbering)
if title != none {
align(center)[#block(inset: 2em)[
diff --git a/man/pandoc.1 b/man/pandoc.1
index bb7f6ccf3..ab2afb7b1 100644
--- a/man/pandoc.1
+++ b/man/pandoc.1
@@ -14,7 +14,7 @@
. ftr VB CB
. ftr VBI CBI
.\}
-.TH "Pandoc User\[cq]s Guide" "" "March 26, 2023" "pandoc 3.1.1" ""
+.TH "Pandoc User\[cq]s Guide" "" "March 26, 2023" "pandoc 3.1.2" ""
.hy
.SH NAME
pandoc - general markup converter
@@ -3235,6 +3235,27 @@ if \f[V]true\f[R] (the default), hyphenation will be used
.TP
\f[V]section\f[R]
section number in man pages
+.SS Variables for Typst
+.TP
+\f[V]margin\f[R]
+A dictionary with the fields defined in the Typst documentation:
+\f[V]x\f[R], \f[V]y\f[R], \f[V]top\f[R], \f[V]bottom\f[R],
+\f[V]left\f[R], \f[V]right\f[R].
+.TP
+\f[V]papersize\f[R]
+Paper size: \f[V]a4\f[R], \f[V]us-letter\f[R], etc.
+.TP
+\f[V]mainfont\f[R]
+Name of system font to use for the main font.
+.TP
+\f[V]fontsize\f[R]
+Font size (e.g., \f[V]12pt\f[R]).
+.TP
+\f[V]section-numbering\f[R]
+Schema to use for numbering sections, e.g.\ \f[V]1.A.1\f[R].
+.TP
+\f[V]columns\f[R]
+Number of columns for body text.
.SS Variables for ms
.TP
\f[V]fontfamily\f[R]
diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs
index 12364ce37..2663a4a37 100644
--- a/src/Text/Pandoc/Writers/Typst.hs
+++ b/src/Text/Pandoc/Writers/Typst.hs
@@ -28,7 +28,7 @@ import Data.Text (Text)
import Data.List (intercalate, intersperse)
import qualified Data.Text as T
import Control.Monad.State ( StateT, evalStateT, gets, modify )
-import Text.Pandoc.Writers.Shared ( metaToContext, defField, setField,
+import Text.Pandoc.Writers.Shared ( metaToContext, defField, resetField,
toLegacyTable, lookupMetaString )
import Text.Pandoc.Shared (isTightList, orderedListMarkers)
import Text.Pandoc.Writers.Math (convertMath)
@@ -36,6 +36,7 @@ import qualified Text.TeXMath as TM
import Text.DocLayout
import Text.DocTemplates (renderTemplate)
import Text.Pandoc.Extensions (Extension(..))
+import Text.Collate.Lang (Lang(..), parseLang)
-- | Convert Pandoc to Typst.
writeTypst :: PandocMonad m => WriterOptions -> Pandoc -> m Text
@@ -73,7 +74,12 @@ pandocToTypst options (Pandoc meta blocks) = do
$ defField "toc" (writerTableOfContents options)
$ (case lookupMetaString "lang" meta of
"" -> id
- lang -> setField "lang" $ T.takeWhile (/='-') lang)
+ lang ->
+ case parseLang lang of
+ Left _ -> id
+ Right l ->
+ resetField "lang" (langLanguage l) .
+ maybe id (resetField "region") (langRegion l))
$ (if writerNumberSections options
then defField "numbering" ("1.1.1.1.1" :: Text)
else id)
@@ -184,11 +190,11 @@ blockToTypst block =
let lab = toLabel ident
return $ "#figure(" <> nest 2 (brackets contents <> "," <> cr <>
("caption: [" $$ nest 2 caption $$ "]"))
- <> cr <> ")" <> lab <> blankline
+ $$ ")" $$ lab $$ blankline
Div (ident,_,_) blocks -> do
let lab = toLabel ident
contents <- blocksToTypst blocks
- return $ lab <> contents
+ return $ lab $$ contents
defListItemToTypst :: PandocMonad m => ([Inline], [[Block]]) -> TW m (Doc Text)
defListItemToTypst (term, defns) = do
diff --git a/test/writer.typst b/test/writer.typst
index 130c53f59..18a618163 100644
--- a/test/writer.typst
+++ b/test/writer.typst
@@ -24,8 +24,9 @@
cols: 1,
margin: (x: 1.25in, y: 1.25in),
paper: "us-letter",
- lang: none,
- font: none,
+ lang: "en",
+ region: "US",
+ font: (),
fontsize: 11pt,
sectionnumbering: none,
doc,
@@ -36,18 +37,11 @@
numbering: "1",
)
set par(justify: true)
- if lang != none {
- set text(lang: lang)
- }
- if font != none {
- set text(font: font)
- }
- if fontsize != none {
- set text(size: fontsize)
- }
- if sectionnumbering != none {
- set heading(numbering: sectionnumbering)
- }
+ set text(lang: lang,
+ region: region,
+ font: font,
+ size: fontsize)
+ set heading(numbering: sectionnumbering)
if title != none {
align(center)[#block(inset: 2em)[