Frage

ich eine persönliche Notiz-Software in PHP gemacht hatte, so dass ich speichern kann und meine Notizen organisieren und für ein schönes, einfaches Format wünschte ihnen in schreiben.

Ich hatte es in Markdown gemacht, aber fand es war ein wenig verwirrend, und es gab keine einfache Syntax-Hervorhebung, so dass ich bbcode tat vor und wünschte sich, dass zu implementieren.

Jetzt für GeSHi, die ich wirklich umsetzen wollen (die Syntax Highlighter), es erfordert den einfachsten Code wie folgt:

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

Dies ist nun der einfache Teil, aber was ich möchte, ist zu tun, damit mein bbcode es nennen.

Mein aktueller regulärer Ausdruck Übereinstimmen a aus [syntax = CPP] [/ Syntax] bbcode ist die folgende:

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

Sie werden feststellen, ich die Sprache und den Inhalt erfassen, wie auf der Erde würde ich es den GeSHi Code verbinden?

preg_replace scheint nur in der Lage sein, sie zu ersetzen mit einem String kein ‚Ausdruck‘, ich bin nicht sicher, wie für GeSHi diese beiden Zeilen Code zu verwenden, bis es mit den erfassten Daten ..

Ich bin wirklich begeistert von diesem Projekt und Wunsch, dies zu überwinden.

War es hilfreich?

Lösung

Ich schrieb diese Klasse eine Weile zurück, der Grund für die Klasse eine einfache Anpassung / Parsing zu ermöglichen war. Vielleicht ein wenig übertrieben, aber gut funktioniert und ich brauchte es viel des Guten für meine Anwendung. Die Benutzung ist ganz einfach:

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

---- ---- ODER

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

Ich hatte einige Hacken von anderen benutzerdefinierten Elementen zu tun hatte ich, aber anhängig keine Syntaxfehler aus den Hacken sollte es funktionieren. Fühlen Sie sich frei, es zu benutzen.

<?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);
    }
}
?>

Andere Tipps

Es scheint mir, wie Sie bereits die regex richtig verstanden haben. Ihr Problem liegt in der Beschwörung, so schlage ich eine Wrapper-Funktion machen:

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

Nun würde dies in der Regel ausreicht, aber der Quellcode ist wahrscheinlich einzelne oder dobule Anführungszeichen selbst enthält. Sie können also nicht preg_replace(".../e", "geshi('$2','$1')", ...) schreiben, wie Sie benötigen. (Beachten Sie, dass '$ 1' und '$ 2' Notwendigkeit Anführungszeichen, weil preg_replace nur den $ 1 ersetzt, $ 2 Platzhalter, aber das muss gültig php Inline-Code sein).

Deshalb sollten Sie auf die Verwendung preg_replace_callback müssen in der / e exec Ersatzcode zu entkommen Probleme zu vermeiden. So zum Beispiel:

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

Und ich würde eine zweite Wrapper machen, aber Sie es mit dem ursprünglichen Code kombinieren können:

function geshi_replace($uu) {
    return geshi($uu[2], $uu[1]);
}

Verwenden Sie preg_match:

$match = preg_match('#\[syntax=(.*?)\](.*?)\[/syntax\]#si', $text);
$geshi = new GeSHi($match[2], $match[1]);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top