Render enclosures.

This commit is contained in:
Brent Simmons committed 2026-10-23 09:13:03 -07:00
1 parent 43a75a5f30
commit 18380e8000
4 files changed
+156 -1

No files matched your search

@@ -0,0 +1,123 @@
//
// ArticleRenderer+Enclosures.swift
// NetNewsWire
//
// Created by Brent Simmons on 10/23/26.
//
import Foundation
import RSCore
import Articles
/// HTML for the `[[enclosures]]` substitution: players for audio and video, and images.
/// Styled with classes, since inline styles get stripped.
extension ArticleRenderer {
private static let secondsPerMinute = 60
private static let secondsPerHour = 3600
/// Empty when there’s nothing to show. Skips attachments whose URLs already appear
/// in the body, since feeds often embed the same image they list as an enclosure.
static func enclosuresHTML(attachments: Set<Attachment>?, body: String, posterURL: String?) -> String {
guard let attachments, !attachments.isEmpty else {
return ""
}
let sortedAttachments = attachments.sorted { $0.url < $1.url }
var enclosures = [String]()
for attachment in sortedAttachments where !bodyContainsURL(body, attachment.url) {
if let enclosure = enclosureHTML(attachment, posterURL: posterURL) {
enclosures.append(enclosure)
}
}
guard !enclosures.isEmpty else {
return ""
}
return "<div class=\"nnw-enclosures\">\n\(enclosures.joined(separator: "\n"))\n</div>"
}
}
private extension ArticleRenderer {
static func enclosureHTML(_ attachment: Attachment, posterURL: String?) -> String? {
guard let displayMedium = attachment.displayMedium else {
return nil
}
let src = attachment.url.escapingSpecialXMLCharacters
let mediaElement: String
let caption: String?
switch displayMedium {
case .audio:
mediaElement = "<audio controls preload=\"none\" src=\"\(src)\"></audio>"
caption = audioCaption(attachment)
case .video:
var attributes = "controls preload=\"metadata\" playsinline src=\"\(src)\"" + dimensionAttributes(attachment)
if let posterURL, !posterURL.isEmpty {
attributes += " poster=\"\(posterURL.escapingSpecialXMLCharacters)\""
}
mediaElement = "<video \(attributes)></video>"
caption = attachment.title
case .image:
mediaElement = "<img src=\"\(src)\" alt=\"\"" + dimensionAttributes(attachment) + ">"
caption = attachment.title
case .document, .executable:
return nil
}
var html = "<figure class=\"nnw-enclosure nnw-enclosure-\(displayMedium.rawValue)\">\(mediaElement)"
if let caption, !caption.isEmpty {
html += "<figcaption>\(caption.escapingSpecialXMLCharacters)</figcaption>"
}
html += "</figure>"
return html
}
/// Title (or file name), duration, and size, such as “Episode 311 · 1:54:09 · 54.9 MB”.
static func audioCaption(_ attachment: Attachment) -> String {
var components = [String]()
if let title = attachment.title, !title.isEmpty {
components.append(title)
} else if let fileName = fileName(attachment.url) {
components.append(fileName)
}
if let durationInSeconds = attachment.durationInSeconds {
components.append(durationString(durationInSeconds))
}
if let sizeInBytes = attachment.sizeInBytes {
components.append(ByteCountFormatter.string(fromByteCount: Int64(sizeInBytes), countStyle: .file))
}
return components.joined(separator: " · ")
}
static func dimensionAttributes(_ attachment: Attachment) -> String {
guard let width = attachment.width, let height = attachment.height else {
return ""
}
return " width=\"\(width)\" height=\"\(height)\""
}
static func durationString(_ durationInSeconds: Int) -> String {
let hours = durationInSeconds / secondsPerHour
let minutes = (durationInSeconds % secondsPerHour) / secondsPerMinute
let seconds = durationInSeconds % secondsPerMinute
if hours > 0 {
return String(format: "%d:%02d:%02d", hours, minutes, seconds)
}
return String(format: "%d:%02d", minutes, seconds)
}
static func fileName(_ urlString: String) -> String? {
guard let lastPathComponent = URL(string: urlString)?.lastPathComponent, !lastPathComponent.isEmpty, lastPathComponent != "/" else {
return nil
}
return lastPathComponent.removingPercentEncoding ?? lastPathComponent
}
/// Body HTML escapes `&` in attributes, so check both forms of the URL.
static func bodyContainsURL(_ body: String, _ url: String) -> Bool {
body.contains(url) || body.contains(url.escapingSpecialXMLCharacters)
}
}
@@ -248,7 +248,14 @@ private extension ArticleRenderer {
d["external_link"] = ""
}
d["body"] = body
// Templates without [[enclosures]] get them at the end of the body.
let enclosures = Self.enclosuresHTML(attachments: article.attachments, body: body, posterURL: article.imageLink)
if template().contains("[[enclosures]]") {
d["body"] = body
d["enclosures"] = enclosures
} else {
d["body"] = body + enclosures
}
#if os(macOS)
d["text_size_class"] = AppDefaults.articleTextSize.cssClass
+23
View File
@@ -63,6 +63,29 @@ img.twemoji, img.emoji {
width: auto;
}
/* Enclosures: audio and video players and images after the article body */
.nnw-enclosures {
margin-top: 1.5em;
}
.nnw-enclosure {
margin: 1em 0;
}
.nnw-enclosure audio {
width: 100%;
}
.nnw-enclosure video,
.nnw-enclosure img {
height: auto;
}
.nnw-enclosure figcaption {
font-size: 0.9em;
}
/* Hide the external link at the bottom of Daring Fireball posts */
.x-netnewswire-hide {
+2
View File
@@ -27,6 +27,7 @@ time_short: Long version of the publish time.
text_size_class: The size class that the user has selected in Preferences for article text.
body: The body of the article.
enclosures: Audio and video players and images from the article’s enclosures. If a template doesn’t include this, they’re added to the end of the body.
-->
@@ -44,4 +45,5 @@ body: The body of the article.
<div class="[[dateline_style]]"><a href="[[preferred_link]]">[[datetime_medium]]</a></div>
<div class="externalLink">[[external_link_label]] <a href="[[external_link]]">[[external_link_stripped]]</a></div>
<div id="bodyContainer" class="articleBody [[text_size_class]]">[[body]]</div>
[[enclosures]]
</article>