• This repository has been archived on 17/Oct/2021
  • Stars
    star
    275
  • Rank 149,796 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 6 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A syntax highlighter for Swift code that uses SwiftSyntax to generate Pygments-compatible HTML.

SwiftSyntaxHighlighter

CI Documentation

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:

$ swift highlight 'print("Hello, world!")'
<pre class="highlight"><code><span class="keyword">let</span> <span class="variable">greeting</span> = <span class="string literal">&quot;</span><span class="string literal">Hello, world!</span><span class="string literal">&quot;</span></code></pre>

Pass the --scheme pygments option to generate Pygments-compatible HTML:

$ swift highlight 'print("Hello, world!")' --scheme pygments
<pre class="highlight"><code><span class="n">print</span><span class="p">(</span><span class="p">&quot;</span><span class="s2">Hello, world!</span><span class="p">&quot;</span><span class="p">)</span></code></pre>

swift-highlight also accepts arguments piped from standard input (stdin):

echo 'print("Hello, world!")' | swift highlight
<pre class="highlight"><code><span class="variable">print</span>(<span class="string literal">&quot;</span><span class="string literal">Hello, world!</span><span class="string literal">&quot;</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 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:

<pre class="highlight"><code><span class="keyword">let</span> <span class="variable">greeting</span> = <span class="string literal">&quot;</span><span class="string literal">Hello, world!</span><span class="string literal">&quot;</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: "1.2.4"
    ),
  ]
)

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.

License

MIT

Contact

Mattt (@mattt)

More Repositories

1

articles

Articles for NSHipster.com
1,209
star
2

ConfettiView

A view that emits confetti ๐ŸŽ‰
Swift
544
star
3

nshipster.com

A journal of the overlooked bits in Objective-C, Swift, and Cocoa.
SCSS
281
star
4

PasswordRules

A Swift library for defining strong password generation rules
Swift
278
star
5

clangwarnings.com

A list of Clang warnings and their descriptions.
276
star
6

DictionaryKit

An Objective-C Wrapper for Private Dictionary Services on Mac OS X
Objective-C
241
star
7

HypertextLiteral

Generate HTML, XML, and other web content using Swift string literal interpolation
Swift
225
star
8

AVSpeechSynthesizer-Example

A companion project to the NSHipster article about AVSpeechSynthesizer
Swift
160
star
9

XcodeBuildSettings.com

A convenient reference of available build settings for Xcode projects.
SCSS
152
star
10

articles-zh-Hans

Articles for NSHipster.cn
119
star
11

DynamicDesktop

Companion playgrounds to the NSHipster article about macOS Dynamic Desktops.
Swift
102
star
12

DBSCAN

Density-based spatial clustering of applications with noise
Swift
86
star
13

ibcolortool

List all colors in Storyboards and XIB files
Swift
80
star
14

Image-Resizing-Example

Swift
78
star
15

swift-log-github-actions

GitHub Actions workflow logging for Swift
Swift
72
star
16

swift-gyb

Evaluates and runs a Swift GYB script
Swift
66
star
17

ContactTracing-Framework-Interface

Objective-C header and synthesized Swift interface for Apple & Google's Contact Tracing Framework
Objective-C
58
star
18

xcderiveddata

A command-line utility that prints the path of the derived data directory for the current Xcode project
Shell
55
star
19

uti

A command-line utility that prints the Uniform Type Identifier for files.
Shell
31
star
20

Swift-Documentation-Example

A companion project to the NSHipster article about Swift Documentation
Swift
29
star
21

JavaScriptCore-JSExport-Example

An example of using Swift with JavaScriptCore's JSExport Protocol
Swift
25
star
22

Cycle

A sequence that cycles between each of the items in a given sequence.
Swift
24
star
23

nshipster.cn

NSHipster ๅ…ณๆณจ่ขซๅฟฝ็•ฅ็š„ Objective-Cใ€Swift ๅ’Œ Cocoa ็‰นๆ€งใ€‚ๆฏๅ‘จๆ›ดๆ–ฐใ€‚
CSS
23
star
24

MapKitJS-Demo

Swift
22
star
25

homebrew-formulae

Collection of Homebrew Formulae
Ruby
21
star
26

articles-ko

Articles for NSHipster.co.kr
21
star
27

update-homebrew-formula-action

Synchronizes a Homebrew formula with a GitHub release
Ruby
21
star
28

UITableViewHeaderFooterView-Demo

Swift
13
star
29

articles-es

Articles for NSHipster.es
6
star
30

articles-fr

Articles for NSHipster.fr
2
star