Skip to content

Commit 3e6ed8e

Browse files
committed
Various fixes.
1 parent a70cc0e commit 3e6ed8e

File tree

2 files changed

+37
-85
lines changed
  • skylighting/bin
  • skylighting-format-typst/src/Skylighting/Format

2 files changed

+37
-85
lines changed

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

+35-83
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import qualified Data.Map as Map
1313
import Data.Text (Text)
1414
import qualified Data.Text as Text
1515
import Skylighting.Types
16-
import Text.Printf
16+
import qualified Data.Map as M
1717
#if !MIN_VERSION_base(4,11,0)
1818
import Data.Semigroup
1919
#endif
@@ -59,87 +59,39 @@ formatTypstBlock opts ls =
5959
-- | Converts a 'Style' to a set of Typst macro definitions,
6060
-- which should be placed in the document's preamble.
6161
styleToTypst :: Style -> Text
62-
styleToTypst f = "TODO"
63-
64-
65-
-- #let Skylighting(body, number: false, start: 1) = body
66-
-- #let EndLine() = raw("\n")
67-
--
68-
-- #let KeywordTok(s) = raw(s)
69-
-- #let DataTypeTok(s) = raw(s)
70-
-- #let DecValTok(s) = raw(s)
71-
-- #let BaseNTok(s) = raw(s)
72-
-- #let FloatTok(s) = raw(s)
73-
-- #let ConstantTok(s) = raw(s)
74-
-- #let CharTok(s) = raw(s)
75-
-- #let SpecialCharTok(s) = raw(s)
76-
-- #let StringTok(s) = raw(s)
77-
-- #let VerbatimStringTok(s) = raw(s)
78-
-- #let SpecialStringTok(s) = raw(s)
79-
-- #let ImportTok(s) = raw(s)
80-
-- #let CommentTok(s) = raw(s)
81-
-- #let DocumentationTok(s) = raw(s)
82-
-- #let AnnotationTok(s) = raw(s)
83-
-- #let CommentVarTok(s) = raw(s)
84-
-- #let OtherTok(s) = raw(s)
85-
-- #let FunctionTok(s) = raw(s)
86-
-- #let VariableTok(s) = raw(s)
87-
-- #let ControlFlowTok(s) = raw(s)
88-
-- #let OperatorTok(s) = raw(s)
89-
-- #let BuiltInTok(s) = raw(s)
90-
-- #let ExtensionTok(s) = raw(s)
91-
-- #let PreprocessorTok(s) = raw(s)
92-
-- #let AttributeTok(s) = raw(s)
93-
-- #let RegionMarkerTok(s) = raw(s)
94-
-- #let InformationTok(s) = raw(s)
95-
-- #let WarningTok(s) = raw(s)
96-
-- #let AlertTok(s) = raw(s)
97-
-- #let ErrorTok(s) = raw(s)
98-
-- #let NormalTok(s) = raw(s)
99-
100-
101-
-- define Skylighting + all the token functions
102-
-- (case backgroundColor f of
103-
-- Nothing -> ["\\newenvironment{Shaded}{}{}"]
104-
-- Just (RGB r g b) -> ["\\usepackage{framed}"
105-
-- ,Text.pack
106-
-- (printf "\\definecolor{shadecolor}{RGB}{%d,%d,%d}" r g b)
107-
-- ,"\\newenvironment{Shaded}{\\begin{snugshade}}{\\end{snugshade}}"])
108-
-- ++ sort (map (macrodef (defaultColor f) (Map.toList (tokenStyles f)))
109-
-- (enumFromTo KeywordTok NormalTok))
110-
111-
-- also define EndLine
112-
-- #raw(\"\\n\")"
62+
styleToTypst f =
63+
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\")"
70+
] <>
71+
sort (map (macrodef (defaultColor f) (Map.toList (tokenStyles f)))
72+
(enumFromTo KeywordTok NormalTok))
11373

11474
macrodef :: Maybe Color -> [(TokenType, TokenStyle)] -> TokenType -> Text
115-
macrodef defaultcol tokstyles tokt = "TODO"
116-
-- "\\newcommand{\\"
117-
-- <> Text.pack (show tokt)
118-
-- <> "}[1]{"
119-
-- <> Text.pack (co . ul . bf . it . bg $ "#1")
120-
-- <> "}"
121-
-- where tokf = case lookup tokt tokstyles of
122-
-- Nothing -> defStyle
123-
-- Just x -> x
124-
-- ul x = if tokenUnderline tokf
125-
-- then "\\underline{" <> x <> "}"
126-
-- else x
127-
-- it x = if tokenItalic tokf
128-
-- then "\\textit{" <> x <> "}"
129-
-- else x
130-
-- bf x = if tokenBold tokf
131-
-- then "\\textbf{" <> x <> "}"
132-
-- else x
133-
-- bcol = fromColor `fmap` tokenBackground tokf
134-
-- :: Maybe (Double, Double, Double)
135-
-- bg x = case bcol of
136-
-- Nothing -> x
137-
-- Just (r, g, b) ->
138-
-- printf "\\colorbox[rgb]{%0.2f,%0.2f,%0.2f}{%s}" r g b x
139-
-- col = fromColor `fmap` (tokenColor tokf `mplus` defaultcol)
140-
-- :: Maybe (Double, Double, Double)
141-
-- co x = case col of
142-
-- Nothing -> x
143-
-- Just (r, g, b) ->
144-
-- printf "\\textcolor[rgb]{%0.2f,%0.2f,%0.2f}{%s}" r g b x
145-
75+
macrodef defaultcol tokstyles' tokt =
76+
"#let " <> Text.pack (show tokt) <> "(s) = " <> (ul . bg . textstyle) ("raw(s)")
77+
where tokstyles = M.fromList tokstyles'
78+
tokf = case M.lookup tokt tokstyles of
79+
Nothing -> defStyle
80+
Just x -> x
81+
ul x = if tokenUnderline tokf
82+
then "underline(" <> x <> ")"
83+
else x
84+
bg x = case tokenBackground tokf of
85+
Nothing -> x
86+
Just _c -> x -- TODO?
87+
textstyle x = "text(" <> bf x <> it x <> co x <> x <> ")"
88+
it x = if tokenItalic tokf
89+
then "style: \"italic\","
90+
else ""
91+
bf x = if tokenBold tokf
92+
then "weight: \"bold\","
93+
else ""
94+
co x = case tokenColor tokf `mplus` defaultcol of
95+
Just c -> "fill: rgb(" <>
96+
Text.pack (show (fromColor c :: String)) <> "),"
97+
Nothing -> ""

skylighting/bin/main.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,10 @@ hlTypst :: Bool -- ^ Fragment
310310
hlTypst frag fname opts sty sourceLines =
311311
if frag
312312
then Text.putStrLn fragment
313-
else Text.putStrLn $ macros <> pageTitle <> fragment
313+
else Text.putStrLn $ macros <> "\n" <> pageTitle <> "\n" <> fragment
314314
where fragment = formatTypstBlock opts sourceLines
315315
macros = styleToTypst sty
316-
pageTitle = "== " <> Text.pack fname <> "\n"
316+
pageTitle = "= " <> Text.pack fname <> "\n"
317317

318318

319319
hlConTeXt :: Bool -- ^ Fragment

0 commit comments

Comments
 (0)