public ActionResult Create(Book temp) { if (Request.Files.Count == 2) { List<Book> books = new List<Book>(); for (int i = 0; i < 2; i++) { var uploadedFile = Request.Files[i]; var fileSavePath = "~/App_Data/UploadedBooks/" + uploadedFile.FileName; uploadedFile.SaveAs(Server.MapPath(fileSavePath)); Book b = BookFormatter.ExtractBook(fileSavePath); books.Add(b); db.Books.Add(b); } db.SaveChanges(); foreach (Book book in books) { BookFormatter.PrepareForAlignment(book); } db.SaveChanges(); return RedirectToAction("AlignChapters", "Alignment", new { id1 = books[0].Id, id2 = books[1].Id }); } return View(); }
public ActionResult Create(Book temp) { if (Request.Files.Count == 2) { BookGroup group = new BookGroup(); for (int i = 0; i < 2; i++) { var uploadedFile = Request.Files[i]; var fileSavePath = "~/App_Data/UploadedBooks/" + uploadedFile.FileName; uploadedFile.SaveAs(Server.MapPath(fileSavePath)); Book b = BookFormatter.ExtractBook(fileSavePath); if (group.Title == null) { group.Title = b.Title; group.Author = b.Author; } group.Books.Add(b); } db.BookGroups.Add(group); db.SaveChanges(); foreach (Book book in group.Books.ToList()) { BookFormatter.PrepareForAlignment(book); } db.SaveChanges(); return RedirectToAction("Index"); } return View(); }
public static void PrepareForAlignment(Book book) { HtmlDocument doc = new HtmlDocument(); doc.Load(System.Web.Hosting.HostingEnvironment.MapPath(book.Path)); StripTrash(doc); BreakIntoSentences(doc.DocumentNode, book.Id + "-" + book.Language + "-"); List<Chapter> toc = extractTOC(doc.DocumentNode, doc.DocumentNode.SelectSingleNode("//ul"), book); book.Chapters = toc; doc.Save(System.Web.Hosting.HostingEnvironment.MapPath(book.Path)); }
public static Book ExtractBook(string filePath) { Book book = new Book(); book.SourcePath = filePath; Epub EpubFile = new Epub(System.Web.Hosting.HostingEnvironment.MapPath(filePath)); book.Title = EpubFile.Title[0]; book.Author = EpubFile.Creator[0]; book.Language = EpubFile.Language[0]; string bookHtml = EpubFile.GetContentAsHtml(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(bookHtml); string newPath = "~/App_Data/HtmlBooks/book_" + DateTime.Now.Ticks + ".html"; book.Path = newPath; doc.Save(System.Web.Hosting.HostingEnvironment.MapPath(newPath)); return book; }
private static List<Chapter> extractTOC(HtmlNode root, HtmlNode toc, Book book, Chapter parent = null) { HtmlNodeCollection nodes = toc.ChildNodes; List<Chapter> chapters = new List<Chapter>(); for (int i = 0; i < nodes.Count; i++) { HtmlNode li = nodes[i]; Chapter ch = new Chapter(); //ch.InBook = book; ch.Parent = parent; // source (epub) chapter's attribute string source = li.ChildNodes[0].Attributes["href"].Value; source = source.Substring(1, source.Length - 1); // calculate chapter id HtmlNode nearestSentence = getNextSentence(root, source); string newId = "ch-" + book.Id + "-" + nearestSentence.Id; ch.ChapterId = newId; ch.Order = getOrder(nearestSentence.Id); //refresh hrefs in contents and ids of chapter anchors nodes[i].FirstChild.Attributes["href"].Value = "#" + newId; nodes[i].FirstChild.SetAttributeValue("class", "chapter"); root.SelectSingleNode( "//body//*[@id='" + source + "']").Id = newId; if (li.ChildNodes.Count > 1) { List<Chapter> child_li = extractTOC(root, li.ChildNodes[1], book, ch); ch.Children = child_li; } chapters.Add(ch); } return chapters; }
public ActionResult Edit(Book book) { if (ModelState.IsValid) { db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(book); }
private static List<Chapter> getBindedChapters(List<ChapterBinding> bindings, Book first, Book second) { var chapters = (bindings.ElementAt(0).ChapterOne.InBook.Id == first.Id) ? bindings.Select(ch => new Chapter { Id = ch.ChapterOne.Id, ChapterId = ch.ChapterOne.ChapterId, InBook = ch.ChapterOne.InBook, Order = ch.ChapterOne.Order }) : bindings.Select(ch => new Chapter { Id = ch.ChapterTwo.Id, ChapterId = ch.ChapterTwo.ChapterId, InBook = ch.ChapterTwo.InBook, Order = ch.ChapterTwo.Order }); chapters.OrderBy(ch => ch.Order).DistinctBy(ch => ch.Id); return chapters.ToList(); }
private static string ExtractChapters(Book book, List<Chapter> chapters) { HtmlDocument doc = new HtmlDocument(); doc.Load(System.Web.Hosting.HostingEnvironment.MapPath(book.Path)); doc.DocumentNode.SelectSingleNode("//body").AppendChild(HtmlNode.CreateNode("<span id=\"eend\" class=\"chapter\"></span>")); string html = ""; foreach (Chapter chapter in chapters) { HtmlNode a = getById(doc.DocumentNode, chapter.ChapterId); HtmlNodeCollection ps = getFollowingChapter(doc.DocumentNode, chapter.ChapterId); HtmlNode ch = HtmlNode.CreateNode("<div class='chapter_div'></div>"); ch.AppendChild(a); ch.AppendChildren(ps); html += ch.OuterHtml; /* html += getById(doc.DocumentNode, chapter.ChapterId).OuterHtml; foreach (HtmlNode node in getFollowingChapter(doc.DocumentNode, chapter.ChapterId)) { html += node.OuterHtml; } */ } return html; }