Generate external link buttons

This commit is contained in:
Christoph Hagen 2022-08-30 11:29:53 +02:00
parent d635431d52
commit 107fa2f877
2 changed files with 36 additions and 2 deletions

View File

@ -62,6 +62,13 @@ struct HTMLElementsGenerator {
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">
@ -73,7 +80,7 @@ struct HTMLElementsGenerator {
private func button(file: String, text: String) -> String {
"""
<a class="download-button" href="\(file)">
\(text)<span class="icon-download"></span>
\(text)<span class="icon icon-download"></span>
</a>
"""
}
@ -81,9 +88,16 @@ struct HTMLElementsGenerator {
private func button(file: String, text: String, downloadName: String) -> String {
"""
<a class="download-button" href="\(file)" download="\(downloadName)">
\(text)<span class="icon-download"></span>
\(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>
"""
}

View File

@ -60,6 +60,9 @@ struct PageContentGenerator {
if alt == "download" {
return handleDownloadButtons(page: page, content: fileAndTitle)
}
if alt == "external" {
return handleExternalButtons(page: page, content: fileAndTitle)
}
let file = fileAndTitle.dropAfterFirst(" ")
let title = fileAndTitle.contains(" ") ? fileAndTitle.dropBeforeFirst(" ").nonEmpty : nil
@ -163,4 +166,21 @@ struct PageContentGenerator {
}
return factory.html.downloadButtons(buttons)
}
private func handleExternalButtons(page: Element, content: String) -> String {
let buttons = content
.components(separatedBy: ";")
.compactMap { button -> (url: String, text: String)? in
let parts = button.components(separatedBy: ",")
guard parts.count == 2 else {
log.add(warning: "Invalid external link definition", source: page.path)
return nil
}
let url = parts[0].trimmed
let title = parts[1].trimmed
return (url, title)
}
return factory.html.externalButtons(buttons)
}
}