Inherits from NSObject
Declared in GMSyntaxHighlighter.h

Overview

GMSyntax highlighter is a fast objective C general purpose syntax highlighter.

It generally takes a GMLanguage object, a theme dictionary, and a NSString as input and produces a NSAttributedString as output.

This class is loosely based on the prism.js syntax highlighter by Lea Verou.

Sample usage:

GMSyntaxHighlighter *highlighter = [[GMSyntaxHighlighter alloc] init];
highlighter.language = [GMLanguage languageFromBundleWithName: @"css"];
highlighter.theme = [GMTheme themeFormBundleWithName: @"light"];
NSAttributedString *result = [highlighter highlight: @"h1 {\nfont: 90% Helvetica;\n}"];

The code is designed such, that you can use [NSAttributedString attribute:atIndex:effectiveRange:] with the custom attribute "GMToken" on the result to find what that part of the string was tokenized as.

Tasks

Highlighting code

Properties

language

The language definition to tokenize the code with.

@property (retain) NSDictionary *language

Declared In

GMSyntaxHighlighter.h

theme

The theme to use as for coloring the code.

@property (retain) GMTheme *theme

Discussion

Defaults to a theme in the app bundle named default.

Declared In

GMSyntaxHighlighter.h

Instance Methods

convertToHTML:

Returns an HTML string from a previously highlighted string.

- (NSString *)convertToHTML:(NSAttributedString *)as

Parameters

as

A previously highlighted attributed string.

Return Value

An HTML string where tokens are set as class names on spans.

Discussion

You should use this like this:

 GMSyntaxHighlighter *sh = [[GMSyntaxHighlighter alloc] init];
 [sh setLanguage: [GMLanguage languageWithBundleName: @"css"]];
 NSAttributedString *as = [sh highlight: @"h1 { font-family: \"Helvetica\"; }"];
 NSString *html = [sh convertToHTML: as];
 html //=> @"<span class='selector'>h1</span> <span class='punctuation'>{</span> 
            <span class='property'>font-family</span> <span class='punctuation'>:</span>
            <span class='string'>&quot;Helvetica&quot;</span> <span class='punctuation'>;</span>
            <span class='punctuation'>}</span>"

Note that to see any actual syntax highlighting, you also need to provide an appropriate CSS declaration.

Declared In

GMSyntaxHighlighter.h

highlight:

Highlights a string of source code.

- (NSAttributedString *)highlight:(NSString *)text

Parameters

text

The code that you wish to highlight.

Return Value

An attributed string where individual language elements have different formatting (like color) applied.

Discussion

Make sure you first set an appropriate language and theme, otherwise you might not see much results.

Declared In

GMSyntaxHighlighter.h

tokenize:

Provides a list of tokens.

- (NSArray *)tokenize:(NSString *)text

Parameters

text

The code you wish to tokenize.

Return Value

Returns an array that contains NSStrings for pieces of code that were not matched to any token, or GMToken instances that are essentially tuples of a tokenType and a content, which is typically either a string or a list of tokens.

Discussion

This is a rather lower level method, which is useful for debuging purposes and also if you wish to do something else then just highlight a language, this is a general purpose tokenizer.

Before usage make sure the language property has been set, preferably via one of GMLanguage class methods.

Declared In

GMSyntaxHighlighter.h