Add rendering as HTML

This commit is contained in:
Maurice Parker 2020-04-18 10:41:18 -05:00
parent 1778a270d6
commit 2a0d75cf23
3 changed files with 66 additions and 1 deletions

View File

@ -53,7 +53,40 @@ final class TwitterStatus: Codable {
}
func renderAsHTML() -> String? {
return nil
if let retweetedStatus = retweetedStatus {
return renderAsRetweetHTML(retweetedStatus)
}
if let quotedStatus = quotedStatus {
return renderAsQuoteHTML(quotedStatus)
}
return renderAsTweetHTML(self)
}
func renderAsTweetHTML(_ status: TwitterStatus) -> String? {
return status.displayText
}
func renderAsRetweetHTML(_ status: TwitterStatus) -> String {
var html = String()
html += "<blockquote>"
if let userHTML = status.user?.renderHTML() {
html += userHTML
}
html += renderAsTweetHTML(status) ?? ""
html += "</blockquote>"
return html
}
func renderAsQuoteHTML(_ quotedStatus: TwitterStatus) -> String {
var html = String()
html += renderAsTweetHTML(self) ?? ""
html += "<blockquote>"
if let userHTML = quotedStatus.user?.renderHTML() {
html += userHTML
}
html += renderAsTweetHTML(quotedStatus) ?? ""
html += "</blockquote>"
return html
}
}

View File

@ -24,4 +24,21 @@ struct TwitterUser: Codable {
return "https://twitter.com/\(screenName ?? "")"
}
func renderHTML() -> String? {
var html = String()
html += "<div><a href=\"\(url)\">"
if let avatarURL = avatarURL {
html += "<img class=\"twitterAvatar\" src=\"\(avatarURL)\">"
}
html += "<span class=\"twitterUsername\">"
if let name = name {
html += " \(name)"
}
if let screenName = screenName {
html += " @\(screenName)"
}
html += "</span></a></div>"
return html
}
}

View File

@ -243,6 +243,21 @@ blockquote {
max-height: 1em;
}
/* Twitter */
.twitterAvatar {
vertical-align: middle;
border-radius: 4px;
height: 24px;
width: 24px;
}
.twitterUsername {
margin-left: 4px;
display: inline-block;
vertical-align: middle;
}
/*Block ads and junk*/
iframe[src*="feedads"],