5

我正在尝试使用 Wikipedia 的 MediaWiki 解析器将 Wikipedia 标记文本解析为 HTML。我在这里浏览了手册 - h​​ttps://www.mediawiki.org/wiki/Manual:Parser.php 但是,由于我对 PHP 完全陌生,我无法编写测试脚本,

这是我想要解析并转换为 HTML 的示例输入:

Shakespeare's sonnets
==Characters==
When analysed as characters, the subjects of the sonnets are usually referred
to as the Fair Youth, the Rival Poet, and the Dark Lady. The speaker expresses
admiration for the Fair Youth's beauty, and later has an affair with the Dark
Lady. It is not known whether the poems and their characters are fiction or
autobiographical; scholars who find the sonnets to be autobiographical, notably
[[A. L. Rowse]], have attempted to identify the characters with historical
individuals.
4

4 回答 4

3

这是解析 wikitext 的最小代码(在 MediaWiki 1.32 上测试):

$text = "Your [[wikitext]]";
$title = $skin->getTitle(); // Get the title object from somewhere or use $wgTitle
$parser = new Parser;
$parserOptions = new ParserOptions;
$parserOutput = $parser->parse( $text, $title, $parserOptions );
$html = $parserOutput->getText();
echo $html;

再见!

于 2019-01-23T13:34:19.423 回答
1

您甚至不必使用 PHP。您可以使用 Wikipedia 的 API(或您自己的 MediaWiki 安装上的 API)。有关更多信息,请参阅解析 wikitext

于 2013-08-22T13:58:27.047 回答
0

您可以使用 JWPL http://code.google.com/p/jwpl/,它将与 wiki 的本地副本一起使用。加载转储,转换 wie Datamaschine,导入数据库,用它做你想做的事。

于 2014-02-26T09:09:45.577 回答
-1
   //<myname></myname>    

    public static function onParserFirstCallInit( Parser $parser ){
        $parser->setHook('myname', 'MyClass::getOutputHtml');
    }
    public static function getOutputHtml(){
        $localParser = new Parser();
        $input = OtherClass::myOutput();
        $context = new RequestContext();
        $title = $context->getTitle();
        $parserOptions = new ParserOptions;
        $output = $localParser->parse($input, $title, $parserOptions);
        return $output->getText();
    }
于 2019-11-12T03:13:38.937 回答