- goalbook.zip
- GoalBook
- Dependencies
- CAG WPF 2
- Microsoft.Practices.Composite.dll
- Microsoft.Practices.Composite.Presentation.dll
- Microsoft.Practices.Composite.UnityExtensions.dll
- Microsoft.Practices.ObjectBuilder2.dll
- Microsoft.Practices.ServiceLocation.dll
- Microsoft.Practices.Unity.dll
- vssver2.scc
- CSLA
- Csla.dll
- Csla.XmlSerializers.dll
- vssver2.scc
- Infragistics
- Infragistics3.Wpf.DataPresenter.v9.1.Express.dll
- Infragistics3.Wpf.Editors.v9.1.Express.dll
- Infragistics3.Wpf.v9.1.Express.dll
- vssver2.scc
- WPFToolkit
- vssver2.scc
- WPFToolkit.dll
- GoalBook.Controls
- GoalBook.Goals
- GoalBook.Infrastructure
- GoalBook.Notes
- GoalBook.Public
- GoalBook.Shell
- App.ico
- App.xaml
- App.xaml.cs
- Bootstrapper.cs
- Commands
- GoalBook.Shell.csproj
- GoalBook.Shell.csproj.user
- Journal.ico
- Journal48.ico
- Misc
- Modules
- Properties
- Resources
- arrow_refresh.png
- arrow_undo.png
- checked.gif
- cross.png
- disk.png
- email.png
- erase.png
- error.png
- exclamation.png
- flag_red.png
- group.png
- help.png
- information.png
- magnifier.png
- page_red.png
- pencil.png
- printer.png
- user.png
- vssver2.scc
- Wave.jpg
- world.png
- world_link.png
- Services
- Splash.png
- Views
- vssver2.scc
- Windows
- GoalBook.sln
- GoalBook.snk
- GoalBook.Synchronisation
- GoalBook.Tasks
- License.txt
- ReadMe.txt
- SolutionInfo.cs
- vssver2.scc
|
// <copyright file="ToodledoToXamlConverter.cs" company="GoalBook">
// Copyright © 2009 Mark Brownsword. All rights reserved.
// This source code and supporting files are licensed under The Code Project
// Open License (CPOL) as detailed at http://www.codeproject.com/info/cpol10.aspx.
// </copyright>
namespace GoalBook.Infrastructure.Converters
{
#region Using Statements
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Xml;
using GoalBook.Public.HtmlConverter;
using Parser = GoalBook.Public.HtmlParser;
#endregion
/// <summary>
/// Value Converter for object model item foreign keys.
/// </summary>
public class ToodledoToXamlConverter : IValueConverter
{
#region Constants and Enums
#endregion
#region IValueConverter Members
/// <summary>
/// Convert Toodledo text to Xaml.
/// </summary>
/// <param name="value">value parameter</param>
/// <param name="targetType">targetType parameter</param>
/// <param name="parameter">object parameter</param>
/// <param name="culture">culture parameter</param>
/// <returns>Xaml string from Toodledo text</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
XamlToFlowDocumentConverter converter = new XamlToFlowDocumentConverter();
// Convert Html to Xaml string.
string xaml = HtmlToXamlConverter.ConvertHtmlToXaml(value.ToString(), true);
// Initialise FlowDocument from xaml.
FlowDocument flowDocument = converter.Convert(xaml, null, null, null) as FlowDocument;
// Add Margin Styling to FlowDocument. This ensures that when the document is
// edited in RichTextBox, new paragraphs have no margin.
Style style = new Style(typeof(Paragraph));
style.Setters.Add(new Setter(Block.MarginProperty, new Thickness(0)));
flowDocument.Resources.Add(typeof(Paragraph), style);
// Convert back to xaml string for result.
return converter.ConvertBack(flowDocument, null, null, null).ToString();
}
/// <summary>
/// Convert Xaml to Toodledo text.
/// </summary>
/// <param name="value">value parameter</param>
/// <param name="targetType">targetType parameter</param>
/// <param name="parameter">object parameter</param>
/// <param name="culture">culture parameter</param>
/// <returns>Toodledo text from Html string</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
Parser.ParseHTML parser = new Parser.ParseHTML();
Parser.AttributeList htmlTag = null;
Parser.Attribute attribute = null;
StringBuilder result = new StringBuilder();
XmlTextWriter writer = new XmlTextWriter(new StringWriter(result));
ushort listItemCounter = 0;
ushort formatTagCounter = 0;
parser.Source = HtmlFromXamlConverter.ConvertXamlToHtml(value.ToString());
while (!parser.Eof())
{
char ch = parser.Parse();
if (ch == 0)
{
// Html Tag
htmlTag = parser.GetTag();
if (htmlTag.Name == "SPAN")
{
attribute = htmlTag[0];
if (attribute != null)
{
if (attribute.Name == "STYLE")
{
if (attribute.Value.Contains("font-weight:bold;"))
{
writer.WriteStartElement("B");
formatTagCounter++;
}
if (attribute.Value.Contains("font-style:italic;"))
{
writer.WriteStartElement("I");
formatTagCounter++;
}
}
}
}
else if (htmlTag.Name == "UL" || htmlTag.Name == "OL" || htmlTag.Name == "LI")
{
writer.WriteStartElement(htmlTag.Name);
listItemCounter++;
}
else if (htmlTag.Name == "A")
{
writer.WriteStartElement(htmlTag.Name);
for (int i = 0; i < htmlTag.Count; i++)
{
attribute = htmlTag[i];
writer.WriteAttributeString(attribute.Name, attribute.Value);
}
}
else if (htmlTag.Name == "/UL" || htmlTag.Name == "/OL" || htmlTag.Name == "/LI")
{
writer.WriteEndElement();
listItemCounter--;
}
else if (htmlTag.Name == "/A")
{
writer.WriteEndElement();
}
else if (htmlTag.Name == "/SPAN")
{
while (formatTagCounter > 0)
{
writer.WriteEndElement();
formatTagCounter--;
}
}
else if (IsEmptyParagraph(htmlTag) || htmlTag.Name == "/P" || htmlTag.Name == "BR")
{
if (listItemCounter == 0 && this.NewLineRequired(parser))
{
writer.WriteString(Environment.NewLine);
}
}
}
else
{
// Text
writer.WriteString(ch.ToString());
}
}
return result.ToString();
}
/// <summary>
/// Determine if tag Is Empty Paragraph.
/// </summary>
/// <returns></returns>
private bool IsEmptyParagraph(Parser.AttributeList htmlTag)
{
if (htmlTag.Name == "P")
{
for (int i = 0; i < htmlTag.List.Count; i++)
{
if ((htmlTag.List[i] as Parser.Attribute).Name == "/")
{
return true;
}
}
}
return false;
}
/// <summary>
/// Determine if NewLine Required.
/// </summary>
/// <param name="parser">ParseHTML parameter</param>
/// <returns>True if NewLine Required</returns>
private bool NewLineRequired(Parser.ParseHTML parser)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 4; i++)
{
if (!parser.Eof())
{
builder.Append(parser.GetCurrentChar(i));
}
}
if (builder.ToString() == "<UL>" || builder.ToString() == "<OL>" || builder.ToString() == "<LI>" || builder.ToString() == "</BO")
{
return false;
}
return true;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been working as a software developer since 2000 and hold a Bachelor of Business degree from The Open Polytechnic of New Zealand. Computers are for people and I aim to build applications for people that they would want to use.