A syntax highlighter for Swift code that uses SwiftSyntax to generate Pygments-compatible HTML.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 313c9c6 Dec 2, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sources Remove print statement Dec 2, 2018
.gitignore Ignore built executable and archive Dec 2, 2018
LICENSE.md Initial commit Oct 22, 2018
Makefile Add @ prefixes to commands Dec 2, 2018
Package.resolved Initial commit Oct 22, 2018
Package.swift Rename executable to swift-syntax-highlight Dec 2, 2018
README.md Update README.md Dec 2, 2018

README.md

SwiftSyntaxHighlighter

A syntax highlighter for Swift code that uses SwiftSyntax to generate Pygments-compatible HTML. You can use it either from the command-line, via the swift-syntax-highlight executable or in Swift code using the SwiftSyntaxHighlighter module.

This functionality is discussed in the NSHipster article SwiftSyntax.

Requirements

  • Xcode 10.0+ and Swift 4.2+

Command-Line Usage

The swift-syntax-highlight executable can be run from the command line to highlight either a path to a source file or source code:

$ swift-syntax-highlight "print(\"Hello, world!\")"
<pre class="highlight"><code><span class="n">print</span><span class="p">(</span><span class="s2">"Hello, world!"</span><span class="p">)</span></code></pre>

Installation

Homebrew

Run the following command to install using homebrew:

$ brew install nshipster/formulae/swift-syntax-highlight

Manually

Run the following commands to build and install manually:

$ git clone https://github.com/NSHipster/SwiftSyntaxHighlighter.git
$ cd SwiftSyntaxHighlighter
$ make install

Code Usage

SwiftSyntaxHighlighter provides two top-level functions named highlight(_:), with overloads for providing a file URL or passing source code directly as a String:

import SwiftSyntaxHighlighter

let code = """
print("Hello, world!")
"""

let html = highlight(code)

After running this code, html contains the following string:

<pre class="highlight"><code><span class="n">print</span><span class="p">(</span><span class="s2">"Hello, world!"</span><span class="p">)</span></code></pre>

Installation

Swift Package Manager

Add the SwiftSyntaxHighlighter package to your target dependencies in Package.swift:

import PackageDescription

let package = Package(
  name: "YourProject",
  dependencies: [
    .package(
        url: "https://github.com/NSHipster/SwiftSyntaxHighlighter",
        from: "0.0.1"
    ),
  ]
)

Then run the swift build command to build your project.

Carthage

To use SwiftSyntaxHighlighter in your Xcode project using Carthage, specify it in Cartfile:

github "NSHipster/SwiftSyntaxHighlighter" ~> 0.0.1

Then run the carthage update command to build the framework, and drag the built SwiftSyntaxHighlighter.framework into your Xcode project.

License

MIT

Contact

Mattt (@mattt)