Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 100 additions & 20 deletions FB2Library/Elements/InternalLinkItem.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace FB2Library.Elements
{
public class InternalLinkItem : StyleType
{
private readonly XNamespace lNamespace = @"http://www.w3.org/1999/xlink";

public string Type { get; set; }

public string HRef { get; set; }

public SimpleText LinkText { get; set; }
private readonly List<StyleType> _linkData = new List<StyleType>();
public List<StyleType> LinkData { get { return _linkData; } }

internal const string Fb2InternalLinkElementName = "a";


public override string ToString()
{
return LinkText.ToString();
StringBuilder builder = new StringBuilder();
builder.Append("<a");
if (string.IsNullOrEmpty(Type))
{
builder.Append($" type='{Type}'");
}
if (string.IsNullOrEmpty(HRef))
{
builder.Append($" href='{HRef}'");
}
builder.Append(">");
foreach (var item in _linkData)
{
builder.Append(item.ToString());
builder.Append(" ");
}
builder.Append("</a>");
return builder.ToString();
}

internal void Load(XElement xLink)
Expand All @@ -36,22 +54,63 @@ internal void Load(XElement xLink)
throw new ArgumentException("Element of wrong type passed", "xLink");
}

LinkText = null;
//if (xLink.Value != null)
if (xLink.HasElements)
{
LinkText = new SimpleText();
try
{
LinkText.Load(xLink);
}
catch (Exception)
IEnumerable<XNode> childElements = xLink.Nodes();
foreach (var element in childElements)
{
LinkText = null;
if ((element.NodeType == XmlNodeType.Element) && !IsSimpleText(element))
{
XElement xElement = (XElement)element;
if (xElement.Name.LocalName == InlineImageItem.Fb2InlineImageElementName)
{
InlineImageItem image = new InlineImageItem();
try
{
image.Load(xElement);
_linkData.Add(image);
}
catch (Exception)
{
}
}
else if (xElement.Name.LocalName == StyleItem.StyleItemName)
{
StyleItem styleItem = new StyleItem();
try
{
styleItem.Load(xElement);
_linkData.Add(styleItem);
}
catch (Exception)
{
}
}
}
else
{
SimpleText text = new SimpleText();
try
{
text.Load(element);
_linkData.Add(text);
}
catch (Exception)
{
continue;
}
}
}
}
else if (!string.IsNullOrEmpty(xLink.Value))
{
SimpleText text = new SimpleText();
text.Load(xLink);
_linkData.Add(text);
}

XAttribute xTypeAttr = xLink.Attribute("type");
if ((xTypeAttr != null)&& (xTypeAttr.Value != null))
if ((xTypeAttr != null) && (xTypeAttr.Value != null))
{
Type = xTypeAttr.Value;
}
Expand All @@ -64,20 +123,41 @@ internal void Load(XElement xLink)

}

private bool IsSimpleText(XNode element)
{
// if not element than we assume simple text
if (element.NodeType != XmlNodeType.Element)
{
return true;
}
XElement xElement = (XElement)element;
if (xElement.Name.LocalName == InternalLinkItem.Fb2InternalLinkElementName)
{
throw new ArgumentException("Schema doesn't support nested links");
}
switch (xElement.Name.LocalName)
{
case InlineImageItem.Fb2InlineImageElementName:
case StyleItem.StyleItemName:
return false;
}
return true;
}

public XNode ToXML()
{
XElement xLink = new XElement(Fb2Const.fb2DefaultNamespace + Fb2InternalLinkElementName);
if (!string.IsNullOrEmpty(Type))
{
xLink.Add(new XAttribute("type",Type));
{
xLink.Add(new XAttribute("type", Type));
}
if(!string.IsNullOrEmpty(HRef))
if (!string.IsNullOrEmpty(HRef))
{
xLink.Add(new XAttribute(lNamespace + "href",HRef));
xLink.Add(new XAttribute(lNamespace + "href", HRef));
}
if (LinkText != null)
foreach (StyleType childElements in _linkData)
{
xLink.Add(LinkText.ToXML());
xLink.Add(childElements.ToXML());
}
return xLink;
}
Expand Down
39 changes: 26 additions & 13 deletions FB2Library/Elements/ParagraphItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ protected virtual string GetElementName()
{
return Fb2ParagraphElementName;
}

internal const string Fb2ParagraphElementName = "p";


public override string ToString()
{
Expand Down Expand Up @@ -78,24 +78,36 @@ protected void LoadData(XElement xParagraph)
}
catch (Exception)
{
}
}
}
}
else //if ( element.NodeType != XmlNodeType.Whitespace)
{
SimpleText text = new SimpleText();
else if (xElement.Name.LocalName == StyleItem.StyleItemName)
{
StyleItem styleItem = new StyleItem();
try
{
text.Load(element);
paragraphData.Add(text);
styleItem.Load(xElement);
paragraphData.Add(styleItem);
}
catch (Exception)
{
continue;
}
}
}
else //if ( element.NodeType != XmlNodeType.Whitespace)
{
SimpleText text = new SimpleText();
try
{
text.Load(element);
paragraphData.Add(text);
}
catch (Exception)
{
continue;
}
}
}

}
else if (!string.IsNullOrEmpty(xParagraph.Value))
{
Expand All @@ -122,7 +134,7 @@ protected void LoadData(XElement xParagraph)
{
Lang = xLang.Value;
}

}

internal virtual void Load(XElement xParagraph)
Expand Down Expand Up @@ -153,8 +165,9 @@ private bool IsSimpleText(XNode element)
{
case InternalLinkItem.Fb2InternalLinkElementName:
case InlineImageItem.Fb2InlineImageElementName:
case StyleItem.StyleItemName:
return false;

}
return true;
}
Expand Down
8 changes: 0 additions & 8 deletions FB2Library/Elements/SectionItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,6 @@ public XNode ToXML()
{
xSection.Add(epItem.ToXML());
}
if (SectionImages.Count != 0)
{
//xSection.Add(SectionImages.ToXML());
foreach (var image in SectionImages)
{
xSection.Add(image.ToXML());
}
}
if (Annotation != null)
{
xSection.Add(Annotation.ToXML());
Expand Down
Loading