Begin to implement FormattedTextObjectModel

This commit is contained in:
Michael Becker 2014-10-01 09:52:05 -04:00
parent 40624f91d2
commit b6bd78e144
10 changed files with 293 additions and 89 deletions

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversalEditor.ObjectModels.Text.Formatted
{
public abstract class FormattedTextItem : ICloneable
{
public class FormattedTextItemCollection
: System.Collections.ObjectModel.Collection<FormattedTextItem>
{
}
private FormattedTextStyleGroup mvarBaseStyleGroup = null;
public FormattedTextStyleGroup BaseStyleGroup { get { return mvarBaseStyleGroup; } set { mvarBaseStyleGroup = value; } }
private FormattedTextStyle.TextStyleCollection mvarStyles = new FormattedTextStyle.TextStyleCollection();
public FormattedTextStyle.TextStyleCollection Styles { get { return mvarStyles; } }
private string mvarText = String.Empty;
public string Text { get { return mvarText; } set { mvarText = value; } }
public abstract object Clone();
}
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversalEditor.ObjectModels.Text.Formatted
{
public class FormattedTextObjectModel : ObjectModel
{
private ObjectModelReference _omr = null;
public override ObjectModelReference MakeReference()
{
if (_omr == null)
{
_omr = base.MakeReference();
_omr.Title = "Formatted Text Document";
_omr.Path = new string[] { "General", "Text", "Formatted" };
}
return _omr;
}
private FormattedTextItem.FormattedTextItemCollection mvarSegments = new FormattedTextItem.FormattedTextItemCollection();
public FormattedTextItem.FormattedTextItemCollection Items { get { return mvarSegments; } }
public override void Clear()
{
mvarSegments.Clear();
}
public override void CopyTo(ObjectModel where)
{
FormattedTextObjectModel clone = (where as FormattedTextObjectModel);
foreach (FormattedTextItem segment in mvarSegments)
{
clone.Items.Add(segment.Clone() as FormattedTextItem);
}
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversalEditor.ObjectModels.Text.Formatted
{
public class FormattedTextStyle
{
public class TextStyleCollection
: System.Collections.ObjectModel.Collection<FormattedTextStyle>
{
}
public object Clone()
{
FormattedTextStyle clone = new FormattedTextStyle();
return clone;
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversalEditor.ObjectModels.Text.Formatted
{
public class FormattedTextStyleGroup
{
private string mvarName = String.Empty;
public string Name { get { return mvarName; } set { mvarName = value; } }
private FormattedTextStyle.TextStyleCollection mvarStyles = new FormattedTextStyle.TextStyleCollection();
public FormattedTextStyle.TextStyleCollection Styles { get { return mvarStyles; } }
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversalEditor.ObjectModels.Text.Formatted.Items
{
public class Container : FormattedTextItem
{
private FormattedTextItem.FormattedTextItemCollection mvarItems = new FormattedTextItemCollection();
public FormattedTextItem.FormattedTextItemCollection Items { get { return mvarItems; } }
public override object Clone()
{
Container clone = new Container();
foreach (FormattedTextItem item in mvarItems)
{
clone.Items.Add(item.Clone() as FormattedTextItem);
}
return clone;
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UniversalEditor.ObjectModels.Text.Formatted.Items
{
public class Literal : FormattedTextItem
{
private string mvarText = String.Empty;
public string Text { get { return mvarText; } set { mvarText = value; } }
public override object Clone()
{
Literal clone = new Literal();
clone.Text = (mvarText.Clone() as string);
return clone;
}
}
}

View File

@ -96,6 +96,7 @@
<Compile Include="ObjectModels\Project\Reference.cs" />
<Compile Include="ObjectModels\Shortcut\ShortcutObjectModel.cs" />
<Compile Include="ObjectModels\Solution\SolutionObjectModel.cs" />
<Compile Include="ObjectModels\Text\Formatted\FormattedTextObjectModel.cs" />
<Compile Include="ObjectModels\Text\Plain\PlainTextObjectModel.cs" />
<Compile Include="ProjectType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -23,6 +23,8 @@ using System;
using UniversalEditor.DataFormats.FileSystem.ZIP;
using UniversalEditor.ObjectModels.FileSystem;
using UniversalEditor.ObjectModels.Text.Formatted;
namespace UniversalEditor
{
public class XPSDataFormat : ZIPDataFormat
@ -33,7 +35,7 @@ namespace UniversalEditor
if (_dfr == null)
{
_dfr = new DataFormatReference(GetType());
// _dfr.Capabilities.Add(typeof(FormattedTextObjectModel), DataFormatCapabilities.All);
_dfr.Capabilities.Add(typeof(FormattedTextObjectModel), DataFormatCapabilities.All);
_dfr.Filters.Add("Microsoft XPS document", new string[] { "*.xps", "*.oxps" });
}
return _dfr;
@ -51,7 +53,7 @@ namespace UniversalEditor
protected override void BeforeSaveInternal(System.Collections.Generic.Stack<ObjectModel> objectModels)
{
// FormattedTextObjectModel text = (objectModels.Pop() as FormattedTextObjectModel);
FormattedTextObjectModel text = (objectModels.Pop() as FormattedTextObjectModel);
FileSystemObjectModel fsom = new FileSystemObjectModel();
@ -95,4 +97,3 @@ namespace UniversalEditor
}
}
}

View File

@ -7,106 +7,158 @@ using UniversalEditor.ObjectModels.Markup;
using UniversalEditor.DataFormats.Markup.XML;
using UniversalEditor.ObjectModels.Text.Plain;
using UniversalEditor.ObjectModels.Text.Formatted;
using UniversalEditor.ObjectModels.Text.Formatted.Items;
namespace UniversalEditor.DataFormats.Text.HTML
{
public class HTMLDataFormat : XMLDataFormat
{
private static DataFormatReference _dfr = null;
public override DataFormatReference MakeReference()
{
if (_dfr == null)
{
_dfr = new DataFormatReference(GetType());
_dfr.Capabilities.Add(typeof(MarkupObjectModel), DataFormatCapabilities.Bootstrap);
_dfr.Capabilities.Add(typeof(PlainTextObjectModel), DataFormatCapabilities.All);
_dfr.Filters.Add("HyperText Markup Language", new string[] { "*.htm", "*.html" });
public class HTMLDataFormat : XMLDataFormat
{
private static DataFormatReference _dfr = null;
public override DataFormatReference MakeReference()
{
if (_dfr == null)
{
_dfr = new DataFormatReference(GetType());
_dfr.Capabilities.Add(typeof(MarkupObjectModel), DataFormatCapabilities.Bootstrap);
_dfr.Capabilities.Add(typeof(PlainTextObjectModel), DataFormatCapabilities.All);
_dfr.Capabilities.Add(typeof(FormattedTextObjectModel), DataFormatCapabilities.All);
_dfr.Filters.Add("HyperText Markup Language", new string[] { "*.htm", "*.html" });
_dfr.ExportOptions.Add(new CustomOptionText("Title", "&Title: "));
}
return _dfr;
}
protected override void BeforeLoadInternal(Stack<ObjectModel> objectModels)
{
base.BeforeLoadInternal(objectModels);
objectModels.Push(new MarkupObjectModel());
}
protected override void AfterLoadInternal(Stack<ObjectModel> objectModels)
{
base.AfterLoadInternal(objectModels);
MarkupObjectModel html = (objectModels.Pop() as MarkupObjectModel);
ObjectModel objectModel = objectModels.Pop();
_dfr.ExportOptions.Add(new CustomOptionText("Title", "&Title: "));
}
return _dfr;
}
protected override void BeforeLoadInternal(Stack<ObjectModel> objectModels)
{
base.BeforeLoadInternal(objectModels);
objectModels.Push(new MarkupObjectModel());
}
protected override void AfterLoadInternal(Stack<ObjectModel> objectModels)
{
base.AfterLoadInternal(objectModels);
MarkupObjectModel html = (objectModels.Pop() as MarkupObjectModel);
ObjectModel objectModel = objectModels.Pop();
if (objectModel is PlainTextObjectModel)
{
PlainTextObjectModel text = (objectModel as PlainTextObjectModel);
if (objectModel is PlainTextObjectModel)
{
PlainTextObjectModel text = (objectModel as PlainTextObjectModel);
MarkupTagElement tagHTML = (html.Elements["html"] as MarkupTagElement);
if (tagHTML == null) throw new InvalidDataFormatException("Cannot find HTML tag");
MarkupTagElement tagHTML = (html.Elements["html"] as MarkupTagElement);
if (tagHTML == null) throw new InvalidDataFormatException("Cannot find HTML tag");
MarkupTagElement tagHEAD = (tagHTML.Elements["head"] as MarkupTagElement);
if (tagHEAD != null)
{
MarkupTagElement tagTITLE = (tagHEAD.Elements["title"] as MarkupTagElement);
if (tagTITLE != null) mvarTitle = tagTITLE.Value;
}
MarkupTagElement tagHEAD = (tagHTML.Elements["head"] as MarkupTagElement);
if (tagHEAD != null)
{
MarkupTagElement tagTITLE = (tagHEAD.Elements["title"] as MarkupTagElement);
if (tagTITLE != null) mvarTitle = tagTITLE.Value;
}
MarkupTagElement tagBODY = (tagHTML.Elements["body"] as MarkupTagElement);
if (tagBODY == null) throw new InvalidDataFormatException("Cannot find BODY tag");
MarkupTagElement tagBODY = (tagHTML.Elements["body"] as MarkupTagElement);
if (tagBODY != null)
{
text.Text = tagBODY.Value;
}
}
else if (objectModel is FormattedTextObjectModel)
{
FormattedTextObjectModel text = (objectModel as FormattedTextObjectModel);
text.Text = tagBODY.Value;
}
}
MarkupTagElement tagHTML = (html.Elements["html"] as MarkupTagElement);
if (tagHTML == null) throw new InvalidDataFormatException("Cannot find HTML tag");
protected override void BeforeSaveInternal(Stack<ObjectModel> objectModels)
{
base.BeforeSaveInternal(objectModels);
ObjectModel objectModel = objectModels.Pop();
MarkupTagElement tagHEAD = (tagHTML.Elements["head"] as MarkupTagElement);
if (tagHEAD != null)
{
MarkupTagElement tagTITLE = (tagHEAD.Elements["title"] as MarkupTagElement);
if (tagTITLE != null) mvarTitle = tagTITLE.Value;
}
MarkupTagElement tagBODY = (tagHTML.Elements["body"] as MarkupTagElement);
if (tagBODY != null)
{
MarkupObjectModel html = new MarkupObjectModel();
#region Html
{
MarkupTagElement tagHTML = new MarkupTagElement();
tagHTML.FullName = "html";
#region Head
{
MarkupTagElement tagHEAD = new MarkupTagElement();
tagHEAD.FullName = "head";
}
}
}
if (!String.IsNullOrEmpty(mvarTitle))
{
MarkupTagElement tagTITLE = new MarkupTagElement();
tagTITLE.FullName = "title";
tagTITLE.Value = mvarTitle;
tagHEAD.Elements.Add(tagTITLE);
}
protected override void BeforeSaveInternal(Stack<ObjectModel> objectModels)
{
base.BeforeSaveInternal(objectModels);
ObjectModel objectModel = objectModels.Pop();
tagHTML.Elements.Add(tagHEAD);
}
#endregion
#region Body
{
MarkupTagElement tagBODY = new MarkupTagElement();
tagBODY.FullName = "body";
MarkupObjectModel html = new MarkupObjectModel();
#region Html
{
MarkupTagElement tagHTML = new MarkupTagElement();
tagHTML.FullName = "html";
#region Head
{
MarkupTagElement tagHEAD = new MarkupTagElement();
tagHEAD.FullName = "head";
if (objectModel is PlainTextObjectModel)
{
PlainTextObjectModel text = (objectModel as PlainTextObjectModel);
tagBODY.Value = text.Text;
}
if (!String.IsNullOrEmpty(mvarTitle))
{
MarkupTagElement tagTITLE = new MarkupTagElement();
tagTITLE.FullName = "title";
tagTITLE.Value = mvarTitle;
tagHEAD.Elements.Add(tagTITLE);
}
tagHTML.Elements.Add(tagBODY);
}
#endregion
html.Elements.Add(tagHTML);
}
#endregion
objectModels.Push(html);
}
tagHTML.Elements.Add(tagHEAD);
}
#endregion
#region Body
{
MarkupTagElement tagBODY = new MarkupTagElement();
tagBODY.FullName = "body";
private string mvarTitle = String.Empty;
public string Title { get { return mvarTitle; } set { mvarTitle = value; } }
}
if (objectModel is PlainTextObjectModel)
{
PlainTextObjectModel text = (objectModel as PlainTextObjectModel);
tagBODY.Value = text.Text;
}
else if (objectModel is FormattedTextObjectModel)
{
FormattedTextObjectModel text = (objectModel as FormattedTextObjectModel);
foreach (FormattedTextItem segment in text.Items)
{
MarkupElement el = RenderFormattedTextItemToHTML(segment);
if (el != null)
{
tagBODY.Elements.Add(el);
}
else
{
Console.WriteLine("HTML: no implementation for segment '" + segment.GetType().Name + "'");
}
}
}
tagHTML.Elements.Add(tagBODY);
}
#endregion
html.Elements.Add(tagHTML);
}
#endregion
objectModels.Push(html);
}
private MarkupElement RenderFormattedTextItemToHTML(FormattedTextItem segment)
{
if (segment is Literal)
{
Literal item = (segment as Literal);
MarkupTagElement tagSpan = new MarkupTagElement();
tagSpan.FullName = "span";
tagSpan.Value = item.Text;
return tagSpan;
}
return null;
}
private string mvarTitle = String.Empty;
public string Title { get { return mvarTitle; } set { mvarTitle = value; } }
}
}

View File

@ -4,7 +4,9 @@ using System.Linq;
using System.Text;
using UniversalEditor.Accessors;
using UniversalEditor.IO;
using UniversalEditor.ObjectModels.Text.Plain;
using UniversalEditor.ObjectModels.Text.Formatted;
namespace UniversalEditor.DataFormats.Text.MHTML
{
@ -18,6 +20,7 @@ namespace UniversalEditor.DataFormats.Text.MHTML
_dfr = base.MakeReference();
_dfr.Clear();
_dfr.Capabilities.Add(typeof(PlainTextObjectModel), DataFormatCapabilities.All);
_dfr.Capabilities.Add(typeof(FormattedTextObjectModel), DataFormatCapabilities.All);
_dfr.Filters.Add("MIME-encoded HyperText Markup Language", new string[] { "*.mht", "*.mhtml" });
}
return _dfr;