Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soffes/SyntaxKit
TextMate-style syntax highlighting
https://github.com/soffes/SyntaxKit
carthage ios macos swift watchos
Last synced: 9 days ago
JSON representation
TextMate-style syntax highlighting
- Host: GitHub
- URL: https://github.com/soffes/SyntaxKit
- Owner: soffes
- License: mit
- Archived: true
- Created: 2015-06-15T08:36:15.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-08-11T18:12:58.000Z (about 5 years ago)
- Last Synced: 2024-09-25T03:40:54.989Z (about 1 month ago)
- Topics: carthage, ios, macos, swift, watchos
- Language: Swift
- Size: 92.8 KB
- Stars: 472
- Watchers: 13
- Forks: 66
- Open Issues: 7
-
Metadata Files:
- Readme: Readme.markdown
- License: LICENSE
Awesome Lists containing this project
- awesome-swift-cn - SyntaxKit - TextMate-style syntax highlighting. (Libs / Text)
- awesome-swift - SyntaxKit - TextMate-style syntax highlighting. (Libs / Text)
README
# SyntaxKit
[![Version](https://img.shields.io/github/release/soffes/SyntaxKit.svg)](https://github.com/soffes/SyntaxKit/releases) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![CocoaPods compatible](https://img.shields.io/cocoapods/v/SyntaxKit.svg)](https://cocoapods.org/pods/SyntaxKit)
SyntaxKit makes TextMate-style syntax highlighting easy. It works on iOS, watchOS, and OS X.
SyntaxKit was abstracted from [Whiskey](http://usewhiskey.com).
## Building
SyntaxKit is written in Swift 2 so Xcode 7 is required. There aren't any dependencies besides system frameworks.
## Installation
[Carthage](https://github.com/carthage/carthage) is the recommended way to install SyntaxKit. Add the following to your Cartfile:
``` ruby
github "soffes/SyntaxKit"
```You can also install with [CocoaPods](https://cocoapods.org):
``` ruby
pod 'SyntaxKit'
```For manual installation, I recommend adding the project as a subproject to your project or workspace and adding the appropriate framework as a target dependency.
## Usage
SyntaxKit uses `tmLanguage` and `tmTheme` files to highlight source code. None are provided with SyntaxKit. Thankfully, there are tons available at [TextMate's GitHub org](https://github.com/textmate).
### Basic Parsing
Once you have a language, you can get started:
```swift
import SyntaxKitlet path = "path to your .tmLanguage file"
let plist = NSDictionary(contentsOfFile: path)! as [NSObject: AnyObject]
let yaml = Language(dictionary: plist)let parser = Parser(language: yaml)
````Parser` is a very simple class that just calls a block when it finds something the language file knows about. Let's print all of the elements in this string:
```swift
let input = "title: \"Hello World\"\n"
parser.parse(input) { scope, range in
print("\(scope) - \(range)")
}
````scope` is the name of an element. This is something like `"string"` or `"constant.numeric"`. `range` is an `NSRange` struct representing where the scope falls in the input string.
### Working with Attributed Strings
SyntaxKit also comes with `AttributedParser`. This is a simple subclass of `Parser` that knows how to work with themes.
```swift
let tomorrow = Theme(dictionary: themePlist)
let attributedParser = AttributedParser(language: yaml, theme: tomorrow)attributedParser.parse(input) { scope, range, attributes in
print("\(scope) - \(range) - \(attributes)")
}
```Notice that `attributes` is the third paramenter to the block now. This is a dictionary of attributes you can give to `NSAttributedString`. Other values may be included here that don't work with `NSAttributedString`. You can do your own inspection and do something custom if you want.
`AttributedParser` includes a convenience method for turning a `String` of source code into an `NSAttributedString`:
```swift
let attributedString = attributedParser.attributedStringForString(input)
```Easy as that. This method takes an optional `baseAttributes` parameter to customize how the string is created. This is great if you want to specify a font, etc.
### Custom Parsers
If you want to build your own parser (for example, to generate HTML) you can subclass whichever one meets your needs. Go wild.
Enjoy.