CHGenerator/Sources/Generator/Generators/HTMLElementsGenerator.swift
2022-12-20 00:33:25 +01:00

113 lines
3.4 KiB
Swift

import Foundation
typealias SVGSelection = (x: Int, y: Int, width: Int, height: Int)
struct HTMLElementsGenerator {
init() {
}
func make(title: String, suffix: String) -> String {
"\(title)<span class=\"suffix\">\(suffix)</span>"
}
func topBarWebsiteTitle(language: String, from page: Element) -> String {
guard let pathToRoot = page.pathToRoot else {
return Element.htmlPageName(for: language)
}
return pathToRoot + Element.htmlPagePathAddition(for: language)
}
func topBarLanguageButton(_ language: String) -> String {
"<a href=\"\(Element.htmlPageName(for: language))\">\(language)</a>"
}
func topBarNavigationLink(url: String, text: String, isActive: Bool) -> String {
"<a\(isActive ? " class=\"active\"" : "") href=\"/\(url)\">\(text)</a>"
}
func linkPreviewImage(file: String) -> String {
"<meta property=\"og:image\" content=\"\(file)\" />"
}
func makePrevText(_ text: String) -> String {
"<span class=\"icon-back\"></span>\(text)"
}
func makeNextText(_ text: String) -> String {
"\(text)<span class=\"icon-next\"></span>"
}
func image(file: String, width: Int, height: Int) -> String {
"""
<span class="image">
<img src="\(file)" loading="lazy" width="\(width)" height="\(height)"/>
</span>
"""
}
func svgImage(file: String, part: SVGSelection, width: Int, height: Int) -> String {
let path = "\(file)#svgView(viewBox(\(part.x),\(part.y),\(part.width),\(part.height))"
return image(file: path, width: width, height: height)
}
func downloadButtons(_ buttons: [(file: String, text: String, downloadName: String?)]) -> String {
let content = buttons.map {
if let download = $0.downloadName {
return button(file: $0.file, text: $0.text, downloadName: download)
} else {
return button(file: $0.file, text: $0.text)
}
}.joined(separator: "\n")
return flexParagraph(content)
}
func externalButtons(_ buttons: [(url: String, text: String)]) -> String {
let content = buttons
.map { externalLink(url: $0.url, text: $0.text) }
.joined(separator: "\n")
return flexParagraph(content)
}
private func flexParagraph(_ content: String) -> String {
"""
<p style="display: flex">
\(content)
</p>
"""
}
private func button(file: String, text: String) -> String {
"""
<a class="download-button" href="\(file)">
\(text)<span class="icon icon-download"></span>
</a>
"""
}
private func button(file: String, text: String, downloadName: String) -> String {
"""
<a class="download-button" href="\(file)" download="\(downloadName)">
\(text)<span class="icon icon-download"></span>
</a>
"""
}
private func externalLink(url: String, text: String) -> String {
"""
<a class="download-button" href="\(url)">
\(text)<span class="icon icon-download icon-rotate"></span>
</a>
"""
}
func scriptInclude(path: String) -> String {
"<script src=\"\(path)\"></script>"
}
func codeHighlightFooter() -> String {
"<script>hljs.highlightAll();</script>"
}
}