Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnSundell committed Dec 30, 2019
0 parents commit 5c37a2e
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
/.build
Package.resolved
.swiftpm
/*.xcodeproj
xcuserdata/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 John Sundell

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// swift-tools-version:5.1

/**
* Splash-plugin for Publish
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/

import PackageDescription

let package = Package(
name: "SplashPublishPlugin",
products: [
.library(
name: "SplashPublishPlugin",
targets: ["SplashPublishPlugin"]
)
],
dependencies: [
.package(url: "git@github.com:johnsundell/publish.git", .branch("master")),
.package(url: "https://github.com/johnsundell/splash.git", from: "0.9.0")
],
targets: [
.target(
name: "SplashPublishPlugin",
dependencies: ["Splash", "Publish"]
),
.testTarget(
name: "SplashPublishPluginTests",
dependencies: ["SplashPublishPlugin"]
),
]
)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Splash plugin for Publish

A [Publish](https://github.com/johnsundell/publish) plugin that makes it easy to integrate the [Splash](https://github.com/johnsundell/splash) Swift syntax highlighter into any Publish website.
44 changes: 44 additions & 0 deletions Sources/SplashPublishPlugin/SplashPublishPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Splash-plugin for Publish
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/

import Publish
import Splash
import Ink

public extension Plugin {
static func splash(withClassPrefix classPrefix: String) -> Self {
Plugin(name: "Splash") { context in
context.markdownParser.addModifier(
.splashCodeBlocks(withFormat: HTMLOutputFormat(
classPrefix: classPrefix
))
)
}
}
}

public extension Modifier {
static func splashCodeBlocks(withFormat format: HTMLOutputFormat = .init()) -> Self {
let highlighter = SyntaxHighlighter(format: format)

return Modifier(target: .codeBlocks) { html, markdown in
var markdown = markdown.dropFirst("```".count)

guard !markdown.hasPrefix("no-highlight") else {
return html
}

markdown = markdown
.drop(while: { !$0.isNewline })
.dropFirst()
.dropLast("\n```".count)

let highlighted = highlighter.highlight(String(markdown))
return "<pre><code>" + highlighted + "\n</code></pre>"
}
}
}

13 changes: 13 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Splash-plugin for Publish
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/

import XCTest

import SplashPublishPluginTests

var tests = [XCTestCaseEntry]()
tests += SplashPublishPluginTests.allTests()
XCTMain(tests)
35 changes: 35 additions & 0 deletions Tests/SplashPublishPluginTests/SplashPublishPluginTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Splash-plugin for Publish
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/

import XCTest
import SplashPublishPlugin
import Ink

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>
""")
}

static var allTests = [
("testHighlightingMarkdown", testHighlightingMarkdown)
]
}
15 changes: 15 additions & 0 deletions Tests/SplashPublishPluginTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Splash-plugin for Publish
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/

import XCTest

#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(SplashPublishPluginTests.allTests),
]
}
#endif

0 comments on commit 5c37a2e

Please sign in to comment.