0

我目前正在制作一个嵌套标签扩展,其中父标签(在我的情况下<tabs>)需要能够影响子标签的内容(在我的情况下<tab>)。我已经尝试通过运行标签的内容来做到这一点$parser->recursiveTagParse($input)$input但是<tabs>在返回的字符串中,似乎不可能匹配任何东西。为了以最基本的方式对此进行测试,我执行了以下操作:

添加到 LocalSettings.php:

require_once "$IP\\extensions\\Test\\Test.php";

在其测试目录中创建 Test.php:

<?php
$wgAutoloadClasses['Test'] = __DIR__ . '/Test.body.php';
$wgHooks['ParserFirstCallInit'][] = 'Test::init';

使用以下命令创建 Test.body.php:

<?php
class Test {
    public static function init( &$parser ) {
        $parser->setHook( 'testfoo', array( 'Test', 'firstTag' ) );
        $parser->setHook( 'testbar', array( 'Test', 'secondTag' ) );
        return true;
    }

    public static function firstTag($input, $attr = array(), $parser, $frame) {
        $newinput = $parser->recursiveTagParse($input);
        return preg_match_all("/ /", $newinput);
    }

    public static function secondTag($input, $attr = array(), $parser, $frame) {
        return htmlspecialchars($input);
    }
}

最后,将以下内容放在 wiki 的页面上:

<testfoo>three spaces out here<testbar>and four more spaces here</testbar></testfoo>

这应该是 be 的最终输出7,因为输出字符串中有 7 个空格。问题是这反而返回标签3外的空格数量。<testbar>

我想知道的是是否有可能匹配和/或替换解析嵌套标签的结果(嵌套标签输出的内容),如果不可能,是否可以制作<testbar>标签嵌套在<testfoo>标签内时表现不同?


我还尝试将PPFrame第二个参数添加到部分$parser->recursiveTagParse($input)以尝试检测嵌套,但是 PPFrame 的文档非常不清楚,以至于我不知道我需要做什么。我现在所做的,根据文档所说的是:

    public static function firstTag($input, $attr = array(), $parser, $frame) {
        $newframe = $frame->newChild(array('nested'=>'true'), $frame->getTitle(), 0);
        $newinput = $parser->recursiveTagParse($input, $newframe);
        return preg_match_all("/ /", $newinput);
    }

但这会导致错误。当我这样做时它说的是:

注意:尝试在第 984 行的 C:\hiddenpath\w\includes\parser\Preprocessor_DOM.php 中获取非对象的属性

可捕获的致命错误:传递给 DOMXPath::__construct() 的参数 1 必须是 DOMDocument 的实例,给定 null,在第 10 行的 C:\hiddenpath\w\extensions\Test\Test.body.php 中调用并在 C 中定义: \hiddenpath\w\includes\parser\Preprocessor_DOM.php 在第 984 行

但我不知道我需要做什么来解决这个问题。我需要让我的第一个论点成为一个特殊的对象吗?如果是这样,为什么文档说我需要将数组作为第一个参数?

4

1 回答 1

1

自己用替代方法回答这个问题,因为没有其他答案

我设法得到我想要的结果的方式就是放弃问题的原始第一部分:“我想知道的是是否有可能匹配和/或替换解析的结果嵌套标签(嵌套标签输出的内容) ”,然后转到第二部分,“如果这不可能,是否可以使标签在嵌套在标签内时表现不同”。

我这样做的方法是在类中定义一个变量,然后无论我是否recursiveTagParse对 a 的内容执行 a 都改变它。Test.php 中的代码将是:

    public static function init( &$parser ) {
        $parser->setHook( 'testfoo', array( new self(), 'firstTag' ) );
        $parser->setHook( 'testbar', array( new self(), 'secondTag' ) );
        return true;
    }

    function firstTag($input, $attr = array(), $parser, $frame) {
        $this::$nested = true;
        $newinput = $parser->recursiveTagParse($input);
        $this::$nested = false;
        return preg_match_all("/ /", $newinput);
    }

    function secondTag($input, $attr = array(), $parser, $frame) {
        if ($this::$nested) {
            $output = 'Display for when nested'.$input;
        } else {
            $output = 'Display for when not nested'.$input;
        }
        return htmlspecialchars($output);
    }
}

这样我仍然能够使<testbar>标签在标签内部时表现不同<testfoo>。我不确定这是否应该这样做,但它确实有效。

PS:我也忽略了$frame->newChild()代码,因为这似乎不起作用,并且没有明确的文档或示例可用。

于 2014-01-06T15:48:29.860 回答