@@ -13,7 +13,7 @@ import qualified Data.Map as Map
13
13
import Data.Text (Text )
14
14
import qualified Data.Text as Text
15
15
import Skylighting.Types
16
- import Text.Printf
16
+ import qualified Data.Map as M
17
17
#if !MIN_VERSION_base(4,11,0)
18
18
import Data.Semigroup
19
19
#endif
@@ -59,87 +59,39 @@ formatTypstBlock opts ls =
59
59
-- | Converts a 'Style' to a set of Typst macro definitions,
60
60
-- which should be placed in the document's preamble.
61
61
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 ))
113
73
114
74
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 -> " "
0 commit comments