Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git
pages/FilePage.php
echo '<div class="empty-state download-state">';
echo '<p>' . htmlspecialchars( $reason ) . '</p>';
- echo '<a href="' . $url . '" class="btn-download">Download Raw File</a>';
+ echo '<a href="' . $url . '" class="btn-download">Download</a>';
echo '</div>';
}
render/Highlighter.php
private array $rules;
- public function __construct(string $filename, string $content, string $mediaType) {
+ public function __construct( string $filename, string $content, string $mediaType ) {
$this->content = $content;
- $this->lang = $this->detectLanguage($mediaType, $filename);
- $this->rules = LanguageDefinitions::get($this->lang) ?? [];
+ $this->lang = $this->detectLanguage( $mediaType, $filename );
+ $this->rules = LanguageDefinitions::get( $this->lang ) ?? [];
}
public function render(): string {
- if (empty($this->rules)) {
- return htmlspecialchars($this->content);
+ if( empty( $this->rules ) ) {
+ return htmlspecialchars( $this->content );
}
$patterns = [];
- foreach ($this->rules as $name => $pattern) {
+ foreach( $this->rules as $name => $pattern ) {
$delim = $pattern[0];
- $inner = substr($pattern, 1, strrpos($pattern, $delim) - 1);
- $inner = str_replace('~', '\~', $inner);
+ $inner = substr( $pattern, 1, strrpos( $pattern, $delim ) - 1 );
+ $inner = str_replace( '~', '\~', $inner );
$patterns[] = "(?P<{$name}>{$inner})";
}
- if (!in_array($this->lang, ['markdown', 'rmd'])) {
+ if( !in_array( $this->lang, ['markdown', 'rmd'] ) ) {
$patterns[] = "(?P<punctuation>[\\{\\}\\(\\)\\[\\]\\;\\,])";
}
$patterns[] = "(?P<any>[\s\S])";
- $combined = '~' . implode('|', $patterns) . '~msu';
+ $combined = '~' . implode( '|', $patterns ) . '~msu';
- return preg_replace_callback($combined, function ($matches) {
- foreach ($matches as $key => $value) {
- if (!is_numeric($key) && $value !== '') {
- if ($key === 'any') {
- return htmlspecialchars($value);
+ return preg_replace_callback( $combined, function( $matches ) {
+ foreach( $matches as $key => $value ) {
+ if( !is_numeric( $key ) && $value !== '' ) {
+ if( $key === 'any' ) {
+ return htmlspecialchars( $value );
}
- if ($key === 'string_interp') {
- return $this->renderInterpolatedString($value);
+ if( $key === 'string_interp' ) {
+ return $this->renderInterpolatedString( $value );
}
- if ($key === 'math') {
- return $this->renderMath($value);
+ if( $key === 'math' ) {
+ return $this->renderMath( $value );
}
- return '<span class="hl-' . $key . '">' . htmlspecialchars($value) . '</span>';
+ return '<span class="hl-' . $key . '">' . htmlspecialchars( $value ) . '</span>';
}
}
- return htmlspecialchars($matches[0]);
- }, $this->content);
+ return htmlspecialchars( $matches[0] );
+ }, $this->content );
}
- private function renderInterpolatedString(string $content): string {
+ private function renderInterpolatedString( string $content ): string {
$pattern = '/(\$\{[a-zA-Z0-9_]+\}|\$[a-zA-Z0-9_]+)/';
- $parts = preg_split($pattern, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
+ $parts = preg_split( $pattern, $content, -1, PREG_SPLIT_DELIM_CAPTURE );
$output = '<span class="hl-string">';
- foreach ($parts as $part) {
- if ($part === '') continue;
+ foreach( $parts as $part ) {
+ if( $part === '' ) continue;
- if (str_starts_with($part, '${') && str_ends_with($part, '}')) {
- $inner = substr($part, 2, -1);
+ if( str_starts_with( $part, '${' ) && str_ends_with( $part, '}' ) ) {
+ $inner = substr( $part, 2, -1 );
$output .= '<span class="hl-interp-punct">${</span>';
- $output .= '<span class="hl-variable">' . htmlspecialchars($inner) . '</span>';
+ $output .= '<span class="hl-variable">' . htmlspecialchars( $inner ) . '</span>';
$output .= '<span class="hl-interp-punct">}</span>';
- } elseif (str_starts_with($part, '$') && strlen($part) > 1) {
+ } elseif( str_starts_with( $part, '$' ) && strlen( $part ) > 1 ) {
$output .= '<span class="hl-interp-punct">$</span>';
- $output .= '<span class="hl-variable">' . htmlspecialchars(substr($part, 1)) . '</span>';
+ $output .= '<span class="hl-variable">' . htmlspecialchars( substr( $part, 1 ) ) . '</span>';
} else {
- $output .= htmlspecialchars($part);
+ $output .= htmlspecialchars( $part );
}
}
$output .= '</span>';
return $output;
}
- private function renderMath(string $content): string {
- $parts = preg_split('/(`[^`]+`)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
+ private function renderMath( string $content ): string {
+ $parts = preg_split( '/(`[^`]+`)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE );
$output = '';
- foreach ($parts as $part) {
- if ($part === '') continue;
+ foreach( $parts as $part ) {
+ if( $part === '' ) continue;
- if (str_starts_with($part, '`') && str_ends_with($part, '`')) {
- $output .= '<span class="hl-function">' . htmlspecialchars($part) . '</span>';
+ if( str_starts_with( $part, '`' ) && str_ends_with( $part, '`' ) ) {
+ $output .= '<span class="hl-function">' . htmlspecialchars( $part ) . '</span>';
} else {
- $output .= '<span class="hl-math">' . htmlspecialchars($part) . '</span>';
+ $output .= '<span class="hl-math">' . htmlspecialchars( $part ) . '</span>';
}
}
return $output;
}
- private function detectLanguage(string $mediaType, string $filename): string {
+ private function detectLanguage( string $mediaType, string $filename ): string {
$lang = match( $mediaType ) {
'text/x-php', 'application/x-php', 'application/x-httpd-php' => 'php',
'application/typescript', 'text/typescript' => 'typescript',
'text/x-gradle' => 'gradle',
+ 'text/x-tex', 'application/x-tex' => 'tex',
default => null
};
'yaml', 'yml' => 'yaml',
'gradle' => 'gradle',
+ 'tex', 'sty', 'cls', 'ltx' => 'tex',
default => 'text'
};
render/LanguageDefinitions.php
<?php
class LanguageDefinitions {
- public static function get(string $lang): array {
+ public static function get( string $lang ): array {
$int = '(-?\b\d+(\.\d+)?\b)';
$str = '(".*?"|\'.*?\')';
'boolean' => '/\b(true|false|null)\b/',
'number' => '/' . $int . '/',
+ ],
+ 'tex' => [
+ 'comment' => '/(%[^\r\n]*)/m',
+ 'math' => '/(\$\$?.*?\$\$?)/s',
+ 'keyword' => '/(\\\\(?:begin|end|documentclass|usepackage|input|include|def|newcommand|renewcommand|part|chapter|section|subsection|subsubsection|paragraph|subparagraph))\b/',
+ 'function' => '/(\\\\[a-zA-Z@]+|\\\\[^a-zA-Z@])/',
+ 'variable' => '/(#[0-9])/',
],
'php' => [
];
- return $rules[strtolower($lang)] ?? [];
+ return $rules[strtolower( $lang )] ?? [];
}
}

Apples TeX formatting

Author Dave Jarvis <email>
Date 2026-02-14 23:51:05 GMT-0800
Commit 352e2327c52210db149e6396df91201733201517
Parent cffafd2
Delta 52 lines added, 43 lines removed, 9-line increase