-
Notifications
You must be signed in to change notification settings - Fork 105
/
NavigationReader.cs
156 lines (148 loc) · 8.22 KB
/
NavigationReader.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Linq;
using VersOne.Epub.Schema;
namespace VersOne.Epub.Internal
{
internal static class NavigationReader
{
public static List<EpubNavigationItemRef>? GetNavigationItems(EpubSchema epubSchema, EpubContentRef epubContentRef)
{
if (epubSchema.Package.EpubVersion == EpubVersion.EPUB_2)
{
return epubSchema.Epub2Ncx != null ? GetNavigationItems(epubSchema, epubContentRef, epubSchema.Epub2Ncx) : null;
}
else
{
return epubSchema.Epub3NavDocument != null ? GetNavigationItems(epubSchema, epubContentRef, epubSchema.Epub3NavDocument) : null;
}
}
public static List<EpubNavigationItemRef> GetNavigationItems(EpubSchema epubSchema, EpubContentRef epubContentRef, Epub2Ncx epub2Ncx)
{
return GetNavigationItems(epubSchema, epubContentRef, epub2Ncx.NavMap.Items);
}
public static List<EpubNavigationItemRef> GetNavigationItems(EpubSchema epubSchema, EpubContentRef epubContentRef, Epub3NavDocument epub3NavDocument)
{
return GetNavigationItems(epubSchema, epubContentRef, epub3NavDocument.Navs.FirstOrDefault(nav => nav.Type == Epub3StructuralSemanticsProperty.TOC),
epub3NavDocument.FilePath);
}
private static List<EpubNavigationItemRef> GetNavigationItems(EpubSchema epubSchema, EpubContentRef epubContentRef, List<Epub2NcxNavigationPoint> navigationPoints)
{
List<EpubNavigationItemRef> result = new();
if (navigationPoints != null)
{
foreach (Epub2NcxNavigationPoint navigationPoint in navigationPoints)
{
EpubNavigationItemType type = EpubNavigationItemType.LINK;
Epub2NcxNavigationLabel? firstNavigationLabel = navigationPoint.NavigationLabels.FirstOrDefault() ??
throw new Epub2NcxException($"Incorrect EPUB 2 NCX: navigation point \"{navigationPoint.Id}\" should contain at least one navigation label.");
string title = firstNavigationLabel.Text;
string source = Uri.UnescapeDataString(navigationPoint.Content.Source);
if (!ContentPathUtils.IsLocalPath(source))
{
throw new Epub2NcxException($"Incorrect EPUB 2 NCX: content source \"{source}\" cannot be a remote resource.");
}
EpubNavigationItemLink link = new(source, epubSchema.ContentDirectoryPath);
EpubLocalTextContentFileRef? htmlContentFileRef = GetLocalHtmlContentFileRef(epubContentRef, link.ContentFilePath) ??
throw new Epub2NcxException($"Incorrect EPUB 2 NCX: content source \"{source}\" not found in EPUB manifest.");
List<EpubNavigationItemRef> nestedItems = GetNavigationItems(epubSchema, epubContentRef, navigationPoint.ChildNavigationPoints);
result.Add(new EpubNavigationItemRef(type, title, link, htmlContentFileRef, nestedItems));
}
}
return result;
}
private static List<EpubNavigationItemRef> GetNavigationItems(EpubSchema epubSchema, EpubContentRef epubContentRef, Epub3Nav epub3Nav, string epub3NavDocumentFilePath)
{
List<EpubNavigationItemRef> result;
if (epub3Nav != null)
{
string epub3NavigationBaseDirectoryPath = ContentPathUtils.GetDirectoryPath(epub3NavDocumentFilePath);
if (epub3Nav.Head != null)
{
result = new List<EpubNavigationItemRef>();
EpubNavigationItemType type = EpubNavigationItemType.HEADER;
string title = epub3Nav.Head;
List<EpubNavigationItemRef> nestedItems = GetNavigationItems(epubSchema, epubContentRef, epub3Nav.Ol, epub3NavigationBaseDirectoryPath);
result.Add(new EpubNavigationItemRef(type, title, null, null, nestedItems));
}
else
{
result = GetNavigationItems(epubSchema, epubContentRef, epub3Nav.Ol, epub3NavigationBaseDirectoryPath);
}
}
else
{
result = new List<EpubNavigationItemRef>();
}
return result;
}
private static List<EpubNavigationItemRef> GetNavigationItems(EpubSchema epubSchema, EpubContentRef epubContentRef,
Epub3NavOl? epub3NavOl, string epub3NavigationBaseDirectoryPath)
{
List<EpubNavigationItemRef> result = new();
if (epub3NavOl != null)
{
foreach (Epub3NavLi epub3NavLi in epub3NavOl.Lis)
{
if (epub3NavLi.Anchor != null)
{
Epub3NavAnchor navAnchor = epub3NavLi.Anchor;
EpubNavigationItemType type;
string title = GetFirstNonEmptyHeader(navAnchor.Text, navAnchor.Title, navAnchor.Alt);
EpubNavigationItemLink? link = null;
EpubLocalTextContentFileRef? htmlContentFileRef = null;
List<EpubNavigationItemRef> nestedItems = GetNavigationItems(epubSchema, epubContentRef, epub3NavLi.ChildOl, epub3NavigationBaseDirectoryPath);
if (navAnchor.Href != null)
{
string href = Uri.UnescapeDataString(navAnchor.Href);
if (!ContentPathUtils.IsLocalPath(href))
{
throw new Epub3NavException($"Incorrect EPUB 3 navigation document: anchor href \"{href}\" cannot be a remote resource.");
}
type = EpubNavigationItemType.LINK;
link = new(href, epub3NavigationBaseDirectoryPath);
htmlContentFileRef = GetLocalHtmlContentFileRef(epubContentRef, link.ContentFilePath);
if (htmlContentFileRef == null)
{
throw new Epub3NavException($"Incorrect EPUB 3 navigation document: target for anchor href \"{href}\" not found in EPUB manifest.");
}
}
else
{
type = EpubNavigationItemType.HEADER;
}
result.Add(new EpubNavigationItemRef(type, title, link, htmlContentFileRef, nestedItems));
}
else if (epub3NavLi.Span != null)
{
Epub3NavSpan navSpan = epub3NavLi.Span;
EpubNavigationItemType type = EpubNavigationItemType.HEADER;
string title = GetFirstNonEmptyHeader(navSpan.Text, navSpan.Title, navSpan.Alt);
List<EpubNavigationItemRef> nestedItems = GetNavigationItems(epubSchema, epubContentRef, epub3NavLi.ChildOl, epub3NavigationBaseDirectoryPath);
result.Add(new EpubNavigationItemRef(type, title, null, null, nestedItems));
}
}
}
return result;
}
private static EpubLocalTextContentFileRef? GetLocalHtmlContentFileRef(EpubContentRef epubContentRef, string localContentFilePath)
{
if (!epubContentRef.Html.TryGetLocalFileRefByFilePath(localContentFilePath, out EpubLocalTextContentFileRef htmlContentFileRef))
{
return null;
}
return htmlContentFileRef;
}
private static string GetFirstNonEmptyHeader(params string?[] options)
{
foreach (string? option in options)
{
if (option != null && option != String.Empty)
{
return option;
}
}
return String.Empty;
}
}
}