A syntax highlighter for Swift code that uses
SwiftSyntax.
You can use it either from the command-line,
via the swift-highlight executable,
or in Swift code using the SwiftSyntaxHighlighter module.
This functionality is discussed in the NSHipster article
SwiftSyntax.
Requirements
Swift 5.3+
Command-Line Usage
The swift-highlight executable can be run from the command line
to highlight either a path to a source file or source code:
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 type methods named highlight
that take either
source code as a String,
a source file URL,
or a SourceFileSyntax AST created by SwiftSyntax.
import SwiftSyntaxHighlighter
let code = """
print("Hello, world!")
"""
let html = try SwiftSyntaxHighlighter.highlight(source: source, using: Xcode.self)
After running this code,
html contains the following string:
Then run the swift build command to build your project.
Known Issues
Xcode cannot run unit tests (⌘U)
when opening the SwiftSyntaxHighlighter package directly,
as opposed first to generating an Xcode project file with
swift package generate-xcodeproj.
(The reported error is:
Library not loaded: @rpath/lib_InternalSwiftSyntaxParser.dylib).
As a workaround,
you can install the latest toolchain
and enable it in “Xcode > Preferences > Components > Toolchains”.
Alternatively,
you can run unit tests from the command line
with swift test.
SwiftSyntaxHighlighter
A syntax highlighter for Swift code that uses SwiftSyntax. You can use it either from the command-line, via the
swift-highlight
executable, or in Swift code using theSwiftSyntaxHighlighter
module.This functionality is discussed in the NSHipster article SwiftSyntax.
Requirements
Command-Line Usage
The
swift-highlight
executable can be run from the command line to highlight either a path to a source file or source code:Pass the
--scheme pygments
option to generate Pygments-compatible HTML:swift-highlight
also accepts arguments piped from standard input (stdin
):Installation
Homebrew
Run the following command to install using homebrew:
Manually
Run the following commands to build and install manually:
Code Usage
SwiftSyntaxHighlighter
provides type methods namedhighlight
that take either source code as aString
, a source fileURL
, or aSourceFileSyntax
AST created by SwiftSyntax.After running this code,
html
contains the following string:Installation
Swift Package Manager
Add the SwiftSyntaxHighlighter package to your target dependencies in
Package.swift
:Then run the
swift build
command to build your project.Known Issues
swift package generate-xcodeproj
. (The reported error is:Library not loaded: @rpath/lib_InternalSwiftSyntaxParser.dylib
). As a workaround, you can install the latest toolchain and enable it in “Xcode > Preferences > Components > Toolchains”. Alternatively, you can run unit tests from the command line withswift test
.License
MIT
Contact
Mattt (@mattt)