Skip to content

Commit

Permalink
Fb2Kindle: SendBookByMail with document title in subject (instead of …
Browse files Browse the repository at this point in the history
…file name)
  • Loading branch information
sergiye committed Oct 2, 2023
1 parent aa8e0a8 commit d71a5cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions Fb2Kindle/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ internal class AppOptions {
internal string AppPath { get; } = Util.GetAppPath();
internal string TargetName { get; set; }
internal string TempFolder { get; set; }
internal string DocumentTitle { get; set; }
}
}
15 changes: 9 additions & 6 deletions Fb2Kindle/Convertor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,22 +225,22 @@ private void CreateNcxFile(TocItem tocItem) {
var navMap = new XElement("navMap", "");
AddNcxItem(navMap, 0, "Описание", "book.html#it");
var playOrder = 2;
AddTocListItems(tocItem, navMap, ref playOrder);
AddTocListItems(tocItem, navMap, ref playOrder, 1);
if (!options.Config.SkipToc)
AddNcxItem(navMap, 1, "Содержание", "toc.html#toc");
ncx.Add(navMap);
SaveXmlToFile(ncx, $"{options.TempFolder}\\toc.ncx");
ncx.RemoveAll();
}

private void AddTocListItems(TocItem tocItem, XElement navMap, ref int playOrder) {
private void AddTocListItems(TocItem tocItem, XElement navMap, ref int playOrder, int depth) {
foreach (var subItem in tocItem.SubItems) {
var navPoint = navMap;
if (subItem.IsLastCollapsibleLevel)
if (depth > 2 && subItem.IsLastCollapsibleLevel)
navPoint = AddNcxItem(navMap, playOrder++, subItem.Name, subItem.Href);
else
AddNcxItem(navMap, playOrder++, subItem.Name, subItem.Href);
AddTocListItems(subItem, navPoint, ref playOrder);
AddTocListItems(subItem, navPoint, ref playOrder, depth + 1);
}
}

Expand Down Expand Up @@ -406,6 +406,7 @@ private string CreateEpub() {
File.WriteAllText($"{epubDir.FullName}/META-INF/container.xml", @"<?xml version=""1.0"" encoding=""UTF-8""?><container xmlns=""urn:oasis:names:tc:opendocument:xmlns:container"" version=""1.0""><rootfiles><rootfile full-path=""OPS/content.opf"" media-type=""application/oebps-package+xml""/></rootfiles></container>");
File.WriteAllText($"{epubDir.FullName}/mimetype", "application/epub+zip");

// var tempFileName = Util.GetValidFileName(options.DocumentTitle);
var tmpBookPath = GetVersionedPath(options.TempFolder, options.TargetName, ".epub");
using (var zip = new ZipFile(tmpBookPath)) {
zip.CompressionLevel = options.Config.CompressionLevel switch {
Expand Down Expand Up @@ -477,8 +478,8 @@ private bool SendBookByMail(string tmpBookPath) {
new MailAddress(options.MailTo))) {
// message.BodyEncoding = message.SubjectEncoding = Encoding.UTF8;
message.IsBodyHtml = false;
message.Subject = options.TargetName;
message.Body = "Hello! Please, check book(s) attached";
message.Subject = options.DocumentTitle; //options.TargetName;
message.Body = $"Hello! Please, check '{options.TargetName}' file with '{options.DocumentTitle}' book attached";

using (var att = new Attachment(tmpBookPath)) {
message.Attachments.Add(att);
Expand Down Expand Up @@ -671,6 +672,8 @@ private XElement GetEmptyPackage(XElement book, bool useSequenceNameOnly = false
bookTitle = $"{seqName} {Util.AttributeValue(book.Elements("description").Elements("title-info").Elements("sequence"), "number")} {bookTitle}";
}
content.Add(bookTitle);

options.DocumentTitle = bookTitle;
Util.Write("Target document title: ");
Util.WriteLine(bookTitle, ConsoleColor.Green);

Expand Down
9 changes: 9 additions & 0 deletions Fb2Kindle/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,14 @@ internal static void Write(string message = null, ConsoleColor? color = null, Co
Console.Write(message);
Console.ResetColor();
}

// internal static string GetValidFileName(string origin) {
// if (string.IsNullOrWhiteSpace(origin))
// throw new ArgumentException("File name can not be empty.");
// foreach (var c in Path.GetInvalidFileNameChars()) {
// origin = origin.Replace(c, '-');
// }
// return origin;
// }
}
}

0 comments on commit d71a5cf

Please sign in to comment.