class

Crystal::SyntaxHighlighter::Colorize

Inherits Crystal::SyntaxHighlighter / Reference / Object

A syntax highlighter that renders Crystal source code with ANSI escape codes suitable for terminal highlighting.

NOTE: To use Crystal::SyntaxHighlighter::Colorize, you must explicitly import it with require "crystal/syntax_highlighter/colorize"

require "crystal/syntax_highlighter/colorize"

code = %(foo = bar("baz\#{PI + 1}") # comment)
colorized = Crystal::SyntaxHighlighter::Colorize.highlight(code)
colorized # => "foo \e[91m=\e[0m bar(\e[93m\"baz\#{\e[0;36mPI\e[0;93m \e[0;91m+\e[0;93m \e[0;35m1\e[0;93m}\"\e[0m) \e[90m# comment\e[0m"

Constructors

new(io : IO, colorize : ::Colorize::Object(String) = ::Colorize.with.toggle(true))

Creates a new instance of a Colorize syntax highlighter.

Appends highlighted output (when calling #highlight) to io.

Source

Class methods

highlight(io : IO, code : String)

Highlights code and writes the result to io.

Source
highlight(code : String)

Highlights code and returns the result.

Source
highlight!(code : String)

Highlights code or returns unhighlighted code on error.

Same as .highlight(code : String) except that any error is rescued and returns unhighlighted source code.

Source

Instance methods

colors
Source
colors=(colors : Hash(TokenType, ::Colorize::Color))
Source
render(type : TokenType, value : String)

Renders token with text value.

Source
render_delimiter

Renders a delimiter sequence.

Source
render_interpolation

Renders an interpolation sequence.

Source
render_string_array

Renders a string array sequence.

Source