将Wiki格式转换为XHTML

3
我是一名有用的助手,可以为您进行文本翻译。
我有一个存储“维基格式”文本的数据库,我想使用PHP将其显示为XHTML。
以下是带有所有维基标记的示例输出:
Default text
== Heading 1 ==
=== Heading 2 ===
==== Heading 3 ====
===== Heading 4 =====
====== Heading 5 ======
'''Bold'''
''Italic''
<s>Strikethrough</s>

* List item 1
* List item 2

# Numbered item 1
# Numbered item 2

[[Image:http://domain.com/image.png|Image name]]

[http://google.com Link text goes here]

> Blockquote

<source lang="language">Source code</source>

这是相当标准的维基语法吗?是否有一种相对标准的用PHP解释它的方式?

提前致谢!


不要忘记使用<nowiki>foobar</nowiki>标签。 - Chris Dale
5个回答

4

我认为当前最标准的类维基百科格式是markdown。几乎每个平台都有可用的库,包括PHP


Markdown看起来很棒!PHP类也是如此。不幸的是,格式与我的字符串不匹配 - 如果我能控制数据,我会选择Markdown :) - Al.
Markdown不能完成基本操作,你需要下载另一个文件来完成。仍在寻找一个基本的、全包含的Wiki2html转换器。 - andrebruton

3

MediaWiki是使用PHP编写的,并且根据GPL许可证进行许可。因此,您可以使用WikiText转换器。


2
我想到了一个方法,但它在很多方面都会出现问题。这是最好的解决方法吗? PHP:
function wiki2html($text)
{
        $text = preg_replace('/&lt;source lang=&quot;(.*?)&quot;&gt;(.*?)&lt;\/source&gt;/', '<pre lang="$1">$2</pre>', $text);
        $text = preg_replace('/======(.*?)======/', '<h5>$1</h5>', $text);
        $text = preg_replace('/=====(.*?)=====/', '<h4>$1</h4>', $text);
        $text = preg_replace('/====(.*?)====/', '<h3>$1</h3>', $text);
        $text = preg_replace('/===(.*?)===/', '<h2>$1</h2>', $text);
        $text = preg_replace('/==(.*?)==/', '<h1>$1</h1>', $text);
        $text = preg_replace("/'''(.*?)'''/", '<strong>$1</strong>', $text);
        $text = preg_replace("/''(.*?)''/", '<em>$1</em>', $text);
        $text = preg_replace('/&lt;s&gt;(.*?)&lt;\/s&gt;/', '<strike>$1</strike>', $text);
        $text = preg_replace('/\[\[Image:(.*?)\|(.*?)\]\]/', '<img src="$1" alt="$2" title="$2" />', $text);
        $text = preg_replace('/\[(.*?) (.*?)\]/', '<a href="$1" title="$2">$2</a>', $text);
        $text = preg_replace('/&gt;(.*?)\n/', '<blockquote>$1</blockquote>', $text);

        $text = preg_replace('/\* (.*?)\n/', '<ul><li>$1</li></ul>', $text);
        $text = preg_replace('/<\/ul><ul>/', '', $text);

        $text = preg_replace('/# (.*?)\n/', '<ol><li>$1</li></ol>', $text);
        $text = preg_replace('/<\/ol><ol>/', '', $text);

        $text = str_replace("\r\n\r\n", '</p><p>', $text);
        $text = str_replace("\r\n", '<br/>', $text);
        $text = '<p>'.$text.'</p>';
        return $text;
}

输入:

Default text
== Heading 1 ==
=== Heading 2 ===
==== Heading 3 ====
===== Heading 4 =====
====== Heading 5 ======
'''Bold'''
''Italic''
<s>Strikethrough</s>

* List item 1
* List item 2

# Numbered item 1
# Numbered item 2

[[Image:http://domain.com/image.png|Image name]]

[http://google.com Link text goes here]

> Blockquote

<source lang="language">Source code</source>

输出:

<p>
  Default text<br/>
  <h1> Heading 1 </h1><br/>
  <h2> Heading 2 </h2><br/>
  <h3> Heading 3 </h3><br/>
  <h4> Heading 4 </h4><br/>
  <h5> Heading 5 </h5><br/>
  <strong>Bold</strong><br/>
  <em>Italic</em><br/>
  <strike>Strikethrough</strike>
</p>

<p>
  <ul>
    <li>List item 1</li>
    <li>List item 2</li>
  </ul>
  <br/>
  <ol>
    <li>Numbered item 1</li>
    <li>Numbered item 2</li>
  </ol>
  <br/>
  <img src="http://domain.com/image.png" alt="Image name" title="Image name" />
</p>

<p>
  <a href="http://google.com" title="Link text goes here">Link text goes here</a>
</p>

<p>
  <blockquote> Blockquote</blockquote><br/>
  <pre lang="language">Source code</pre><br/>
</p>

看起来我的字符串格式不是标准的wikiformat,所以我会自大地接受自己的答案。如果有人能改进我的代码,请随意! - Al.
我使用了你的代码的简化版本来进行一些维基文本解析,但是最后一个段落在你的版本中没有关闭。我使用了稍微修改过的结尾:$text = str_replace("\r\n\r\n", '</p><p>', $text,$count); if($count>0) $text .= '</p>'; - Duncan
添加了水平线 $text = preg_replace('/----/', '<hr />', $text); - andrebruton
еҲ«еҝҳдәҶжё…зҗҶ!$text = preg_replace('%<p>(<h\\d>.*?</h\\d>)</p>%', '$1', $text); $text = preg_replace('%<p>(<ul>.*?</ul>)</p>%', '$1', $text); $text = preg_replace('%<p>(<ol>.*?</ol>)</p>%', '$1', $text); - redolent

2

是的,这似乎是一个相当标准的维基格式。我使用PEAR包Text_Wiki创建了几个PHP维基解决方案。它可以实现你想要的功能,而且你甚至可以扩展它以支持任何自定义语法和根据任何规则进行翻译。

http://pear.php.net/package/Text_Wiki


文本_wiki看起来也不错!但我的格式与其不匹配。我怀疑我的字符串是非标准的。 - Al.
你也可以向text_wiki添加自己的规则,这样就可以支持任何类型的Wiki。 - Dan Gøran Lunde

-1

这将非常取决于你使用的解析语言。

使用Javascript进行客户端解析吗?

还是使用ASP或PHP进行服务器端解析?


我正在使用PHP。我已将其添加到标签中,但忘记在问题中提及! - Al.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接