Source code, HTML, and an Exercise for the Reader
No clue why I want to be vague about this. Probably because I'm late for a party, but really wanted to share.
And you're smart. You'll figure it out.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="VS Copy-Paste to HTML" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<RichTextBox Name="m_richTextBox" TextChanged="m_richTextBox_TextChanged" />
<TextBox Grid.Column="1" Name="m_textBoxHTML" />
</Grid>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="VS Copy-Paste to HTML" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<RichTextBox Name="m_richTextBox" TextChanged="m_richTextBox_TextChanged" />
<TextBox Grid.Column="1" Name="m_textBoxHTML" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void m_richTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
m_textBoxHTML.Clear();
m_textBoxHTML.AppendText("<div style=\"font-family:monospace;\">" + Environment.NewLine);
foreach (Paragraph paragraph in m_richTextBox.Document.Blocks.Cast<Paragraph>())
{
foreach (Run run in Decompose(paragraph.Inlines))
{
m_textBoxHTML.AppendText(toCleanedColoredSpan(run));
}
m_textBoxHTML.AppendText("<br/>" + Environment.NewLine);
}
m_textBoxHTML.AppendText("</div>");
}
private string toCleanedColoredSpan(Run run)
{
SolidColorBrush solidColorBrush = run.Foreground as SolidColorBrush;
if (solidColorBrush == null)
{
return cleanString(run.Text);
}
else
{
if (solidColorBrush.Color == Colors.Black)
{
return cleanString(run.Text);
}
else
{
string colorString = solidColorBrush.Color.ToString();
Debug.Assert(colorString.Length == 9);
colorString = colorString.Substring(3);
return string.Format("<span style=\"color:#{0};\">{1}</span>",
colorString, cleanString(run.Text));
}
}
}
private string cleanString(string source)
{
source = HttpUtility.HtmlEncode(source);
source = source.Replace(" ", " ");
return source;
}
private static IEnumerable<Run> Decompose(IEnumerable<Inline> inlines)
{
foreach (Inline inline in inlines)
{
if (inline is Span)
{
foreach (Run run in Decompose(((Span)inline).Inlines))
{
yield return run;
}
}
else if (inline is Run)
{
yield return (Run)inline;
}
else
{
throw new NotSupportedException();
}
}
}
}
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void m_richTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
m_textBoxHTML.Clear();
m_textBoxHTML.AppendText("<div style=\"font-family:monospace;\">" + Environment.NewLine);
foreach (Paragraph paragraph in m_richTextBox.Document.Blocks.Cast<Paragraph>())
{
foreach (Run run in Decompose(paragraph.Inlines))
{
m_textBoxHTML.AppendText(toCleanedColoredSpan(run));
}
m_textBoxHTML.AppendText("<br/>" + Environment.NewLine);
}
m_textBoxHTML.AppendText("</div>");
}
private string toCleanedColoredSpan(Run run)
{
SolidColorBrush solidColorBrush = run.Foreground as SolidColorBrush;
if (solidColorBrush == null)
{
return cleanString(run.Text);
}
else
{
if (solidColorBrush.Color == Colors.Black)
{
return cleanString(run.Text);
}
else
{
string colorString = solidColorBrush.Color.ToString();
Debug.Assert(colorString.Length == 9);
colorString = colorString.Substring(3);
return string.Format("<span style=\"color:#{0};\">{1}</span>",
colorString, cleanString(run.Text));
}
}
}
private string cleanString(string source)
{
source = HttpUtility.HtmlEncode(source);
source = source.Replace(" ", " ");
return source;
}
private static IEnumerable<Run> Decompose(IEnumerable<Inline> inlines)
{
foreach (Inline inline in inlines)
{
if (inline is Span)
{
foreach (Run run in Decompose(((Span)inline).Inlines))
{
yield return run;
}
}
else if (inline is Run)
{
yield return (Run)inline;
}
else
{
throw new NotSupportedException();
}
}
}
}