Exemple #1
0
 public void DeleteImage(EpubItem item)
 {
     System.Diagnostics.Trace.Assert(item.IsImage);
     Manifest.Remove(item);
     AbsolutePathIndex.Remove(item.AbsolutePath);
     IdIndex.Remove(item.Id);
 }
Exemple #2
0
 public void InsertChapter(List <EpubItem> chapters, EpubItem insertAt)
 {
     for (int i = 0; i < Manifest.Count; ++i)
     {
         if (insertAt == Manifest[i])
         {
             for (int j = 0; j < chapters.Count; ++j)
             {
                 Manifest.Insert(i + j, chapters[j]);
             }
             break;
         }
     }
     for (int i = 0; i < Spine.Count; ++i)
     {
         if (insertAt == Spine[i])
         {
             for (int j = 0; j < chapters.Count; ++j)
             {
                 Spine.Insert(i + j, chapters[j]);
             }
             break;
         }
     }
     for (int j = 0; j < chapters.Count; ++j)
     {
         var chapter = chapters[j];
         AbsolutePathIndex.Add(chapter.AbsolutePath, chapter);
         IdIndex.Add(chapter.Id, chapter);
     }
 }
        public void CalcNewPathAndID(EpubItem item, int offset)
        {
            string oldAbsolutePath = item.AbsolutePath;
            var    oldFileName     = oldAbsolutePath.getZipFileName();
            var    oldprefix       = EpubItem.PrefixAsInt(oldFileName);
            var    fileName        = oldFileName.StripPrefixFromFileName();
            var    path            = oldAbsolutePath.GetZipPath();

            if (!string.IsNullOrEmpty(path))
            {
                path += '/';
            }

            // note possible conflict as "cover" does not have a prefix
            // which might conflict if there was also a page with 0000_cover.xhtml
            // So, if page has a prefix, bump offest by one.
            var bump = EpubItem.ExtractPrefixFromFileName(oldFileName) == null ? 0 : 1;

            var newPrefix       = (oldprefix + offset + bump).ToString("D4");
            var newAbsolutePath = $"{path}{newPrefix}_{fileName}";

            NewAbsolutePaths.Add(oldAbsolutePath, newAbsolutePath);

            string newId = item.Id.StripDigits() + newPrefix;

            NewItemIds.Add(item.Id, newId);
        }
Exemple #4
0
        public static byte[] UpdateXhtmlPage(this EpubItem item, Dictionary <string, string> newAbsolutePaths)
        {
            System.Diagnostics.Trace.WriteLine($"Fixing up references on page {item.AbsolutePath}");
            var xhtml    = item.RawBytes.ToXhtml();
            var itemPath = item.AbsolutePath.GetZipPath();

            FixupReferences(xhtml, itemPath, newAbsolutePaths);
            return(xhtml.ToSBytes());
        }
Exemple #5
0
        public static void ConvertToJpeg(this EpubItem item)
        {
            item.MediaType = Epub.jpegMedia;
            int index = item.AbsolutePath.LastIndexOf(".");

            item.AbsolutePath = item.AbsolutePath.Substring(0, index + 1) + "jpeg";
            var image = item.RawBytes.ExtractImage(true);

            item.RawBytes = image.ConvertToJpeg();
        }
        public GuideReference(XElement element, string opfFolder, Dictionary <string, EpubItem> absolutePathIndex)
        {
            string   path = ZipUtils.RelativePathToAbsolute(opfFolder, element.Attribute("href").Value);
            EpubItem temp = null;

            absolutePathIndex.TryGetValue(path, out temp);
            Item     = temp;
            Title    = element.Attribute("title")?.Value;
            TypeName = element.Attribute("type").Value;
        }
Exemple #7
0
 public void InsertChapter(EpubItem chapter, TocEntry tocEntry, EpubItem preceedingItem)
 {
     Opf.InsertChapter(new List <EpubItem>()
     {
         chapter
     }, preceedingItem);
     ToC.InsertChapter(new List <TocEntry>()
     {
         tocEntry
     }, preceedingItem);
 }
