Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/NSHipster/SwiftSyntaxHighlighter
A syntax highlighter for Swift code that uses SwiftSyntax to generate Pygments-compatible HTML.
https://github.com/NSHipster/SwiftSyntaxHighlighter
pygments swift swiftsyntax syntax-highlighter
Last synced: about 2 months ago
JSON representation
A syntax highlighter for Swift code that uses SwiftSyntax to generate Pygments-compatible HTML.
- Host: GitHub
- URL: https://github.com/NSHipster/SwiftSyntaxHighlighter
- Owner: NSHipster
- License: mit
- Archived: true
- Created: 2018-10-22T18:38:10.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2021-05-13T16:05:54.000Z (over 3 years ago)
- Last Synced: 2024-07-27T18:53:01.207Z (about 2 months ago)
- Topics: pygments, swift, swiftsyntax, syntax-highlighter
- Language: Swift
- Homepage: https://nshipster.com/swiftsyntax/
- Size: 120 KB
- Stars: 275
- Watchers: 4
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# SwiftSyntaxHighlighter
![CI][ci badge]
[![Documentation][documentation badge]][documentation]A syntax highlighter for Swift code that uses
[SwiftSyntax](https://github.com/apple/swift-syntax).
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](https://nshipster.com/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:```terminal
$ swift highlight 'print("Hello, world!")'let greeting = "Hello, world!"
```Pass the `--scheme pygments` option
to generate [Pygments](http://pygments.org)-compatible HTML:```terminal
$ swift highlight 'print("Hello, world!")' --scheme pygmentsprint("Hello, world!")
````swift-highlight` also accepts arguments piped from standard input (`stdin`):
```terminal
echo 'print("Hello, world!")' | swift highlightprint("Hello, world!")
```### Installation
#### Homebrew
Run the following command to install using [homebrew](https://brew.sh/):
```terminal
$ brew install nshipster/formulae/swift-syntax-highlight
```#### Manually
Run the following commands to build and install manually:
```terminal
$ 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.```swift
import SwiftSyntaxHighlighterlet code = """
print("Hello, world!")
"""let html = try SwiftSyntaxHighlighter.highlight(source: source, using: Xcode.self)
```After running this code,
`html` contains the following string:```
let greeting = "Hello, world!"
```### Installation
#### Swift Package Manager
Add the SwiftSyntaxHighlighter package to your target dependencies in `Package.swift`:
```swift
import PackageDescriptionlet 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](https://swift.org/download/)
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](https://twitter.com/mattt))
[ci badge]: https://github.com/NSHipster/SwiftSyntaxHighlighter/workflows/CI/badge.svg
[documentation badge]: https://github.com/NSHipster/SwiftSyntaxHighlighter/workflows/Documentation/badge.svg
[documentation]: https://github.com/NSHipster/SwiftSyntaxHighlighter/wiki