Ejemplo n.º 1
0
        // 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);
        }