Skip to content

Commit 5001ec3

Browse files
author
Jesse Haigh
committed
add code block options onto RenderBlockContent.CodeListing
1 parent a511fb5 commit 5001ec3

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

Sources/SwiftDocC/Checker/Checkers/InvalidCodeBlockOption.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ public import Markdown
1818
public struct InvalidCodeBlockOption: Checker {
1919
public var problems = [Problem]()
2020

21-
// FIXME: populate this from the parse options
2221
/// Parsing options for code blocks
23-
private let knownOptions = ["nocopy"]
22+
private let knownOptions = RenderBlockContent.CodeListing.knownOptions
2423

2524
private var sourceFile: URL?
2625

@@ -56,7 +55,6 @@ public struct InvalidCodeBlockOption: Checker {
5655
replacements: []
5756
)
5857
}
59-
// FIXME: figure out the position of 'token' and provide solutions
6058
problems.append(Problem(diagnostic: diagnostic, possibleSolutions: possibleSolutions))
6159
}
6260
}

Sources/SwiftDocC/Model/Rendering/Content/RenderBlockContent.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ public enum RenderBlockContent: Equatable {
126126
public var metadata: RenderContentMetadata?
127127
public var copyToClipboard: Bool = true
128128

129+
public enum OptionName: String, CaseIterable {
130+
case nocopy
131+
132+
init?<S: StringProtocol>(caseInsensitive raw: S) {
133+
self.init(rawValue: raw.lowercased())
134+
}
135+
}
136+
137+
public static var knownOptions: Set<String> {
138+
Set(OptionName.allCases.map(\.rawValue))
139+
}
140+
129141
/// Make a new `CodeListing` with the given data.
130142
public init(syntax: String?, code: [String], metadata: RenderContentMetadata?, copyToClipboard: Bool) {
131143
self.syntax = syntax

Sources/SwiftDocC/Model/Rendering/RenderContentCompiler.swift

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,42 @@ struct RenderContentCompiler: MarkupVisitor {
5050

5151
if FeatureFlags.current.isExperimentalCodeBlockEnabled {
5252

53-
struct ParsedOptions {
54-
var lang: String?
55-
var nocopy = false
56-
}
57-
58-
func parseLanguageString(_ input: String?) -> ParsedOptions {
59-
guard let input else { return ParsedOptions() }
60-
53+
func parseLanguageString(_ input: String?) -> (lang: String? , tokens: [RenderBlockContent.CodeListing.OptionName]) {
54+
guard let input else { return (lang: nil, tokens: []) }
6155
let parts = input
6256
.split(separator: ",")
6357
.map { $0.trimmingCharacters(in: .whitespaces) }
64-
65-
var options = ParsedOptions()
58+
var lang: String? = nil
59+
var options: [RenderBlockContent.CodeListing.OptionName] = []
6660

6761
for part in parts {
68-
let lower = part.lowercased()
69-
if lower == "nocopy" {
70-
options.nocopy = true
71-
} else if options.lang == nil {
72-
options.lang = part
62+
if let opt = RenderBlockContent.CodeListing.OptionName(caseInsensitive: part) {
63+
options.append(opt)
64+
} else if lang == nil {
65+
lang = String(part)
7366
}
7467
}
75-
return options
68+
return (lang, options)
7669
}
7770

7871
let options = parseLanguageString(codeBlock.language)
7972

80-
return [RenderBlockContent.codeListing(.init(syntax: options.lang ?? bundle.info.defaultCodeListingLanguage, code: codeBlock.code.splitByNewlines, metadata: nil, copyToClipboard: !options.nocopy))]
73+
var listing = RenderBlockContent.CodeListing(
74+
syntax: options.lang ?? bundle.info.defaultCodeListingLanguage,
75+
code: codeBlock.code.splitByNewlines,
76+
metadata: nil,
77+
copyToClipboard: true // default value
78+
)
79+
80+
// apply code block options
81+
for option in options.tokens {
82+
switch option {
83+
case .nocopy:
84+
listing.copyToClipboard = false
85+
}
86+
}
87+
88+
return [RenderBlockContent.codeListing(listing)]
8189

8290
} else {
8391
return [RenderBlockContent.codeListing(.init(syntax: codeBlock.language ?? bundle.info.defaultCodeListingLanguage, code: codeBlock.code.splitByNewlines, metadata: nil, copyToClipboard: false))]

Tests/SwiftDocCTests/Checker/Checkers/InvalidCodeBlockOptionTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ import Markdown
1414

1515
class InvalidCodeBlockOptionTests: XCTestCase {
1616

17+
func testNoOptions() {
18+
let markupSource = """
19+
```
20+
let a = 1
21+
```
22+
"""
23+
let document = Document(parsing: markupSource, options: [])
24+
var checker = InvalidCodeBlockOption(sourceFile: nil)
25+
checker.visit(document)
26+
XCTAssertTrue(checker.problems.isEmpty)
27+
XCTAssertEqual(RenderBlockContent.CodeListing.knownOptions, ["nocopy"])
28+
}
29+
1730
func testOption() {
1831
let markupSource = """
1932
```nocopy

0 commit comments

Comments
 (0)