Hilite plugin

Using highlight-js and Deparse-syntax highlighter to highlight Raku

Introduction§

This plugin replaces the code-block template so that the highlight-js library is included to highlight code according to the :lang attribute.

Raku and RakuDoc code is highlighted using either:

  • @Lizmat's Deparse highlighter,
  • or @PatrickBoker's Rainbow highlighter (the default).

Since the Deparse highlighter requires correct Raku code and generates an error when the code is not correct, while the Rainbow highlighter is more forgiving, the latter is the default.

In order to select the Deparse highlighter, use the option :highlighter<deparse> , eg.,

    =begin code :highlighter<deparse>
...
=end code

Examples§

The following are examples of code blocks that are highlighted.

Highlighting status (working/not working and why) is given below the example. Hover for more info.

Other languages (via highlight-js)§

Code in Haskell


greeting :: String -> String
greeting  ""   = "Hello, World!"
greeting "bub" = "Hey bub."
greeting  name = "Hello, " ++ name ++ "!"

Code in Ruby


# Ruby
if x > 5
    puts "Bigger!"
elsif x == 5
    puts "The same!"
else
    puts "Smaller!"
end

Code in Python


squares = []
for x in range(5):
    squares.append(lambda: x ** 2)
print(squares[2]())
print(squares[4]())
# both 16 since there is only one x

Raku examples§

Inside a code block with no :lang set, so Raku, :highlighter<deparse>

my $x = 2;
# a brilliant program!

Code from indented para

# indenting causes an implicit code block
my $raku = 'fantastic';

Code with :allow<B K> so not highlighted

# a renderer should observe the basis markup
# and the markup but render R<markup> verbatim
my $x = 3;

Code from operators, using Rainbow

    my @arr = [10, 20, 30];
    my $i = 0;

    if rand < 1/2 {
        @arr[++$i+= 1;                # @arr = [10,21,30]
    } else {
        @arr[++$i= @arr[++$i+ 1;    # @arr = [10,31,30] (or [10,20,21]?)
    }                                   # the result may be implementation-specific
    say @arr;

Credits§

Richard Hainsworth aka finanalyst

VERSION§