Skip to content

Commit

Permalink
More improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgm committed Jan 7, 2025
1 parent 3e6ed8e commit 33785d1
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions skylighting-format-typst/src/Skylighting/Format/Typst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,32 @@ formatTypstBlock :: FormatOptions -> [SourceLine] -> Text
formatTypstBlock opts ls =
"#Skylighting(" <>
(if numberLines opts
then "number: true, start: " <> Text.pack (show (startNumber opts)) <> ")"
else ")") <>
"[" <> formatTypstInline opts ls <> "];"
then "number: true, start: " <> Text.pack (show (startNumber opts)) <> ", "
else "") <>
"(" <> -- an array
Text.intercalate "\n" (map (\ln -> "[" <> formatTypstInline opts [ln] <> "],") ls)
<> "));"

-- | Converts a 'Style' to a set of Typst macro definitions,
-- which should be placed in the document's preamble.
styleToTypst :: Style -> Text
styleToTypst f =
Text.unlines $
[ "#let Skylighting(body, number: false, start: 1) = block(" <>
(case backgroundColor f of
Nothing -> ""
Just c -> "fill: rgb(" <> Text.pack (show (fromColor c :: String)) <> "), ")
<> "body)"
, "#let EndLine() = raw(\"\\n\")"
[ "#let EndLine() = raw(\"\\n\")"
, "#let Skylighting(fill: none, number: false, start: 1, sourcelines) = {"
, " let blocks = []"
, " let lnum = start - 1"
, " for ln in sourcelines {"
, " if number { lnum = lnum + 1; blocks = blocks + box(width: 2em, [ #lnum ]) }"
, " blocks = blocks + ln + EndLine()"
, " }"
, " let bgcolor = " <> case backgroundColor f of
Nothing -> "none"
Just c -> "rgb(" <>
Text.pack (show (fromColor c :: String)) <>
")"
, " block(fill: bgcolor, blocks)"
, "}"
] <>
sort (map (macrodef (defaultColor f) (Map.toList (tokenStyles f)))
(enumFromTo KeywordTok NormalTok))
Expand Down

0 comments on commit 33785d1

Please sign in to comment.