Exemple #8
0
        public void DeleteItem(EpubItem item)
        {
            // ToDo, better handling of case when item appears in ToC
            // in multple places.  (Due to nested chapters)
            var entryDetails = FindTocEntry(item.AbsolutePath);

            while (entryDetails.entries != null)
            {
                entryDetails.entries.RemoveAt(entryDetails.index);
                entryDetails = FindTocEntry(item.AbsolutePath);
            }
        }
        public void PopulateControls(Epub epub, EpubItem itemToInsertBefore, int chapterNum)
        {
            this.epub = epub;
            this.itemToInsertBefore = itemToInsertBefore;
            int    idVal = epub.Opf.GetPageItems().GetMaxPrefix() + 1;
            string idNum = idVal.ToString("D4");

            textBoxId.Text    = "xhtml" + idNum;
            textBoxTitle.Text = $"Chapter {chapterNum}";
            textBoxPath.Text  = $"OEBPS/Text/{idNum}_Chapter{chapterNum}.xhtml";
            changeEpubAction  = InsertChapter;
        }
        public override bool Clean(XDocument doc, EpubItem item)
        {
            if (!string.Equals(HostName(item), "vipnovel.com"))
            {
                return(false);
            }

            bool modified = false;
            var  toDelete = doc.FindElementsMatching("div", IsZipNovelPromo);

            toDelete.RemoveElements();
            return(modified);
        }
        public void CopyEpubItem(EpubItem item, Func <EpubItem, byte[]> docUpdater)
        {
            var newItem = new EpubItem()
            {
                Id           = NewItemIds[item.Id],
                AbsolutePath = NewAbsolutePaths[item.AbsolutePath],
                MediaType    = item.MediaType,
                RawBytes     = docUpdater(item),
                Source       = item.Source
            };

            InitialEpub.Opf.AppendItem(newItem);
        }
Exemple #12
0
        public override bool Clean(XDocument doc, EpubItem item)
        {
            if (!string.Equals(HostName(item), "re-library.com"))
            {
                return(false);
            }

            var targets = doc.FindElementsMatching("a", e => e.Value == "⌈ Index ⌋");

            targets.AddRange(doc.FindElementsWithClassName("div", "code-block"));
            targets.AddRange(FindLeaveCommentElements(doc));
            targets.RemoveElements();
            return(0 < targets.Count);
        }
Exemple #13
0
        public void InsertChapter(List <TocEntry> tocEntries, EpubItem insertAt)
        {
            var entryDetails = FindTocEntry(insertAt.AbsolutePath);

            if (entryDetails.entries == null)
            {
                MessageBox.Show("Preceeding ToC entry not found, inserting at start");
                entryDetails.entries = Entries;
            }
            ;
            for (int j = 0; j < tocEntries.Count; ++j)
            {
                entryDetails.entries.Insert(entryDetails.index + j, tocEntries[j]);
            }
        }
        public bool Clean(EpubItem item)
        {
            bool changed = false;
            var  doc     = item.RawBytes.ToXhtml();

            foreach (var cleaner in Cleaners)
            {
                changed |= cleaner.Clean(doc, item);
            }
            if (changed)
            {
                item.RawBytes = doc.ToSBytes();
            }
            return(changed);
        }
        public override bool Clean(XDocument doc, EpubItem item)
        {
            if (!string.Equals(HostName(item), "novelfull.com"))
            {
                return(false);
            }

            var toDelete = doc.GetTextNodes().Where(ShouldRemoveTextNode).ToList();

            foreach (var node in toDelete)
            {
                System.Diagnostics.Trace.WriteLine(node.Value);
                node.Remove();
            }
            return(0 < toDelete.Count);
        }
        public void CopyImageEpubItem(EpubItem item)
        {
            var hash = item.RawBytes.ToHash();

            // don't copy images that are already in epub
            if (hash != 0)
            {
                string existingImage = null;
                if (ImageHashes.TryGetValue(hash, out existingImage))
                {
                    NewAbsolutePaths[item.AbsolutePath] = existingImage;
                    return;
                }
            }

            CopyEpubItem(item, (i) => i.RawBytes);
        }
        public void InsertChapter()
        {
            var newChapter = new EpubItem()
            {
                Id           = textBoxId.Text,
                AbsolutePath = textBoxPath.Text,
                MediaType    = Epub.XhtmlMedia,
                RawBytes     = Encoding.UTF8.GetBytes(Epub.EmptyXhtmlDoc),
                Source       = textBoxSource.Text
            };
            var newTocEntry = new TocEntry()
            {
                Title = textBoxTitle.Text,
                Item  = newChapter
            };

            epub.InsertChapter(newChapter, newTocEntry, itemToInsertBefore);
        }
        public void PopulateControlsForUpdate(Epub epub, EpubItem toUpdate)
        {
            this.toUpdate = toUpdate;
            var entryDetails = epub.ToC.FindTocEntry(toUpdate.AbsolutePath);

            tocEntry              = entryDetails.entries[entryDetails.index];
            textBoxId.Enabled     = false;
            textBoxId.Text        = toUpdate.Id;
            textBoxTitle.Text     = tocEntry.Title;
            textBoxSource.Text    = tocEntry.ContentSrc;
            textBoxSource.Enabled = false;
            textBoxPath.Text      = toUpdate.AbsolutePath;
            textBoxPath.Enabled   = false;
            textBox1.Text         = toUpdate.RawBytes.ToXhtml().ToString();
            changeEpubAction      = UpdateChapter;
            this.Text             = "Update Chapter";
            button2.Text          = "Update";
        }
