private void ResolveKeywords(DitaElement parentElement, DitaCollection collection) { if (parentElement != null) { if (parentElement.Type == "keyword") { string keyref = parentElement.AttributeValueOrDefault("keyref", String.Empty); if (!string.IsNullOrWhiteSpace(keyref)) { // Try to find the referred to file DitaKeyDef keyDef = collection.GetKeyDefByKey(keyref); if (keyDef != null) { if (!string.IsNullOrWhiteSpace(keyDef.Keywords)) { parentElement.SetInnerText(keyDef.Keywords); } } } } else { // Check the child elements if (parentElement.Children != null) { foreach (DitaElement childElement in parentElement.Children) { ResolveKeywords(childElement, collection); } } } } }
// The main conversion action public bool Convert(string input, string output, string rootMapFile, bool rename = false, bool deleteExistingOutput = false) { try { // Make sure the output path exists VerifyOutputPath(output); // Try to load all of the input files Collection = new DitaCollection(input); // Find the rootmap FindRootMap(rootMapFile); // Try renaming the files in the collection, if requested if (rename) { Collection.RenameFiles(); } // Delete and existing output, if asked if (deleteExistingOutput) { DeleteOutputFiles(output); } return(true); } catch (Exception ex) { Trace.TraceError(ex); return(false); } }
// Verify all of the DITA files in the given directory // Returns true if *any* file in the directory is valid DITA (map or topic) public bool VerifyDirectory(string input) { try { // Try to load all of the input files DitaCollection collection = new DitaCollection(input); return(true); } catch { return(false); } }
// Construct from a single topic public DitaPageJson(DitaFile file, DitaCollection collection) { Collection = collection; // Get the title of the page Title = DitaFile.FixSpecialCharacters(file.Title); // Create the file name FileName = file.NewFileName ?? file.FileName; FileName = Path.ChangeExtension(FileName, ".json"); OriginalFileName = file.FileName; // Find the body element string bodyElementName = null; if (DitaFile.DitaFileBodyElement.ContainsKey(file.GetType())) { bodyElementName = DitaFile.DitaFileBodyElement[file.GetType()](); } if (!string.IsNullOrEmpty(bodyElementName)) { DitaElement bodyElement = file.RootElement.FindOnlyChild(bodyElementName); if (bodyElement != null) { Sections = new List <DitaPageSectionJson>(); // Convert the body to html DitaElementToHtmlConverter htmlConverter = new DitaElementToHtmlConverter(collection); htmlConverter.Convert(bodyElement, Sections, file.FileName, out string bodyHtml); BodyHtml = bodyHtml; // Convert the body to text DitaElementToTextConverter textConverter = new DitaElementToTextConverter(); textConverter.Convert(bodyElement, out string bodyText); BodyText = bodyText; } else { Trace.TraceWarning($"Body element not found in {file.FileName}."); } } else { Trace.TraceWarning($"No body element identified in {file.FileName}."); } IsEmpty = string.IsNullOrEmpty(BodyText) || string.IsNullOrEmpty(Title); }
// Construct a collection from a Dita bookmap in a Dita collection public DitaCollectionJson(DitaCollection collection, DitaFile rootMap) { // Store the construction properties Collection = collection; RootMap = rootMap; // Initialize the properties BookTitle = new Dictionary <string, string>(); BookMeta = new Dictionary <string, string>(); Chapters = new List <DitaCollectionLinkJson>(); Pages = new List <DitaPageJson>(); // Create the output object if (RootMap is DitaFileBookMap) { ParseBookMap(); } else if (RootMap is DitaFileMap) { ParseMap(); } }
// Resolves conrefs in this file private void ResolveConRefs(DitaElement parentElement, DitaCollection collection) { if (parentElement != null) { bool updated = false; // Does this element have a conref? string conref = parentElement.AttributeValueOrDefault("conref", String.Empty); if (!string.IsNullOrEmpty(conref)) { // We expect the conref to be in the form of filename.xml#fileid/elementid Regex conRefRegex = new Regex("^(.*)#(.*)/(.*)$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); MatchCollection conRefMatchCollection = conRefRegex.Matches(conref); if (conRefMatchCollection?.Count > 0 && (conRefMatchCollection[0].Groups.Count == 4)) { // Extract the components of the conref string refFileName = conRefMatchCollection[0].Groups[1].Value; string refFileId = conRefMatchCollection[0].Groups[2].Value; string refElementId = conRefMatchCollection[0].Groups[3].Value; if (Path.GetFileNameWithoutExtension(refFileName) != refFileId) { Trace.TraceWarning($"conref file name '{refFileName}' is not equal to file id '{refFileId}'."); } // Try to find the file that this conref refers to DitaFile refFile = collection.GetFileByName(refFileName); if (refFile != null) { // Does the references element exist in this file if (refFile.ElementsById.ContainsKey(refElementId)) { DitaElement refElement = refFile.ElementsById[refElementId]; // Copy the refernce element parentElement.Copy(refElement); updated = true; } else { Trace.TraceWarning($"Element '{refElementId}' not found in file '{refFileName}'."); } } else { Trace.TraceWarning($"Can't find file '{refFileName}' referenced in file '{FileName}'."); } } else { Trace.TraceWarning($"conref {conref} not in expected format."); } } // Update child references if (!updated && parentElement.Children != null) { foreach (DitaElement childElement in parentElement.Children) { ResolveConRefs(childElement, collection); } } } }
// Resolve keyword references public void ResolveKeywords(DitaCollection collection) { ResolveKeywords(RootElement, collection); SetTitleFromXml(); }
public DitaElementToHtmlConverter(DitaCollection collection) { Collection = collection; }