| | 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' |
| | }; |