Add HtmlOutputNodePre::guessHighlighter()
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 15 Sep 2020 10:52:49 +0000 (12:52 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 15 Sep 2020 11:33:47 +0000 (13:33 +0200)
This nicely contains the guessing logic within a single function, improving
readability.

wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodePre.class.php

index a546ab6ee8d5c45c843281916bff4b6b1d02eb46..84a3d9895a61bb5ca9a3b6f4c1257ed685c0d7c8 100644 (file)
@@ -108,47 +108,7 @@ class HtmlOutputNodePre extends AbstractHtmlOutputNode {
                }
                
                if (!$highlighter) {
-                       // try to guess highlighter
-                       if (mb_strpos($content, '<?php') !== false) {
-                               $highlighter = 'php';
-                       }
-                       else if (mb_strpos($content, '<html') !== false) {
-                               $highlighter = 'html';
-                       }
-                       else if (mb_strpos($content, '<?xml') === 0) {
-                               $highlighter = 'xml';
-                       }
-                       else if (       mb_strpos($content, 'SELECT') === 0
-                               ||      mb_strpos($content, 'UPDATE') === 0
-                               ||      mb_strpos($content, 'INSERT') === 0
-                               ||      mb_strpos($content, 'DELETE') === 0) {
-                               $highlighter = 'sql';
-                       }
-                       else if (mb_strpos($content, 'import java.') !== false) {
-                               $highlighter = 'java';
-                       }
-                       else if (       mb_strpos($content, "---") !== false
-                               &&      mb_strpos($content, "\n+++") !== false) {
-                               $highlighter = 'diff';
-                       }
-                       else if (mb_strpos($content, "\n#include ") !== false) {
-                               $highlighter = 'c';
-                       }
-                       else if (mb_strpos($content, '#!/usr/bin/perl') === 0) {
-                               $highlighter = 'perl';
-                       }
-                       else if (mb_strpos($content, 'def __init__(self') !== false) {
-                               $highlighter = 'python';
-                       }
-                       else if (Regex::compile('^#!/bin/(ba|z)?sh')->match($content)) {
-                               $highlighter = 'bash';
-                       }
-                       else if (mb_strpos($content, '\\documentclass') !== false) {
-                               $highlighter = 'latex';
-                       }
-                       else if (mb_strpos($content, '!important;') !== false) {
-                               $highlighter = 'css';
-                       }
+                       $highlighter = $this->guessHighlighter($content);
                }
                $eventData = [
                        'highlighter' => $highlighter,
@@ -190,6 +150,59 @@ class HtmlOutputNodePre extends AbstractHtmlOutputNode {
                return WCF::getTPL()->fetch('codeMetaCode');
        }
        
+       /**
+        * Returns a likely highlighter for the given content.
+        * 
+        * @param string $content
+        * @return string
+        */
+       public function guessHighlighter($content) {
+               $highlighter = '';
+               
+               if (mb_strpos($content, '<?php') !== false) {
+                       $highlighter = 'php';
+               }
+               else if (mb_strpos($content, '<html') !== false) {
+                       $highlighter = 'html';
+               }
+               else if (mb_strpos($content, '<?xml') === 0) {
+                       $highlighter = 'xml';
+               }
+               else if (       mb_strpos($content, 'SELECT') === 0
+                       ||      mb_strpos($content, 'UPDATE') === 0
+                       ||      mb_strpos($content, 'INSERT') === 0
+                       ||      mb_strpos($content, 'DELETE') === 0) {
+                       $highlighter = 'sql';
+               }
+               else if (mb_strpos($content, 'import java.') !== false) {
+                       $highlighter = 'java';
+               }
+               else if (       mb_strpos($content, "---") !== false
+                       &&      mb_strpos($content, "\n+++") !== false) {
+                       $highlighter = 'diff';
+               }
+               else if (mb_strpos($content, "\n#include ") !== false) {
+                       $highlighter = 'c';
+               }
+               else if (mb_strpos($content, '#!/usr/bin/perl') === 0) {
+                       $highlighter = 'perl';
+               }
+               else if (mb_strpos($content, 'def __init__(self') !== false) {
+                       $highlighter = 'python';
+               }
+               else if (Regex::compile('^#!/bin/(ba|z)?sh')->match($content)) {
+                       $highlighter = 'bash';
+               }
+               else if (mb_strpos($content, '\\documentclass') !== false) {
+                       $highlighter = 'latex';
+               }
+               else if (mb_strpos($content, '!important;') !== false) {
+                       $highlighter = 'css';
+               }
+               
+               return $highlighter;
+       }
+       
        /**
         * Returns a unique ID for this code block.
         *