GeminiRenderer: allow disabling element classes/ids

This commit is contained in:
Shadowfacts 2021-10-01 19:03:26 -04:00
parent 2b82e3b545
commit c59fe5b319
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 12 additions and 2 deletions

View File

@ -12,6 +12,8 @@ import HTMLEntities
public class GeminiHTMLRenderer { public class GeminiHTMLRenderer {
public var linkPrefix: ((URL) -> String?)? public var linkPrefix: ((URL) -> String?)?
public var addHeadingLineIDs = true
public var addLinkClass = true
public init() { public init() {
} }
@ -33,7 +35,11 @@ public class GeminiHTMLRenderer {
case let .link(url, text: maybeText): case let .link(url, text: maybeText):
let text = maybeText ?? url.absoluteString let text = maybeText ?? url.absoluteString
let linkPrefix = self.linkPrefix?(url) ?? "" let linkPrefix = self.linkPrefix?(url) ?? ""
str += "<p class=\"link\">\(linkPrefix)<a href=\"\(url.absoluteString)\">\(text.htmlEscape())</a></p>" str += "<p"
if addLinkClass {
str += " class=\"link\""
}
str += ">\(linkPrefix)<a href=\"\(url.absoluteString)\">\(text.htmlEscape())</a></p>"
case .preformattedToggle(alt: _): case .preformattedToggle(alt: _):
inPreformatting = !inPreformatting inPreformatting = !inPreformatting
if inPreformatting { if inPreformatting {
@ -46,7 +52,11 @@ public class GeminiHTMLRenderer {
str += "\n" str += "\n"
case let .heading(text, level: level): case let .heading(text, level: level):
let tag = "h\(level.rawValue)" let tag = "h\(level.rawValue)"
str += "<\(tag) id=\"l\(index)\">\(text.htmlEscape())</\(tag)>" str += "<\(tag)"
if addHeadingLineIDs {
str += " id=\"l\(index)\""
}
str += ">\(text.htmlEscape())</\(tag)>"
case let .unorderedListItem(text): case let .unorderedListItem(text):
if !inList { if !inList {
inList = true inList = true