3 | import Language.Reflection
10 | %language ElabReflection
13 | convert : Decoration -> String
14 | convert Typ = "IdrisType"
15 | convert Function = "IdrisFunction"
16 | convert Data = "IdrisData"
17 | convert Keyword = "IdrisKeyword"
18 | convert Bound = "IdrisBound"
19 | convert Namespace = "IdrisNamespace"
20 | convert Postulate = "IdrisPostulate"
21 | convert Module = "IdrisModule"
22 | convert Comment = "IdrisComment"
25 | record Category where
26 | constructor MkCategory
32 | constructor MkConfig
33 | font, space : String
47 | (.toString) : Category -> (prefixString : String) ->
48 | {default (length prefixString) prefixLength : Nat} -> String
49 | cat.toString prefixString =
50 | let initPadding = (prefixLength `minus` (length prefixString `minus` 2))
51 | restPadding = prefixLength + 2
53 | \{prefixString}\{replicate initPadding ' ' } = { style = \{show cat.style }
54 | \{indent restPadding " "}, colour = \{show cat.colour}}
58 | (.toString) : Config -> String
60 | { font = \{show conf.font}
61 | , space = \{show conf.space}
62 | \{conf.datacons.toString ", datacons" {prefixLength = length "datacons"}}
63 | \{conf.typecons.toString ", typecons" {prefixLength = length "datacons"}}
64 | \{conf.bound .toString ", bound" {prefixLength = length "datacons"}}
65 | \{conf.function.toString ", function" {prefixLength = length "datacons"}}
66 | \{conf.keyword .toString ", keyword" {prefixLength = length "datacons"}}
67 | \{conf.comment .toString ", comment" {prefixLength = length "datacons"}}
68 | \{conf.hole .toString ", hole" {prefixLength = length "datacons"}}
69 | \{conf.namespce.toString ", namespce" {prefixLength = length "datacons"}}
70 | \{conf.postulte.toString ", postulte" {prefixLength = length "datacons"}}
71 | \{conf.aModule .toString ", aModule" {prefixLength = length "datacons"}}
77 | defaultHTMLConfig : Config
78 | defaultHTMLConfig = MkConfig
79 | { font = #"\ttfamily"#
81 | , datacons = MkCategory
83 | , colour = "darkred"
85 | , typecons = MkCategory
89 | , bound = MkCategory
93 | , function = MkCategory
95 | , colour = "darkgreen"
97 | , keyword = MkCategory
98 | { style = "text-decoration: underline;"
101 | , comment = MkCategory
103 | , colour = "#b22222"
105 | , hole = MkCategory
106 | { style = "font-weight: bold;"
107 | , colour = "yellow"
109 | , namespce = MkCategory
110 | { style = "font-style: italic;"
113 | , postulte = MkCategory
114 | { style = "font-weight: bold;"
117 | , aModule = MkCategory
118 | { style = "font-style: italic;"
124 | defaultLatexConfig : Config
125 | defaultLatexConfig = MkConfig
126 | { font = #"\ttfamily"#
128 | , datacons = MkCategory
130 | , colour = "IndianRed1"
132 | , typecons = MkCategory
134 | , colour = "DeepSkyBlue3"
136 | , bound = MkCategory
138 | , colour = "DarkOrchid3"
140 | , function = MkCategory
142 | , colour = "Chartreuse4"
144 | , keyword = MkCategory
145 | { style = #"\bfseries"#
148 | , comment = MkCategory
149 | { style = #"\itshape"#
150 | , colour = "Ivory3"
152 | , hole = MkCategory
153 | { style = #"\bfseries"#
154 | , colour = "yellow"
156 | , namespce = MkCategory
157 | { style = #"\itshape"#
160 | , postulte = MkCategory
161 | { style = #"\bfseries"#
162 | , colour = "DarkOrchid3"
164 | , aModule = MkCategory
165 | { style = #"\itshape"#
171 | defaultTypstConfig : Config
172 | defaultTypstConfig = MkConfig
173 | { font = #"Liberation Mono"#
175 | , datacons = MkCategory
176 | { style = #", "normal", "medium""#
177 | , colour = "rgb(\"#ff6a6a\")"
179 | , typecons = MkCategory
180 | { style = #", "normal", "medium""#
181 | , colour = "rgb(\"#009acd\")"
183 | , bound = MkCategory
184 | { style = #", "normal", "medium""#
185 | , colour = "rgb(\"#9a32cd\")"
187 | , function = MkCategory
188 | { style = #", "normal", "medium""#
189 | , colour = "rgb(\"#458b00\")"
191 | , keyword = MkCategory
192 | { style = #", "normal", "bold""#
193 | , colour = "color.black"
195 | , comment = MkCategory
196 | { style = #", "italic", "medium""#
197 | , colour = "rgb(\"#cdcdc1\")"
199 | , hole = MkCategory
200 | { style = #", "normal", "bold""#
201 | , colour = "color.yellow"
203 | , namespce = MkCategory
204 | { style = #", "italic", "medium""#
205 | , colour = "color.black"
207 | , postulte = MkCategory
208 | { style = #", "normal", "bold""#
209 | , colour = "rgb(\"#9a32cd\")"
211 | , aModule = MkCategory
212 | { style = #", "italic", "medium""#
213 | , colour = "color.black"
216 | %runElab (deriveFromDhall Record `{ Category
})
217 | %runElab (deriveFromDhall Record `{ Config
})
220 | data Backend = LaTeX | Typst | HTML | Markdown | Literate
223 | defaultConfig : Backend -> Config
224 | defaultConfig LaTeX = defaultLatexConfig
225 | defaultConfig Typst = defaultTypstConfig
226 | defaultConfig HTML = defaultHTMLConfig
227 | defaultConfig Markdown = defaultHTMLConfig
228 | defaultConfig Literate = defaultLatexConfig
231 | getConfiguration : Backend -> (configFile : Maybe String) -> IO Config
232 | getConfiguration backend Nothing = pure $
defaultConfig backend
233 | getConfiguration backend (Just filename) = do
234 | Right fileContent <- readFile filename
235 | | Left err => putStrLn "Error reading file: \{show err}"
237 | Right config <- liftIOEither (deriveFromDhallString {ty = Config} fileContent)
238 | | Left err => do putStrLn """
239 | Error while parsing configuration file \{filename}:
241 | Using default configuration instead.
243 | pure $
defaultConfig backend
249 | record Driver where
250 | constructor MkDriver
251 | line : ((width, lineNumber : Nat) -> String, String)
252 | escape : Char -> List Char
253 | annotate : Maybe Decoration -> String -> String
254 | standalone : (String, String)
255 | inlineMacro : (String -> String, String)
256 | blockMacro : (String -> String, String)
259 | initExec : (backend : Backend) -> (moutput : Maybe String) -> IO ()
260 | initExec backend moutput = do
261 | Right file <- maybe (pure $
Right stdout) (flip openFile WriteTruncate) moutput
262 | | Left err => do putStrLn """
263 | Error while opening configuration file \{fromMaybe "stdout" moutput}:
267 | Right () <- fPutStrLn file $
(defaultConfig backend).toString
268 | | Left err => do putStrLn """
269 | Error while writing preamble file \{fromMaybe "stdout" moutput}: