Reference - PAD functions - highlight


App: demo - Page: highlight
PHP
highlight.php
HTML
highlight.pad
Result
<?php

$title
= 'Syntax Highlighting';

// A query as it would come from a database or a log - a value, coloured by the pipe.

$query = "SELECT name, COUNT(*) AS orders FROM customers JOIN orders USING (customer_id) WHERE country = 'NL' GROUP BY name";

?>
<p>The <code>{ignore}{highlight}{/ignore}</code> tag colours source code on the server - PAD, PHP, HTML,
CSS, JavaScript, JSON, YAML, SQL and Bash. No JavaScript library, and the braces inside need no
<code>&#123;ignore&#125;</code>.</p>

<h2>PHP, with line numbers and a marked line</h2>

{highlight 'php', lines, mark='4'}
<?php

$orders
= db ( "ARRAY * FROM orders WHERE status = 'open'" );
$total = padArrSum ( $orders, 'amount' );

if ( !
$orders )
padRedirect ( 'empty' );

?>
{/highlight}

<p><code>{ignore}{highlight 'php', lines, mark='4'}...{/highlight}{/ignore}</code></p>

<h2>PAD itself</h2>

{highlight 'pad'}
{orders sort='date', first=10}
<li class="{even@orders ? 'even' : 'odd'}">
{$number}: {$total | money}
</li>
@else@
<li>No orders yet</li>
{/orders}
{/highlight}

<h2>CSS and JavaScript</h2>

{highlight 'css'}
.card { padding: 12px 16px; border-radius: 8px; color: #333; }
@media (prefers-color-scheme: dark) { .card { color: #eee; } }
{/highlight}

{highlight 'js'}
// Counts the clicks on every button with a data-count attribute
document.querySelectorAll('[data-count]').forEach(button => {
let clicks = 0;
button.addEventListener('click', () => button.textContent = `Clicked ${++clicks} times`);
});
{/highlight}

<h2>A file of this application</h2>

{highlight file='qr.php'}

<p><code>{ignore}{highlight file='qr.php'}{/ignore}</code> - the language follows the extension.</p>

<h2>A value, through the pipe</h2>

{$query | highlight('sql')}

<p><code>{ignore}{$query | highlight('sql')}{/ignore}</code> - a query kept in a database or a log.</p>


App: islands - Page: examples/build
PHP
examples/build.php
HTML
examples/build.pad
Result
<?php

// What {vite 'src/main.js'} wrote into this page's <head>, and where it came from: the
// manifest of the build - every source and the file it became - or the dev server, while it
// runs. The clock is a Solid island started from the server's time.

$dev = padViteDev ();
$manifest = $dev === '' ? ( padViteManifest () ?? [] ) : [];
$written = trim ( padViteTags ( [ 'src/main.js' ], FALSE, FALSE ) );

$sources = [];

foreach (
$manifest as $source => $one )
if ( !
str_starts_with ( $source, '_' ) )
$sources [] = [ 'source' => $source,
'file' => $one ['file'],
'kind' => ( $one ['isEntry'] ?? FALSE ) ? 'entry' : ( ( $one ['isDynamicEntry'] ?? FALSE ) ? 'loaded on demand' : 'chunk' ),
'css' => implode ( ', ', $one ['css'] ?? [] ) ];

$sourceCount = count ( $sources );
$clock = [ 'now' => time () ];

?>
<div class="cols-2">
<div>
<div class="panel-title">{if $dev}The dev server runs: {$dev}{else}The build: www/islands/build/{/if}</div>
<p class="muted small">{if $dev}{vite} asks the dev server - every save shows at once.{else}Start <code>npm run dev</code> in <code>_frontend/</code> and reload: {vite} then asks the dev server instead.{/if}</p>
<div class="panel-title" style="margin-top: 18px">What {vite 'src/main.js'} wrote into the head</div>
{echo $written | highlight('html')}
</div>
<div>
<div class="panel-title">The server's time, ticking in a Solid island</div>
<div class="clock-box" data-island="Clock" data-props="{^clock}"><span class="clock">-</span></div>
</div>
</div>

{if $sourceCount}
<div class="panel-title" style="margin-top: 26px">The manifest - every source and what it became</div>
<table class="table">
<thead><tr><th>Source</th><th>File</th><th>Kind</th><th>CSS</th></tr></thead>
<tbody>
{sources}<tr><td><code>{$source}</code></td><td><code>{$file}</code></td><td>{$kind}</td><td><code>{$css}</code></td></tr>{/sources}
</tbody>
</table>
{/if}


App: regression/errors - Page: eval/the_highlight_pipe_names_a_language_it_does_not_know
PHP
HTML
eval/the_highlight_pipe_names_a_language_it_does_not_know.pad
Result
{echo 'x' | highlight('phpp')}


App: regression/framework - Page: escaping/a_field_piped_through_highlight_with_its_language_from_a_field_is_not_sanitized_over_its_markup
PHP
escaping/a_field_piped_through_highlight_with_its_language_from_a_field_is_not_sanitized_over_its_markup.php
HTML
escaping/a_field_piped_through_highlight_with_its_language_from_a_field_is_not_sanitized_over_its_markup.pad
Result
<?php

$snippets
= [ [ 'code' => "SELECT a FROM t WHERE b = '<x>'", 'lang' => 'sql' ] ];

?>
{snippets}{$code | highlight($lang)}{/snippets}


App: regression/framework - Page: functions/the_highlight_pipe_takes_an_empty_language_as_text
PHP
functions/the_highlight_pipe_takes_an_empty_language_as_text.php
HTML
functions/the_highlight_pipe_takes_an_empty_language_as_text.pad
Result
<?php

$lang
= '';
$code = 'a < b';

?>
[{echo $code | highlight($lang) | php:substr_count(@, 'data-lang="text"')}]


App: regression/framework - Page: tags/highlight_pipe_escapes_a_value
PHP
tags/highlight_pipe_escapes_a_value.php
HTML
tags/highlight_pipe_escapes_a_value.pad
Result
<?php

$query
= "SELECT * FROM t WHERE a = '<script>' -- {php:getcwd}";

?>
{$query | highlight('sql')}