splash

package module
v1.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2023 License: BSD-3-Clause Imports: 9 Imported by: 1

README

Splash

Highlight code embedded in HTML using the chroma package.

The generated output is tested by visual inspection in Chromium, Midori and Netsurf.

Example usage

package main

import (
    "github.com/xyproto/splash"
    "io/ioutil"
)

func main() {
    // Read "input.html"
    inputHTML, err := ioutil.ReadFile("input.html")
    if err != nil {
        panic(err)
    }

    // Highlight the source code in the HTML document with the monokai style
    outputHTML, err := splash.Splash(inputHTML, "monokai")
    if err != nil {
        panic(err)
    }

    // Write the highlighted HTML to "output.html"
    if err := ioutil.WriteFile("output.html", outputHTML, 0644); err != nil {
        panic(err)
    }
}

Available syntax highlighting styles

See the Style Gallery for a full overview of available styles and how they may appear.

General information

Documentation

Overview

Package splash adds a dash of color to embedded source code in HTML

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCSSToHTML

func AddCSSToHTML(htmlData, cssData []byte) ([]byte, error)

AddCSSToHTML takes htmlData and adds cssData in a <style> tag. Returns an error if </head> or <html> does not already exists. Tries to add CSS as late in <head> as possible.

func Highlight

func Highlight(htmlData []byte, styleName string, unescape bool) ([]byte, []byte, error)

Highlight takes HTML code as bytes and tries to syntax highlight code between <pre> and </pre> tags.

"style" is a syntax highlight style, like "monokai".

Full style list here: https://github.com/alecthomas/chroma/tree/master/styles

Returns the modified HTML source code and CSS style.

unescape can be set to true for unescaping already escaped code in <pre> tags, which can be useful when highlighting code in newly rendered markdown.

func SetDefaultLanguage

func SetDefaultLanguage(languageName string)

SetDefaultLanguage changes the default language from "shell" to something else. Must be supported by chroma.

func Splash

func Splash(htmlData []byte, styleName string) ([]byte, error)

Splash takes HTML code as bytes and tries to syntax highlight code between <pre> and </pre> tags.

"style" is a syntax highlight style, like "monokai".

Full style list here: https://github.com/alecthomas/chroma/tree/master/styles

Returns the modified HTML source code with embedded CSS as a <style> tag. Requires the given HTML to contain </head> or <html>.

language specifiers like <code class="language-c"> are supported.

func UnescapeSplash

func UnescapeSplash(htmlData []byte, styleName string) ([]byte, error)

UnescapeSplash does the same as Splash, but unescapes the HTML in the source code before highlighting. Use this if "&amp;" appears in the highlighted code instead of "&", and that is not what you wanted. Useful when highlighting source code after having rendered Markdown.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL