public override void Process(RenderWordArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.HtmlDoc, "args.HtmlDoc"); Assert.ArgumentNotNull( args.HtmlDoc.DocumentNode, "args.HtmlDoc.DocumentNode"); Assert.ArgumentNotNullOrEmpty(args.Name, "args.Name"); Assert.ArgumentNotNull(args.Path, "args.Path"); this.Process(args.HtmlDoc.DocumentNode, args); }
//TODO: ensure adding things to body, not root public override void Process(RenderWordArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.HtmlDoc, "args.HtmlDoc"); Assert.ArgumentNotNull(args.HtmlDoc.DocumentNode, "args.HtmlDoc.DocumentNode"); //TODO: this should not be necessary; may be due to no <html> element or wrong method for adding elements args.HtmlDoc.LoadHtml(args.HtmlDoc.DocumentNode.WriteTo()); foreach (HtmlNode child in args.HtmlDoc.DocumentNode.ChildNodes) { if (child.NodeType != HtmlNodeType.Element || child.Name != "body") { continue; } foreach (HtmlNode grandchild in child.ChildNodes) { if (grandchild.NodeType != HtmlNodeType.Element) { continue; } ProcessWordNodeTreeArgs innerArgs = new ProcessWordNodeTreeArgs(grandchild); CorePipeline.Run("processWordNodeTree", innerArgs); HtmlNode next = innerArgs.NextElement; if (innerArgs.TokenTable != null) { args.TokenTable = innerArgs.TokenTable; } while (next != null) { ProcessWordNodeTreeArgs innerInnerArgs = new ProcessWordNodeTreeArgs(next); CorePipeline.Run("processWordNodeTree", innerInnerArgs); if (innerInnerArgs.TokenTable != null) { Assert.IsNull(args.TokenTable, "multiple token tables"); args.TokenTable = innerInnerArgs.TokenTable; } next = innerInnerArgs.NextElement; } break; } } }
public override void Process(RenderWordArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.WordMediaItem, "args.WordMediaItem"); if (args.WordMediaItem.Publishing.NeverPublish) { return; } using (new EditContext(args.WordMediaItem)) { args.WordMediaItem.Publishing.NeverPublish = true; } }
private void Process(HtmlNode node, RenderWordArgs args) { Assert.ArgumentNotNull(node, "node"); if (node.NodeType == HtmlNodeType.Element) { ProcessWordElementArgs innerArgs = new ProcessWordElementArgs( args.Database, node, args.Name, args.Path); CorePipeline.Run("processWordElement", innerArgs); } foreach (HtmlNode child in node.ChildNodes) { this.Process(child, args); } }
public override void Process(RenderWordArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.HtmlDoc, "args.HtmlDoc"); Assert.ArgumentNotNull(args.HtmlDoc.DocumentNode, "args.HtmlDoc.DocumentNode"); Assert.ArgumentNotNull(args.TitleField, "args.TitleField"); Assert.ArgumentNotNull(args.BodyField, "args.BodyField"); Assert.ArgumentNotNull(args.TokenTableField, "args.TokenTableField"); using (new EditContext(args.BodyField.Item)) { args.BodyField.Value = Regex.Replace( args.HtmlDoc.DocumentNode.WriteTo(), "</?(body|sitecorebulletedlistchar)>", string.Empty).Replace("<code>\r\n", "<code>"); } if ((!string.IsNullOrEmpty(args.Title)) && args.Title != args.TitleField.Value) { using (new EditContext(args.TitleField.Item)) { args.TitleField.Value = args.Title; } } string value = string.Empty; //TODO: if token table is empty, should clear token table? if (args.TokenTable != null) { foreach (string key in args.TokenTable.Keys) { value += key + "=" + args.TokenTable[key].Trim() + "&"; } using (new EditContext(args.TokenTableField.Item)) { args.TokenTableField.Value = value.TrimEnd('&'); } } }
public override void Process(RenderWordArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.WordDoc, "args.WordDoc"); // create a root <body> element for an XML structure args.HtmlDoc = new HtmlDocument(); HtmlNode htmlBody = args.HtmlDoc.CreateElement("body"); args.HtmlDoc.DocumentNode.ChildNodes.Add(htmlBody); Body body = args.WordDoc.MainDocumentPart.Document.Body; // for each element in the Word document foreach (OpenXmlElement current in body.ChildElements) { Paragraph paragraph = current as Paragraph; // Word treats almost everything (list items, etc.) as paragraphs. if (paragraph != null) { // the name of the style, which can also identify what should be <li> // or could be used as or to determine values for the class attribute string style = null; if (paragraph.ParagraphProperties != null && paragraph.ParagraphProperties.ParagraphStyleId != null) { style = paragraph.ParagraphProperties.ParagraphStyleId.Val.ToString().ToLower(); } // the text of the "paragraph" string text = string.Empty; // for each thing in the paragraph foreach (var child in paragraph.ChildElements) { // ignore paragraph properties. if (child is ParagraphProperties) { continue; } // some paragraphs may consist of a single run in a single style SdtRun sdtRun = child as SdtRun; if (sdtRun != null) { text += HttpUtility.HtmlEncode(sdtRun.InnerText); if (sdtRun.HasChildren) { //TODO: process images and runs? } continue; } // outer for-each invokes this for each run in the paragraph Run run = child as Run; if (run != null) { // if the run has a specific style, add it as an element name around the text if (run.RunProperties != null && run.RunProperties.RunStyle != null) { string name = run.RunProperties.RunStyle.Val.ToString().ToLower(); text += "<" + name + ">" + HttpUtility.HtmlEncode(run.InnerText) + "</" + name + ">"; } else { text += HttpUtility.HtmlEncode(run.InnerText); } foreach (Drawing drawing in run.Descendants <Drawing>()) { string alt = null; string title = null; // path to image file on disk string file = null; string width = null; string height = null; // for each drawing in the run foreach (DocumentFormat.OpenXml.Drawing.Extents extent in drawing.Descendants <DocumentFormat.OpenXml.Drawing.Extents>()) { if (extent.Cx != null) { width = this.GetPixels(extent.Cx.ToString()).ToString(CultureInfo.InvariantCulture); } if (extent.Cy != null) { height = this.GetPixels(extent.Cy.ToString()).ToString(CultureInfo.InvariantCulture); } } // there is probably only one DocProperties per image // retrieve alt text, title, and part of file name if possible foreach (DocProperties docProps in drawing.Descendants <DocProperties>()) { if (!string.IsNullOrEmpty(docProps.Title)) { title = docProps.Title; } if (!string.IsNullOrEmpty(docProps.Description)) { alt = docProps.Description; } if (!string.IsNullOrEmpty(docProps.Name)) { file = docProps.Name; } if (string.IsNullOrEmpty(alt)) { if (string.IsNullOrEmpty(title)) { if (!string.IsNullOrEmpty(file)) { alt = file; } } else { alt = title; } } } string unique = null; foreach (Blip blip in drawing.Descendants <Blip>()) { OpenXmlAttribute attribute = blip.GetAttribute( "embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); if (attribute != null) { Assert.IsNull(unique, "unique"); unique = attribute.Value; ImagePart imagePart = (ImagePart)args.WordDoc.MainDocumentPart.GetPartById(attribute.Value); Assert.IsNotNull(imagePart, "imagePart"); } else { Log.Info("no embed", this); } } if (!string.IsNullOrEmpty(unique)) { string path = this.WriteImageFile(args.WordDoc, "C:\\temp", unique, file).Replace("\\", "\\\\"); text += "<img src=\"" + path + "\" height=\"" + height + "\" width=\"" + width + "\" alt=\"" + alt + "\" title=\"" + title + "\" />"; } } continue; } // ignore these types of elements for now if (child is ProofError || child is BookmarkStart || child is BookmarkEnd) { continue; } // identify any new types of elements throw new Exception("Unknown child element: " + child.GetType()); } string html = "<p"; if (!string.IsNullOrEmpty(style)) { html += " class=\"" + style + "\""; } HtmlNode para = HtmlNode.CreateNode(html + ">" + text + "</p>"); htmlBody.ChildNodes.Add(para); continue; } Table table = current as Table; if (table != null) { HtmlNode tableNode = args.HtmlDoc.CreateElement("table"); HtmlNode tableBody = args.HtmlDoc.CreateElement("tbody"); tableNode.ChildNodes.Add(tableBody); foreach (TableRow row in table.Elements <TableRow>()) { HtmlNode tableRow = args.HtmlDoc.CreateElement("tr"); tableBody.ChildNodes.Add(tableRow); foreach (TableCell cell in row.Elements <TableCell>()) { HtmlNode tableCell = args.HtmlDoc.CreateElement("td"); tableRow.ChildNodes.Add(tableCell); foreach (Paragraph para in cell.Elements <Paragraph>()) { //TODO: process runs? tableCell.ChildNodes.Add(HtmlNode.CreateNode(para.InnerText)); } } } htmlBody.ChildNodes.Add(tableNode); continue; } // ignore these types of elements if (current is SectionProperties || current is BookmarkEnd) { continue; } throw new Exception("Unknown element: " + current.GetType()); } }
public override void Process(RenderWordArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.WordDoc, "args.WordDoc"); args.Title = args.WordDoc.PackageProperties.Title; }