What is the process for printing the CSS stylesheet for PPI::HTML highlight?

PPI::HTML has done an impressive job of formatting the HTML highlighting for my Perl code, similar to the example on CPAN. However, I am facing challenges in making the output usable without the appropriate CSS styles. Unfortunately, I am unsure of how to integrate these styles into the code.

use PPI;
use PPI::HTML;

my %colors=(
    cast => '#339999',
    comment => '#008080',
    core => '#FF0000',
    double => '#999999',
    heredoc_content => '#FF0000',
    interpolate => '#999999',
    keyword => '#0000FF',
    line_number => '#666666',
    literal => '#999999',
    magic => '#0099FF',
    match => '#9900FF',
    number => '#990000',
    operator => '#DD7700',
    pod => '#008080',
    pragma => '#990000',
    regex => '#9900FF',
    single => '#999999',
    substitute => '#9900FF',
    transliterate => '#9900FF',
    word => '#999999'
);

my $highlighter=PPI::HTML->new(line_numbers => 1, colors => \%colors);
my $perl_doc=PPI::Document->new(\$perl_block);  # read from a file

my $perl_block_highlighted=$highlighter->html($perl_doc);
print "<p>$perl_block_highlighted</p>";

It would be greatly appreciated if you could provide a straightforward example that demonstrates how to display colored code. At present, everything is being displayed using the default color scheme.

Answer №1

According to the limited documentation provided:

When you choose not to utilize an external stylesheet, you can input colors as a hash reference where the keys represent CSS classes (typically aligned with the token name) and the values signify the colors.

The POD for PPI::HTML::CodeFolder lists the available class names for use, along with example colors like the ones below:

cast => '#339999',
comment => '#008080',
core => '#FF0000',
double => '#999999',
heredoc_content => '#FF0000',
interpolate => '#999999',
keyword => '#0000FF',
line_number => '#666666',
literal => '#999999',
magic => '#0099FF',
match => '#9900FF',
number => '#990000',
operator => '#DD7700',
pod => '#008080',
pragma => '#990000',
regex => '#9900FF',
single => '#999999',
substitute => '#9900FF',
transliterate => '#9900FF',
word => '#999999',

The code snippet below generates an independent HTML page featuring its own source code styled using the provided color schemes:

#!/usr/bin/env perl
use strict;
use warnings;

use PPI;
use PPI::HTML;

my %colors = (
    # listed colors above
);

my $highlighter = PPI::HTML->new(page => 1, line_numbers => 1, colors => \%colors);
my $perl_doc = PPI::Document->new(do { local $/; open 0; \ <0>; });

print $highlighter->html($perl_doc);

If the page => 1 option is omitted from the constructor, only an HTML fragment will be obtained without the accompanying CSS. In such instances, your site's stylesheet must include the essential styles.

Alternatively, you could employ HTML::TokeParser::Simple to straightforwardly post-process the HTML snippet:

#!/usr/bin/env perl

use strict;
use warnings;

use PPI;
use PPI::HTML;
use HTML::TokeParser::Simple;

my %colors = (
    # same colors as mentioned earlier
);

my $highlighter = PPI::HTML->new(line_numbers => 0);
my $html = $highlighter->html(\ do { local $/; open 0; <0> });

# print highlighted HTML content
print qq{<pre style="background-color:#fff;color:#000">},
      map_class_to_style($html, \%colors),
      qq{</pre>\n}
;