Exemple #19
0
        static public string CheckForErrors(this EpubItem item, Signature sig1, Signature prevSig)
        {
            var  baseName           = Path.GetFileName(item.AbsolutePath);
            bool littleTextExpected = baseName.Equals("0000_Information.xhtml") || sig1.HasImages;

            if (littleTextExpected)
            {
                return(null);
            }
            if (sig1.Count < 3)
            {
                return($"{item.AbsolutePath} might be empty");
            }
            else if (sig1.ProbableDuplicate(prevSig))
            {
                return($"Possible Duplicate chapters {item.AbsolutePath}");
            }
            return(null);
        }
Exemple #20
0
        public ToC(XDocument doc, EpubItem ncxItem, Dictionary <string, EpubItem> absolutePathIndex)
        {
            NcxItem  = ncxItem;
            Version  = doc.Root.Attribute("version").Value;
            Language = doc.Root.Attribute(Epub.xmlNs + "lang")?.Value;

            Uid = doc.Root.Element(Epub.ncxNs + "head")
                  .Elements(Epub.ncxNs + "meta")
                  .Where(e => e.Attribute("name").Value == "dtb:uid")
                  .First()
                  .Attribute("content").Value;

            DocTitle = doc.Root.Element(Epub.ncxNs + "docTitle")
                       .Element(Epub.ncxNs + "text")
                       .Value;

            var    map     = doc.Root.Element(Epub.ncxNs + "navMap");
            string ncxPath = NcxFileName.GetZipPath();

            Entries = map.Elements(Epub.ncxNs + "navPoint")
                      .Select(e => new TocEntry(e, ncxPath, absolutePathIndex))
                      .ToList();
        }
 public override bool Clean(XDocument doc, EpubItem item)
 {
     return(RemoveScripts(doc)
            | RemoveEzoic(doc)
            | doc.RemoveEmptyDivElements());
 }
Exemple #22
0
 public void InsertChapters(List <EpubItem> chapters, List <TocEntry> tocEntries, EpubItem insertAt)
 {
     Opf.InsertChapter(chapters, insertAt);
     ToC.InsertChapter(tocEntries, insertAt);
 }
 public void CopyPageEpubItem(EpubItem item)
 {
     CopyEpubItem(item, (i) => EpubUtils.UpdateXhtmlPage(i, NewAbsolutePaths));
 }
Exemple #24
0
 public void AppendItem(EpubItem item)
 {
     Manifest.Add(item);
     AbsolutePathIndex.Add(item.AbsolutePath, item);
     IdIndex.Add(item.Id, item);
 }
        public EpubItem GetUpdatedItem(EpubItem item)
        {
            var newId = NewItemIds[item.Id];

            return(InitialEpub.Opf.IdIndex[newId]);
        }
Exemple #26
0
 public static Image ExtractImage(this EpubItem item)
 {
     System.Diagnostics.Trace.Assert(item.IsImage);
     return(item.RawBytes.ExtractImage(item.IsWebp()));
 }
 public abstract bool Clean(XDocument doc, EpubItem item);
 public static string HostName(EpubItem item)
 {
     return(string.IsNullOrEmpty(item.Source)
         ? null
         : new Uri(item.Source)?.Host);
 }
Exemple #29
0
 public static bool IsWebp(this EpubItem item)
 {
     return(item.MediaType == Epub.webpMedia);
 }