generated from MaxGripe/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 31
/
SkunkHtml.fs
118 lines (98 loc) · 4.16 KB
/
SkunkHtml.fs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module SkunkHtml
open SkunkUtils
open System.IO
open FSharp.Formatting.Markdown
let generateFinalHtml (head: string) (header: string) (footer: string) (content: string) (script: string) =
$"""
<!DOCTYPE html>
<html lang="en">
<head>
{head}
</head>
<body>
<header>
{header}
</header>
<main>
{content}
</main>
<hr />
<footer>
{footer}
</footer>
<script>
{script}
</script>
</body>
</html>
"""
let head (titleSuffix: string) =
let headTemplate =
Path.Combine(Config.htmlDir, "head.html")
|> Disk.readFile
let titleTemplate =
Path.Combine(Config.htmlDir, "title.html")
|> Disk.readFile
headTemplate.Replace("{{title.html content}}", titleTemplate + titleSuffix)
let isArticle (file: string) =
System.Char.IsDigit(Path.GetFileName(file).[0])
let highlightingScript =
Path.Combine(Config.htmlDir, "script_syntax_highlighting.html")
|> Disk.readFile
let extractTitleFromMarkdownFile (markdownFilePath: string) =
File.ReadAllLines(markdownFilePath)
|> Array.tryFind _.StartsWith("# ")
|> Option.defaultValue "# No Title"
|> _.TrimStart('#').Trim()
let createPage (header: string) (footer: string) (markdownFilePath: string) =
let title = extractTitleFromMarkdownFile(markdownFilePath)
let fileName = Url.toUrlFriendly title
let outputHtmlFilePath = Path.Combine(Config.outputDir, fileName + ".html")
let markdownContent = File.ReadAllText(markdownFilePath)
let htmlContent =
match isArticle markdownFilePath with
| false -> Markdown.ToHtml(markdownContent)
| true ->
let date = Path.GetFileNameWithoutExtension(markdownFilePath)
let publicationDate =
$"""<p class="publication-date">Published on <time datetime="{date}">{date}</time></p>"""
let giscusScript =
Path.Combine(Config.htmlDir, "script_giscus.html")
|> Disk.readFile
let mainHtmlContent = Markdown.ToHtml(
markdownContent
+ "\n\n"
+ publicationDate
+ "\n\n"
)
mainHtmlContent + giscusScript
let finalHtmlContent =
generateFinalHtml (head (" - " + title)) header footer htmlContent highlightingScript
printfn $"Processing {Path.GetFileName markdownFilePath} ->"
Disk.writeFile outputHtmlFilePath finalHtmlContent
let createIndexPage (header: string) (footer: string) (listOfAllBlogArticles: (string * string * string) list) =
let frontPageMarkdownFilePath = Path.Combine(Config.markdownDir, Config.frontPageMarkdownFileName)
let frontPageContentHtml =
if File.Exists(frontPageMarkdownFilePath) then
printfn $"Processing {Path.GetFileName frontPageMarkdownFilePath} ->"
Markdown.ToHtml(File.ReadAllText(frontPageMarkdownFilePath))
else
printfn $"Warning! File {Config.frontPageMarkdownFileName} does not exist! The main page will only contain blog entries, without a welcome message"
""
let listOfAllBlogArticlesContentHtml =
listOfAllBlogArticles
|> List.map (fun (date, title, link) -> $"""<li>{date}: <a href="{link}">{title}</a></li>""")
|> String.concat "\n"
let content =
$"""
{frontPageContentHtml}
<section class="publications">
<h1>blog entries</h1>
<ul>
{listOfAllBlogArticlesContentHtml}
</ul>
</section>
"""
let frontPageHtmlContent = generateFinalHtml (head "") header footer content highlightingScript
let indexHtmlFilePath = Path.Combine(Config.outputDir, "index.html")
Disk.writeFile indexHtmlFilePath frontPageHtmlContent