Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Sources/CLI/renderer/DefaultSupportRenderer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

#if DEBUG
protocol SupportRenderer: Sendable {
func render(_ payload: SupportPayload) throws -> String
}
extension DefaultSupportRenderer: SupportRenderer {}
#else
typealias SupportRenderer = DefaultSupportRenderer
#endif

struct DefaultSupportRenderer: Sendable {
let output: OutputFormat
let audience: OutputAudience

func render(_ payload: SupportPayload) throws -> String {
let presentation = SupportPresenter().present(payload, audience: audience)
switch output {
case .json:
return try StructuredDocumentationRenderer().render(presentation)
case .text:
return SupportTextRenderer(agent: audience == .agent).render(presentation)
}
}
}
79 changes: 79 additions & 0 deletions Sources/CLI/renderer/SupportContentEncoding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import Foundation

extension SupportInline {
private enum CodingKeys: CodingKey { case type, text, content, label, url, alt }

func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .image(let url, let alt):
try container.encode("image", forKey: .type)
try container.encode(url, forKey: .url)
try container.encode(alt, forKey: .alt)
case .text(let text):
try container.encode("text", forKey: .type)
try container.encode(text, forKey: .text)
case .code(let text):
try container.encode("code", forKey: .type)
try container.encode(text, forKey: .text)
case .emphasis(let content):
try container.encode("emphasis", forKey: .type)
try container.encode(content, forKey: .content)
case .strong(let content):
try container.encode("strong", forKey: .type)
try container.encode(content, forKey: .content)
case .superscript(let content):
try container.encode("superscript", forKey: .type)
try container.encode(content, forKey: .content)
case .link(let label, let url):
try container.encode("link", forKey: .type)
try container.encode(label, forKey: .label)
try container.encode(url, forKey: .url)
case .lineBreak:
try container.encode("lineBreak", forKey: .type)
}
}
}

extension SupportContentBlock {
private enum CodingKeys: CodingKey {
case type, content, level, text, anchor, ordered, start, items, syntax, style, rows, url, alt, caption
}

func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .paragraph(let content):
try container.encode("paragraph", forKey: .type)
try container.encode(content, forKey: .content)
case .heading(let level, let text, let anchor):
try container.encode("heading", forKey: .type)
try container.encode(level, forKey: .level)
try container.encode(text, forKey: .text)
try container.encodeIfPresent(anchor, forKey: .anchor)
case .list(let ordered, let start, let items):
try container.encode("list", forKey: .type)
try container.encode(ordered, forKey: .ordered)
try container.encode(start, forKey: .start)
try container.encode(items, forKey: .items)
case .codeListing(let text, let syntax):
try container.encode("codeListing", forKey: .type)
try container.encode(text, forKey: .text)
try container.encodeIfPresent(syntax, forKey: .syntax)
case .aside(let style, let content):
try container.encode("aside", forKey: .type)
try container.encode(style, forKey: .style)
try container.encode(content, forKey: .content)
case .table(let rows):
try container.encode("table", forKey: .type)
try container.encode(rows, forKey: .rows)
case .image(let url, let alt, let caption):
try container.encode("image", forKey: .type)
try container.encode(url, forKey: .url)
try container.encode(alt, forKey: .alt)
try container.encode(caption, forKey: .caption)
case .separator:
try container.encode("separator", forKey: .type)
}
}
}
143 changes: 143 additions & 0 deletions Sources/CLI/renderer/SupportPresentation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import Foundation

enum SupportPayload: Sendable {
case articles(SupportArticlesCatalog)
case help(SupportHelpCatalog)
case contents(SupportGuideContents)
case glossary(SupportGlossary)
case glossaryEntry(SupportGlossaryEntry)
case terms(SupportTermsCatalog)
case termsEntry(SupportTermsEntry)
case article(SupportArticle)

var kind: String {
switch self {
case .articles: return "articlesCatalog"
case .help: return "helpCatalog"
case .contents: return "guideContents"
case .glossary: return "glossary"
case .glossaryEntry: return "glossaryEntry"
case .terms: return "termsCatalog"
case .termsEntry: return "termsEntry"
case .article: return "article"
}
}

func encodeContent(to encoder: any Encoder) throws {
switch self {
case .articles(let value): try value.encode(to: encoder)
case .help(let value): try value.encode(to: encoder)
case .contents(let value): try value.encode(to: encoder)
case .glossary(let value): try value.encode(to: encoder)
case .glossaryEntry(let value): try value.encode(to: encoder)
case .terms(let value): try value.encode(to: encoder)
case .termsEntry(let value): try value.encode(to: encoder)
case .article(let value): try value.encode(to: encoder)
}
}
}

