krz/hutch

an ios client for sourcehut

clone: git clone https://gitbay.org/krz/hutch.git

v3.11.0: Hutch/Views/More/ManPageCatalog.swift · raw

 1import Foundation
 2
 3struct ManPageCatalogEntry: Decodable, Hashable, Identifiable {
 4    let title: String
 5    let url: URL
 6
 7    var id: String { title }
 8}
 9
10enum ManPageCatalog {
11    /// Official sr.ht man pages, loaded from the bundled `man-pages.json` that
12    /// the scheduled sync workflow keeps in step with man.sr.ht. Falls back to a
13    /// built-in list if the resource is missing or unreadable, so the browser is
14    /// never empty.
15    static func load(bundle: Bundle = .main) -> [ManPageCatalogEntry] {
16        guard let url = bundle.url(forResource: "man-pages", withExtension: "json"),
17              let data = try? Data(contentsOf: url),
18              let entries = try? JSONDecoder().decode([ManPageCatalogEntry].self, from: data),
19              !entries.isEmpty else {
20            return fallback
21        }
22        return entries
23    }
24
25    static let fallback: [ManPageCatalogEntry] = [
26        entry("builds.sr.ht", "https://man.sr.ht/builds.sr.ht/"),
27        entry("chat.sr.ht", "https://man.sr.ht/chat.sr.ht/"),
28        entry("git.sr.ht", "https://man.sr.ht/git.sr.ht/"),
29        entry("hg.sr.ht", "https://man.sr.ht/hg.sr.ht/"),
30        entry("hub.sr.ht", "https://man.sr.ht/hub.sr.ht/"),
31        entry("lists.sr.ht", "https://man.sr.ht/lists.sr.ht/"),
32        entry("man.sr.ht", "https://man.sr.ht/man.sr.ht/"),
33        entry("meta.sr.ht", "https://man.sr.ht/meta.sr.ht/"),
34        entry("paste.sr.ht", "https://man.sr.ht/paste.sr.ht/"),
35        entry("sr.ht", "https://man.sr.ht/sr.ht/"),
36        entry("srht.site", "https://srht.site/"),
37        entry("todo.sr.ht", "https://man.sr.ht/todo.sr.ht/")
38    ]
39
40    private static func entry(_ title: String, _ urlString: String) -> ManPageCatalogEntry {
41        ManPageCatalogEntry(title: title, url: URL(string: urlString)!)
42    }
43}