// Parse the metadata from the given element? private void ParseMetaCategories(DitaElement parentElement) { try { List <DitaElement> categoriesData = parentElement?.FindChildren("category"); if (categoriesData != null) { foreach (DitaElement categoryData in categoriesData) { foreach (DitaElement data in categoryData.Children) { if (data?.Attributes.ContainsKey("name") ?? false) { if (BookMeta.ContainsKey(data?.Attributes["name"])) { Trace.TraceWarning($"BookMeta already contains a value for {data?.Attributes["name"]}"); } else { BookMeta.Add(data?.Attributes["name"], data?.Attributes["value"]); } } } } } } catch (Exception ex) { Trace.TraceError(ex); } }
// Removes blank pages from the output private void RemoveBlankPages() { // Remove links to blank pages MarkBlankChapterLinks(Chapters); // Remove the blank pages List <DitaPageJson> removePages = new List <DitaPageJson>(); foreach (DitaPageJson page in Pages) { if (page.IsEmpty) { Trace.TraceWarning($"Removing empty page {page.FileName} ({page.OriginalFileName})"); removePages.Add(page); } } Pages = Pages.Except(removePages).ToList(); }
private List <DitaCollectionLinkJson> ParseRefs(List <DitaElement> topicRefElements) { List <DitaCollectionLinkJson> chapters = new List <DitaCollectionLinkJson>(); if (topicRefElements?.Count > 0) { foreach (DitaElement topicRefElement in topicRefElements) { // Try to find the linked file string topicRefHref = topicRefElement.AttributeValueOrDefault("href", ""); string topicRefKeyRef = topicRefElement.AttributeValueOrDefault("keyref", ""); string topicRefNavTitle = topicRefElement?.FindOnlyChild("topicmeta")?.FindOnlyChild("navtitle")?.ToString(); // If there is no navtitle, check the topicmeta if (string.IsNullOrWhiteSpace(topicRefNavTitle)) { topicRefNavTitle = topicRefElement.AttributeValueOrDefault("navtitle", ""); } // Is this an external link? if (topicRefElement.AttributeValueOrDefault("scope", "") == "external") { DitaCollectionLinkJson externalLink = new DitaCollectionLinkJson { FileName = topicRefHref, Title = topicRefNavTitle, IsExternal = true }; chapters.Add(externalLink); } else { // Local scope DitaFile linkedFile = null; if (!string.IsNullOrWhiteSpace(topicRefHref)) { linkedFile = Collection.GetFileByName(topicRefHref); } // If no href, try to find by keyref if (linkedFile == null && !string.IsNullOrWhiteSpace(topicRefKeyRef)) { linkedFile = Collection.GetFileByKey(topicRefKeyRef); } if (linkedFile != null) { if (string.IsNullOrWhiteSpace(linkedFile.Title)) { linkedFile.Title = topicRefNavTitle; } else if (string.IsNullOrWhiteSpace(topicRefNavTitle)) { topicRefNavTitle = linkedFile.Title; } // Add references from the linked files List <DitaCollectionLinkJson> newChapters = ParseChaptersFromFile(linkedFile, topicRefNavTitle); if (newChapters != null && newChapters.Count > 0) { // Are there child chapters? List <DitaCollectionLinkJson> childChapters = ParseRefs(topicRefElement.FindChildren(_refElements)); if (newChapters.Count > 1 && childChapters.Count > 0) { // This should never happen throw new Exception("Found multiple children in a map and topic refs."); } if (childChapters != null && childChapters.Count > 0) { newChapters[0]?.Children?.AddRange(childChapters); } chapters.AddRange(newChapters); } } else { Trace.TraceWarning($"Reference with missing href/keyref: {topicRefElement}"); } } } } return(chapters); }