TextMate 风格语法高亮类库:SyntaxKit

jopen 9年前

轻松实现TextMate 风格语法高亮类库(基于 Swift 2.0),适用于 iOS & OS X。

Building

SyntaxKit is written in Swift 2 so Xcode 7 is required. There aren't any dependencies besides system frameworks.

Installation

Carthage is the recommended way to install SyntaxKit. Add the following to your Cartfile:

github "soffes/SyntaxKit"

You can also install with CocoaPods:

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 usestmLanguageandtmThemefiles to highlight source code. None are provided with SyntaxKit. Thankfully, there are tons available at TextMate's GitHub org.

Basic Parsing

Once you have a language, you can get started:

import SyntaxKit    let path = "path to your .tmLanguage file"  let plist = NSDictionary(contentsOfFile: path)! as [NSObject: AnyObject]  let yaml = Language(dictionary: plist)    let parser = Parser(language: yaml)

Parseris 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:

let input = "title: \"Hello World\"\n"  parser.parse(input) { scope, range in      print("\(scope) - \(range)")  }

scopeis the name of an element. This is something like"string"or"constant.numeric".rangeis anNSRangestruct representing where the scope falls in the input string.

项目主页:http://www.open-open.com/lib/view/home/1434610259739