public function testAutoDetection()
 {
     $hl = new Highlight\Highlighter();
     $lngs = $hl->listLanguages();
     $hl->setAutodetectLanguages($lngs);
     $failed = array();
     foreach ($lngs as $language) {
         $path = __DIR__ . DIRECTORY_SEPARATOR . "detect" . DIRECTORY_SEPARATOR . $language;
         $this->assertTrue(file_exists($path));
         $d = dir($path);
         while (false !== ($entry = $d->read())) {
             if ($entry[0] !== ".") {
                 $filePath = $path . DIRECTORY_SEPARATOR . $entry;
                 $content = file_get_contents($filePath);
                 $expected = $language;
                 $r = $hl->highlightAuto($content);
                 $actual = $r->language;
                 if ($expected !== $actual) {
                     $failed[] = "{$expected} was detected as {$actual}";
                 }
             }
         }
         $d->close();
     }
     $this->assertEquals(array(), $failed);
 }
Example #2
0
                                         </div>
                                     </div>
                                 </div>
                             </div>
                             <div class="comment-body">
                                 <div class="text">
                                     <?php 
     $pre_processed_text = \yii\helpers\Markdown::process($comment->text);
     $highlighter = new \Highlight\Highlighter();
     $highlighter->setAutodetectLanguages(array("php", "javascript", "html"));
     $matches = array();
     $pattern = "/<code>(.*?)<\\/code>/is";
     preg_match_all($pattern, $pre_processed_text, $matches);
     if (count($matches) > 0) {
         foreach ($matches[1] as $match) {
             $processed = $highlighter->highlightAuto(html_entity_decode($match));
             $pre_processed_text = str_replace($match, $processed->value, $pre_processed_text);
             $pre_processed_text = str_replace('<code>', '<pre><code class="hljs ' . $processed->language . '">', $pre_processed_text);
             $pre_processed_text = str_replace('</code>', '</code></pre>', $pre_processed_text);
         }
     }
     echo $pre_processed_text;
     ?>
                                 </div>
                             </div>
                         </div>
                     </div>
                 </div>
             <?php 
 }
 ?>
pre { margin: 0px; }

    </style>
    
  </head>
  <body>
    <table id="test">
    <tr>
      <th>result</th>
      <th>highlight.php</th>
      <th>highlight.js</th>
    </tr>
<?php 
foreach ($hl->listLanguages() as $languageId) {
    $snippet = file_get_contents("../test/detect/{$languageId}/default.txt");
    $r = $hl->highlightAuto($snippet);
    ?>
    <tr>
      <td class="signal"><p><?php 
    echo $r->language;
    ?>
</p><p>pass</p></td>
      <td>
        <p><?php 
    echo $r->language;
    ?>
: <?php 
    echo $r->relevance;
    ?>
</p>
        <p><?php