将 Wiki 标记转换为 HTML 的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the easiest way to convert Wiki markup to HTML?
提问by Jim
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.
我正在构建一个需要非常基本的标记功能的网站。我不能使用任何 3rd 方插件,所以我只需要一种将标记转换为 HTML 的简单方法。我可能总共允许使用 3 个标签。
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?
什么是转换的最佳方式==Heading==
来<h2>Heading</h2>
,还是--bold--
要<b>bold</b>
?这可以用 Regex 简单地完成,还是有人有一个简单的功能?
I'm writing this in C#, but examples from other languages would probably work.
我是用 C# 写的,但其他语言的例子可能会起作用。
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:
跟进:这是我网站的一小部分,我喜欢使用简单的 Regex 替换的简单性。我使用以下代码在 C# 中完成了这项工作:
string html = Regex.Replace("==This will be inside h2==", "==([^=]*)==", "< h2>< /h2>")
.NET uses $1notation instead of the \1notation that is used in other languages.
.NET 使用$1表示法而不是其他语言中使用的\1表示法。
采纳答案by Matt Sheppard
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 scriptingvulnerabilities.
这不是一个简单的问题,因为如果您要将内容显示给用户,您还需要清理输入以确保不会创建任何跨站点脚本漏洞。
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></h2>
回答by Paul Wicks
There is also a perl moduleand a php projectto do this. The source code to either could be useful in developing your own solution.
回答by Joseph Daigle
I use Markdown (the same lightweight markup language used on this site). For C# there is a very good bit of source code available here. It fully supports Markdown, although it doesn't appear to be maintained. But for the time being it works really well and it's free open source.
我使用 Markdown(与本网站使用的轻量级标记语言相同)。对于C#有可用的源代码,一个很好的位在这里。它完全支持 Markdown,尽管它似乎没有得到维护。但就目前而言,它运行良好,而且是免费开源的。
The best part is all the work is done for you if you include this source with your project. It's very small; basically a single method call to transform a chunk of text into HTML.
如果您将这个源代码包含在您的项目中,那么最好的部分就是为您完成所有工作。它非常小;基本上是将一段文本转换为 HTML 的单个方法调用。
回答by pix0r
This really depends on the Wiki syntax you're using as there are several different ones. Obviously the wiki software has this functionality somewhere; if you can't find a software package that does this for you, you could start looking for the relevant code in your wiki software.
这实际上取决于您使用的 Wiki 语法,因为有几种不同的语法。显然维基软件在某处有这个功能;如果您找不到可以为您执行此操作的软件包,您可以开始在您的 wiki 软件中寻找相关代码。
回答by wener
Maybe thisis what you need.
也许这就是你所需要的。
This page is a compilation of links, descriptions, and status reports of the various alternative MediaWiki parsers — that is, programs and projects, other than MediaWiki itself, which are able or intended to translate MediaWiki's text markup syntax into something else.
此页面是各种替代 MediaWiki 解析器的链接、描述和状态报告的汇编 - 即除 MediaWiki 本身之外的程序和项目,它们能够或打算将 MediaWiki 的文本标记语法转换为其他内容。
回答by DalSoft
As Joseph said Markdown is the best solution to solve the text to html problem.
正如约瑟夫所说,Markdown 是解决 text to html 问题的最佳解决方案。
MarkdownSharp is lightweight, easy to use and well tested as it is the stackoverflow implementation!
MarkdownSharp 是轻量级的,易于使用且经过良好测试,因为它是 stackoverflow 实现!
new Markdown().Transform("**markdown text**");
http://blog.stackoverflow.com/2009/12/introducing-markdownsharp/
http://blog.stackoverflow.com/2009/12/introducing-markdownsharp/
More about Markdown syntax - http://en.wikipedia.org/wiki/Markdown
更多关于 Markdown 语法 - http://en.wikipedia.org/wiki/Markdown