private static List<Inline> CreateInlineCollection(string newValue)
{
var inlines = new List<Inline>();
bool bold = false;
bool list_item = false;
bool unordered_list = false;
bool ordered_list = false;
string href = "";
int ordered_number = -1;
int linebreak_count = 0;
var splits = Regex.Split(newValue, htmlLineBreakRegex, RegexOptions.IgnoreCase | RegexOptions.ECMAScript);
foreach (var line in splits)
{
if (string.IsNullOrWhiteSpace(line)) continue;
var line_lowercase = line.ToLower();
if (IsHtmlTag(line_lowercase))
{
switch (line_lowercase)
{
case "</div>":
case "</p>":
case "<br/>":
case "<br />":
case "<br>":
if (!ordered_list && !unordered_list)
WriteLineBreak(inlines, ref linebreak_count);
break;
case "<bold>":
case "<b>":
bold = true;
break;
case "</bold>":
case "</b>":
bold = false;
break;
case "<ul>":
WriteLineBreak(inlines, ref linebreak_count);
unordered_list = true;
break;
case "</ul>":
unordered_list = false;
break;
case "<ol>":
WriteLineBreak(inlines, ref linebreak_count);
ordered_list = true;
ordered_number = 1;
break;
case "</ol>":
ordered_list = false;
ordered_number = -1;
break;
case "<li>":
list_item = true;
break;
case "</li>":
list_item = false;
WriteLineBreak(inlines, ref linebreak_count);
break;
case "</a>":
href = "";
break;
}
}
if (line_lowercase == "<p>" || (line_lowercase.StartsWith("<p ") && line_lowercase.EndsWith(">")))
{
if (!ordered_list && !unordered_list)
WriteLineBreak(inlines, ref linebreak_count);
}
if (line_lowercase.StartsWith("<a ") && line_lowercase.Contains("href="))
{
char quote = line_lowercase.Contains("href='") ? '\'' : '"';
int start_index = line_lowercase.IndexOf(string.Format("href={0}", quote)) + 6;
int end_index = line_lowercase.IndexOf(string.Format("{0}", quote), start_index);
href = line.Substring(start_index, end_index - start_index);
}
if (line_lowercase.StartsWith("<img") && line_lowercase.Contains("src='"))
{
int start_index = line_lowercase.IndexOf("src='") + 5;
int end_index = line.IndexOf("'", start_index);
string src = line.Substring(start_index, end_index - start_index);
var image = new Image() { Source = new BitmapImage(new Uri(src, UriKind.Absolute)) };
image.Stretch = Stretch.None;
var inline_ui_container = new InlineUIContainer();
inline_ui_container.Child = image;
inlines.Add(inline_ui_container);
WriteLineBreak(inlines, ref linebreak_count);
WriteLineBreak(inlines, ref linebreak_count);
}
string text = Regex.Replace(line, htmlStripperRegex, string.Empty);
Regex regex = new Regex(@"[ ]{2,}", RegexOptions.None);
if (!string.IsNullOrWhiteSpace(text))
{
text = regex.Replace(text, @" "); //Remove multiple spaces
text = text.Replace(""", "\""); //Unencode quotes
text = text.Replace(" ", " "); //Unencode spaces
var run = new Run() { Text = text };
if (bold)
run.FontWeight = FontWeights.SemiBold;
if (unordered_list && list_item)
{
run.Text = run.Text.Insert(0, "• ");
list_item = false;
}
if (ordered_list && list_item)
{
run.Text = run.Text.Insert(0, string.Format("{0}. ", ordered_number++));
list_item = false;
}
if (!string.IsNullOrEmpty(href))
{
int pos = 0;
foreach (var str in text.Split(new char[] { ' ', '/' }))
{
var word = str;
pos += word.Length;
if (pos < text.Length)
{
word += text[pos];
pos++;
}
var hyperlink = new HyperlinkButton();
hyperlink.NavigateUri = new Uri(href, UriKind.Absolute);
hyperlink.Content = word;
hyperlink.Template = (ControlTemplate)XamlReader.Load(
"<ControlTemplate TargetType='HyperlinkButton' xmlns='http://schemas.microsoft.com/client/2007' >" +
"<TextBlock Text='{TemplateBinding Content}' Padding='0' Margin='0' RenderTransformOrigin='0.5,0.5'>" +
"<TextBlock.RenderTransform>" +
"<CompositeTransform TranslateY='5' />" +
"</TextBlock.RenderTransform>" +
"</TextBlock>" +
"</ControlTemplate>");
hyperlink.FontFamily = new FontFamily("Segoe UI Light");
hyperlink.FontWeight = FontWeights.Normal;
hyperlink.Margin = new Thickness(0);
if (word.EndsWith(" "))
hyperlink.Margin = new Thickness(0, 0, 4, 0);
hyperlink.Padding = new Thickness(0);
var inline_ui_container = new InlineUIContainer();
inline_ui_container.Child = hyperlink;
inlines.Add(inline_ui_container);
}
}
else
inlines.Add(run);
linebreak_count = 0;
}
}
return inlines;
}