/// <summary> /// Exports the tree. /// </summary> /// <param name="root">The root element.</param> /// <param name="includeRoot">if set to <c>true</c> the also export the root element, else /// if set to <c>false</c>, then the children are exported.</param> public void ExportTree(EATree root, bool includeRoot) { m_XmlDocument = new XmlDocument() { XmlResolver = null }; XmlDocumentFragment xmlFragment = m_XmlDocument.CreateDocumentFragment(); DocBookFormat format = new DocBookFormat(); if (includeRoot) { XmlNode xnode = ExportElement(root, format); xmlFragment.AppendChild(xnode); } else { foreach (EATree child in root.Children) { XmlNode xnode = ExportElement(child, format); xmlFragment.AppendChild(xnode); } } xmlFragment.WriteContentTo(m_XmlWriter); }
private void ParseHtmlChildren(HtmlNode node, DocBookFormat format, XmlNode xmlNode) { foreach (HtmlNode child in node.ChildNodes) { xmlNode = ParseHtml(child, format, xmlNode); } }
private XmlNode ConvertHtmlToDocBook45(string text, DocBookFormat format) { if (!string.IsNullOrWhiteSpace(text)) { HtmlDocument html = new HtmlDocument(); html.LoadHtml(text); XmlDocumentFragment xmlFragment = m_XmlDocument.CreateDocumentFragment(); return(ParseHtml(html.DocumentNode, format, xmlFragment)); } return(null); }
private XmlNode ExportElement(EATree element, DocBookFormat format) { XmlElement xmlSectionElement; if (format.SectionDepth == 0) { xmlSectionElement = m_XmlDocument.CreateElement("chapter"); } else { xmlSectionElement = m_XmlDocument.CreateElement("section"); } format.SectionDepth++; string heading = (element.Heading == null) ? string.Empty : element.Heading.Trim(); XmlElement xmlTitleElement = m_XmlDocument.CreateElement("title"); XmlText xmlTitleText = m_XmlDocument.CreateTextNode(HtmlEntity.DeEntitize(heading)); xmlTitleElement.AppendChild(xmlTitleText); xmlSectionElement.AppendChild(xmlTitleElement); string text = (element.Text == null) ? string.Empty : element.Text.Trim(); XmlNode infoNode = CreateInfoNode(element); if (infoNode != null) { xmlSectionElement.AppendChild(infoNode); } XmlNode textNode = ConvertHtmlToDocBook45(text, format); if (textNode != null) { xmlSectionElement.AppendChild(textNode); } if (textNode == null && infoNode == null) { if (element.Children.Count == 0) { xmlSectionElement.AppendChild(m_XmlDocument.CreateElement("para")); } } foreach (EATree child in element.Children) { XmlNode xmlChild = ExportElement(child, format); xmlSectionElement.AppendChild(xmlChild); } format.SectionDepth--; return(xmlSectionElement); }
private XmlNode ParseHtml(HtmlNode node, DocBookFormat format, XmlNode xmlNode) { string html; XmlNode xmlParent; switch (node.NodeType) { case HtmlNodeType.Document: ParseHtmlChildren(node, format, xmlNode); break; case HtmlNodeType.Comment: // Don't output comments break; case HtmlNodeType.Text: string parentName = node.ParentNode.Name; if (parentName.Equals("script") || parentName.Equals("style")) { // Ignore scripts and styles break; } html = ((HtmlTextNode)node).Text; if (HtmlNode.IsOverlappedClosingElement(html)) { // Is it in fact a special closing node output as text? break; } xmlNode = ParseHtmlText(HtmlEntity.DeEntitize(html), xmlNode); break; case HtmlNodeType.Element: DocBookFormat nextFormat = format; XmlNode nextNode = xmlNode; switch (node.Name) { case "ol": // Add the ordered list after the previous paragraph if there is one. xmlParent = GetParent(xmlNode, "para"); if (xmlParent != null) { xmlNode = xmlParent.ParentNode; } nextFormat = new DocBookFormat(format.SectionDepth, HtmlFormatMode.OrderedList); nextNode = m_XmlDocument.CreateElement("orderedlist"); xmlNode.AppendChild(nextNode); break; case "ul": xmlParent = GetParent(xmlNode, "para"); if (xmlParent != null) { xmlNode = xmlParent.ParentNode; } nextFormat = new DocBookFormat(format.SectionDepth, HtmlFormatMode.UnorderedList); nextNode = m_XmlDocument.CreateElement("itemizedlist"); xmlNode.AppendChild(nextNode); break; case "li": nextFormat = new DocBookFormat(format.SectionDepth, HtmlFormatMode.ListItem); XmlElement xmlListItem = m_XmlDocument.CreateElement("listitem"); XmlElement xmlPara = m_XmlDocument.CreateElement("para"); xmlListItem.AppendChild(xmlPara); xmlNode.AppendChild(xmlListItem); nextNode = xmlPara; break; case "u": xmlParent = GetParent(xmlNode, "para", "screen"); if (xmlParent == null) { XmlElement xmlNewPara = m_XmlDocument.CreateElement("para"); xmlNode.AppendChild(xmlNewPara); xmlNode = xmlNewPara; } XmlElement xmlUnderline = m_XmlDocument.CreateElement("emphasis"); XmlAttribute xmlUnderlineAttr = m_XmlDocument.CreateAttribute("role"); xmlUnderlineAttr.Value = "underline"; xmlUnderline.Attributes.Append(xmlUnderlineAttr); xmlNode.AppendChild(xmlUnderline); nextNode = xmlUnderline; break; case "i": xmlParent = GetParent(xmlNode, "para", "screen"); if (xmlParent == null) { XmlElement xmlNewPara = m_XmlDocument.CreateElement("para"); xmlNode.AppendChild(xmlNewPara); xmlNode = xmlNewPara; } XmlElement xmlItalic = m_XmlDocument.CreateElement("emphasis"); xmlNode.AppendChild(xmlItalic); nextNode = xmlItalic; break; case "b": xmlParent = GetParent(xmlNode, "para", "screen"); if (xmlParent == null) { XmlElement xmlNewPara = m_XmlDocument.CreateElement("para"); xmlNode.AppendChild(xmlNewPara); xmlNode = xmlNewPara; } XmlElement xmlBold = m_XmlDocument.CreateElement("emphasis"); XmlAttribute xmlBoldAttr = m_XmlDocument.CreateAttribute("role"); xmlBoldAttr.Value = "bold"; xmlBold.Attributes.Append(xmlBoldAttr); xmlNode.AppendChild(xmlBold); nextNode = xmlBold; break; case "sub": xmlParent = GetParent(xmlNode, "para", "screen"); if (xmlParent == null) { XmlElement xmlNewPara = m_XmlDocument.CreateElement("para"); xmlNode.AppendChild(xmlNewPara); xmlNode = xmlNewPara; } XmlElement xmlSub = m_XmlDocument.CreateElement("subscript"); xmlNode.AppendChild(xmlSub); nextNode = xmlSub; break; case "sup": xmlParent = GetParent(xmlNode, "para", "screen"); if (xmlParent == null) { XmlElement xmlNewPara = m_XmlDocument.CreateElement("para"); xmlNode.AppendChild(xmlNewPara); xmlNode = xmlNewPara; } XmlElement xmlSup = m_XmlDocument.CreateElement("superscript"); xmlNode.AppendChild(xmlSup); nextNode = xmlSup; break; } if (node.HasChildNodes) { ParseHtmlChildren(node, nextFormat, nextNode); } break; } return(xmlNode); }