0

我用 PHP 制作了一个个人笔记软件,所以我可以存储和组织我的笔记,并希望有一个很好的简单格式来写它们。

我在 Markdown 中做过,但发现它有点混乱,并且没有简单的语法突出显示,所以我之前做过 bbcode,并希望实现它。

现在对于我真正希望实现的 GeSHi(语法高亮器),它需要最简单的代码,如下所示:

$geshi = new GeSHi($sourcecode, $language);
$geshi->parse_code();

现在这是简单的部分,但我想做的是让我的 bbcode 调用它。

我当前匹配组成的 [syntax=cpp][/syntax] bbcode 的正则表达式如下:

preg_replace('#\[syntax=(.*?)\](.*?)\[/syntax\]#si' , 'geshi(\\2,\\1)????', text);

你会注意到我捕获了语言和内容,我到底如何将它连接到 GeSHi 代码?

preg_replace 似乎只能用字符串而不是“表达式”替换它,我不知道如何将这两行代码用于 GeSHi 与捕获的数据..

我对这个项目真的很兴奋,并希望克服这个问题。

4

3 回答 3

2

在我看来,您已经正确使用了正则表达式。你的问题在于调用,所以我建议制作一个包装函数:

function geshi($src, $l) {
    $geshi = new GeSHi($sourcecode, $language);
    $geshi->parse_code();
    return $geshi->how_do_I_get_the_results();
}

现在这通常就足够了,但源代码本身可能包含单引号或双引号。因此,您无法preg_replace(".../e", "geshi('$2','$1')", ...)按照需要进行编写。(注意 '$1' 和 '$2' 需要引号,因为 preg_replace 只是替换了 $1,$2 占位符,但这需要是有效的 php 内联代码)。

这就是为什么您需要使用preg_replace_callback来避免在 /e exec 替换代码中出现转义问题。例如:

preg_replace_callback('#\[syntax=(.*?)\](.*?)\[/syntax\]#si' , 'geshi_replace', $text);

我会制作第二个包装器,但你可以将它与原始代码结合起来:

function geshi_replace($uu) {
    return geshi($uu[2], $uu[1]);
}
于 2010-11-17T17:15:15.640 回答
2

我不久前写了这个类,该类的原因是允许轻松自定义/解析。也许有点矫枉过正,但效果很好,我的应用程序需要它矫枉过正。用法很简单:

$geshiH = new Geshi_Helper();
$text = $geshiH->geshi($text); // this assumes that the text should be parsed (ie inline syntaxes)

- - 或者 - -

$geshiH = new Geshi_Helper();
$text = $geshiH->geshi($text, $lang);  // assumes that you have the language, good for a snippets deal

我必须从我拥有的其他自定义项目中进行一些切割,但是在切割没有语法错误之前它应该可以工作。随意使用它。

<?php

require_once 'Geshi/geshi.php';

class Geshi_Helper 
{
    /**
     * @var array Array of matches from the code block.
     */
    private $_codeMatches = array();

    private $_token = "";

    private $_count = 1;

    public function __construct()
    {
        /* Generate a unique hash token for replacement) */
        $this->_token = md5(time() . rand(9999,9999999));
    }

    /**
     * Performs syntax highlights using geshi library to the content.
     *
     * @param string $content - The context to parse
     * @return string Syntax Highlighted content
     */
    public function geshi($content, $lang=null)
    {
        if (!is_null($lang)) {
            /* Given the returned results 0 is not set, adding the "" should make this compatible */
            $content = $this->_highlightSyntax(array("", strtolower($lang), $content));
        }else {
            /* Need to replace this prior to the code replace for nobbc */
            $content = preg_replace('~\[nobbc\](.+?)\[/nobbc\]~ie', '\'[nobbc]\' . strtr(\'$1\', array(\'[\' => \'&#91;\', \']\' => \'&#93;\', \':\' => \'&#58;\', \'@\' => \'&#64;\')) . \'[/nobbc]\'', $content);

            /* For multiple content we have to handle the br's, hence the replacement filters */
            $content = $this->_preFilter($content);

            /* Reverse the nobbc markup */
            $content = preg_replace('~\[nobbc\](.+?)\[/nobbc\]~ie', 'strtr(\'$1\', array(\'&amp;#91;\' => \'[\', \'&amp;#93;\' => \']\', \'&amp;#58;\' => \':\', \'&amp;#64;\' => \'@\'))', $content);

            $content = $this->_postFilter($content);
        }

        return $content;
    }

    /**
     * Performs syntax highlights using geshi library to the content.
     * If it is unknown the number of blocks, use highlightContent
     * instead.
     *
     * @param string $content - The code block to parse
     * @param string $language - The language to highlight with
     * @return string Syntax Highlighted content
     * @todo Add any extra / customization styling here.
     */
    private function _highlightSyntax($contentArray)
    {
        $codeCount = $contentArray[1];

        /* If the count is 2 we are working with the filter */
        if (count($contentArray) == 2) {
            $contentArray = $this->_codeMatches[$contentArray[1]];
        }

        /* for default [syntax] */
        if ($contentArray[1] == "")
            $contentArray[1] = "php";

        /* Grab the language */
        $language = (isset($contentArray[1]))?$contentArray[1]:'text';

        /* Remove leading spaces to avoid problems */
        $content = ltrim($contentArray[2]);

        /* Parse the code to be highlighted */
        $geshi = new GeSHi($content, strtolower($language));
        return $geshi->parse_code();
    }

    /**
     * Substitute the code blocks for formatting to be done without
     * messing up the code.
     *
     * @param array $match - Referenced array of items to substitute
     * @return string Substituted content
     */
    private function _substitute(&$match)
    {
        $index = sprintf("%02d", $this->_count++);
        $this->_codeMatches[$index] = $match;
        return "----" . $this->_token . $index . "----";
    }

    /**
     * Removes the code from the rest of the content to apply other filters.
     *
     * @param string $content - The content to filter out the code lines
     * @return string Content with code removed.
     */
    private function _preFilter($content)
    {
        return preg_replace_callback("#\s*\[syntax=(.*?)\](.*?)\[/syntax\]\s*#siU", array($this, "_substitute"), $content);
    }

    /**
     * Replaces the code after the filters have been ran.
     *
     * @param string $content - The content to replace the code lines
     * @return string Content with code re-applied.
     */
    private function _postFilter($content)
    {
        /* using dashes to prevent the old filtered tag being escaped */
        return preg_replace_callback("/----\s*" . $this->_token . "(\d{2})\s*----/si", array($this, "_highlightSyntax"), $content);
    }
}
?>
于 2010-11-17T17:18:39.807 回答
1

使用 preg_match:

$match = preg_match('#\[syntax=(.*?)\](.*?)\[/syntax\]#si', $text);
$geshi = new GeSHi($match[2], $match[1]);
于 2010-11-17T17:06:37.270 回答