Splash

repository·master·Indexed 23 days ago

https://github.com/johnsundell/splash

A fast, lightweight, and flexible Swift syntax highlighter. Available as a Swift library for generating HTML or attributed strings, or via command-line tools including SplashHTMLGen for HTML generation, SplashMarkdown for decorating Markdown files, SplashImageGen for creating code sample images on macOS, and SplashTokenizer for debugging code tokens.

Tokens
1.3K
Snippets
7
Records
12
Agent score
84%

What's inside Splash

  1. Install Splash as a Swift Package

    master

    To use Splash in your own Swift Package Manager-powered tool, script, or server-side application, add it as a dependency in your Package.swift file.

    .package(url: "https://github.com/JohnSundell/Splash", from: "0.1.0")
  2. Install Splash command line tools

    master

    You can install the built-in command line tools globally using make install after cloning the repository, or compile and move a specific tool manually.

    To install all tools (SplashHTMLGen, SplashMarkdown, SplashImageGen, SplashTokenizer) to /usr/local/bin:

    git clone https://github.com/johnsundell/splash.git
    cd splash
    make install

    To install a single tool manually:

    swift build -c release -Xswiftc -static-stdlib
    install .build/release/SplashHTMLGen /usr/local/bin/SplashHTMLGen
    $ git clone https://github.com/johnsundell/splash.git
    $ cd splash
    $ make install
  3. Use Splash in Swift code with SyntaxHighlighter

    master

    To highlight code programmatically, import Splash and use the SyntaxHighlighter class. You must provide an instance of an OutputFormat (such as HTMLOutputFormat) to the initializer.

    Splash supports HTML and NSAttributedString out of the box, but you can implement the OutputFormat protocol to create custom formats.

    import Splash
    
    let highlighter = SyntaxHighlighter(format: HTMLOutputFormat())
    let html = highlighter.highlight("func hello() -> String")
  4. Decorate Markdown files with SplashMarkdown

    master

    The SplashMarkdown tool iterates through all code blocks in a Markdown file and converts them into Splash-highlighted HTML.

    To skip highlighting for a specific code block, add no-highlight to the opening backticks (e.g., ```no-highlight).

    Example usage:

    $ SplashMarkdown ~/Documents/Article.md
  5. Generate HTML with SplashHTMLGen

    master

    The SplashHTMLGen command line tool converts Swift code into an HTML string. It assigns CSS classes to tokens rather than hardcoding colors.

    To use the output, wrap the generated HTML in <pre><code> tags and link a CSS file that defines the styles for the assigned classes (e.g., .keyword, .type).

    Example usage:

    $ SplashHTMLGen "func hello(world: String) -> Int"
  6. Debug tokens with SplashTokenizer

    master

    The SplashTokenizer tool is a debugging utility that outputs the individual components (tokens) of a string of Swift code, excluding whitespace. This is useful for seeing how Splash breaks down code.

    $ SplashTokenizer "func hello(world: String) -> Int"
  7. Generate code images with SplashImageGen

    master

    The SplashImageGen tool converts Swift code into an image file. It works by generating an NSAttributedString and drawing it into a graphics context.

    Note: This tool is currently only available on macOS.

    Example usage:

    $ SplashImageGen "func hello(world: String) -> Int" "MyImage.png"
  8. Use the SplashImageGen CLI to generate syntax highlighted images

    master

    The SplashImageGen command-line tool generates syntax-highlighted images of code snippets on macOS. It requires two positional arguments: the source code string and the output file path.

    Required Arguments:

    1. The code to be highlighted.
    2. The destination path for the generated image.

    Optional Flags:

    • -p: The amount of padding (in pixels) to apply around the code.
    • -f: A path to a specific font file to use for rendering.
    • -s: The text size to use for rendering.
  9. Use the SplashTokenizer CLI to tokenize code strings

    master

    The SplashTokenizer command-line tool takes a single string argument representing the code you want to tokenize. It uses the SyntaxHighlighter with a TokenizerOutputFormat to output the tokenized representation of the input string to standard output.

    Usage:

    splash-tokenizer "your code string here"

    Note: Ensure the code string is passed as a single argument. If no argument is provided, the tool will print a warning and exit.

  10. Use the Splash CLI to generate Markdown syntax highlighting

    master

    The Splash command-line tool accepts a single argument: the path to a Markdown file. It reads the file content and outputs the decorated Markdown string (with syntax highlighting markup) to standard output.

    To use it, provide the path to your Markdown file as the first argument. The tool supports tilde (~) expansion for home directory paths.

  11. Use the SplashHTMLGen CLI to generate HTML syntax highlighting

    master

    The SplashHTMLGen command-line tool takes a string of code as its first argument and outputs the corresponding HTML syntax-highlighted string to standard output. It uses the HTMLOutputFormat to wrap the highlighted code in appropriate HTML tags.

    Usage:

    SplashHTMLGen "your code here"

    If no argument is provided, the tool will print a warning and exit with status code 1.