struct SupportNavigation: Encodable, Sendable {
let title: String
let url: URL?
let command: String
}

struct SupportPresentation: Encodable, Sendable {
let payload: SupportPayload
let navigation: [SupportNavigation]?

private enum CodingKeys: CodingKey { case kind, navigation }

func encode(to encoder: any Encoder) throws {
try payload.encodeContent(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(payload.kind, forKey: .kind)
try container.encodeIfPresent(navigation, forKey: .navigation)
}
}

struct SupportPresenter {
func present(_ payload: SupportPayload, audience: OutputAudience) -> SupportPresentation {
SupportPresentation(payload: payload, navigation: audience == .agent ? navigation(payload) : nil)
}

private func navigation(_ payload: SupportPayload) -> [SupportNavigation] {
switch payload {
case .glossary(let glossary):
return glossary.entries.map { glossaryNavigation($0) }
case .glossaryEntry(let entry):
return [glossaryNavigation(entry)]
case .terms(let catalog):
return catalog.categories.flatMap(\.entries).map(termsNavigation)
case .termsEntry(let entry):
return entry.resources.compactMap { followUp($0.link) }
default:
return references(payload).compactMap(followUp)
}
}

private func glossaryNavigation(_ entry: SupportGlossaryEntry) -> SupportNavigation {
SupportNavigation(
title: entry.term, url: nil,
command: "apple-docs support glossary view --term " + quote(entry.term) + " --agent")
}

private func termsNavigation(_ entry: SupportTermsEntry) -> SupportNavigation {
SupportNavigation(
title: entry.title, url: nil,
command: "apple-docs support terms view --name " + quote(entry.id) + " --agent")
}

private func references(_ payload: SupportPayload) -> [SupportPageLink] {
switch payload {
case .articles(let catalog): return catalog.categories.flatMap(\.links)
case .help(let catalog):
return catalog.featuredResources.map(\.link) + catalog.topics.flatMap { $0.groups.flatMap(\.links) }
+ [catalog.glossary.link]
case .contents(let contents): return contents.entries.flatMap(contentsReferences)
case .article(let article): return article.related
default: return []
}
}

private func contentsReferences(_ entry: SupportContentsEntry) -> [SupportPageLink] {
(entry.link.map { [$0] } ?? []) + entry.children.flatMap(contentsReferences)
}

private func followUp(_ link: SupportPageLink) -> SupportNavigation? {
guard let destination = try? SupportDestination(responseURL: link.url) else { return nil }
let path = destination.path
let parts = path.split(separator: "/")
let command: String
switch path {
case "help": command = "apple-docs support guides list"
case "help/glossary": command = "apple-docs support glossary list"
case "support/articles": command = "apple-docs support articles list"
case "support/terms": command = "apple-docs support terms list"
default:
if parts.count == 2, parts[0] == "help", ["account", "app-store-connect", "app-review"].contains(parts[1]) {
command = "apple-docs support guides contents --name " + quote(String(parts[1]))
} else if isReadable(path) {
command = "apple-docs support view --path " + quote(path)
} else {
return nil
}
}
return SupportNavigation(title: link.title, url: link.url, command: command + " --agent")
}

private func isReadable(_ path: String) -> Bool {
let parts = path.split(separator: "/")
guard !path.lowercased().hasSuffix(".pdf") else { return false }
return
(parts.count > 2 && parts[0] == "help"
&& ["account", "app-store-connect", "app-review"].contains(parts[1]))
|| path == "support/fee-waiver"
|| path == "support/terms/apple-developer-program-license-agreement"
}

private func quote(_ text: String) -> String {
"'" + text.replacingOccurrences(of: "'", with: "'\\''") + "'"
}
}
Loading
Loading