forked from vieapps/Components.Utility.Epub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NavPoint.cs
60 lines (56 loc) · 2.14 KB
/
NavPoint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#region Related components
using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Collections.Generic;
#endregion
namespace net.vieapps.Components.Utility.Epub
{
/// <summary>
/// Class for TOC entry. Top-level navPoints should be created by Epub.Document.AddNavPoint method
/// </summary>
public class NavPoint
{
private readonly string _label;
private readonly string _id;
private readonly string _content;
private readonly string _class;
private readonly int _playOrder;
List<NavPoint> _navPoints;
internal NavPoint(string label, string id, string content, int playOrder, string @class = "")
{
this._label = label;
this._id = id;
this._content = content;
this._playOrder = playOrder;
this._class = @class;
this._navPoints = new List<NavPoint>();
}
/// <summary>
/// Add TOC entry as a direct child of this NavPoint
/// </summary>
/// <param name="label">Text of TOC entry</param>
/// <param name="content">Link to TOC entry</param>
/// <param name="playOrder">play order counter</param>
/// <returns>newly created NavPoint </returns>
public NavPoint AddNavPoint(string label, string content, int playOrder)
{
var id = $"{this._id}x{(this._navPoints.Count + 1)}";
var nav = new NavPoint(label, id, content, playOrder);
this._navPoints.Add(nav);
return nav;
}
internal XElement ToElement()
{
var element = new XElement(NCX.NcxNS + "navPoint", new XAttribute("id", this._id), new XAttribute("playOrder", this._playOrder));
if (!string.IsNullOrEmpty(this._class))
element.Add(new XAttribute("class", this._class));
element.Add(new XElement(NCX.NcxNS + "navLabel", new XElement(NCX.NcxNS + "text", this._label)));
element.Add(new XElement(NCX.NcxNS + "content", new XAttribute("src", this._content)));
foreach (var navPoint in this._navPoints)
element.Add(navPoint.ToElement());
return element;
}
}
}