I have some text like this:
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
How can I parse it in C# into HTML like below
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
I am currently splitting and concatenating but sub lists are not being handled properly.
UPDATE: - Sample Code
This is what I am currently doing.
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;
}
Note That Some list items span multiple lines and may also include line breaks
Consider following approach (note it is "quick and dirty" in determining tags).
Pretty straightforward - just reading your text line-by-line and converting it (with some look-aheads and counting depths level of sublists).
Output looks exactly like you want: