An Option for Syntax Highlighting on blogs.perl.org

I've struggled with the syntax highlighting here on this blog. I really want to use this site and I will continue to do so.

After trying in vain to get some "auto" syntax highlighting here via the editor, I reached for an old trick I've used in the past. Generating HTML using some external service. After a quick Google search, I found https://tohtml.com/perl/. Given a block of Perl code, it'll generate HTML based syntax highlighting that one may add to their post. For example,

Decent Syntax Highlighting (from tohtml.com/perl)

use Dispatch::Fu;          # exports 'dispatch' and 'on', which are needed
use Test::More tests => 1;
 
my $INPUT = q{case1};
 
my $results = dispatch {
    my $input_str = shift;
    xdefault $input_str, q{do_default};
}
$INPUT,
  on default => sub { 6 },
  on case0   => sub { 0 },
  on case1   => sub { 1 },
  on case2   => sub { 2 },
  on case3   => sub { 3 },
  on case4   => sub { 4 },
  on case5   => sub { 5 };
 
is $results, 1, q{POD example for xdefault works};

It has been suggested to use an approach that uses "pre" and "code" HTML, and results in the following:

Insufficient Syntax Highlighing (internal)


use Dispatch::Fu;    # exports 'dispatch' and 'on', which are needed
use Test::More tests => 1;
my $INPUT = q{case1};
my $results = dispatch {
    my $inputstr = shift;
    xdefault $inputstr, q{do_default};
}
$INPUT,
  on default => sub { 6 },
  on case0   => sub { 0 },
  on case1   => sub { 1 },
  on case2   => sub { 2 },
  on case3   => sub { 3 },
  on case4   => sub { 4 },
  on case5   => sub { 5 };
is $results, 1, q{POD example for xdefault works};

Conclusion

However this is not going to satisfy what I want to see. Some notes with using https://tohtml.com/perl/:

  • when using the site, choose the "Copy Styled" in the "Highlighted RTF:" section
  • drop this as-is in your markdown editor of this site
  • blogs.perl.org will cut off anything at what looks like 80 characters
  • this approach is clearly using unmaintanable code blocks, if you change your code you must regenerate the HTML blob from https://tohtml.com/perl/

The last thing I want to do is bag on this site. I prefer to use blogs.perl.org over anything else, I just think we need to try harder to provide properly colored code examples.

Do you have a method that works internally or a better suggestion for creating properly highlighted code blocks at blogs.perl.org?

Many have suggested using dev.to or some other site. As much as I appreciate that suggestion, I am trying to solve an issue here so that we can improve the experience by both readers and authors. Please comment below.

Thanks to the folks who helped me sort this out on the TPRF Slack.

Cheers!

1 Comment

If you're already going to be doing something outside of the post to get the HTML, you could look at Perl::Tidy? If you go to http://pukku.com/bostonpm/2017-04-tumblr/index.html?showall#s47, you can see an example of how to call Perl::Tidy in a useful way...

Leave a comment

About Brett Estrade

user-pic PAUSE Id: OODLER