Ben bir hack ile geldi, ama bir çok şey üzerinde keser. Bu ileriye doğru en iyi yolu nedir?
PHP:
function wiki2html($text)
{
$text = preg_replace('/<source lang="(.*?)">(.*?)<\/source>/', '<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('/<s>(.*?)<\/s>/', '<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('/>(.*?)\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;
}
Input:
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>
Output:
<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>