-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSplashPublishPluginTests.swift
106 lines (94 loc) · 2.98 KB
/
SplashPublishPluginTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Splash-plugin for Publish
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/
import XCTest
import SplashPublishPlugin
import Ink
import Splash
final class SplashPublishPluginTests: XCTestCase {
func testHighlightingMarkdown() {
let parser = MarkdownParser(modifiers: [.splashCodeBlocks()])
let html = parser.html(from: """
Some text
```
let int = 7
```
More text
```no-highlight
not.highlighted()
```
""")
XCTAssertEqual(html, """
<p>Some text</p><pre><code><span class="keyword">let</span> int = <span class="number">7</span>
</code></pre><p>More text</p><pre><code class="language-no-highlight">not.highlighted()
</code></pre>
""")
}
func testHighlightingMultipleGrammarsMarkdown() {
let parser = MarkdownParser(modifiers: [.splashCodeBlocks(withGrammars: [(TestGrammar(), "test"), (SwiftGrammar(), "swift")])])
let html = parser.html(from: """
Some text
```test
some should be a keyword
```
here's some fake text
```swift
let int = 7
```
fake text
```no-highlight
not.highlighted()
```
""")
XCTAssertEqual(html, """
<p>Some text</p><pre><code><span class=\"keyword\">some</span> should be a keyword\n</code></pre><p>here\'s some fake text</p><pre><code><span class=\"keyword\">let</span> int = <span class=\"number\">7</span>\n</code></pre><p>fake text</p><pre><code class=\"language-no-highlight\">not.highlighted()\n</code></pre>
""")
}
static var allTests = [
("testHighlightingMarkdown", testHighlightingMarkdown)
]
}
public struct TestGrammar: Grammar {
public var delimiters: CharacterSet
public var syntaxRules: [SyntaxRule]
public init() {
var delimiters = CharacterSet.alphanumerics.inverted
delimiters.remove("_")
delimiters.remove("-")
delimiters.remove("\"")
delimiters.remove("#")
delimiters.remove("@")
delimiters.remove("$")
self.delimiters = delimiters
syntaxRules = [
KeywordRule(),
]
}
public func isDelimiter(_ delimiterA: Character, mergableWith delimiterB: Character) -> Bool {
switch (delimiterA, delimiterB) {
case (_, ":"):
return false
case (":", "/"):
return true
case (":", _):
return false
case ("-", _):
return false
case ("#", _):
return false
default:
return true
}
}
static let keywords = ([
"some", "fake", "keywords"
] as Set<String>)
struct KeywordRule: SyntaxRule {
var tokenType: TokenType { return .keyword }
func matches(_ segment: Segment) -> Bool {
return keywords.contains(segment.tokens.current)
}
}
}