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"#
169 | %runElab (deriveFromDhall Record `{ Category
})
170 | %runElab (deriveFromDhall Record `{ Config
})
173 | data Backend = LaTeX | HTML | Markdown | Literate
176 | defaultConfig : Backend -> Config
177 | defaultConfig LaTeX = defaultLatexConfig
178 | defaultConfig HTML = defaultHTMLConfig
179 | defaultConfig Markdown = defaultHTMLConfig
180 | defaultConfig Literate = defaultLatexConfig
183 | getConfiguration : Backend -> (configFile : Maybe String) -> IO Config
184 | getConfiguration backend Nothing = pure $
defaultConfig backend
185 | getConfiguration backend (Just filename) = do
186 | Right fileContent <- readFile filename
187 | | Left err => putStrLn "Error reading file: \{show err}"
189 | Right config <- liftIOEither (deriveFromDhallString {ty = Config} fileContent)
190 | | Left err => do putStrLn """
191 | Error while parsing configuration file \{filename}:
193 | Using default configuration instead.
195 | pure $
defaultConfig backend
201 | record Driver where
202 | constructor MkDriver
203 | line : ((width, lineNumber : Nat) -> String, String)
204 | escape : Char -> List Char
205 | annotate : Maybe Decoration -> String -> String
206 | standalone : (String, String)
207 | inlineMacro : (String -> String, String)
208 | blockMacro : (String -> String, String)
211 | initExec : (backend : Backend) -> (moutput : Maybe String) -> IO ()
212 | initExec backend moutput = do
213 | Right file <- maybe (pure $
Right stdout) (flip openFile WriteTruncate) moutput
214 | | Left err => do putStrLn """
215 | Error while opening configuration file \{fromMaybe "stdout" moutput}:
219 | Right () <- fPutStrLn file $
(defaultConfig backend).toString
220 | | Left err => do putStrLn """
221 | Error while writing preamble file \{fromMaybe "stdout" moutput}: