summaryrefslogtreecommitdiff
path: root/Hutch/Views
diff options
context:
space:
mode:
Diffstat (limited to 'Hutch/Views')
-rw-r--r--Hutch/Views/Lookup/LookupView.swift4
-rw-r--r--Hutch/Views/More/ManPageBrowserView.swift33
-rw-r--r--Hutch/Views/More/ManPageDetailView.swift103
-rw-r--r--Hutch/Views/More/MoreView.swift5
-rw-r--r--Hutch/Views/Repositories/ReadmeView.swift55
5 files changed, 197 insertions, 3 deletions
diff --git a/Hutch/Views/Lookup/LookupView.swift b/Hutch/Views/Lookup/LookupView.swift
index bee2453..eb585fb 100644
--- a/Hutch/Views/Lookup/LookupView.swift
+++ b/Hutch/Views/Lookup/LookupView.swift
@@ -449,6 +449,10 @@ struct LookupView: View {
NeedsAttentionSnapshotStore.adjustUnreadInboxThreads(by: 1)
}
)
+ case .manPageBrowser:
+ ManPageBrowserView()
+ case .manPage(let url):
+ ManPageDetailView(url: url)
}
}
.environment(appState)
diff --git a/Hutch/Views/More/ManPageBrowserView.swift b/Hutch/Views/More/ManPageBrowserView.swift
new file mode 100644
index 0000000..f854c79
--- /dev/null
+++ b/Hutch/Views/More/ManPageBrowserView.swift
@@ -0,0 +1,33 @@
+import SwiftUI
+
+/// Entry point for the man.sr.ht browser in the More tab.
+/// Shows a pre-populated list of official sr.ht man pages.
+struct ManPageBrowserView: View {
+ private let officialDocs: [(title: String, url: URL)] = [
+ ("sr.ht", URL(string: "https://man.sr.ht/sr.ht/")!),
+ ("hub.sr.ht", URL(string: "https://man.sr.ht/hub.sr.ht/")!),
+ ("git.sr.ht", URL(string: "https://man.sr.ht/git.sr.ht/")!),
+ ("hg.sr.ht", URL(string: "https://man.sr.ht/hg.sr.ht/")!),
+ ("lists.sr.ht", URL(string: "https://man.sr.ht/lists.sr.ht/")!),
+ ("todo.sr.ht", URL(string: "https://man.sr.ht/todo.sr.ht/")!),
+ ("builds.sr.ht", URL(string: "https://man.sr.ht/builds.sr.ht/")!),
+ ("paste.sr.ht", URL(string: "https://man.sr.ht/paste.sr.ht/")!),
+ ("man.sr.ht", URL(string: "https://man.sr.ht/man.sr.ht/")!),
+ ("meta.sr.ht", URL(string: "https://man.sr.ht/meta.sr.ht/")!),
+ ("srht.site", URL(string: "https://srht.site/")!)
+ ]
+
+ var body: some View {
+ List {
+ Section("Official Man Pages") {
+ ForEach(officialDocs, id: \.title) { doc in
+ NavigationLink(value: MoreRoute.manPage(doc.url)) {
+ Text(doc.title)
+ }
+ }
+ }
+ }
+ .navigationTitle("Man Pages")
+ .navigationBarTitleDisplayMode(.inline)
+ }
+}
diff --git a/Hutch/Views/More/ManPageDetailView.swift b/Hutch/Views/More/ManPageDetailView.swift
new file mode 100644
index 0000000..96a92fb
--- /dev/null
+++ b/Hutch/Views/More/ManPageDetailView.swift
@@ -0,0 +1,103 @@
+import SwiftUI
+
+/// Fetches and renders a single man.sr.ht page.
+/// Internal man.sr.ht links update the current URL in-place rather than
+/// opening the browser, so the user can follow wiki links without leaving
+/// the view.
+struct ManPageDetailView: View {
+ let initialURL: URL
+
+ @Environment(\.colorScheme) private var colorScheme
+ @State private var currentURL: URL
+ @State private var page: ManPage?
+ @State private var isLoading = false
+ @State private var error: String?
+
+ init(url: URL) {
+ initialURL = url
+ _currentURL = State(initialValue: url)
+ }
+
+ var body: some View {
+ ScrollView {
+ if isLoading {
+ SRHTLoadingStateView(message: "Loading page…")
+ .padding(.top, 40)
+ } else if let error {
+ SRHTErrorStateView(
+ title: "Couldn't Load Page",
+ message: error,
+ retryAction: { await loadPage() }
+ )
+ .padding()
+ } else if let page {
+ HTMLWebView(
+ html: page.contentHTML,
+ colorScheme: colorScheme,
+ style: .readme,
+ baseURL: page.url,
+ onInterceptURL: { url in
+ guard let destinationURL = normalizedManPageURL(for: url) else {
+ return false
+ }
+ currentURL = destinationURL
+ return true
+ }
+ )
+ .padding()
+ }
+ }
+ .navigationTitle(page?.title ?? "Documentation")
+ .navigationBarTitleDisplayMode(.inline)
+ .toolbar {
+ ToolbarItem(placement: .topBarTrailing) {
+ if let page {
+ Link(destination: page.url) {
+ Image(systemName: "safari")
+ }
+ }
+ }
+ }
+ .task(id: currentURL) {
+ await loadPage()
+ }
+ }
+
+ private func loadPage() async {
+ isLoading = true
+ error = nil
+
+ do {
+ page = try await ManPageService.fetch(url: currentURL)
+ } catch {
+ self.error = error.localizedDescription
+ }
+
+ isLoading = false
+ }
+
+ private func normalizedManPageURL(for url: URL) -> URL? {
+ if ManPageService.isTrustedDocumentationURL(url) {
+ return url
+ }
+
+ guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
+ let scheme = components.scheme?.lowercased(),
+ scheme == "about" || scheme == "file" else {
+ return nil
+ }
+
+ let rawPath = url.path.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
+ guard !rawPath.isEmpty else {
+ if currentURL.host?.lowercased() == "srht.site" {
+ return ManPageService.pagesBaseURL
+ }
+ return ManPageService.baseURL
+ }
+
+ if currentURL.host?.lowercased() == "srht.site" {
+ return URL(string: "https://srht.site/\(rawPath)") ?? ManPageService.pagesBaseURL
+ }
+ return URL(string: "https://man.sr.ht/\(rawPath)/") ?? ManPageService.baseURL
+ }
+}
diff --git a/Hutch/Views/More/MoreView.swift b/Hutch/Views/More/MoreView.swift
index 9c5cf92..ae7228d 100644
--- a/Hutch/Views/More/MoreView.swift
+++ b/Hutch/Views/More/MoreView.swift
@@ -5,7 +5,6 @@ struct MoreView: View {
private let unsupportedLinks: [(title: String, url: URL)] = [
("chat.sr.ht", URL(string: "https://chat.sr.ht")!),
- ("man.sr.ht", URL(string: "https://man.sr.ht")!),
("srht.site", URL(string: "https://srht.site")!)
]
@@ -21,6 +20,10 @@ struct MoreView: View {
NavigationLink(value: MoreRoute.lists) {
Label("Mailing Lists", systemImage: "list.bullet.rectangle")
}
+
+ NavigationLink(value: MoreRoute.manPageBrowser) {
+ Label("Man Pages", systemImage: "book")
+ }
NavigationLink(value: MoreRoute.pastes) {
Label("Pastes", systemImage: "doc.on.clipboard")
diff --git a/Hutch/Views/Repositories/ReadmeView.swift b/Hutch/Views/Repositories/ReadmeView.swift
index 93fee26..5742811 100644
--- a/Hutch/Views/Repositories/ReadmeView.swift
+++ b/Hutch/Views/Repositories/ReadmeView.swift
@@ -1522,6 +1522,8 @@ struct HTMLWebView: View {
let html: String
let colorScheme: ColorScheme
var style: HTMLWebViewStyle = .readme
+ var baseURL: URL? = nil
+ var onInterceptURL: ((URL) -> Bool)? = nil
@Environment(\.openURL) private var openURL
@State private var contentHeight: CGFloat = 1
@State private var loadError: String?
@@ -1545,6 +1547,8 @@ struct HTMLWebView: View {
html: html,
colorScheme: colorScheme,
style: style,
+ baseURL: baseURL,
+ onInterceptURL: onInterceptURL,
openURL: openURL,
dynamicHeight: $contentHeight,
loadError: $loadError,
@@ -1581,6 +1585,8 @@ private struct HTMLWebViewRepresentable: UIViewRepresentable {
let html: String
let colorScheme: ColorScheme
let style: HTMLWebViewStyle
+ let baseURL: URL?
+ let onInterceptURL: ((URL) -> Bool)?
let openURL: OpenURLAction
@Binding var dynamicHeight: CGFloat
@Binding var loadError: String?
@@ -1607,6 +1613,7 @@ private struct HTMLWebViewRepresentable: UIViewRepresentable {
}
func updateUIView(_ webView: WKWebView, context: Context) {
+ context.coordinator.parent = self
let textColor = colorScheme == .dark ? "#fff" : "#000"
let linkColor = colorScheme == .dark ? "#58a6ff" : "#0066cc"
@@ -1648,6 +1655,10 @@ private struct HTMLWebViewRepresentable: UIViewRepresentable {
overflow-wrap: normal;
}
img { max-width: 100%; height: auto; }
+ svg {
+ max-width: 100%;
+ height: auto;
+ }
input[type="checkbox"] {
margin-right: 0.45rem;
vertical-align: middle;
@@ -1711,6 +1722,22 @@ private struct HTMLWebViewRepresentable: UIViewRepresentable {
color: rgba(128, 128, 128, 0.85);
font-size: 0.9em;
}
+ .btn {
+ display: inline-flex;
+ align-items: center;
+ gap: 0.4em;
+ }
+ .icon {
+ display: inline-flex;
+ align-items: center;
+ vertical-align: middle;
+ }
+ .icon svg {
+ width: 0.65em;
+ height: 0.65em;
+ display: block;
+ fill: currentColor;
+ }
.org-title { margin: 0 0 0.25em; }
.org-author, .org-date {
margin: 0;
@@ -1743,7 +1770,7 @@ private struct HTMLWebViewRepresentable: UIViewRepresentable {
self.loadError = nil
}
}
- webView.loadHTMLString(wrapped, baseURL: nil)
+ webView.loadHTMLString(wrapped, baseURL: baseURL)
}
}
@@ -1751,7 +1778,7 @@ private final class HTMLWebViewCoordinator: NSObject, WKNavigationDelegate, @unc
static let websiteDataStore = WKWebsiteDataStore.nonPersistent()
static let heightCache = NSCache<NSString, NSNumber>()
- let parent: HTMLWebViewRepresentable
+ var parent: HTMLWebViewRepresentable
var lastHTML: String?
var lastReloadToken = 0
@@ -1785,6 +1812,14 @@ private final class HTMLWebViewCoordinator: NSObject, WKNavigationDelegate, @unc
}
if navigationAction.navigationType == .linkActivated {
+ if isSameDocumentFragmentNavigation(requestURL) {
+ decisionHandler(.allow)
+ return
+ }
+ if let intercept = parent.onInterceptURL, intercept(requestURL) {
+ decisionHandler(.cancel)
+ return
+ }
if isAllowedReadmeNavigationURL(requestURL) {
parent.openURL(requestURL)
}
@@ -1824,4 +1859,20 @@ private final class HTMLWebViewCoordinator: NSObject, WKNavigationDelegate, @unc
}
}
}
+
+ private func isSameDocumentFragmentNavigation(_ url: URL) -> Bool {
+ guard url.fragment != nil,
+ let baseURL = parent.baseURL else {
+ return false
+ }
+
+ guard var destination = URLComponents(url: url, resolvingAgainstBaseURL: false),
+ var base = URLComponents(url: baseURL, resolvingAgainstBaseURL: false) else {
+ return false
+ }
+
+ destination.fragment = nil
+ base.fragment = nil
+ return destination.url == base.url
+ }
}