将Wiki标记转换为HTML的最简单方法是什么? [英] What's the easiest way to convert Wiki markup to HTML?

查看:502
本文介绍了将Wiki标记转换为HTML的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个需要非常基本的标记功能的网站。我无法使用任何第三方插件,因此我只需要一种简单的方法将标记转换为HTML。我可能总共有3个标签,我将允许。



转换 ==标题== < h2>标题< / h2> - 粗体 - < b取代;粗体< / b个?这可以简单地用Regex来完成,还是有人有一个简单的函数?

我正在用C#写这个,但其他语言的例子可能会起作用。 p>

后续操作:
这是我网站的一小部分,我喜欢简单的使用简单的Regex替换。我使用以下代码在C#中完成了这项工作:

  string html = Regex.Replace(==这将在h2 ==,==([^ =] *)==,< h2> $ 1< / h2>)

.NET使用 $ 1 表示法,而不是其他语言使用的 \ 1 表示法。

解决方案

这不是一个简单的问题,因为如果你要显示给用户的东西,你还需要清理输入以确保您不会创建任何跨站点脚本漏洞。



也就是说,您可以做一些非常简单的事情,因为您可以通过正则表达式替换来轻松描述。 例如

 将模式==([^ =] *)==替换为< h2> \1< / h2> 


I'm building a website that requires very basic markup capabilities. I can't use any 3rd party plugins, so I just need a simple way to convert markup to HTML. I might have a total of 3 tags that I'll allow.

What is the best way to convert ==Heading== to <h2>Heading</h2>, or --bold-- to <b>bold</b>? Can this be done simply with Regex, or does somebody have a simple function?

I'm writing this in C#, but examples from other languages would probably work.

Follow up: This is such a small part of my website that I liked the simplicity of using a simple Regex replace. I made this work in C# with the following code:

string html = Regex.Replace("==This will be inside h2==", "==([^=]*)==", "< h2>$1< /h2>")

.NET uses $1 notation instead of the \1 notation that is used in other languages.

解决方案

It's not really a simple problem, because if you're going to display things back to the user, you'll need to also sanitise the input to ensure you don't create any cross site scripting vulnerabilities.

That said, you could probably do something pretty simple as you describe most easily with a regular expression replacement.

For example

replace the pattern ==([^=]*)== with <h2>\1</h2>

这篇关于将Wiki标记转换为HTML的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