/// <summary> /// Initializes a new instance of the <see cref="OebpsTextToc"/> class. /// </summary> /// <param name="publicationMeta">deserialized <see cref="PublicationFiles.EpubMetadata"/></param> /// <param name="chapterSet">chapter data</param> /// <param name="epubTextDirectory">conventional <c>epub/OEBPS/Text</c> directory</param> public OebpsTextToc(JObject publicationMeta, Dictionary <string, string> chapterSet, string epubTextDirectory) { _publicationMeta = publicationMeta; _chapterSet = chapterSet; _documentPath = ProgramFileUtility.GetCombinedPath(epubTextDirectory, PublicationFiles.EpubFileToc, fileIsExpected: true); _document = XDocument.Load(_documentPath); }
/// <summary> /// Writes the <see cref="PublicationFiles.EpubFileTitle"/> file. /// </summary> public void WriteTitle() { Console.WriteLine("writing Title data..."); var jPublication = _publicationMeta.GetJObject("publication"); var title = jPublication.GetValue <string>("title"); var author = jPublication.GetValue <string>("author"); var xhtml = PublicationNamespaces.Xhtml; var path = ProgramFileUtility.GetCombinedPath(_epubTextDirectory, PublicationFiles.EpubFileTitle, fileIsExpected: true); var titleDocument = XDocument.Load(path); var h1Element = titleDocument.Root .Element(xhtml + "body") .Element(xhtml + "div") .Element(xhtml + "h1"); var spanElement = titleDocument.Root .Element(xhtml + "body") .Element(xhtml + "div") .Element(xhtml + "span"); h1Element.Value = title; spanElement.Value = author; EpubUtility.SaveAsUnicodeWithBom(titleDocument, path); }
internal void SetEpubTextDirectory() { var epubRoot = ProgramFileUtility.GetCombinedPath(_publicationRoot, "epub", fileIsExpected: false); var epubOebpsDirectory = ProgramFileUtility.GetCombinedPath(epubRoot, "OEBPS", fileIsExpected: false); _epubTextDirectory = ProgramFileUtility.GetCombinedPath(epubOebpsDirectory, "Text", fileIsExpected: false); }
/// <summary> /// Initializes a new instance of the <see cref="OebpsTextCopyright" /> class. /// </summary> /// <param name="publicationMeta">The publication meta.</param> /// <param name="epubTextDirectory">conventional <c>epub/OEBPS/Text</c> directory</param> public OebpsTextCopyright(JObject publicationMeta, string epubTextDirectory) { _publicationMeta = publicationMeta; _documentPath = ProgramFileUtility.GetCombinedPath(epubTextDirectory, PublicationFiles.EpubFileCopyright, fileIsExpected: true); _document = XDocument.Load(_documentPath); this.SetSpans(); }
/// <summary> /// Initializes a new instance of the <see cref="IdpfPackage"/> class. /// </summary> /// <param name="publicationMeta">deserialized <see cref="PublicationFiles.EpubMetadata"/></param> /// <param name="isbn13">International Standard Book Number (ISBN)</param> /// <param name="chapterSet">chapter data</param> /// <param name="epubOebpsDirectory">conventional <c>epub/OEBPS</c> directory</param> public IdpfPackage(JObject publicationMeta, string isbn13, Dictionary <string, string> chapterSet, string epubOebpsDirectory) { _publicationMeta = publicationMeta; _isbn13 = isbn13; _chapterSet = chapterSet; _idpfDocumentPath = ProgramFileUtility.GetCombinedPath(epubOebpsDirectory, PublicationFiles.IdpfcOpfManifest, fileIsExpected: true); _idpfDocument = XDocument.Load(_idpfDocumentPath); }
/// <summary> /// Initializes a new instance of the <see cref="DaisyConsortiumNcx"/> class. /// </summary> /// <param name="publicationMeta">The publication meta.</param> /// <param name="isbn13">The isbn13.</param> /// <param name="chapterSet">The chapter set.</param> /// <param name="epubOebpsDirectory">The epub oebps directory.</param> public DaisyConsortiumNcx(JObject publicationMeta, string isbn13, Dictionary <string, string> chapterSet, string epubOebpsDirectory) { _publicationMeta = publicationMeta; _isbn13 = isbn13; _chapterSet = chapterSet; _ncxDocumentPath = ProgramFileUtility.GetCombinedPath(epubOebpsDirectory, PublicationFiles.DaisyConsortiumNcxToc, fileIsExpected: true); _ncxDocument = XDocument.Load(_ncxDocumentPath); }
public void GenerateEntryFor11ty_Test(string entryRoot, string title) { entryRoot = ProgramAssemblyUtility.GetPathFromAssembly(this.GetType().Assembly, entryRoot); var entry = MarkdownEntryUtility.GenerateEntryFor11ty(entryRoot, title); Assert.NotNull(entry); Assert.True(File.Exists(ProgramFileUtility.GetCombinedPath(entryRoot, $"{entry.FrontMatter.GetValue<string>("clientId")}.md"))); }
internal void SetPublicationMetaAndChapterSet() { var publicationMetaPath = ProgramFileUtility.GetCombinedPath(_publicationRoot, "json", fileIsExpected: false); var publicationMetaFile = ProgramFileUtility.GetCombinedPath(publicationMetaPath, PublicationFiles.EpubMetadata, fileIsExpected: true); _publicationMeta = JObject.Parse(File.ReadAllText(publicationMetaFile)); _chapterSet = _publicationMeta .GetJObject("publication") .GetValue <Dictionary <string, string> >("chapterSet"); }
/// <summary> /// Publishes a <see cref="MarkdownEntry"/> /// from the specified entry root /// to the specified presentation root /// for the eleventy pipeline. /// </summary> /// <param name="entryRoot">the conventional directory of <see cref="MarkdownEntry"/> drafts</param> /// <param name="presentationRoot">the presentation target directory for publication</param> /// <param name="fileName">the name of the <see cref="MarkdownEntry"/> file in the entry directory</param> /// <param name="publicationDate">the <see cref="DateTime"/> of publication</param> /// <returns> /// Returns the path of the published file. /// </returns> /// <remarks> /// When the publication date is one day later or more than the entry incept date /// new eleventy <see cref="MarkdownEntry.FrontMatter"/> will be generated /// and the presentation file will be renamed accordingly. /// </remarks> public static string PublishEntryFor11ty(string entryRoot, string presentationRoot, string fileName, DateTime publicationDate) { if (!Directory.Exists(entryRoot)) { throw new DirectoryNotFoundException($"The expected entry root directory, `{entryRoot ?? "[null]"}`, is not here."); } if (!Directory.Exists(presentationRoot)) { throw new DirectoryNotFoundException($"The expected presentation root directory, `{presentationRoot ?? "[null]"}`, is not here."); } if (string.IsNullOrWhiteSpace(fileName)) { throw new NullReferenceException("The expected file name is not here."); } if (!fileName.EndsWith(".md")) { throw new FormatException("The expected file name format, `*.md`, is not here."); } var rootInfo = new DirectoryInfo(entryRoot); var draftInfo = rootInfo.GetFiles().FirstOrDefault(i => i.Name.EqualsInvariant(fileName)); if (draftInfo == null) { throw new FileNotFoundException($"The expected file, `{fileName ?? "[no file name]"}`, under `{rootInfo.FullName}` is not here."); } var draftEntry = draftInfo.ToMarkdownEntry(); var inceptDate = draftEntry.FrontMatter.GetValue <DateTime>("date"); var clientId = draftEntry.FrontMatter.GetValue <string>("clientId"); var path = draftEntry.FrontMatter.GetValue <string>("path").Replace(clientId, string.Empty); if ((publicationDate - inceptDate).Days >= 1) { var title = draftEntry.FrontMatter.GetValue <string>("title"); var tag = draftEntry.FrontMatter.GetValue <string>("tag", throwException: false); draftEntry.WithNew11tyFrontMatter(title, publicationDate, path, tag); } var combinedPath = ProgramFileUtility.GetCombinedPath(presentationRoot, $"{draftEntry.FrontMatter.GetValue<string>("clientId")}.md"); File.WriteAllText(combinedPath, draftEntry.ToFinalEdit()); draftInfo.Delete(); return(combinedPath); }
/// <summary> /// Converts <see cref="FeedsMetadata"/> to root directory. /// </summary> /// <param name="meta">The meta.</param> /// <param name="args">The arguments.</param> /// <returns></returns> /// <exception cref="DirectoryNotFoundException">The expected root directory is not here.</exception> public static string ToRootDirectory(this FeedsMetadata meta, ProgramArgs args) { var basePath = args.HasArg(ProgramArgs.BasePath, requiresValue: false) ? args.GetBasePathValue() : Directory.GetCurrentDirectory(); var rootDirectory = meta.FeedsDirectory.StartsWith("./") ? ProgramFileUtility.GetCombinedPath(basePath, meta.FeedsDirectory) : meta.FeedsDirectory; rootDirectory = Path.GetFullPath(rootDirectory); if (!Directory.Exists(rootDirectory)) { Directory.CreateDirectory(rootDirectory); } return(rootDirectory); }
/// <summary> /// Generates EPUB chapters /// from <see cref="PublicationFiles.EpubMetadata"/>. /// </summary> public void GenerateChapters() { _chapterSet.ToList().ForEach(pair => { var chapterDirectory = ProgramFileUtility.GetCombinedPath(_markdownDirectory, pair.Value, fileIsExpected: false); Console.WriteLine("looking for {0}...", pair.Key); if (!Directory.Exists(chapterDirectory)) { Throw(string.Format("ERROR: cannot find {0}", chapterDirectory)); } var chapter = new PublicationChapter(pair, _chapterTemplate, chapterDirectory); var xhtml = chapter.GenerateXhtml(); var path = ProgramFileUtility.GetCombinedPath(_epubTextDirectory, string.Format("{0}.xhtml", pair.Key), fileIsExpected: true); Console.WriteLine("writing to {0}...", path); File.WriteAllText(path, xhtml, EpubUtility.GetUnicodeWithBomEncoding()); }); }
/// <summary> /// Writes the /// <see cref="PublicationFiles.EpubFileDedication"/> file. /// </summary> public void Write() { var xhtml = PublicationNamespaces.Xhtml; var xhtmlFile = ProgramFileUtility.GetCombinedPath(_epubTextDirectory, PublicationFiles.EpubFileDedication, fileIsExpected: true); var markdownFile = ProgramFileUtility.GetCombinedPath(_markdownDirectory, PublicationFiles.EpubMarkdownDedication, fileIsExpected: true); Console.WriteLine(" markdown file {0}...", markdownFile); var markdown = File.ReadAllText(markdownFile); var raw = Markdown.ToHtml(markdown); var rawElement = XElement.Parse(string.Format(@"<div class=""rx raw tmp"" xmlns=""{0}"">{1}</div>", xhtml, raw)); var dedicationDocument = new XDocument(_dedicationTemplate); var divElement = dedicationDocument.Root .Element(xhtml + "body") .Element(xhtml + "div") .Element(xhtml + "div"); divElement.ReplaceWith(rawElement.Nodes()); EpubUtility.SaveAsUnicodeWithBom(dedicationDocument, xhtmlFile); }
public void ShouldLoadEntries(string entriesRoot, string postsRoot) { entriesRoot = ProgramAssemblyUtility.GetPathFromAssembly(this.GetType().Assembly, entriesRoot); this._testOutputHelper.WriteLine($"loading entries from `{entriesRoot}`"); postsRoot = ProgramAssemblyUtility.GetPathFromAssembly(this.GetType().Assembly, postsRoot); this._testOutputHelper.WriteLine($"writing entries to `{postsRoot}`"); var entriesRootInfo = new DirectoryInfo(entriesRoot); foreach (var entryInfo in entriesRootInfo.GetFiles("*.json")) { this._testOutputHelper.WriteLine($"reading {entryInfo.Name}..."); var jsonString = GetJsonString(File.ReadAllText(entryInfo.FullName)); var jO = JObject.Parse(jsonString); var md = GetMarkdown(jO); var mdPath = ProgramFileUtility.GetCombinedPath(postsRoot, entryInfo.Name.Replace(".json", ".md")); this._testOutputHelper.WriteLine($"writing `{mdPath}`..."); File.WriteAllText(mdPath, md); } }
internal void SetChapterTemplate(string csxRoot) { var chapterTemplateFile = ProgramFileUtility.GetCombinedPath(csxRoot, PublicationFiles.EpubTemplateChapter, fileIsExpected: true); _chapterTemplate = XDocument.Load(chapterTemplateFile); }
internal void SetMarkdownDirectory() { _markdownDirectory = ProgramFileUtility.GetCombinedPath(_publicationRoot, "markdown", fileIsExpected: false); }
internal void SetBiographyTemplate(string csxRoot) { var biographyTemplateFile = ProgramFileUtility.GetCombinedPath(csxRoot, PublicationFiles.EpubTemplateBiography, fileIsExpected: true); _biographyTemplate = XDocument.Load(biographyTemplateFile); }
internal void SetTemplate(string csxRoot) { var dedicationTemplateFile = ProgramFileUtility.GetCombinedPath(csxRoot, PublicationFiles.EpubTemplateDedication, fileIsExpected: true); _dedicationTemplate = XDocument.Load(dedicationTemplateFile); }