Skip to content

Commit

Permalink
Generate navigation from documentation files tree (#74)
Browse files Browse the repository at this point in the history
* Generate navigation from documentation files tree
  • Loading branch information
KovalevDima authored Dec 4, 2024
1 parent 29039b7 commit 3c182a2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 60 deletions.
2 changes: 1 addition & 1 deletion QA/QA.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ executable documentation-compiler
, filepath
, time
, pandoc
ghc-options: -threaded
ghc-options: -threaded -Wall

-- * Testing

Expand Down
8 changes: 0 additions & 8 deletions QA/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
# Documentation

Want to contribute to library?

- [development](/development)
- [usage](/usage)
- [testing](/testing)

# Design

ClickHaskell was designed to **avoid boilerplate** code\
Expand Down
10 changes: 0 additions & 10 deletions QA/development/README.md

This file was deleted.

58 changes: 27 additions & 31 deletions QA/documentation-compiler/documentation-compiler.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

import Data.List (stripPrefix)
import Data.Maybe (fromMaybe)
import GHC.IO.Encoding as Encoding (setLocaleEncoding, utf8)
import System.FilePath ( (</>), takeDirectory )
import System.FilePath ((</>), takeFileName, replaceExtension, takeBaseName, normalise, dropFileName, replaceFileName)
import Hakyll

configuration :: Configuration
Expand All @@ -15,35 +12,34 @@ main = do
Encoding.setLocaleEncoding Encoding.utf8

hakyllWith configuration $ do
match "./README.md" $ do
route (constRoute "index.html")
compile $
pandocCompiler
>>= loadAndApplyTemplate "documentation-compiler/tmpl-code.html" defaultContext
>>= loadAndApplyTemplate "documentation-compiler/tmpl-main.html" defaultContext
>>= relativizeUrls

let indexFilePattern = "**/README.md" .||. "**/README.lhs"
nonIndexFilePattern = "**.lhs" .||. "**.md"

match
(nonIndexFilePattern .&&. complement indexFilePattern)
(do
route (setExtension "html")
compile $
pandocCompiler
>>= loadAndApplyTemplate "documentation-compiler/tmpl-code.html" defaultContext
>>= loadAndApplyTemplate "documentation-compiler/tmpl-main.html" defaultContext
>>= relativizeUrls
)

match indexFilePattern
$ do
route $ customRoute ((</> "index.html") . takeDirectory . toFilePath)
compile $
match "documentation-compiler/tmpl-*" $ compile templateCompiler

let pattern = ("**.lhs" .||. "**.md")

match pattern $ do
let
filePathToUrlPath filePath =
case takeBaseName filePath of
"README" -> replaceFileName filePath "index.html"
_ -> replaceExtension filePath "html"

trailIndexHtml path
| takeFileName path == "index.html" = dropFileName path
| otherwise = path

route (customRoute $ filePathToUrlPath . toFilePath)
compile $ do
navigation <-
traverse (makeItem . MkNavigationLink . normalise . ("/" </>) . trailIndexHtml . filePathToUrlPath . toFilePath)
=<< getMatches pattern

pandocCompiler
>>= loadAndApplyTemplate "documentation-compiler/tmpl-code.html" defaultContext
>>= loadAndApplyTemplate "documentation-compiler/tmpl-main.html" defaultContext
>>= loadAndApplyTemplate "documentation-compiler/tmpl-main.html" (defaultContext <> listField "nav" mkLinkCtx (pure navigation))
>>= relativizeUrls

match "documentation-compiler/tmpl-*" $ compile templateCompiler
data NavigationLink = MkNavigationLink { link :: FilePath }

mkLinkCtx :: Context NavigationLink
mkLinkCtx = mconcat [field "link" (pure . link . itemBody)]
11 changes: 9 additions & 2 deletions QA/documentation-compiler/tmpl-main.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<body>
<style type="text/css">
nav {
header {
padding: 4px 8px;
align-items: center;
display: flex;
Expand All @@ -42,7 +42,7 @@
};
ol li p {margin: 0;}
</style>
<nav>
<header>
<a href="https://github.com/KovalevDima/ClickHaskell">
<svg width="36px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128">
<g fill="rgb(240, 246, 252)">
Expand All @@ -54,6 +54,13 @@
<div>
<a style="font-size:24px; color: rgb(240, 246, 252); text-decoration: none;" href="/">ClickHaskell</a>
</div>
</header>
<nav>
<ul>
$for(nav)$
<li><a href="$link$">$link$</a></li>
$endfor$
</ul>
</nav>
<main role="main">
$body$
Expand Down
8 changes: 0 additions & 8 deletions QA/usage/README.md

This file was deleted.

0 comments on commit 3c182a2

Please sign in to comment.