Skip to content

Commit

Permalink
Override documentation typography
Browse files Browse the repository at this point in the history
  • Loading branch information
li3zhen1 committed Oct 20, 2023
1 parent 35e2e8e commit 39df8ba
Showing 1 changed file with 54 additions and 39 deletions.
93 changes: 54 additions & 39 deletions DocPostprocess.swift
Original file line number Diff line number Diff line change
@@ -1,52 +1,67 @@
import Foundation

// Define the paths for the files
let htmlFilePath = "./docs/documentation/forcesimulation/index.html"
let docsDirectoryPath = "./docs"
let iconSourcePath = "./assets/grape_icon_256.png"
let iconDestPath = "./docs/favicon.png"

do {
// Read the HTML file into a string
var htmlString = try String(contentsOfFile: htmlFilePath, encoding: .utf8)

// Perform the replacements
htmlString = htmlString.replacingOccurrences(of: """
<link rel="icon" href="/Grape/favicon.ico">
""", with: """
<link rel="icon" href="/Grape/favicon.png">
""")
htmlString = htmlString.replacingOccurrences(of: """
<link rel="mask-icon" href="/Grape/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.title {
font-weight: 600!important;
font-variation-settings: 'wght' 600, 'opsz' 24!important;
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
let indexPath = "\(docsDirectoryPath)/\(element)"
var htmlString = try String(contentsOfFile: indexPath, encoding: .utf8)

htmlString = htmlString.replacingOccurrences(
of: """
<link rel="icon" href="/Grape/favicon.ico">
""",
with: """
<link rel="icon" href="/Grape/favicon.png">
""")
htmlString = htmlString.replacingOccurrences(
of: """
<link rel="mask-icon" href="/Grape/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.title {
font-weight: 600!important;
font-variation-settings: 'wght' 600, 'opsz' 24!important;
}
</style>
""")

try htmlString.write(toFile: indexPath, atomically: false, encoding: .utf8)
}
}
}
}
</style>
""")

// Write the modified HTML string back to the file
try htmlString.write(toFile: htmlFilePath, atomically: false, encoding: .utf8)

// Copy the icon file
let fileManager = FileManager.default
if fileManager.fileExists(atPath: iconDestPath) {
try fileManager.removeItem(atPath: iconDestPath)
// Copy the icon file if it doesn't exist at the destination
if !fileManager.fileExists(atPath: iconDestPath) {
try fileManager.copyItem(atPath: iconSourcePath, toPath: iconDestPath)
}
try fileManager.copyItem(atPath: iconSourcePath, toPath: iconDestPath)

} catch {
// Handle errors by printing to the console for now
Expand Down

0 comments on commit 39df8ba

Please sign in to comment.