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

Commit 85b5872

Browse files
authored
Merge pull request #21 from SwiftDocOrg/html
Add support for HTML output
2 parents b5274fb + 746356f commit 85b5872

35 files changed

+2274
-473
lines changed

.github/workflows/ci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ jobs:
2323
steps:
2424
- name: Checkout
2525
uses: actions/checkout@v1
26+
- name: Install System Dependencies
27+
run: |
28+
apt-get update
29+
apt-get install -y libxml2-dev
2630
- name: Build and Test
2731
run: swift test --enable-test-discovery

Package.resolved

+42-6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,31 @@
1515
"repositoryURL": "https://github.com/SwiftDocOrg/GraphViz.git",
1616
"state": {
1717
"branch": null,
18-
"revision": "08e0cddd013fa2272379d27ec3e0093db51f34fb",
19-
"version": "0.1.0"
18+
"revision": "03405c13dc1c31f50c08bbec6e7587cbee1c7fb3",
19+
"version": null
20+
}
21+
},
22+
{
23+
"package": "HypertextLiteral",
24+
"repositoryURL": "https://github.com/NSHipster/HypertextLiteral.git",
25+
"state": {
26+
"branch": null,
27+
"revision": "3e45da849e507d171c7264146176bb834a01be4f",
28+
"version": "0.0.2"
29+
}
30+
},
31+
{
32+
"package": "Markup",
33+
"repositoryURL": "https://github.com/SwiftDocOrg/Markup.git",
34+
"state": {
35+
"branch": null,
36+
"revision": "9a429d0011d738059bc94f5f92ee406689597a91",
37+
"version": "0.0.3"
2038
}
2139
},
2240
{
2341
"package": "swift-argument-parser",
24-
"repositoryURL": "https://github.com/apple/swift-argument-parser",
42+
"repositoryURL": "https://github.com/apple/swift-argument-parser.git",
2543
"state": {
2644
"branch": null,
2745
"revision": "8d31a0905c346a45c87773ad50862b5b3df8dff6",
@@ -37,6 +55,15 @@
3755
"version": "0.28.3+20200207.1168665"
3856
}
3957
},
58+
{
59+
"package": "HTMLEntities",
60+
"repositoryURL": "https://github.com/IBM-Swift/swift-html-entities.git",
61+
"state": {
62+
"branch": null,
63+
"revision": "744c094976355aa96ca61b9b60ef0a38e979feb7",
64+
"version": "3.0.14"
65+
}
66+
},
4067
{
4168
"package": "SwiftSyntax",
4269
"repositoryURL": "https://github.com/apple/swift-syntax.git",
@@ -51,16 +78,25 @@
5178
"repositoryURL": "https://github.com/SwiftDocOrg/SwiftMarkup.git",
5279
"state": {
5380
"branch": null,
54-
"revision": "8e82d625b0342fc80525956c22f9f0defa0cffce",
55-
"version": "0.0.4"
81+
"revision": "431f418ae1833a312646e934a2891e778c1b03b0",
82+
"version": "0.0.5"
5683
}
5784
},
5885
{
5986
"package": "SwiftSemantics",
6087
"repositoryURL": "https://github.com/SwiftDocOrg/SwiftSemantics.git",
6188
"state": {
62-
"branch": "swift-5.2",
89+
"branch": null,
6390
"revision": "4fdc48bddbbb8311079ed111e5a4f2b92423b05c",
91+
"version": "0.1.0"
92+
}
93+
},
94+
{
95+
"package": "SwiftSyntaxHighlighter",
96+
"repositoryURL": "https://github.com/NSHipster/SwiftSyntaxHighlighter.git",
97+
"state": {
98+
"branch": "1.0.0",
99+
"revision": "4a20d10bba17241b66650d99081801536146b43c",
64100
"version": null
65101
}
66102
}

