Giter VIP home page Giter VIP logo

highlightr's Introduction

Highlightr - SPM

License Platform

SPM Branch: 2020-03-01: WIP to convert this to SwiftPM.

Highlightr is an iOS & macOS syntax highlighter built with Swift. It uses highlight.js as its core, demo.

Takes your lame string with code and returns a NSAttributtedString with proper syntax highlighting.

Sample Gif A Sample Gif B

Note: This is a fork of the original Highlightr which uses Swift Package Manager instead of CocoaPods. The current version uses HighlightJS-Swift which packages just the default Highlight.js style and language selection.

Installation

Requirements

  • iOS 12+
  • macOS 10.14+

Sample Package Manifest

// swift-tools-version:5.1

import PackageDescription

let package = Package(
    name: "HolyCow",
    dependencies: [
        .package(url: "[email protected]:helje5/Highlightr.git",
                 from: "3.0.0")
    ],
    targets: [
        .target(
            name: "HolyCow",
            dependencies: [ "Highlightr" ])
    ]
)

Usage

Highlightr provides two main classes:

Highlightr

This is the main endpoint, you can use it to convert code strings into NSAttributed strings.

	let highlightr = Highlightr()!
	highlightr.setTheme(to: "default") // only default provided right now
	let code = "let a = 1"
	// You can omit the second parameter to use automatic language detection.
	let highlightedCode = highlightr.highlight(code, as: "swift") 
	

CodeAttributedString

A subclass of NSTextStorage, you can use it to highlight text on real time.

	let textStorage = CodeAttributedString()
	textStorage.language = "Swift"
	let layoutManager = NSLayoutManager()
	textStorage.addLayoutManager(layoutManager)

	let textContainer = NSTextContainer(size: view.bounds.size)
	layoutManager.addTextContainer(textContainer)

	let textView = UITextView(frame: yourFrame, textContainer: textContainer)

JavaScript?

Yes, Highlightr relies on iOS & macOS JavaScriptCore to parse the code using highlight.js. This is actually quite fast!

Performance

It will never be as fast as a native solution, but it's fast enough to be used on a real time editor.

It comes with a custom made HTML parser for creating NSAttributtedStrings, is pre-processing the themes and is preloading the JS libraries. As result it's taking around of 50 ms on my iPhone 6s for processing 500 lines of code.

Documentation

You can find the documentation for the latest release on cocoadocs.

License

Highlightr is available under the MIT license. See the LICENSE file for more info.

Highlight.js is available under the BSD license. You can find the license file here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.