Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git
File.php
public function highlight( FileRenderer $renderer, string $content ): string {
- $lang = match( $this->mediaType ) {
- 'text/x-php', 'application/x-php', 'application/x-httpd-php' => 'php',
- 'text/html' => 'html',
- 'text/css' => 'css',
- 'application/javascript', 'text/javascript', 'text/x-javascript' => 'javascript',
- 'application/json', 'text/json', 'application/x-json' => 'json',
- 'application/xml', 'text/xml', 'image/svg+xml' => 'xml',
- 'text/x-shellscript', 'application/x-sh' => 'bash',
- 'text/x-c', 'text/x-csrc' => 'c',
- 'text/x-c++src', 'text/x-c++', 'text/x-cpp' => 'cpp',
- 'text/x-java', 'text/x-java-source', 'application/java-archive' => 'java',
- 'text/x-python', 'application/x-python-code' => 'python',
- 'text/x-ruby', 'application/x-ruby' => 'ruby',
- 'text/x-go', 'text/go' => 'go',
- 'text/rust', 'text/x-rust' => 'rust',
- 'text/x-lua', 'text/lua' => 'lua',
- 'text/markdown', 'text/x-markdown' => 'markdown',
- 'text/x-r', 'text/x-r-source', 'application/R' => 'r',
- 'application/sql', 'text/sql', 'text/x-sql' => 'sql',
- 'text/yaml', 'text/x-yaml', 'application/yaml' => 'yaml',
- 'application/typescript', 'text/typescript' => 'typescript',
- default => null
- };
-
- if( $lang === null ) {
- $ext = strtolower( pathinfo( $this->name, PATHINFO_EXTENSION ) );
-
- $lang = match( $ext ) {
- 'php', 'phtml', 'php8', 'php7' => 'php',
- 'c', 'h' => 'c',
- 'cpp', 'hpp', 'cc', 'cxx' => 'cpp',
- 'java' => 'java',
- 'js', 'jsx', 'mjs' => 'javascript',
- 'ts', 'tsx' => 'typescript',
- 'go' => 'go',
- 'rs' => 'rust',
- 'py', 'pyw' => 'python',
- 'rb', 'erb' => 'ruby',
- 'lua' => 'lua',
- 'sh', 'bash', 'zsh' => 'bash',
- 'bat', 'cmd' => 'batch',
- 'md', 'markdown' => 'markdown',
- 'rmd' => 'rmd',
- 'r' => 'r',
- 'xml', 'svg' => 'xml',
- 'html', 'htm' => 'html',
- 'css' => 'css',
- 'json', 'lock' => 'json',
- 'sql' => 'sql',
- 'yaml', 'yml' => 'yaml',
- default => 'text'
- };
- }
-
- return $renderer->highlight( $content, $lang );
+ return $renderer->highlight( $this->name, $content, $this->mediaType );
}
render/Highlighter.php
private array $rules;
- public function __construct(string $content, string $lang) {
+ public function __construct(string $filename, string $content, string $mediaType) {
$this->content = $content;
- $this->lang = strtolower($lang);
+
+ $this->lang = $this->detectLanguage($mediaType, $filename);
$this->rules = LanguageDefinitions::get($this->lang) ?? [];
}
$output .= '</span>';
+
return $output;
+ }
+
+ private function detectLanguage(string $mediaType, string $filename): string {
+ $lang = match( $mediaType ) {
+ 'text/x-php', 'application/x-php', 'application/x-httpd-php' => 'php',
+ 'text/html' => 'html',
+ 'text/css' => 'css',
+ 'application/javascript', 'text/javascript', 'text/x-javascript' => 'javascript',
+ 'application/json', 'text/json', 'application/x-json' => 'json',
+ 'application/xml', 'text/xml', 'image/svg+xml' => 'xml',
+ 'text/x-shellscript', 'application/x-sh' => 'bash',
+ 'text/x-c', 'text/x-csrc' => 'c',
+ 'text/x-c++src', 'text/x-c++', 'text/x-cpp' => 'cpp',
+ 'text/x-java', 'text/x-java-source', 'application/java-archive' => 'java',
+ 'text/x-python', 'application/x-python-code' => 'python',
+ 'text/x-ruby', 'application/x-ruby' => 'ruby',
+ 'text/x-go', 'text/go' => 'go',
+ 'text/rust', 'text/x-rust' => 'rust',
+ 'text/x-lua', 'text/lua' => 'lua',
+ 'text/markdown', 'text/x-markdown' => 'markdown',
+ 'text/x-r', 'text/x-r-source', 'application/R' => 'r',
+ 'application/sql', 'text/sql', 'text/x-sql' => 'sql',
+ 'text/yaml', 'text/x-yaml', 'application/yaml' => 'yaml',
+ 'application/typescript', 'text/typescript' => 'typescript',
+ 'text/x-gradle' => 'gradle',
+ default => null
+ };
+
+ if( $lang !== null ) {
+ return $lang;
+ }
+
+ $ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
+
+ return match( $ext ) {
+ 'php', 'phtml', 'php8', 'php7' => 'php',
+ 'c', 'h' => 'c',
+ 'cpp', 'hpp', 'cc', 'cxx' => 'cpp',
+ 'java' => 'java',
+ 'js', 'jsx', 'mjs' => 'javascript',
+ 'ts', 'tsx' => 'typescript',
+ 'go' => 'go',
+ 'rs' => 'rust',
+ 'py', 'pyw' => 'python',
+ 'rb', 'erb' => 'ruby',
+ 'lua' => 'lua',
+ 'sh', 'bash', 'zsh' => 'bash',
+ 'bat', 'cmd' => 'batch',
+ 'md', 'markdown' => 'markdown',
+ 'rmd' => 'rmd',
+ 'r' => 'r',
+ 'xml', 'svg' => 'xml',
+ 'html', 'htm' => 'html',
+ 'css' => 'css',
+ 'json', 'lock' => 'json',
+ 'sql' => 'sql',
+ 'yaml', 'yml' => 'yaml',
+ 'gradle' => 'gradle',
+ default => 'text'
+ };
}
}
render/LanguageDefinitions.php
$rules = [
+ 'gradle' => [
+ 'comment' => '/(\/\/[^\r\n]*|\/\*.*?\*\/)/ms',
+ 'string_interp' => '/(".*?"|""".*?""")/',
+ 'string' => '/(\'.*?\'|\'\'\'.*?\'\'\'|\/.*?\/)/', // Includes regex literals
+ 'keyword' => '/\b(def|task|apply|plugin|sourceCompatibility|targetCompatibility|repositories|dependencies|test|group|version|plugins|buildscript|allprojects|subprojects|project|ext|implementation|api|compileOnly|runtimeOnly|testImplementation|testRuntimeOnly|mavenCentral|google|jcenter|classpath)\b/',
+ 'function' => '/\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\{)/', // Gradle tasks often look like taskname {
+ 'variable' => '/(\$[a-zA-Z_][a-zA-Z0-9_]*|\$\{[^}]+\})/',
+ 'boolean' => '/\b(true|false|null)\b/',
+ 'number' => '/' . $int . '/',
+ ],
'php' => [
'tag' => '/(<\?php|<\?|=\?>|\?>)/',
}
}
+

Refactors file and highlighter responsibilities

Author Dave Jarvis <email>
Date 2026-02-14 20:41:01 GMT-0800
Commit ca9f344f5d46cc00259a9a696125f81d7226d1cb
Parent 05982a6
Delta 76 lines added, 57 lines removed, 19-line increase