Extend automated guessing logic of highlighters
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 15 Sep 2020 11:39:31 +0000 (13:39 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 15 Sep 2020 11:47:08 +0000 (13:47 +0200)
wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodePre.class.php

index 84a3d9895a61bb5ca9a3b6f4c1257ed685c0d7c8..8a6f8b46ad6a0ccb1d421465ab3430da09459ab9 100644 (file)
@@ -157,50 +157,89 @@ class HtmlOutputNodePre extends AbstractHtmlOutputNode {
         * @return string
         */
        public function guessHighlighter($content) {
-               $highlighter = '';
+               // PHP at the beginning is almost surely PHP.
+               if (mb_strpos($content, '<?php') === 0) {
+                       return 'php';
+               }
                
-               if (mb_strpos($content, '<?php') !== false) {
-                       $highlighter = 'php';
+               if (    mb_strpos($content, 'SELECT') === 0
+               ||      mb_strpos($content, 'UPDATE') === 0
+               ||      mb_strpos($content, 'INSERT') === 0
+               ||      mb_strpos($content, 'DELETE') === 0) {
+                       return 'sql';
+               }
+               
+               if (mb_strpos($content, 'import java.') !== false) {
+                       return 'java';
+               }
+               
+               if (    mb_strpos($content, "---") !== false
+               &&      mb_strpos($content, "\n+++") !== false) {
+                       return 'diff';
                }
-               else if (mb_strpos($content, '<html') !== false) {
-                       $highlighter = 'html';
+               
+               if (mb_strpos($content, "\n#include ") !== false) {
+                       return 'c';
+               }
+               
+               if (mb_strpos($content, '#!/usr/bin/perl') === 0) {
+                       return 'perl';
+               }
+               
+               if (    mb_strpos($content, '#!/usr/bin/python') === 0
+               ||      mb_strpos($content, 'def __init__(self') !== false
+               ||      Regex::compile("from (\S+) import (\S+)")->match($content)) {
+                       return 'python';
                }
-               else if (mb_strpos($content, '<?xml') === 0) {
-                       $highlighter = 'xml';
+               
+               if (Regex::compile('^#!/bin/(ba|z)?sh')->match($content)) {
+                       return 'bash';
                }
-               else if (       mb_strpos($content, 'SELECT') === 0
-                       ||      mb_strpos($content, 'UPDATE') === 0
-                       ||      mb_strpos($content, 'INSERT') === 0
-                       ||      mb_strpos($content, 'DELETE') === 0) {
-                       $highlighter = 'sql';
+               
+               if (    mb_strpos($content, 'FROM') === 0
+               &&      mb_strpos($content, "RUN") !== false) {
+                       return 'docker';
                }
-               else if (mb_strpos($content, 'import java.') !== false) {
-                       $highlighter = 'java';
+               
+               if (    mb_stripos($content, "RewriteRule") !== false
+               ||      mb_stripos($content, "RewriteEngine On") !== false
+               ||      mb_stripos($content, "AuthUserFile") !== false) {
+                       return 'apacheconf';
                }
-               else if (       mb_strpos($content, "---") !== false
-                       &&      mb_strpos($content, "\n+++") !== false) {
-                       $highlighter = 'diff';
+               
+               if (mb_strpos($content, '\\documentclass') !== false) {
+                       return 'latex';
                }
-               else if (mb_strpos($content, "\n#include ") !== false) {
-                       $highlighter = 'c';
+               
+               // PHP somewhere later might not necessarily be PHP, it could also be
+               // a .patch or a Dockerfile.
+               if (mb_strpos($content, '<?php') !== false) {
+                       return 'php';
                }
-               else if (mb_strpos($content, '#!/usr/bin/perl') === 0) {
-                       $highlighter = 'perl';
+               
+               if (    mb_strpos($content, '{/if}') !== false
+               &&      (       mb_strpos($content, '<div') !== false
+                       ||      mb_strpos($content, '<span') !== false)) {
+                       return 'smarty';
                }
-               else if (mb_strpos($content, 'def __init__(self') !== false) {
-                       $highlighter = 'python';
+               
+               if (mb_strpos($content, '<html') !== false) {
+                       return 'html';
                }
-               else if (Regex::compile('^#!/bin/(ba|z)?sh')->match($content)) {
-                       $highlighter = 'bash';
+               
+               if (mb_strpos($content, '<?xml') === 0) {
+                       return 'xml';
                }
-               else if (mb_strpos($content, '\\documentclass') !== false) {
-                       $highlighter = 'latex';
+               
+               if (mb_strpos($content, '@mixin') !== false) {
+                       return 'scss';
                }
-               else if (mb_strpos($content, '!important;') !== false) {
-                       $highlighter = 'css';
+               
+               if (mb_strpos($content, '!important;') !== false) {
+                       return 'css';
                }
                
-               return $highlighter;
+               return '';
        }
        
        /**