public EPubFile(Stream s) { try { zip = new ZipFile(s); string mpath = getRootFromContainer (GetContent("META-INF/container.xml")); OPFParser.ParseStream(GetContent(mpath), mpath, out Manifest, out Spine); if (Spine.TocId == null) { Toc = new TableOfContents(); } else { string tocPath = Manifest.GetById(Spine.TocId).Linkref; Toc = NCXParser.ParseStream(GetContent(tocPath), tocPath); } stream = s; } catch (Exception ex) { s.Dispose(); throw ex; } }
public static TableOfContents ParseStream(Stream s, string rel) { TableOfContents toc = new TableOfContents(); var parser = new NCXParser(toc, rel); parser.Parse(s); return toc; }
public static TableOfContents Scan(Stream s) { var result = new TableOfContents(); var levelPath = new Stack<int>(); int ent = -1; using (XmlTextReader r = new XmlTextReader(s)) { r.XmlResolver = null; while (r.Read()) if (r.NodeType == XmlNodeType.Element) { if ((r.Name.Length == 2) && (char.ToLower(r.Name[0]) == 'h') && char.IsDigit(r.Name[1])) { int level = r.Name[1] - '0'; string id = r.GetAttribute("id"); string text = r.ReadElementContentAsString(); if ((id != null) && (text != null)) { while ((levelPath.Count > 0) && (levelPath.Peek() >= level)) { levelPath.Pop(); ent = result.Parent(ent); } levelPath.Push(level); ent = result.AddEntry(ent); result.SetName(ent, text); result.SetLinkref(ent, id); } } else if (r.Name.ToLower() == "title") { result.Title = r.ReadElementContentAsString(); } } } return result; }
public NCXParser(TableOfContents t, string rel) { toc = t; relPath = rel; }
static void PrintEntry(TableOfContents toc, IDocumentConsumer doc, int ent, int depth, int depthLimit) { if ((depthLimit >= 0) && (depth > depthLimit)) return; doc.PushBlock(new Block() { Indent = depth }); doc.PushFragment(new Fragment() { Attr = Fragment.Attributes.Heading, Text = toc.GetName(ent), Linkref = toc.GetLinkref(ent) }); for (int i = toc.FirstChild(ent); i >= 0; i = toc.NextSibling(i)) PrintEntry(toc, doc, i, depth + 1, depthLimit); }
public static void PrintToc(TableOfContents toc, IDocumentConsumer doc, int depthLimit = -1) { doc.PushBlock(new Block()); doc.PushFragment(new Fragment() { Attr = Fragment.Attributes.Heading, Text = toc.Title }); for (int i = toc.FirstRoot(); i >= 0; i = toc.NextSibling(i)) PrintEntry(toc, doc, i, 0, depthLimit); doc.Close(); }
public SectionIndex(AnchorIndex anchor, TableOfContents toc) { for (int i = 0; i < toc.Count; i++) { int word = anchor.FindAnchor(toc.GetLinkref(i)); if (word >= 0) items.Add(new Item(toc.GetName(i), word)); } items.Sort(cmpWord); }