Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 3e11a0c

Browse files
authored
Merge pull request #97 from SwiftDocOrg/commonmark-home-abstract
Include symbol summaries on Home page in CommonMark format
2 parents bedc0b3 + 240bfd5 commit 3e11a0c

File tree

5 files changed

+158
-149
lines changed

5 files changed

+158
-149
lines changed

Package.resolved

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ let package = Package(
1212
dependencies: [
1313
.package(url: "https://github.com/apple/swift-syntax.git", .revision("0.50200.0")),
1414
.package(url: "https://github.com/SwiftDocOrg/SwiftSemantics.git", .upToNextMinor(from: "0.1.0")),
15-
.package(url: "https://github.com/SwiftDocOrg/CommonMark.git", .branch("master")),
16-
.package(url: "https://github.com/SwiftDocOrg/SwiftMarkup.git", .upToNextMinor(from: "0.0.5")),
15+
.package(url: "https://github.com/SwiftDocOrg/CommonMark.git", .revision("902cc82abc2e8ad23b73c982eed27c63ae3d9384")),
16+
.package(url: "https://github.com/SwiftDocOrg/SwiftMarkup.git", .revision("0.2.0")),
1717
.package(url: "https://github.com/SwiftDocOrg/GraphViz.git", .revision("03405c13dc1c31f50c08bbec6e7587cbee1c7fb3")),
1818
.package(url: "https://github.com/NSHipster/HypertextLiteral.git", .upToNextMinor(from: "0.0.2")),
1919
.package(url: "https://github.com/SwiftDocOrg/Markup.git", .upToNextMinor(from: "0.0.3")),
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import CommonMarkBuilder
2+
import SwiftDoc
3+
import SwiftMarkup
4+
import SwiftSemantics
5+
import HypertextLiteral
6+
7+
struct Abstract: Component {
8+
var symbol: Symbol
9+
10+
init(for symbol: Symbol) {
11+
self.symbol = symbol
12+
}
13+
14+
// MARK: - Component
15+
16+
var fragment: Fragment {
17+
if let summary = symbol.documentation?.summary {
18+
return Fragment {
19+
List.Item {
20+
Paragraph {
21+
Link(urlString: path(for: symbol), text: symbol.id.description)
22+
Text { ":" }
23+
}
24+
25+
Fragment {
26+
summary
27+
}
28+
}
29+
}
30+
} else {
31+
return Fragment {
32+
List.Item {
33+
Paragraph {
34+
Link(urlString: path(for: symbol), text: symbol.id.description)
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
var html: HypertextLiteral.HTML {
42+
let descriptor = String(describing: type(of: symbol.api)).lowercased()
43+
44+
return #"""
45+
<dt class="\#(descriptor)">
46+
<a href=\#(path(for: symbol)) title="\#(descriptor) - \#(symbol.id.description)">
47+
\#(softbreak(symbol.id.description))
48+
</a>
49+
</dt>
50+
<dd>
51+
\#(commonmark: symbol.documentation?.summary ?? "")
52+
</dd>
53+
"""#
54+
}
55+
}

Sources/swift-doc/Supporting Types/Components/Documentation.swift

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ struct Documentation: Component {
4040
Declaration(of: symbol, in: module)
4141

4242
ForEach(in: documentation.discussionParts) { part in
43-
if part is SwiftMarkup.Documentation.Callout {
44-
Callout(part as! SwiftMarkup.Documentation.Callout)
45-
} else {
46-
Fragment { "\(part)" }
47-
}
43+
DiscussionPart(part, for: symbol, in: module)
4844
}
4945

5046
if !documentation.parameters.isEmpty {
@@ -100,27 +96,8 @@ struct Documentation: Component {
10096
if !documentation.discussionParts.isEmpty {
10197
fragments.append(#"""
10298
<div class="discussion">
103-
\#(documentation.discussionParts.compactMap { part -> HypertextLiteral.HTML? in
104-
if let part = part as? SwiftMarkup.Documentation.Callout {
105-
return Callout(part).html
106-
} else if let part = part as? String {
107-
if part.starts(with: "```"),
108-
let codeBlock = (try? CommonMark.Document(part))?.children.compactMap({ $0 as? CodeBlock }).first,
109-
(codeBlock.fenceInfo ?? "") == "" ||
110-
codeBlock.fenceInfo?.compare("swift", options: .caseInsensitive) == .orderedSame,
111-
let source = codeBlock.literal
112-
{
113-
var html = try! SwiftSyntaxHighlighter.highlight(source: source, using: Xcode.self)
114-
html = linkCodeElements(of: html, for: symbol, in: module)
115-
return HTML(html)
116-
} else {
117-
var html = (try! CommonMark.Document(part)).render(format: .html, options: [.unsafe])
118-
html = linkCodeElements(of: html, for: symbol, in: module)
119-
return HTML(html)
120-
}
121-
} else {
122-
return nil
123-
}
99+
\#(documentation.discussionParts.compactMap { part -> HTML? in
100+
DiscussionPart(part, for: symbol, in: module).html
124101
})
125102
</div>
126103
"""# as HypertextLiteral.HTML)
@@ -197,29 +174,86 @@ struct Documentation: Component {
197174
}
198175

199176
extension Documentation {
200-
struct Callout: Component {
201-
var callout: SwiftMarkup.Documentation.Callout
202-
203-
init(_ callout: SwiftMarkup.Documentation.Callout) {
204-
self.callout = callout
177+
struct DiscussionPart: Component {
178+
var symbol: Symbol
179+
var module: Module
180+
var part: SwiftMarkup.DiscussionPart
181+
182+
init(_ part: SwiftMarkup.DiscussionPart, for symbol: Symbol, in module: Module) {
183+
self.part = part
184+
self.symbol = symbol
185+
self.module = module
205186
}
206187

207188
// MARK: - Component
208189

209190
var fragment: Fragment {
210-
Fragment {
211-
"""
212-
> \(callout.delimiter.rawValue.capitalized): \(callout.content)
213-
"""
191+
switch part {
192+
case .blockQuote(let blockquote):
193+
return Fragment {
194+
blockquote.render(format: .commonmark)
195+
}
196+
case .callout(let callout):
197+
return Fragment {
198+
BlockQuote {
199+
"\(callout.delimiter.rawValue.capitalized): \(callout.content)"
200+
}
201+
}
202+
case .codeBlock(let codeBlock):
203+
return Fragment {
204+
codeBlock.render(format: .commonmark)
205+
}
206+
case .heading(let heading):
207+
return Fragment {
208+
heading.render(format: .commonmark)
209+
}
210+
case .htmlBlock(let htmlBlock):
211+
return Fragment {
212+
htmlBlock.literal ?? ""
213+
}
214+
case .list(let list):
215+
return Fragment {
216+
list.render(format: .commonmark)
217+
}
218+
case .paragraph(let paragraph):
219+
return Fragment {
220+
paragraph.render(format: .commonmark)
221+
}
214222
}
215223
}
216224

217225
var html: HypertextLiteral.HTML {
218-
return #"""
219-
<aside class=\#(callout.delimiter.rawValue)>
220-
\#(commonmark: callout.content)
221-
</aside>
222-
"""#
226+
switch part {
227+
case .blockQuote(let blockquote):
228+
return HTML(blockquote.render(format: .html, options: [.unsafe]))
229+
case .callout(let callout):
230+
return #"""
231+
<aside class=\#(callout.delimiter.rawValue)>
232+
\#(commonmark: callout.content)
233+
</aside>
234+
"""# as HTML
235+
case .codeBlock(let codeBlock):
236+
if (codeBlock.fenceInfo ?? "") == "" ||
237+
codeBlock.fenceInfo?.compare("swift", options: .caseInsensitive) == .orderedSame,
238+
let source = codeBlock.literal
239+
{
240+
var html = try! SwiftSyntaxHighlighter.highlight(source: source, using: Xcode.self)
241+
html = linkCodeElements(of: html, for: symbol, in: module)
242+
return HTML(html)
243+
} else {
244+
var html = codeBlock.render(format: .html, options: [.unsafe])
245+
html = linkCodeElements(of: html, for: symbol, in: module)
246+
return HTML(html)
247+
}
248+
case .heading(let heading):
249+
return HTML(heading.render(format: .html, options: [.unsafe]))
250+
case .htmlBlock(let htmlBlock):
251+
return HTML(htmlBlock.literal ?? "")
252+
case .list(let list):
253+
return HTML(list.render(format: .html, options: [.unsafe]))
254+
case .paragraph(let paragraph):
255+
return HTML(paragraph.render(format: .html, options: [.unsafe]))
256+
}
223257
}
224258
}
225259
}

0 commit comments

Comments
 (0)