我有一些这样的文字:
This is a simple line
[olist]
[#]This is line 1
[#]This is line 2
[olist]
[#]This is line 2.1
[#]This is line 2.2
[#]This is line 2.3
and it continues here
[/olist]
[#]This is line 3
[/olist]
Another line
如何在 C# 中将其解析为 HTML,如下所示
This is a simple line
<ol>
<li>This is line 1</li>
<li>This is line 2
<ol>
<li>This is line 2.1</li>
<li>This is line 2.2</li>
<li>This is line 2.3
and it continues here</li>
</ol>
</li>
<li>This is line 3</li>
</ol>
Another line
我目前正在拆分和连接,但子列表没有得到正确处理。
更新: - 示例代码
这就是我目前正在做的事情。
var html = ReplaceList(customHtml,"olist","ol");
private static string ReplaceList(string text, string key, string tag)
{
var itemTmpl = GetListEntry(text, key);
while (itemTmpl != null)
{
var buf = new StringBuilder();
var arr = itemTmpl.Split(new[] { "[#]" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var str in arr)
{
if (!string.IsNullOrWhiteSpace(str))
buf.AppendFormat("<li>{0}</li>", str.Trim());
}
var content = string.Format("<{0}>{1}</{0}>", tag, buf);
text = text.SubstringBefore("[" + key + "]") + content +
text.SubstringAfter("[/" + key + "]");
itemTmpl = GetListEntry(text, key);
}
return text;
}
private static string GetListEntry(string text, string key)
{
var tag1 = string.Format("[{0}]", key);
var tag2 = string.Format("[/{0}]", key);
var start = text.IndexOf(tag1, StringComparison.Ordinal);
var end = (start > -1) ? text.IndexOf(tag2, start, StringComparison.Ordinal) : -1;
if (start < 0 || end <= start)
return null;
var result = text.Substring(start + tag1.Length, end - start - tag1.Length);
return result;
}
请注意, 某些列表项跨越多行,还可能包括换行符