sub map_class_to_style {
    my $html = shift;
    my $colors = shift;

    my $parser = HTML::TokeParser::Simple->new(string => $html);
    my $out;

    while (my $token = $parser->get_token) {
        next if $token->is_tag('br');
        my $class = $token->get_attr('class');
        if ($class) {
            $token->delete_attr('class');
            if (defined(my $color = $colors->{$class})) {
                # shortening color codes if possible
                $color =~ s{
                    \A \#
                    ([[:xdigit:]])\1
                    ([[:xdigit:]])\2
                    ([[:xdigit:]])\3
                    \z
                }{#$1$2$3}x;
                $token->set_attr(style => "color:$color");
            }
        }
        $out .= $token->as_is;
    }
    $out;
}

By the way, this represents a "self-contained example": one that functions seamlessly without additional requirements on the user's end. It appears your program may not execute because the generation of contents within $perl_block was left up to those assisting you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The attempt to showcase items in a div element using inline display did not produce

I am having trouble with creating a webpage. I have designed a website but I am struggling to get it right. I have included my HTML and CSS code below. What I am trying to achieve is a header at the top, followed by several sections containing a photo on ...

Stopping a parent's event from firing when clicking on child elements without interfering with the child element events

Having read several posts such as this one, I am exploring a method to click on the parent div for it to hide, without hiding when clicking on either the search box or the 'click for event 2' text. I have tried using onclick stop propagation to ...

Having trouble with displaying the logo on the navigation bar (using bootstrap and django)?

Hello, I am struggling to include a logo in my navbar and it's not being displayed. Despite researching similar queries from others, I still can't figure out why the image won't show up. I'm uncertain whether the issue lies within my co ...

Align boxes in the center within a DIV container

Here is the box design that I have created: https://i.sstatic.net/3rtcn.jpg The green color boxes are generated dynamically inside a "col-md-10" div. If there are less than 3 boxes in the second row, I would like to center align them. For example, in thi ...

Bootstrap: Arrange multiple images horizontally within a row

I am trying to design a list item for a game that includes a background, an iconholder with an icon, a title, description, and up to three resources. I have been experimenting with Bootstrap and attempted using a container-fluid with an inner row but the i ...

The close button is not functioning properly

I created a script to close my div element by clicking on the 'x' icon, but instead of deleting the div only, the whole page gets deleted. I'm not sure where I went wrong, can anyone help me? HTML <div class="note"> <span id ...

Locator for finding compound text within a div class using Selenium WebDriver

I am struggling to select a specific button in the UI that shares similarities with other elements. Below is the code snippet for the button in question: <div class="ui green ok inverted button"> <i class="checkmark icon"></i> Yes </d ...

Utilizing Bootstrap 5: Breaking a Page into Two Separate Vertical Sections

I am looking to design a unique grid system that divides the page into either 2 columns or 2 rows that are completely separate from each other. This means that the content on one side of the page does not expand to match the height of the content on the ot ...

Create a customized HTML popup featuring various packages

I am struggling to create an HTML popup within a React component due to some errors Here is the code snippet: <div> <button class="toggle">Button</button> <div id="link-box"> <ul> ...

Extract content within Python pre tags

A Python code snippet is being used to extract content between PRE tags: s = br.open(base_url+str(string)) u = br.geturl() seq = br.open(u) blat = BeautifulSoup(seq) for res in blat.find('pre').findChildren(): seq = res.string ...

Building a Dynamic Checkbox Validation Feature in Angular Using Data retrieved from an API

Currently, I have a function that retrieves and displays a list obtained from an API: displayEventTicketDetails() { this.Service .getEventTicketDetails().subscribe((data: any) => { this.eventTicketDetails = data.map(ticket => ticket. ...

Sass: Setting a maximum width relative to the parent element's width

I have a resizable container with two buttons inside, one of which has dynamic text. Within my scss file, I am aiming to implement a condition where if the width of the container is less than 200, then the max width of the dynamic button should be 135px, ...

Angular JS effectively prevents redundant data from being displayed to users when scrolling infinitely while also efficiently removing DOM elements for previous data

I'm currently working on implementing AngularJS Infinite Scroll without using jQuery. My goal is to display the first 3 data items when the page loads and then load the next 3 data items from a JSON array object as the user scrolls. The issue I am fac ...

Showing off the latest products at the top of the list

Typically, when utilizing ngFor, the most recent item is displayed underneath the initial element. For instance, a list containing: [Apple, Orange, Banana] If we use ngFor to display this list: Apple Orange Banana I am interested in learning a method t ...

Fill the image with width using Mat

While working on creating cards using Angular Materials, I encountered an issue where the image was not filling up the space as desired. The red area in the image indicates where I want the image to take up space. https://i.sstatic.net/vcc9U.png HTML: & ...

Is it possible to include line breaks within a CSS value?

(I am posting this here because I couldn't find a solution and eventually figured it out through trial and error. Hopefully, this information can help others with the same question.) Note: This is not the same as the discussion on Single-line vs mult ...

I am in search of a clean and efficient method to modify the class of a link that triggers an HTMX request in Django. Perhaps something like "auto-refresh" or a similar solution would be ideal

I've encountered an issue with HTMX in Django. The page consists of two main components: a list of categories and the content that is displayed when a category is clicked. Initially, everything was working smoothly with standard htmx functionality. H ...

difference in flex-shrink behavior between Firefox and Chrome

Why does this code sample render differently on Firefox than on Chrome? Is this a browser bug, and if so, which browser is rendering per spec? If there is a bug report available, I would appreciate a link to it. Currently, adding flex-shrink: 0 to the na ...

The button is not being vertically centered when using the boostrap pull-right class

---------------------------------------------------------------- | Title | | Button | --> (using .pull-right) the button is not aligned vertically ------------------------------------------------------------- ...

Exploring Objects within an array using Angular loops

Hey there, I'm currently working on an Angular project and I need to retrieve the userName of the user for each comment that is posted. These entities are coming from my Spring Boot project. Is there a way to access the username for every comment? He ...