Skip to content

Add 'style' tag processing #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2022
Merged
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
20 changes: 19 additions & 1 deletion FB2Library/Elements/InternalLinkItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ internal void Load(XElement xLink)
{
}
}
else if (xElement.Name.LocalName == StyleItem.StyleItemName)
{
StyleItem styleItem = new StyleItem();
try
{
styleItem.Load(xElement);
_linkData.Add(styleItem);
}
catch (Exception)
{
}
}
}
else
{
Expand Down Expand Up @@ -123,7 +135,13 @@ private bool IsSimpleText(XNode element)
{
throw new ArgumentException("Schema doesn't support nested links");
}
return xElement.Name.LocalName != InlineImageItem.Fb2InlineImageElementName;
switch (xElement.Name.LocalName)
{
case InlineImageItem.Fb2InlineImageElementName:
case StyleItem.StyleItemName:
return false;
}
return true;
}

public XNode ToXML()
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
171 changes: 171 additions & 0 deletions FB2Library/Elements/StyleItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace FB2Library.Elements
{
/// <summary>
/// Represents FB2 style tag
/// </summary>
public class StyleItem : IFb2TextItem, StyleType
{
internal const string StyleItemName = "style";

/// <summary>
/// Get/Set name of the sequence
/// </summary>
public string Name { get; set; }
/// <summary>
/// Get/Set language
/// </summary>
public string Lang { get; set; }
public List<StyleType> StyleData { get; set; } = new List<StyleType>();
protected virtual string GetElementName()
{
return StyleItemName;
}
public override string ToString()
{
StringBuilder builder = new StringBuilder($"<style name='{Name}' xml:lang='{Lang}'>");
foreach (var textItem in StyleData)
{
builder.Append(textItem.ToString());
builder.Append(" ");
}
builder.Append("</style>");
return builder.ToString();
}
/// <summary>
/// Load element data from the node
/// </summary>
/// <param name="xStyle"></param>
public void Load(XElement xStyle)
{
if (xStyle == null)
{
throw new ArgumentNullException("style");
}
if (xStyle.Name.LocalName != StyleItemName)
{
throw new ArgumentException(string.Format("The element is of type {0} while StyleItem accepts only {1} types", xStyle.Name.LocalName, StyleItemName));
}

Lang = null;
XAttribute xLang = xStyle.Attribute(XNamespace.Xml + "lang");
if (xLang != null)
{
Lang = xLang.Value;
}

Name = string.Empty;
XAttribute xName = xStyle.Attribute("name");
if (xName != null && xName.Value != null)
{
Name = xName.Value;
}

if (xStyle.HasElements)
{
IEnumerable<XNode> childElements = xStyle.Nodes();
foreach (var element in childElements)
{
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);
StyleData.Add(image);
}
catch (Exception)
{
}
}
else if (xElement.Name.LocalName == InternalLinkItem.Fb2InternalLinkElementName)
{
InternalLinkItem linkItem = new InternalLinkItem();
try
{
linkItem.Load(xElement);
StyleData.Add(linkItem);
}
catch (Exception)
{
}
}
else if (xElement.Name.LocalName == StyleItemName)
{
StyleItem styleItem = new StyleItem();
try
{
styleItem.Load(xElement);
StyleData.Add(styleItem);
}
catch (Exception)
{
}
}
}
else
{
SimpleText text = new SimpleText();
try
{
text.Load(element);
StyleData.Add(text);
}
catch (Exception)
{
}
}
}

}
else if (!string.IsNullOrEmpty(xStyle.Value))
{
SimpleText text = new SimpleText();
text.Load(xStyle);
StyleData.Add(text);
}
}
private bool IsSimpleText(XNode element)
{
// if not element than we assume simple text
if (element.NodeType != XmlNodeType.Element)
{
return true;
}
XElement xElement = (XElement)element;
switch (xElement.Name.LocalName)
{
case InternalLinkItem.Fb2InternalLinkElementName:
case InlineImageItem.Fb2InlineImageElementName:
case StyleItem.StyleItemName:
return false;
}
return true;
}
public XNode ToXML()
{
if (Name == null || string.IsNullOrEmpty(Name))
{
throw new Exception("Name attribute is required by standard");
}
XElement xStyle = new XElement(Fb2Const.fb2DefaultNamespace + StyleItemName, new XAttribute("name", Name));
if (!string.IsNullOrEmpty(Lang))
{
xStyle.Add(new XAttribute(XNamespace.Xml + "lang", Lang));
}
foreach (StyleType childElements in StyleData)
{
xStyle.Add(childElements.ToXML());
}
return xStyle;
}
}
}