Swiftpack.co - marcocapano/SplashObjc as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by marcocapano.
marcocapano/SplashObjc 0.3.3
An Objective-C syntax highlighter written in Swift
⭐️ 1
🕓 4 years ago
.package(url: "https://github.com/marcocapano/SplashObjc.git", from: "0.3.3")

SplashObjc

SplashObjc

Swift Package Manager Mac + Linux Twitter: @marcocapano1

This is SplashObjc, an Objective-C syntax highlighter based on Splash, a Swift syntax highlighter. It can be used to generate HTML and Images and NSAttributedStrings from your Objective-C code.

It is already quite capable, with support for blocks, macros, functions, control statements, classes, procolocols, properties, object literals and more. See the following example image, generated with SplashObjc from Foundation's NSLock.h:

NSLock code sample

Usage

SplashObjc can be used as a Swift Package, or you can use one of the command line tools provided!

To install using the Swift Package Manager:

.package(url: "https://github.com/marcocapano/SplashObjc", from: "0.3.2"))

The command line tools mimic the ones provided by Splash's swift version, so that if you know how to use that already, using these should be easy.

SplashObjcHTMLGen

SplashObjcHTMLGen generates HTML that you can copy and paste wherever you need

For example, if you call it like this:

$ SplashObjcHTMLGen "[UIView alloc] init];"

You'll get the following output back:

[<span class="type">UIView</span> <span class="call">alloc</span>] <span class="call">init</span>];

Since it uses Splash under the hood, it supports themes, which you can provide in the form of .css files. To learn more about the html format, see the original source here

When rendering your outputted html, make sure to wrap your output code in the <pre> and <code> tags and properly link to your .css file. Like this:

<!DOCTYPE html>
<head>
    <title>Hello World</title>
    <link rel="stylesheet" href="style.css">
</head>

<pre>
    <code>
        [<span class="type">UIView</span> <span class="call">alloc</span>] <span class="call">init</span>];
    </code>
</pre>

SplashObjcImageGen

SplashObjcImageGen generates NSAttributedString using SplashObjc, draws the string on an image and stores it to disk. Note that because of the dependency on NSAttributedString, the tool is only available on macOS.`

For example, if you call it like this:

$ SplashObjcImageGen "[UIView alloc] init];" Code.png

The following image will be generated (and written to disk as Code.png):

Code sample

SplashObjcMarkdown

SplashObjcMarkdown enables code decoration within Markdown files. The way it works is you simply give it a path to a Markdown file and it will convert any code blocks in it to Splash-highlighted HTML. Just like the HTML generated by SplashObjcHTMLGen itself, a CSS file should also be added to any page serving the processed Markdown, since Splash only adds CSS classes to tokens — rather than hardcoding styles inline.

Say you wanted to highlight a file at ~/Documents/MarkdownFile.md:

$ SplashObjcMarkdown ~/Documents/MarkdownFile.md

The decorated Markdown will be returned as standard output.

Highlighting can be skipped for any code block by adding no-highlight next to the block’s opening row of backticks — like this: “```no-highlight”.

SplashObjcTokenizer

If you're interested in seeing the actual bits the package produces, you can use the tool SplashObjcTokenizer. Example:

$ SplashObjcTokenizer "[UIView alloc] init];"

Output:

Plain text: [
Type token: UIView
Call token: alloc
Plain text: ]
Call token: init
Plain text: ];

📦 Using it in your projects

Since it comes as a package, once installed you can simply import it and start using it like this:

import Splash
import SplashObjc

let highlighter = SyntaxHighlighter(format: HTMLOutputFormat(), grammar: ObjcGrammar())
let html = highlighter.highlight("[UIView alloc] init];")

You can even create your own custom OutputFormat.

Installation

🛠 Command line tools

If you want to use the command line tools, clone the repo:

$ git clone https://github.com/marcocapano/SplashObjc.git
$ cd SplashObjc

Then install all the tools in your /usr/local/bin like this:

$ make install

Coming Soon

  • Jekyll plugin to highlight code in your blog posts

Limitations

SplashObjc works on a few assumptions:

  • class names always have the first letter uppercase
  • Macros are all uppercased, with words separated by an underscore. Example: NS_CLOSED_ENUM

Acknowledgements

This project could not exist without the work on Splash by John Sundell. If you like this project, I strongly recommend you try creating support for another language. John has made a really great job making the code readable and easy to work with.

Why is this not a fork of Splash?

I could have just forked the Splash project, added ObjcGrammar.swift and its tests and modified the command line tools provided to accept a grammar parameter. The reason i didnt is I would then have had to manually merge the original project with future updates.

Instead, by using SwiftPM, the whole process is nicer 😊

Contacts

[email protected]

marcocapano.dev

If you'd like to contribute or if you see any missing thing or mistake, please do not esitate to contact me or open a PR!

GitHub

link
Stars: 1
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Add SplashObjcMarkdown
4 years ago

This release adds a SplashObjcMarkdown command line tool to decorate code blocks within .md files.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics