diff --git a/Package.resolved b/Package.resolved index 144e7e7a..a57ba99e 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "package": "CommonMark", "repositoryURL": "https://github.com/SwiftDocOrg/CommonMark.git", "state": { - "branch": "master", - "revision": "00d3a8ce879b6578979a3470430867f85af26b92", + "branch": null, + "revision": "902cc82abc2e8ad23b73c982eed27c63ae3d9384", "version": null } }, @@ -33,8 +33,8 @@ "repositoryURL": "https://github.com/SwiftDocOrg/Markup.git", "state": { "branch": null, - "revision": "9a429d0011d738059bc94f5f92ee406689597a91", - "version": "0.0.3" + "revision": "029ad8c1115ab32b7c20ab52eb092fbc030deb17", + "version": "0.0.4" } }, { @@ -51,8 +51,8 @@ "repositoryURL": "https://github.com/SwiftDocOrg/swift-cmark.git", "state": { "branch": null, - "revision": "1168665f6b36be747ffe6b7b90bc54cfc17f42b7", - "version": "0.28.3+20200207.1168665" + "revision": "2a766030bee955b4806044fd7aca1b6884475138", + "version": "0.28.3+20200110.2a76603" } }, { @@ -95,9 +95,9 @@ "package": "SwiftMarkup", "repositoryURL": "https://github.com/SwiftDocOrg/SwiftMarkup.git", "state": { - "branch": null, - "revision": "431f418ae1833a312646e934a2891e778c1b03b0", - "version": "0.0.5" + "branch": "0.2.0", + "revision": "f395f3bd9e345402cc744aa9051780ed403d3b26", + "version": null } }, { diff --git a/Package.swift b/Package.swift index b5b63b6b..564ddef4 100644 --- a/Package.swift +++ b/Package.swift @@ -12,8 +12,8 @@ let package = Package( dependencies: [ .package(url: "https://github.com/apple/swift-syntax.git", .revision("0.50200.0")), .package(url: "https://github.com/SwiftDocOrg/SwiftSemantics.git", .upToNextMinor(from: "0.1.0")), - .package(url: "https://github.com/SwiftDocOrg/CommonMark.git", .branch("master")), - .package(url: "https://github.com/SwiftDocOrg/SwiftMarkup.git", .upToNextMinor(from: "0.0.5")), + .package(url: "https://github.com/SwiftDocOrg/CommonMark.git", .revision("902cc82abc2e8ad23b73c982eed27c63ae3d9384")), + .package(url: "https://github.com/SwiftDocOrg/SwiftMarkup.git", .revision("0.2.0")), .package(url: "https://github.com/SwiftDocOrg/GraphViz.git", .revision("03405c13dc1c31f50c08bbec6e7587cbee1c7fb3")), .package(url: "https://github.com/NSHipster/HypertextLiteral.git", .upToNextMinor(from: "0.0.2")), .package(url: "https://github.com/SwiftDocOrg/Markup.git", .upToNextMinor(from: "0.0.3")), diff --git a/Sources/swift-doc/Supporting Types/Components/Abstract.swift b/Sources/swift-doc/Supporting Types/Components/Abstract.swift new file mode 100644 index 00000000..f9f68425 --- /dev/null +++ b/Sources/swift-doc/Supporting Types/Components/Abstract.swift @@ -0,0 +1,55 @@ +import CommonMarkBuilder +import SwiftDoc +import SwiftMarkup +import SwiftSemantics +import HypertextLiteral + +struct Abstract: Component { + var symbol: Symbol + + init(for symbol: Symbol) { + self.symbol = symbol + } + + // MARK: - Component + + var fragment: Fragment { + if let summary = symbol.documentation?.summary { + return Fragment { + List.Item { + Paragraph { + Link(urlString: path(for: symbol), text: symbol.id.description) + Text { ":" } + } + + Fragment { + summary + } + } + } + } else { + return Fragment { + List.Item { + Paragraph { + Link(urlString: path(for: symbol), text: symbol.id.description) + } + } + } + } + } + + var html: HypertextLiteral.HTML { + let descriptor = String(describing: type(of: symbol.api)).lowercased() + + return #""" +
+ + \#(softbreak(symbol.id.description)) + +
+
+ \#(commonmark: symbol.documentation?.summary ?? "") +
+ """# + } +} diff --git a/Sources/swift-doc/Supporting Types/Components/Documentation.swift b/Sources/swift-doc/Supporting Types/Components/Documentation.swift index 132ffc89..793388fd 100644 --- a/Sources/swift-doc/Supporting Types/Components/Documentation.swift +++ b/Sources/swift-doc/Supporting Types/Components/Documentation.swift @@ -40,11 +40,7 @@ struct Documentation: Component { Declaration(of: symbol, in: module) ForEach(in: documentation.discussionParts) { part in - if part is SwiftMarkup.Documentation.Callout { - Callout(part as! SwiftMarkup.Documentation.Callout) - } else { - Fragment { "\(part)" } - } + DiscussionPart(part, for: symbol, in: module) } if !documentation.parameters.isEmpty { @@ -100,27 +96,8 @@ struct Documentation: Component { if !documentation.discussionParts.isEmpty { fragments.append(#"""
- \#(documentation.discussionParts.compactMap { part -> HypertextLiteral.HTML? in - if let part = part as? SwiftMarkup.Documentation.Callout { - return Callout(part).html - } else if let part = part as? String { - if part.starts(with: "```"), - let codeBlock = (try? CommonMark.Document(part))?.children.compactMap({ $0 as? CodeBlock }).first, - (codeBlock.fenceInfo ?? "") == "" || - codeBlock.fenceInfo?.compare("swift", options: .caseInsensitive) == .orderedSame, - let source = codeBlock.literal - { - var html = try! SwiftSyntaxHighlighter.highlight(source: source, using: Xcode.self) - html = linkCodeElements(of: html, for: symbol, in: module) - return HTML(html) - } else { - var html = (try! CommonMark.Document(part)).render(format: .html, options: [.unsafe]) - html = linkCodeElements(of: html, for: symbol, in: module) - return HTML(html) - } - } else { - return nil - } + \#(documentation.discussionParts.compactMap { part -> HTML? in + DiscussionPart(part, for: symbol, in: module).html })
"""# as HypertextLiteral.HTML) @@ -197,29 +174,86 @@ struct Documentation: Component { } extension Documentation { - struct Callout: Component { - var callout: SwiftMarkup.Documentation.Callout - - init(_ callout: SwiftMarkup.Documentation.Callout) { - self.callout = callout + struct DiscussionPart: Component { + var symbol: Symbol + var module: Module + var part: SwiftMarkup.DiscussionPart + + init(_ part: SwiftMarkup.DiscussionPart, for symbol: Symbol, in module: Module) { + self.part = part + self.symbol = symbol + self.module = module } // MARK: - Component var fragment: Fragment { - Fragment { - """ - > \(callout.delimiter.rawValue.capitalized): \(callout.content) - """ + switch part { + case .blockQuote(let blockquote): + return Fragment { + blockquote.render(format: .commonmark) + } + case .callout(let callout): + return Fragment { + BlockQuote { + "\(callout.delimiter.rawValue.capitalized): \(callout.content)" + } + } + case .codeBlock(let codeBlock): + return Fragment { + codeBlock.render(format: .commonmark) + } + case .heading(let heading): + return Fragment { + heading.render(format: .commonmark) + } + case .htmlBlock(let htmlBlock): + return Fragment { + htmlBlock.literal ?? "" + } + case .list(let list): + return Fragment { + list.render(format: .commonmark) + } + case .paragraph(let paragraph): + return Fragment { + paragraph.render(format: .commonmark) + } } } var html: HypertextLiteral.HTML { - return #""" - - """# + switch part { + case .blockQuote(let blockquote): + return HTML(blockquote.render(format: .html, options: [.unsafe])) + case .callout(let callout): + return #""" + + """# as HTML + case .codeBlock(let codeBlock): + if (codeBlock.fenceInfo ?? "") == "" || + codeBlock.fenceInfo?.compare("swift", options: .caseInsensitive) == .orderedSame, + let source = codeBlock.literal + { + var html = try! SwiftSyntaxHighlighter.highlight(source: source, using: Xcode.self) + html = linkCodeElements(of: html, for: symbol, in: module) + return HTML(html) + } else { + var html = codeBlock.render(format: .html, options: [.unsafe]) + html = linkCodeElements(of: html, for: symbol, in: module) + return HTML(html) + } + case .heading(let heading): + return HTML(heading.render(format: .html, options: [.unsafe])) + case .htmlBlock(let htmlBlock): + return HTML(htmlBlock.literal ?? "") + case .list(let list): + return HTML(list.render(format: .html, options: [.unsafe])) + case .paragraph(let paragraph): + return HTML(paragraph.render(format: .html, options: [.unsafe])) + } } } } diff --git a/Sources/swift-doc/Supporting Types/Pages/HomePage.swift b/Sources/swift-doc/Supporting Types/Pages/HomePage.swift index b7a976d8..69fb19d9 100644 --- a/Sources/swift-doc/Supporting Types/Pages/HomePage.swift +++ b/Sources/swift-doc/Supporting Types/Pages/HomePage.swift @@ -11,7 +11,7 @@ struct HomePage: Page { var structures: [Symbol] = [] var protocols: [Symbol] = [] var operators: [Symbol] = [] - var globalTypealias: [Symbol] = [] + var globalTypealiases: [Symbol] = [] var globalFunctions: [Symbol] = [] var globalVariables: [Symbol] = [] @@ -29,7 +29,7 @@ struct HomePage: Page { case is Protocol: protocols.append(symbol) case is Typealias: - globalTypealias.append(symbol) + globalTypealiases.append(symbol) case is Operator: operators.append(symbol) case let function as Function where function.isOperator: @@ -51,48 +51,20 @@ struct HomePage: Page { } var document: CommonMark.Document { - let types = classes + enumerations + structures - let typeNames = Set(types.map { $0.id.description }) - let protocolNames = Set(protocols.map { $0.id.description }) - let operatorNames = Set(operators.map { $0.id.description }) - - let globalTypealiasNames = Set(globalTypealias.map { $0.id.description }) - let globalFunctionNames = Set(globalFunctions.map { $0.id.description }) - let globalVariableNames = Set(globalVariables.map { $0.id.description }) - return Document { ForEach(in: [ - ("Types", typeNames), - ("Protocols", protocolNames), - ("Operators", operatorNames) - ]) { (heading, names) in - if (!names.isEmpty) { + ("Types", classes + enumerations + structures), + ("Protocols", protocols), + ("Operators", operators), + ("Global Typealiases", globalTypealiases), + ("Global Functions", globalFunctions), + ("Global Variables", globalVariables), + ]) { (heading, symbols) in + if (!symbols.isEmpty) { Heading { heading } - List(of: names.sorted()) { name in - Link(urlString: path(for: name), text: name) - } - } - } - - if !globalTypealiasNames.isEmpty || - !globalFunctionNames.isEmpty || - !globalVariableNames.isEmpty - { - Heading { "Globals" } - Section { - ForEach(in: [ - ("Typealiases", globalTypealiasNames), - ("Functions", globalFunctionNames), - ("Variables", globalVariableNames) - ]) { (heading, names) in - if (!names.isEmpty) { - Heading { heading } - - List(of: names.sorted()) { name in - Link(urlString: path(for: name), text: softbreak(name)) - } - } + List(of: symbols.sorted()) { symbol in + Abstract(for: symbol).fragment } } } @@ -106,73 +78,21 @@ struct HomePage: Page { ("Structures", structures), ("Enumerations", enumerations), ("Protocols", protocols), + ("Typealiases", globalTypealiases), + ("Functions", globalFunctions), + ("Variables", globalVariables) ].compactMap { (heading, symbols) -> HypertextLiteral.HTML? in guard !symbols.isEmpty else { return nil } return #"""

\#(heading)

- \#(listHTML(symbols: symbols)) +
+ \#(symbols.sorted().map { Abstract(for: $0).html }) +
"""# }) - \#(globalsHTML) - """# - } - - private var globalsHTML: HypertextLiteral.HTML { - guard !globalTypealias.isEmpty || - !globalFunctions.isEmpty || - !globalVariables.isEmpty else { - return "" - } - - let heading = "Globals" - return #""" -
-

\#(heading)

- \#(globalsListHTML) -
- """# - } - - private var globalsListHTML: HypertextLiteral.HTML { - let globals = [ - ("Typealiases", globalTypealias), - ("Functions", globalFunctions), - ("Variables", globalVariables), - ] - return #""" - \#(globals.compactMap { (heading, symbols) -> HypertextLiteral.HTML? in - guard !symbols.isEmpty else { return nil } - - return #""" -
-

\#(heading)

- \#(listHTML(symbols: symbols)) -
- """# - }) - """# - } - - private func listHTML(symbols: [Symbol]) -> HypertextLiteral.HTML { - #""" -
- \#(symbols.sorted().map { symbol -> HypertextLiteral.HTML in - let descriptor = String(describing: type(of: symbol.api)).lowercased() - return #""" -
- - \#(softbreak(symbol.id.description)) - -
-
- \#(commonmark: symbol.documentation?.summary ?? "") -
- """# - }) -
"""# } }