Search code examples
laravel-bladesyntax-highlightinggitbook

Are there similar languages to Laravel Blade syntax-wise that I could use for approximate syntax highlighting where Blade isn’t supported?


I write documentation for a Web platform that uses the Laravel Blade engine to allow users to make and share scripts. Code snippets are often useful or necessary, and obviously syntax highlighting makes them easier to read.

The problem is I’m moving away from Github, and few other places seem to support Blade highlighting, so I’m wondering if there are other, more popular languages with a similar syntax that I could target instead to have decent highlighting, even if it’s not perfect.

I’ve settled on Gitbook, which uses Prism for highlighting, so anything there is fair game. Here is a sample code block for those not familiar with Blade:

@foreach($_abilities as $ability)
    {{-- Set up a more convenient array name --}}
    @if($ability = $_abilities[$loop->index]) @endif

    {{-- Ability name (and type) --}}
    <h4>{{ $ability["name"] }} @isset($ability["type"]) ({{ $ability["type"] }}) @endisset</h4>

    {{-- Link to Ability; use {!! !!} to parse HTML --}}
    {!! $ability["link"] !!} 

    {{-- Parent Ability --}}
    @isset($ability["parent"]) has a parent Ability named <b>{{ $ability["parent"]["name"] }}</b>. 
    @else has no parent Ability. 
    @endisset

    {{-- Ability tags --}}
    @if($ability["tags"]) It has the following tags: <i>
        {{-- This array only has names, not key-value pairs, so we only need "as $tag" --}}
        @foreach($ability["tags"] as $tag)
            {{-- We’ll add a period at the end, or a comma in between tags --}}
            @if($loop->last){{ $tag }}.
            @else{{ $tag }}, 
            @endif
        @endforeach </i><br>
    @else It has no tags.<br>
    @endif

    {{-- Ability charges --}}
    Charges: 
    {{-- Used charges can be null, but we want them to default to 0 if max charges are set, so we use "?: 0" --}}
    @isset($ability["charges"]) {{ $ability["used_charges"] ?: 0 }} used out of {{ $ability["charges"] }}.<br>
    @else N/A.<br>
    @endisset

    {{-- Ability entry and image; use {!! !!} to parse HTML --}}
    Description:<br>
    {!! $ability["entry"] !!}<br>
    {!! $ability["thumb"] !!}
@endforeach

Basically, a bastardized mix of PHP, HTML and more.


Solution

  • I ended up going with Parser for the following reasons:

    • Highlights directives and variables with different colors (most other languages got confused identifying directives or used the same color for both).
    • Highlights the name of HTML tags (though not the brackets), and even does a pretty good job with attributes and inline CSS.
    • Unlike most others, it doesn’t highlight reserved words in comments or HTML (but it also doesn’t indicate comments at all).

    SPARQL was a good contender, but isn’t as good with HTML and highlights reserved words including "if" in @if directives.

    There were a few that could have been acceptable but don’t come really close to those two. Listing them in case others are looking for other options where Parser and SPARQL are not supported: Concurnas, Crystal, Perl.