【问题标题】:How to use the MediaWiki parser to get HTML from wikitext如何使用 MediaWiki 解析器从 wikitext 获取 HTML
【发布时间】:2023-03-30 05:16:01
【问题描述】:

我正在尝试使用 Wikipedia 的 MediaWiki 解析器将 Wikipedia 标记文本解析为 HTML。 我在这里浏览了手册 - https://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.

【问题讨论】:

  • 我用这个项目来解析code.google.com/p/gwtwiki这个项目给了我HTML输出,但保留了超链接和一些我希望删除的标签,所以我必须为它写一个洗涤器但是文档mediawiki 解析器声明它定义了函数来获得我需要的东西,所以我希望使用它..而且它是维基百科的官方解析器

标签: php parsing mediawiki wikipedia


【解决方案1】:

这是解析 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;

再见!

【讨论】:

  • 我一直在寻找代码特定的 sn-p 来将 wikiText 转换为 html。我找到的所有答案都使用 mediawiki API。感谢sn-p的人。它就像魅力一样。和平
  • 嗨@AzharSyed,我正在尝试将wikitext 转换为html。您能否详细说明在哪里运行 Sophivorus 提供的这些命令?我是Windows 10,所以之前没用过这个语法。
  • @LEAnhDung 这是一个 php sn-p。如果您安装了 mediawiki,您可以编写一个小扩展并在其中添加此代码。对于您的扩展,您可以在请求正文中发送“wikitext”并将 html 发送回客户端。
  • @AzharSyed 非常感谢您的提示。我会看看。我在解析(一个简单的)wikitext here 时遇到问题。我不确定这是否是 MediaWiki 在其他机器上的常见问题。请问您是否也遇到这个问题?
【解决方案2】:

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

【讨论】:

  • 会检查的,谢谢!
【解决方案3】:

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

【讨论】:

  • 欢迎来到 SO,仅链接答案不是 SO 中的最佳实践,因为链接被删除/关闭或删除时答案已过时。
【解决方案4】:
   //<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();
    }

【讨论】:

  • 嗨@Roco,我正在尝试将wikitext 转换为html。您能否详细说明在哪里运行这些命令?我是Windows 10,所以之前没用过这个语法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 2012-03-09
  • 2011-08-22
相关资源
最近更新 更多