Pygments代码中的行号在Windows上的xampp中突出显示

时间:2016-04-19 00:22:57

标签: python line-numbers pygments

我已在Windows上配置xampp以使用python 2.7和Pygments。我的PHP代码在网站上的Pygments中正确突出显示。代码有颜色,span元素,类。

它的外观如下:

enter image description here

但我无法获得行号。

我读过教程时,它取决于python脚本中的linenos值。该值应为tableinline1True

但它对我不起作用。我仍然提供相同的最终代码

<!doctype html>
<html lang="pl">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="gh.css">
</head>
<body>
<div class="highlight highlight-php"><pre><code><span class="nv">$name</span> <span class="o">=</span> <span class="s2">"Jaś"</span><span class="p">;</span>
<span class="k">echo</span> <span class="s2">"Zażółć gęślą jaźń, "</span> <span class="o">.</span> <span class="nv">$name</span> <span class="o">.</span> <span class="s1">'.'</span><span class="p">;</span>
<span class="k">echo</span> <span class="s2">"hehehe@jo.io"</span><span class="p">;</span>
</code></pre></div>
</html>

如何添加行号?我在下面放了两个网站文件:

index.py

import sys
from pygments import highlight
from pygments.formatters import HtmlFormatter

# If there isn't only 2 args something weird is going on
expecting = 2;
if ( len(sys.argv) != expecting + 1 ):
  exit(128)

# Get the code
language = (sys.argv[1]).lower()
filename = sys.argv[2]
f = open(filename, 'rb')
code = f.read()
f.close()

# PHP
if language == 'php':
  from pygments.lexers import PhpLexer
  lexer = PhpLexer(startinline=True)

# GUESS
elif language == 'guess':
  from pygments.lexers import guess_lexer
  lexer = guess_lexer( code )

# GET BY NAME
else:
  from pygments.lexers import get_lexer_by_name
  lexer = get_lexer_by_name( language )


# OUTPUT
formatter = HtmlFormatter(linenos='table', encoding='utf-8', nowrap=True)
highlighted = highlight(code, lexer, formatter)

print highlighted

的index.php

<?php
define('MB_WPP_BASE', dirname(__FILE__));
function mb_pygments_convert_code($matches)
{
    $pygments_build = MB_WPP_BASE . '/index.py';
    $source_code = isset($matches[3]) ? $matches[3] : '';
    $class_name = isset($matches[2]) ? $matches[2] : '';

    // Creates a temporary filename
    $temp_file = tempnam(sys_get_temp_dir(), 'MB_Pygments_');

    // Populate temporary file
    $filehandle = fopen($temp_file, "w");
    fwrite($filehandle, html_entity_decode($source_code, ENT_COMPAT, 'UTF-8'));
    fclose($filehandle);

    // Creates pygments command
    $language = $class_name ? $class_name : 'guess';
    $command = sprintf('C:\Python27/python %s %s %s', $pygments_build, $language, $temp_file);

    // Executes the command
    $retVal = -1;
    exec($command, $output, $retVal);
    unlink($temp_file);

    // Returns Source Code
    $format = '<div class="highlight highlight-%s"><pre><code>%s</code></pre></div>';

    if ($retVal == 0)
        $source_code = implode("\n", $output);
    $highlighted_code = sprintf($format, $language, $source_code);
    return $highlighted_code;
}

// This prevent throwing error
libxml_use_internal_errors(true);

// Get all pre from post content
$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding('
<pre class="php">
<code>
$name = "Jaś";
echo "Zażółć gęślą jaźń, " . $name . \'.\';
echo "<address>hehehe@jo.io</address>";
</code>
</pre>', 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NODEFDTD);
$pres = $dom->getElementsByTagName('pre');

foreach ($pres as $pre) {
    $class = $pre->attributes->getNamedItem('class')->nodeValue;
    $code = $pre->nodeValue;

    $args = array(
        2 => $class, // Element at position [2] is the class
        3 => $code // And element at position [2] is the code
    );

    // convert the code
    $new_code = mb_pygments_convert_code($args);

    // Replace the actual pre with the new one.
    $new_pre = $dom->createDocumentFragment();
    $new_pre->appendXML($new_code);
    $pre->parentNode->replaceChild($new_pre, $pre);
}
// Save the HTML of the new code.
$newHtml = "";
foreach ($dom->getElementsByTagName('body')->item(0)->childNodes as $child) {
    $newHtml .= $dom->saveHTML($child);
}

?>
<!doctype html>
<html lang="pl">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="gh.css">
</head>
<body>
<?= $newHtml ?>
</body>
</html>

谢谢

2 个答案:

答案 0 :(得分:0)

在阅读文件时,请尝试readlines

f = open(filename, 'rb')
code = f.readlines()
f.close()

这样您可以执行以下操作,它将获得多行:

formatter = HtmlFormatter(linenos='table', encoding='utf-8', nowrap=True)

建议: 更多pythonic打开文件的方式是:

with open(filename, 'rb') as f:
    code = f.readlines()

这就是python上下文管理器为你关闭这个文件。

答案 1 :(得分:0)

解决!

  

NOWRAP

     

如果设置为True,则根本不包括标记,甚至不包括标记内。 这会禁用大多数其他选项(默认值:False)。

http://pygments.org/docs/formatters/#HtmlFormatter