Package.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@ import PackageDescription
66
let package = Package(
77
name: "swift-doc",
88
products: [
9+
.executable(name: "swift-doc", targets: ["swift-doc"]),
910
.library(name: "SwiftDoc", targets: ["SwiftDoc"])
1011
],
1112
dependencies: [
1213
.package(url: "https://github.com/apple/swift-syntax.git", .revision("0.50200.0")),
1314
.package(url: "https://github.com/SwiftDocOrg/SwiftSemantics.git", .upToNextMinor(from: "0.1.0")),
1415
.package(url: "https://github.com/SwiftDocOrg/CommonMark.git", .branch("master")),
15-
.package(url: "https://github.com/SwiftDocOrg/SwiftMarkup.git", .upToNextMinor(from: "0.0.4")),
16-
.package(url: "https://github.com/SwiftDocOrg/GraphViz.git", .upToNextMinor(from: "0.1.0")),
17-
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "0.0.2")),
16+
.package(url: "https://github.com/SwiftDocOrg/SwiftMarkup.git", .upToNextMinor(from: "0.0.5")),
17+
.package(url: "https://github.com/SwiftDocOrg/GraphViz.git", .revision("03405c13dc1c31f50c08bbec6e7587cbee1c7fb3")),
18+
.package(url: "https://github.com/NSHipster/HypertextLiteral.git", .upToNextMinor(from: "0.0.2")),
19+
.package(url: "https://github.com/SwiftDocOrg/Markup.git", .upToNextMinor(from: "0.0.3")),
20+
.package(url: "https://github.com/NSHipster/SwiftSyntaxHighlighter.git", .revision("1.0.0")),
21+
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "0.0.2")),
1822
],
1923
targets: [
2024
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2125
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2226
.target(
2327
name: "swift-doc",
24-
dependencies: ["ArgumentParser", "SwiftDoc", "SwiftSemantics", "SwiftMarkup", "CommonMarkBuilder", "DCOV", "GraphViz"]
28+
dependencies: ["ArgumentParser", "SwiftDoc", "SwiftSemantics", "SwiftMarkup", "CommonMarkBuilder", "HypertextLiteral", "Markup", "DCOV", "GraphViz", "SwiftSyntaxHighlighter"]
2529
),
2630
.target(
2731
name: "DCOV",

README.md

+19-120
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@
44

55
A package for generating documentation for Swift projects.
66

7-
**This project is under active development
8-
and is expected to change significantly before its first stable release.**
9-
107
Given a directory of Swift files,
11-
`swift-doc` generates CommonMark (Markdown) files
8+
`swift-doc` generates HTML or CommonMark (Markdown) files
129
for each class, structure, enumeration, and protocol
1310
as well as top-level type aliases, functions, and variables.
1411

15-
For an example of generated documentation,
16-
[check out the Wiki for our fork of Alamofire][alamofire wiki].
12+
**Example Output**
1713

18-
> **Note**:
19-
> Output is currently limited to CommonMark,
20-
> but the plan is to support HTML and other formats as well.
14+
- [HTML][swiftsemantics html]
15+
- [CommonMark / GitHub Wiki][alamofire wiki]
2116

2217
## Requirements
2318

@@ -54,18 +49,26 @@ collecting all Swift files into a single "module"
5449
and generating documentation accordingly.
5550

5651
```terminal
57-
$ swift doc generate path/to/SwiftProject/Sources --output Documentation
58-
$ tree Documentation
59-
$ Documentation/
52+
$ swift doc generate path/to/SwiftProject/Sources
53+
$ tree .build/documentation
54+
$ documentation/
6055
├── Home
6156
├── (...)
6257
├── _Footer.md
6358
└── _Sidebar.md
6459
```
6560

6661
By default,
67-
output files are written to `.build/documentation`,
68-
but you can change that with the `--output` option flag.
62+
output files are written to `.build/documentation`
63+
in CommonMark / GitHub Wiki format,
64+
but you can change that with the `--output` and `--format` option flags.
65+
66+
```terminal
67+
$ swift doc generate path/to/SwiftProject/Sources --output Documentation --format html
68+
$ Documentation/
69+
├── (...)
70+
└── index.html
71+
```
6972

7073
#### swift-doc coverage
7174

@@ -178,114 +181,9 @@ jobs:
178181
with:
179182
path: "Documentation"
180183
env:
181-
GITHUB_PERSONAL_ACCESS_TOKEN: ${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }}
182-
```
183-
184-
* * *
185-
186-
## Motivation
187-
188-
From its earliest days,
189-
Swift has been fortunate to have [Jazzy][jazzy],
190-
which is a fantastic tool for generating documentation
191-
for both Swift and Objective-C projects.
192-
Over time, however,
193-
the way we write Swift code —
194-
and indeed the language itself —
195-
has evolved to incorporate patterns and features
196-
that are difficult to understand using
197-
the same documentation standards that served us well for Objective-C.
198-
199-
Whereas in Objective-C,
200-
you could get a complete view of a type's functionality from its class hierarchy,
201-
Swift code today tends to layer and distribute functionality across
202-
[a network of types][swift number protocols diagram].
203-
While adopting a
204-
[protocol-oriented paradigm][protocol-oriented programming]
205-
can make Swift easier and more expressive to write,
206-
it can also make Swift code more difficult to understand.
207-
208-
Our primary goal for `swift-doc`
209-
is to make Swift documentation more useful
210-
by surfacing the information you need to understand how an API works
211-
and presenting it in a way that can be easily searched and accessed.
212-
We want developers to be empowered to use Swift packages to their full extent,
213-
without being reliant on (often outdated) blog posts or Stack Overflow threads.
214-
We want documentation coverage to become as important as test coverage:
215-
a valuable metric for code quality,
216-
and an expected part of first-rate open source projects.
217-
218-
Jazzy styles itself after Apple's official documentation circa 2014
219-
(code-named "Jazz", as it were),
220-
which was well-suited to understanding Swift code as we wrote it back then
221-
when it was more similar to Objective-C.
222-
But this design is less capable of documenting the behavior of
223-
generically-constrained types,
224-
default implementations,
225-
[dynamic member lookup][se-0195],
226-
[property wrappers][se-o258], or
227-
[function builders][se-xxxx].
228-
(Alas,
229-
Apple's [most recent take][apple documentation] on reference documentation
230-
hasn't improved matters,
231-
having instead focused on perceived cosmetic issues.)
232-
233-
Without much in the way of strong design guidance,
234-
we're not entirely sure what Swift documentation _should_ look like.
235-
But we do think plain text is a good place to start.
236-
We look forward to
237-
soliciting feedback and ideas from everyone
238-
so that we can identify those needs
239-
and figure out the best ways to meet them.
240-
241-
In the meantime,
242-
we've set ourselves up for success
243-
by investing in the kind of foundation we'll need
244-
to build whatever we decide best solves the problems at hand.
245-
`swift-doc` is built on top of a constellation of projects
246-
that take advantage of modern infrastructure and tooling:
247-
248-
- [SwiftSemantics][swiftsemantics]:
249-
Parses Swift code into its constituent declarations
250-
using [SwiftSyntax][swiftsyntax]
251-
- [SwiftMarkup][swiftmarkup]:
252-
Parses Swift documentation comments into structured entities
253-
using [CommonMark][commonmark]
254-
- [github-wiki-publish-action][github-wiki-publish-action]:
255-
Publishes the contents of a directory to your project's wiki
256-
257-
These new technologies have already yielded some promising results.
258-
`swift-doc` is built in Swift,
259-
and can be installed on both macOS and Linux as a small, standalone binary.
260-
Because it relies only on a syntactic reading of Swift source code,
261-
without needing code first to be compiled,
262-
`swift-doc` is quite fast.
263-
As a baseline,
264-
compare its performance to Jazzy
265-
when generating documentation for [SwiftSemantics][swiftsemantics]:
266-
267-
```terminal
268-
$ cd SwiftSemantics
269-
270-
$ time swift-doc Sources
271-
0.21 real 0.16 user 0.02 sys
272-
273-
$ time jazzy # fresh build
274-
jam out ♪♫ to your fresh new docs in `docs`
275-
67.36 real 98.76 user 8.89 sys
276-
277-
278-
$ time jazzy # with build cache
279-
jam out ♪♫ to your fresh new docs in `docs`
280-
17.70 real 2.17 user 0.88 sys
184+
GH_PERSONAL_ACCESS_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
281185
```
282186
283-
Of course,
284-
some of that is simply Jazzy doing more,
285-
generating HTML, CSS, and a search index instead of just text.
286-
Compare its [generated HTML output][jazzy swiftsemantics]
287-
to [a GitHub wiki generated with `swift-doc`][swift-doc swiftsemantics].
288-
289187
## License
290188
291189
MIT
@@ -297,6 +195,7 @@ Mattt ([@mattt](https://twitter.com/mattt))
297195
[ci badge]: https://github.com/SwiftDocOrg/swift-doc/workflows/CI/badge.svg
298196
299197
[alamofire wiki]: https://github.com/SwiftDocOrg/Alamofire/wiki
198+
[swiftsemantics html]: https://swift-doc-preview.netlify.app
300199
[github wiki]: https://help.github.com/en/github/building-a-strong-community/about-wikis
301200
[github actions]: https://github.com/features/actions
302201
[swiftsyntax]: https://github.com/apple/swift-syntax

Sources/SwiftDoc/Extensions/Array+Parallel.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import Dispatch
22

3-
extension Array {
3+
public extension RandomAccessCollection {
44
func parallelMap<T>(_ transform: (Element) throws -> T) throws -> [T] {
55
guard count > 1 else {
66
return try map(transform)
77
}
88

9-
var results = [(index: Int, result: Result<T, Error>)]()
9+
let indices = Array(self.indices)
10+
11+
var results = [(index: Index, result: Result<T, Error>)]()
1012
results.reserveCapacity(count)
1113

12-
let queue = DispatchQueue(label: "org.swiftdoc.swift-doc.parallelMap")
14+
let queue = DispatchQueue(label: #function)
1315
withoutActuallyEscaping(transform) { escapingtransform in
14-
DispatchQueue.concurrentPerform(iterations: count) { (index) in
16+
DispatchQueue.concurrentPerform(iterations: count) { (iteration) in
17+
let index = indices[iteration]
18+
1519
do {
1620
let transformed = try escapingtransform(self[index])
1721
queue.sync {
@@ -36,4 +40,8 @@ extension Array {
3640
func parallelFlatMap<T>(transform: (Element) throws -> [T]) throws -> [T] {
3741
return try parallelMap(transform).flatMap { $0 }
3842
}
43+
44+
func parallelForEach(_ body: (Element) throws -> Void) throws {
45+
_ = try parallelMap(body)
46+
}
3947
}

0 commit comments

Comments
 (0)