Ace Editor使用括号的PHP语法

时间:2017-03-27 20:58:43

标签: javascript php wordpress ace-editor

在Ace Editor中使用PHP模式获取短代码时尝试弄清楚如何处理语法错误(包括PHP代码中的短代码)。突出显示已经/看起来很好,问题在于使用短代码时显示的PHP语法错误。

简单的短代码为[shortcode],而扩展的短代码则为[shortcode]something[/shortcode]

function foo( $title ) {

    // Example single shortcode (without closing shortcode), [...] will be replaced with "Some value entered by user"
    $site_name = [get_input type="text" desc="Please enter the site name"];

    // Example with open closing shortcodes
    [check_question question="Do you want to add period to the end of the title?"]
        $title .= '.';
    [/check_question]

    $title = ucfirst( $title );

    return $title;
}

有人可以帮我弄清楚如何扩展PHP模式/ worker,或者需要做些什么来防止在包含短代码时在编辑器中显示PHP语法错误?除了短代码上的简单PHP语法错误外,一切都很好。

https://github.com/ajaxorg/ace/wiki/Creating-or-Extending-an-Edit-Mode

小提琴示例: http://jsfiddle.net/tripflex/crm4ywu0/

后端用于将[...]替换为自定义值/代码的正则表达式是:

\[(\[?)(get_input|check_question)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)

直接来自WordPress短代码处理: https://github.com/WordPress/WordPress/blob/master/wp-includes/js/shortcode.js#L105 https://github.com/WordPress/WordPress/blob/master/wp-includes/shortcodes.php#L264

1 个答案:

答案 0 :(得分:0)

在将值传递给php词法分析器之前,您可以使用该正则表达式替换https://github.com/ajaxorg/ace/blob/v1.2.6/lib/ace/mode/php_worker.js#L51处的括号。 不幸的是,如果不直接修改worker-php.js文件中的源代码,就无法做到这一点。