Latest 0.1
Homepage https://github.com/uranusjr/Prism-objc
License MIT
Platforms ios 7.0, osx 10.7, requires ARC
Frameworks JavaScriptCore
Authors

Prism-objc

Objective-C Wrapper for Prism (the JavaScript syntax highlighter) using JavaScriptCore.

What?

Prism is a “lightweight, robust, elegant syntax highlighting library” that renders code snippets into syntax highlighted HTML. It is created by Lea Verou, written in JavaScript, and is based on regular expression.

This library/framework uses JavaScriptCore to perform so-called “server-side rendering” with Prism. It supports most modern OS X versions (I’m not very sure how far back this goes, but at least 10.7+ should be fine) and iOS 7 or later.

How?

Quick example:

#import <Prism/Prism.h>

// ...

PRISyntaxHighlighter *highlighter = [[PRISyntaxhighlighter alloc] initWithDefaultAliases:YES];
NSString *output = [highlighter highlight:@"int" asLanguage:@"objc" error:NULL];
NSLog(@"%@", output);   // <span class="token keyword">int</span>

The main class is PRISyntaxHighlighter. It constructs Prism internally when init it. After that, you can use -highlight:asLanguage:error: to highlight code snippets.

The language names you pass into when you perform highlighting need to be one of the followings:

  1. A built-in definition that is a key of -languages.
  2. One of the language aliases that is a key of -aliases.

You can add/remove aliases using -addAlias:forName:, -addAliasesFromDictionary:, and -removeAlias:. An alias should point to a built-in language name, or another alias.

A set of recommended aliases is provided via +defaultAliases. Note that they are not loaded if you use init to initialise PRISyntaxHighlighter.

Prism provides some built-in styling. You can get a set of themes and URLs pointing to their respective CSS files via -themes.

Demo

There’s a demo project in the PrismDemo directory showing what this library can do. You can build it on OS X in Xcode by opening Prism.xcworkspace and run the PrismDemo scheme.

Choose “PrismDemo” before you hit “Run”

PrismDemo highlighting PRISyntaxHighlighter API

Installing

This repository can build as an OS X framework that you can use directly. Alternatively you can also install via CocoaPods:

pod install Prism

Since the original Prism needs to be bundled with the main application, I don’t recommend you just try to include this as a submodule or even copy-paste the code. You will need to structure your project very carefully so that resources can be found correctly. It can work, but you’ll waste a lot of time to this.

TODO

  • Support built-in Prism plugins.
  • Support external Prism plugins.
  • Travis CI.
  • Test Coverage.
  • Proper documentation.

Latest podspec

{
    "name": "Prism",
    "version": "0.1",
    "summary": "Objective-C Wrapper for Prism using JavaScriptCore",
    "description": "    Prism is a u201clightweight, robust, elegant syntax highlighting libraryu201d thatn    renders code snippets into syntax highlighted HTML. It is created by Lean    Verou, written in JavaScript, and is based on regular expression.nn    This library/framework uses JavaScriptCore to perform so-called u201cserver-n    side renderingu201d with Prism. It supports most modern OS X versions (Iu2019m not n    very sure how far back this goes, but at least 10.7+ should be fine) and n    iOS 7 or later.n",
    "homepage": "https://github.com/uranusjr/Prism-objc",
    "license": "MIT",
    "authors": {
        "Tzu-ping Chung": "[email protected]"
    },
    "social_media_url": "https://twitter.com/uranusjr",
    "platforms": {
        "ios": "7.0",
        "osx": "10.7"
    },
    "source": {
        "git": "https://github.com/uranusjr/Prism-objc.git",
        "tag": "v0.1",
        "submodules": true
    },
    "source_files": "Prism/Prism/*.{h,m}",
    "public_header_files": "Prism/Prism/*.h",
    "resource_bundles": {
        "Prism": [
            "Prism/Dependency/prism/components.js",
            "Prism/Dependency/prism/components",
            "Prism/Dependency/prism/themes"
        ]
    },
    "frameworks": "JavaScriptCore",
    "requires_arc": true
}

Pin It on Pinterest

Share This