Skip to content

Commit ac44cb5

Browse files
committed
Generate only md files
1 parent 8b09a42 commit ac44cb5

File tree

1 file changed

+26
-79
lines changed

1 file changed

+26
-79
lines changed

build-tools/agda/src/Main.hs

Lines changed: 26 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ main = runAgda [Backend flsBackend']
8686
-- | Options for HTML generation
8787
data FlsOpts = FlsOpts
8888
{ flsOptEnabled :: Bool
89-
, flsOptHtmlDir :: FilePath
89+
, flsOptMdDir :: FilePath
9090
, flsOptMainOnly :: Bool
9191
}
9292
deriving (Eq, Generic)
@@ -133,41 +133,41 @@ initialFlsOpts :: FlsOpts
133133
initialFlsOpts =
134134
FlsOpts
135135
{ flsOptEnabled = False
136-
, flsOptHtmlDir = defaultHTMLDir
136+
, flsOptMdDir = defaultMdDir
137137
, flsOptMainOnly = False
138138
}
139139

140140
-- | The default output directory for HTML.
141-
defaultHTMLDir :: FilePath
142-
defaultHTMLDir = "html"
141+
defaultMdDir :: FilePath
142+
defaultMdDir = "md"
143143

144144
flsOpts :: [OptDescr (Flag FlsOpts)]
145145
flsOpts =
146146
[ Option
147147
[]
148148
["fls"]
149149
(NoArg flsOpt)
150-
"generate HTML files with highlighted source code"
150+
"generate Markdown files with highlighted source code"
151151
, Option
152152
[]
153153
["fls-html-dir"]
154-
(ReqArg flsHtmlDirOpt "DIR")
155-
( "directory in which HTML files are placed (default: "
156-
++ defaultHTMLDir
154+
(ReqArg flsMdDirOpt "DIR")
155+
( "directory in which Markdown files are placed (default: "
156+
++ defaultMdDir
157157
++ ")"
158158
)
159159
, Option
160160
[]
161161
["fls-main-only"]
162162
(NoArg flsMainOnly)
163-
"generate HTML for the main file ONLY"
163+
"generate Markdown for the main file ONLY"
164164
]
165165

166166
flsOpt :: Flag FlsOpts
167167
flsOpt o = return $ o{flsOptEnabled = True}
168168

169-
flsHtmlDirOpt :: FilePath -> Flag FlsOpts
170-
flsHtmlDirOpt d o = return $ o{flsOptHtmlDir = d}
169+
flsMdDirOpt :: FilePath -> Flag FlsOpts
170+
flsMdDirOpt d o = return $ o{flsOptMdDir = d}
171171

172172
flsMainOnly :: Flag FlsOpts
173173
flsMainOnly o = return $ o{flsOptMainOnly = True}
@@ -183,7 +183,7 @@ preCompileFls opts = runLogHtmlWithMonadDebug $ do
183183
unless (flsOptMainOnly opts) $ do
184184
logHtml $
185185
unlines
186-
[ "Warning: HTML is currently generated for ALL files which can be"
186+
[ "Warning: Markdown is currently generated for ALL files which can be"
187187
, "reached from the given module, including library files."
188188
]
189189
prepareCommonDestinationAssets opts
@@ -218,8 +218,8 @@ postModuleFls ::
218218
postModuleFls env menv isMain _modName _defs = do
219219
when (isMain == IsMain || (not . flsOptMainOnly . flsCompileEnvOpts $ env)) $ do
220220
let generatePage = defaultPageGen . flsCompileEnvOpts . flsModEnvCompileEnv $ menv
221-
htmlSrc <- srcFileOfInterface (flsModEnvName menv) <$> curIF
222-
runLogHtmlWithMonadDebug $ generatePage htmlSrc
221+
src <- srcFileOfInterface (flsModEnvName menv) <$> curIF
222+
runLogHtmlWithMonadDebug $ generatePage src
223223
return FlsModule
224224

225225
postCompileFls ::
@@ -230,19 +230,6 @@ postCompileFls ::
230230
m ()
231231
postCompileFls _cenv _isMain _modulesByName = pure ()
232232

233-
-- | The name of the default CSS file.
234-
defaultCSSFile :: FilePath
235-
defaultCSSFile = "Agda.css"
236-
237-
-- | The name of the default Agda KaTeX JS file.
238-
defaultAgdaKaTeXJsFile :: FilePath
239-
defaultAgdaKaTeXJsFile = "AgdaKaTeX.js"
240-
241-
-- | Determine the generated file extension
242-
highlightedFileExt :: FileType -> String
243-
highlightedFileExt AgdaFileType = "html"
244-
highlightedFileExt MdFileType = "md"
245-
246233
-- | Internal type bundling the information related to a module source file
247234
data FlsInputSourceFile = FlsInputSourceFile
248235
{ _srcFileModuleName :: TopLevelModuleName
@@ -259,92 +246,55 @@ srcFileOfInterface ::
259246
TopLevelModuleName -> TCM.Interface -> FlsInputSourceFile
260247
srcFileOfInterface m i = FlsInputSourceFile m (TCM.iFileType i) (TCM.iSource i) (TCM.iHighlighting i)
261248

262-
-- | Logging during HTML generation
263-
type HtmlLogMessage = String
264-
265-
type HtmlLogAction m = HtmlLogMessage -> m ()
266-
267249
renderSourceFile :: FlsOpts -> FlsInputSourceFile -> Text
268250
renderSourceFile opts = renderSourcePage
269251
where
270252
renderSourcePage (FlsInputSourceFile moduleName fileType sourceCode hinfo) =
271-
page fileType moduleName pageContents
253+
page moduleName pageContents
272254
where
273255
tokens = tokenStream sourceCode hinfo
274256
pageContents = code fileType tokens
275257

276258
defaultPageGen :: (MonadIO m, MonadLogHtml m) => FlsOpts -> FlsInputSourceFile -> m ()
277259
defaultPageGen opts srcFile@(FlsInputSourceFile moduleName ft _ _) = do
278-
logHtml $ render $ "Generating HTML for" <+> pretty moduleName <+> (parens (pretty target) <> ".")
279-
writeRenderedHtml html target
260+
logHtml $ render $ "Generating Markdown for" <+> pretty moduleName <+> (parens (pretty target) <> ".")
261+
writeRenderedMd md target
280262
where
281-
ext = highlightedFileExt ft
282-
target = flsOptHtmlDir opts </> modToFile moduleName ext
283-
html = renderSourceFile opts srcFile
263+
target = flsOptMdDir opts </> modToFile moduleName "md"
264+
md = renderSourceFile opts srcFile
284265

285266
prepareCommonDestinationAssets :: (MonadIO m) => FlsOpts -> m ()
286267
prepareCommonDestinationAssets options = liftIO $ do
287-
-- There is a default directory given by 'defaultHTMLDir'
288-
let htmlDir = flsOptHtmlDir options
289-
createDirectoryIfMissing True htmlDir
268+
-- There is a default directory given by 'defaultMdDir'
269+
createDirectoryIfMissing True (flsOptMdDir options)
290270

291-
-- | Converts module names to the corresponding HTML file names.
271+
-- | Converts module names to the corresponding md file names.
292272
modToFile :: TopLevelModuleName -> String -> FilePath
293273
modToFile m ext = Network.URI.Encode.encode $ render (pretty m) <.> ext
294274

295275
-- | Generates a highlighted, hyperlinked version of the given module.
296-
writeRenderedHtml ::
276+
writeRenderedMd ::
297277
(MonadIO m) =>
298278
-- | Rendered page
299279
Text ->
300280
-- | Output path.
301281
FilePath ->
302282
m ()
303-
writeRenderedHtml html target = liftIO $ UTF8.writeTextToFile target html
283+
writeRenderedMd html target = liftIO $ UTF8.writeTextToFile target html
304284

305285
-- | Attach multiple Attributes
306286
(!!) :: Html -> [Attribute] -> Html
307287
h !! as = h ! mconcat as
308288

309289
-- | Constructs the web page, including headers.
310290
page ::
311-
-- | Whether to reserve literate md
312-
FileType ->
313291
-- | Module to be highlighted.
314292
TopLevelModuleName ->
315293
Html ->
316294
Text
317295
page
318-
fileType
319296
modName
320-
pageContent =
321-
renderHtml $
322-
case fileType of
323-
AgdaFileType -> Html5.docTypeHtml $ hdr <> rest
324-
MdFileType -> pageContent
325-
where
326-
hdr =
327-
Html5.head $
328-
mconcat
329-
[ Html5.meta !! [Attr.charset "utf-8"]
330-
, Html5.title (toHtml . render $ pretty modName)
331-
, Html5.link
332-
!! [ Attr.rel "stylesheet"
333-
, Attr.href $ stringValue "https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/katex.min.css"
334-
]
335-
, Html5.link
336-
!! [ Attr.rel "stylesheet"
337-
, Attr.href $ stringValue defaultCSSFile
338-
]
339-
, Html5.script mempty !! [Attr.src "https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/katex.min.js"]
340-
, Html5.script mempty !! [Attr.src "https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/contrib/auto-render.min.js"]
341-
, Html5.script mempty
342-
!! [ Attr.src $ stringValue defaultAgdaKaTeXJsFile
343-
, Attr.defer mempty
344-
]
345-
]
346-
347-
rest = Html5.body $ (Html5.pre ! Attr.class_ "Agda") pageContent
297+
pageContent = renderHtml pageContent
348298

349299
-- | Position, Contents, Infomation
350300
type TokenInfo =
@@ -377,10 +327,7 @@ code ::
377327
[TokenInfo] ->
378328
Html
379329
code fileType =
380-
mconcat
381-
. case fileType of
382-
MdFileType -> map mkMd . splitByMarkup
383-
AgdaFileType -> mkCustomHtml
330+
mconcat . map mkMd . splitByMarkup
384331
where
385332
mkCustomHtml :: [TokenInfo] -> [Html]
386333
mkCustomHtml = goHtml

0 commit comments

Comments
 (0)