-
Notifications
You must be signed in to change notification settings - Fork 13
/
Pages.hs
75 lines (64 loc) · 3.09 KB
/
Pages.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{-# LANGUAGE OverloadedStrings, RecordWildCards, ViewPatterns #-}
module Pages (fileContent, pageContent, pagePath, writePage, applyPageStyle, Link(..), outputDir, PageStyle(..)) where
import Prelude hiding ((++), (.), writeFile)
import System.Directory (createDirectoryIfMissing)
import Control.Monad (when)
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LazyText
import qualified Data.Text.Lazy.Builder as TextBuilder
import Util ((++), (.), Text, writeFile)
outputDir :: FilePath
outputDir = "14882/"
data PageStyle = Bare | WithExtension | InSubdir
deriving (Eq, Read)
fileContent :: TextBuilder.Builder -> TextBuilder.Builder -> TextBuilder.Builder -> TextBuilder.Builder -> TextBuilder.Builder
fileContent pathHome title extraHead body =
"<!DOCTYPE html>" ++
"<html lang='en'>" ++
"<head>" ++
"<title>" ++ title ++ "</title>" ++
"<meta charset='UTF-8'/>" ++
"<link rel='stylesheet' type='text/css' href='" ++ pathHome ++ "14882.css'/>" ++
"<link rel='stylesheet' type='text/css' href='https://fonts.googleapis.com/css2?family=Noto+Serif'/>" ++
"<link rel='stylesheet' type='text/css' href='https://fonts.googleapis.com/css2?family=Noto+Sans'/>" ++
"<link rel='stylesheet' type='text/css' href='https://fonts.googleapis.com/css2?family=Noto+Sans+Mono'/>" ++
"<link rel='icon' href='icon.png'/>" ++
extraHead ++
"</head>" ++
"<body><div class='wrapper'>" ++ body ++ "</div></body>" ++
"</html>"
data Link = TocToSection | SectionToToc | SectionToSection
deriving Show
doLink :: PageStyle -> Link -> Text -> Text
doLink sfs l = LazyText.toStrict . TextBuilder.toLazyText . go . Text.splitOn (Text.pack (show l) ++ "/")
where
go :: [Text] -> TextBuilder.Builder
go (x : (Text.break (`elem` ("'#" :: String)) -> (a, b)) : z) = TextBuilder.fromText x ++ f (TextBuilder.fromText a) ++ go (b : z)
go [x] = TextBuilder.fromText x
go _ = undefined
f :: TextBuilder.Builder -> TextBuilder.Builder
f = case (sfs, l) of
(Bare, SectionToToc) -> ("./#" ++)
(Bare, TocToSection) -> id
(Bare, SectionToSection) -> id
(InSubdir, SectionToToc) -> ("../#" ++)
(InSubdir, TocToSection) -> (++ "/")
(InSubdir, SectionToSection) -> ("../" ++)
(WithExtension, SectionToToc) -> ("index.html#" ++)
(WithExtension, TocToSection) -> (++ ".html")
(WithExtension, SectionToSection) -> (++ ".html")
applyPageStyle :: PageStyle -> Text -> Text
applyPageStyle sfs =
doLink sfs SectionToSection
. doLink sfs SectionToToc
. doLink sfs TocToSection
pagePath :: FilePath -> PageStyle -> String
pagePath n Bare = outputDir ++ n
pagePath n WithExtension = outputDir ++ n ++ ".html"
pagePath n InSubdir = outputDir ++ n ++ "/index.html"
pageContent :: PageStyle -> TextBuilder.Builder -> Text
pageContent sfs content = applyPageStyle sfs $ LazyText.toStrict $ TextBuilder.toLazyText $ content
writePage :: FilePath -> PageStyle -> Text -> IO ()
writePage n sfs content = do
when (sfs == InSubdir) $ createDirectoryIfMissing True (outputDir ++ n)
writeFile (pagePath n sfs) content