|
| 1 | +import Foundation |
| 2 | +import HTML |
| 3 | +import CommonMark |
| 4 | + |
| 5 | +/// A protocol which needs to be implemented by the different documentation generators. It provides an API to operate |
| 6 | +/// on the generated documentation. |
| 7 | +protocol GeneratedDocumentation { |
| 8 | + |
| 9 | + /// The name of the output format. This needs to be the name name like the value passed to swift-doc's `format` option. |
| 10 | + static var outputFormat: String { get } |
| 11 | + |
| 12 | + init(directory: URL) |
| 13 | + |
| 14 | + var directory: URL { get } |
| 15 | + |
| 16 | + func symbol(_ symbolType: GenerateTestCase.SymbolType) -> Page? |
| 17 | +} |
| 18 | + |
| 19 | +protocol Page { |
| 20 | + var type: String? { get } |
| 21 | + |
| 22 | + var name: String? { get } |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +struct GeneratedHTMLDocumentation: GeneratedDocumentation { |
| 28 | + |
| 29 | + static let outputFormat = "html" |
| 30 | + |
| 31 | + let directory: URL |
| 32 | + |
| 33 | + func symbol(_ symbolType: GenerateTestCase.SymbolType) -> Page? { |
| 34 | + switch symbolType { |
| 35 | + case .class(let name): |
| 36 | + return page(for: name, ofType: "Class") |
| 37 | + case .typealias(let name): |
| 38 | + return page(for: name, ofType: "Typealias") |
| 39 | + case .struct(let name): |
| 40 | + return page(for: name, ofType: "Structure") |
| 41 | + case .enum(let name): |
| 42 | + return page(for: name, ofType: "Enumeration") |
| 43 | + case .protocol(let name): |
| 44 | + return page(for: name, ofType: "Protocol") |
| 45 | + case .function(let name): |
| 46 | + return page(for: name, ofType: "Function") |
| 47 | + case .variable(let name): |
| 48 | + return page(for: name, ofType: "Variable") |
| 49 | + case .extension(let name): |
| 50 | + return page(for: name, ofType: "Extensions on") |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private func page(for symbolName: String, ofType type: String) -> Page? { |
| 55 | + guard let page = page(named: symbolName) else { return nil } |
| 56 | + guard page.type == type else { return nil } |
| 57 | + |
| 58 | + return page |
| 59 | + } |
| 60 | + |
| 61 | + private func page(named name: String) -> HtmlPage? { |
| 62 | + let fileUrl = directory.appendingPathComponent(fileName(forSymbol: name)).appendingPathComponent("index.html") |
| 63 | + guard |
| 64 | + FileManager.default.isReadableFile(atPath: fileUrl.path), |
| 65 | + let contents = try? String(contentsOf: fileUrl), |
| 66 | + let document = try? HTML.Document(string: contents) |
| 67 | + else { return nil } |
| 68 | + |
| 69 | + return HtmlPage(document: document) |
| 70 | + } |
| 71 | + |
| 72 | + private func fileName(forSymbol symbolName: String) -> String { |
| 73 | + symbolName |
| 74 | + .replacingOccurrences(of: ".", with: "_") |
| 75 | + .replacingOccurrences(of: " ", with: "-") |
| 76 | + } |
| 77 | + |
| 78 | + private struct HtmlPage: Page { |
| 79 | + let document: HTML.Document |
| 80 | + |
| 81 | + var type: String? { |
| 82 | + let results = document.search(xpath: "//h1/small") |
| 83 | + assert(results.count == 1) |
| 84 | + return results.first?.content |
| 85 | + } |
| 86 | + |
| 87 | + var name: String? { |
| 88 | + let results = document.search(xpath: "//h1/code") |
| 89 | + assert(results.count == 1) |
| 90 | + return results.first?.content |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +struct GeneratedCommonMarkDocumentation: GeneratedDocumentation { |
| 97 | + |
| 98 | + static let outputFormat = "commonmark" |
| 99 | + |
| 100 | + let directory: URL |
| 101 | + |
| 102 | + func symbol(_ symbolType: GenerateTestCase.SymbolType) -> Page? { |
| 103 | + switch symbolType { |
| 104 | + case .class(let name): |
| 105 | + return page(for: name, ofType: "class") |
| 106 | + case .typealias(let name): |
| 107 | + return page(for: name, ofType: "typealias") |
| 108 | + case .struct(let name): |
| 109 | + return page(for: name, ofType: "struct") |
| 110 | + case .enum(let name): |
| 111 | + return page(for: name, ofType: "enum") |
| 112 | + case .protocol(let name): |
| 113 | + return page(for: name, ofType: "protocol") |
| 114 | + case .function(let name): |
| 115 | + return page(for: name, ofType: "func") |
| 116 | + case .variable(let name): |
| 117 | + return page(for: name, ofType: "var") ?? page(for: name, ofType: "let") |
| 118 | + case .extension(let name): |
| 119 | + return page(for: name, ofType: "extension") |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private func page(for symbolName: String, ofType type: String) -> Page? { |
| 124 | + guard let page = page(named: symbolName) else { return nil } |
| 125 | + guard page.type == type else { return nil } |
| 126 | + |
| 127 | + return page |
| 128 | + } |
| 129 | + |
| 130 | + private func page(named name: String) -> CommonMarkPage? { |
| 131 | + let fileUrl = directory.appendingPathComponent("\(name).md") |
| 132 | + guard |
| 133 | + FileManager.default.isReadableFile(atPath: fileUrl.path), |
| 134 | + let contents = try? String(contentsOf: fileUrl), |
| 135 | + let document = try? CommonMark.Document(contents) |
| 136 | + else { return nil } |
| 137 | + |
| 138 | + return CommonMarkPage(document: document) |
| 139 | + } |
| 140 | + |
| 141 | + private func fileName(forSymbol symbolName: String) -> String { |
| 142 | + symbolName |
| 143 | + .replacingOccurrences(of: ".", with: "_") |
| 144 | + .replacingOccurrences(of: " ", with: "-") |
| 145 | + } |
| 146 | + |
| 147 | + private struct CommonMarkPage: Page { |
| 148 | + let document: CommonMark.Document |
| 149 | + |
| 150 | + private var headingElement: Heading? { |
| 151 | + document.children.first(where: { ($0 as? Heading)?.level == 1 }) as? Heading |
| 152 | + } |
| 153 | + |
| 154 | + var type: String? { |
| 155 | + // Our CommonMark pages don't give a hint of the actual type of a documentation page. That's why we extract |
| 156 | + // it via a regex out of the declaration. Not very nice, but works for now. |
| 157 | + guard |
| 158 | + let name = self.name, |
| 159 | + let code = document.children.first(where: { $0 is CodeBlock}) as? CodeBlock, |
| 160 | + let codeContents = code.literal, |
| 161 | + let extractionRegex = try? NSRegularExpression(pattern: "([a-z]+) \(name)") |
| 162 | + else { return nil } |
| 163 | + |
| 164 | + guard |
| 165 | + let match = extractionRegex.firstMatch(in: codeContents, range: NSRange(location: 0, length: codeContents.utf16.count)), |
| 166 | + match.numberOfRanges > 0, |
| 167 | + let range = Range(match.range(at: 1), in: codeContents) |
| 168 | + else { return nil } |
| 169 | + |
| 170 | + return String(codeContents[range]) |
| 171 | + } |
| 172 | + |
| 173 | + var name: String? { |
| 174 | + headingElement?.children.compactMap { ($0 as? Literal)?.literal }.joined() |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments