Skip to content

Commit 33785d1

Browse files
committed
More improvements.
1 parent 3e6ed8e commit 33785d1

File tree

1 file changed

+20
-9
lines changed
  • skylighting-format-typst/src/Skylighting/Format

1 file changed

+20
-9
lines changed

skylighting-format-typst/src/Skylighting/Format/Typst.hs

+20-9
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,32 @@ formatTypstBlock :: FormatOptions -> [SourceLine] -> Text
5252
formatTypstBlock opts ls =
5353
"#Skylighting(" <>
5454
(if numberLines opts
55-
then "number: true, start: " <> Text.pack (show (startNumber opts)) <> ")"
56-
else ")") <>
57-
"[" <> formatTypstInline opts ls <> "];"
55+
then "number: true, start: " <> Text.pack (show (startNumber opts)) <> ", "
56+
else "") <>
57+
"(" <> -- an array
58+
Text.intercalate "\n" (map (\ln -> "[" <> formatTypstInline opts [ln] <> "],") ls)
59+
<> "));"
5860

5961
-- | Converts a 'Style' to a set of Typst macro definitions,
6062
-- which should be placed in the document's preamble.
6163
styleToTypst :: Style -> Text
6264
styleToTypst f =
6365
Text.unlines $
64-
[ "#let Skylighting(body, number: false, start: 1) = block(" <>
65-
(case backgroundColor f of
66-
Nothing -> ""
67-
Just c -> "fill: rgb(" <> Text.pack (show (fromColor c :: String)) <> "), ")
68-
<> "body)"
69-
, "#let EndLine() = raw(\"\\n\")"
66+
[ "#let EndLine() = raw(\"\\n\")"
67+
, "#let Skylighting(fill: none, number: false, start: 1, sourcelines) = {"
68+
, " let blocks = []"
69+
, " let lnum = start - 1"
70+
, " for ln in sourcelines {"
71+
, " if number { lnum = lnum + 1; blocks = blocks + box(width: 2em, [ #lnum ]) }"
72+
, " blocks = blocks + ln + EndLine()"
73+
, " }"
74+
, " let bgcolor = " <> case backgroundColor f of
75+
Nothing -> "none"
76+
Just c -> "rgb(" <>
77+
Text.pack (show (fromColor c :: String)) <>
78+
")"
79+
, " block(fill: bgcolor, blocks)"
80+
, "}"
7081
] <>
7182
sort (map (macrodef (defaultColor f) (Map.toList (tokenStyles f)))
7283
(enumFromTo KeywordTok NormalTok))

0 commit comments

Comments
 (0)