forked from li3zhen1/Grape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocPostprocess.swift
83 lines (74 loc) · 3.71 KB
/
DocPostprocess.swift
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
import Foundation
// Define the paths for the files
let docsDirectoryPath = "./docs"
let iconSourcePath = "./assets/grape_icon_256.png"
let iconDestPath = "./docs/favicon.png"
let moduleNames = [
"Grape",
"ForceSimulation",
]
do {
let fileManager = FileManager.default
// Check if docs directory exists
var isDir: ObjCBool = false
if fileManager.fileExists(atPath: docsDirectoryPath, isDirectory: &isDir) {
if isDir.boolValue {
// Docs directory exists, proceed with enumeration
let enumerator = fileManager.enumerator(atPath: docsDirectoryPath)
while let element = enumerator?.nextObject() as? String {
if element.hasSuffix("index.html") { // checks the extension
print(element)
let indexPath = "\(docsDirectoryPath)/\(element)"
var htmlString = try String(contentsOfFile: indexPath, encoding: .utf8)
for moduleName in moduleNames {
htmlString = htmlString.replacingOccurrences(
of: """
<link rel="icon" href="/Grape/\(moduleName)/favicon.ico">
""",
with: """
<link rel="icon" href="/Grape/\(moduleName)/favicon.png">
""")
htmlString = htmlString.replacingOccurrences(
of: """
<link rel="mask-icon" href="/Grape/\(moduleName)/favicon.svg" color="#333333">
""",
with: """
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,400;0,600;1,400;1,600&display=swap" rel="stylesheet">
<link rel="stylesheet" media="all" href="https://lizhen.me/inter/inter.css" type="text/css"/>
<style>
:root {
--typography-html-font: "intervar";
--typography-html-font-mono: "SF Mono", ui-monospace, "JetBrains Mono";
}
h1.title {
font-weight: 600!important;
font-variation-settings: 'wght' 600, 'opsz' 24!important;
}
h2 {
font-weight: 600!important;
font-variation-settings: 'wght' 600, 'opsz' 24!important;
}
</style>
""")
}
try htmlString.write(toFile: indexPath, atomically: false, encoding: .utf8)
}
}
}
}
// Copy the icon file if it doesn't exist at the destination
if !fileManager.fileExists(atPath: iconDestPath) {
try fileManager.copyItem(atPath: iconSourcePath, toPath: iconDestPath)
}
for moduleName in moduleNames {
let iconDestPath = "./docs/\(moduleName)/favicon.png"
if !fileManager.fileExists(atPath: iconDestPath) {
try fileManager.copyItem(atPath: iconSourcePath, toPath: iconDestPath)
}
}
} catch {
// Handle errors by printing to the console for now
print("An error occurred: \(error)")
}