GeSHi语法高亮显示:显示源而不是呈现HTML

时间:2015-12-18 16:44:56

标签: html geshi

如何输出突出显示的代码片段的HTML源代码,而不是显示呈现的HTML?在下面的示例中,输出完全呈现,但我需要HTML源。

<?php

header('Content-Type: text/html; charset=utf-8');
include_once '../geshi.php';

$fill_source = false;
if (isset($_POST['submit'])) {
    if (get_magic_quotes_gpc()) {
        $_POST['source'] = stripslashes($_POST['source']);
    }
    if (!strlen(trim($_POST['source']))) {
        $_POST['language'] = preg_replace('#[^a-zA-Z0-9\-_]#', '', $_POST['language']);
        $_POST['source'] = implode('', @file($path . 'geshi/' . $_POST['language'] . '.php'));
        $_POST['language'] = 'php';
    } else {
        $fill_source = true;
    }

    $geshi = new GeSHi($_POST['source'], $_POST['language']);
    $geshi->enable_classes();

} else {
    // make sure we don't preselect any language
    $_POST['language'] = 'c';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<body>
<?php
if (isset($_POST['submit'])) {
    // The fun part :)
    echo $geshi->parse_code();
    echo '<hr />';
}
?>
<form action="<?php echo basename($_SERVER['PHP_SELF']); ?>" method="post">
<h3>Source to highlight</h3>
<p>
<textarea rows="10" cols="60" name="source" id="source"><?php echo $fill_source ? htmlspecialchars($_POST['source']) : '' ?></textarea>
</p>
<h3>Choose a language</h3>
<p>
<select name="language" id="language">
<?php
if (!($dir = @opendir(dirname(__FILE__) . '/geshi'))) {
    if (!($dir = @opendir(dirname(__FILE__) . '/../geshi'))) {
        echo '<option>No languages available!</option>';
    }
}
$languages = array();
while ($file = readdir($dir)) {
    if ( $file[0] == '.' || strpos($file, '.', 1) === false) {
        continue;
    }
    $lang = substr($file, 0,  strpos($file, '.'));
    $languages[] = $lang;
}
closedir($dir);
sort($languages);
foreach ($languages as $lang) {
    if (isset($_POST['language']) && $_POST['language'] == $lang) {
        $selected = 'selected="selected"';
    } else {
        $selected = '';
    }
    echo '<option value="' . $lang . '" '. $selected .'>' . $lang . "</option>\n";
}

?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Highlight Source" />
<input type="submit" name="clear" onclick="document.getElementById('source').value='';document.getElementById('language').value='';return false" value="clear" />
</p>
</form>
</body>
</html>

为了获得源HTML代码,我必须在上面的代码中进行更改?

1 个答案:

答案 0 :(得分:0)

htmlspecialchars()是你的朋友:

 echo htmlspecialchars($geshi->parse_code());