public string RetrieveListItem(XElement paragraph, string bulletReplacementString)
 {
     return(ListItemRetriever.RetrieveListItem(this, paragraph, bulletReplacementString));
 }
Exemplo n.º 2
0
        private static object ConvertToHtmlTransform(WordprocessingDocument wordDoc,
                                                     HtmlConverterSettings settings, XNode node,
                                                     Func <ImageInfo, XElement> imageHandler)
        {
            XElement element = node as XElement;

            if (element != null)
            {
                if (element.Name == W.document)
                {
                    return(new XElement(Xhtml.html,
                                        new XElement(Xhtml.head,
                                                     new XElement(Xhtml.meta,
                                                                  new XAttribute(HtmlNoNamespace.http_equiv, "Content-Type"),
                                                                  new XAttribute(HtmlNoNamespace.content,
                                                                                 "text/html; charset=windows-1252")),
                                                     new XElement(Xhtml.meta,
                                                                  new XAttribute(HtmlNoNamespace.name, "Generator"),
                                                                  new XAttribute(HtmlNoNamespace.content,
                                                                                 "PowerTools for Open XML")),
                                                     settings.PageTitle != null ? new XElement(Xhtml.title,
                                                                                               settings.PageTitle) : null,
                                                     settings.Css != null ? new XElement(Xhtml.style,
                                                                                         new XComment(Environment.NewLine +
                                                                                                      settings.Css + Environment.NewLine)) : null
                                                     ),
                                        element.Elements().Select(e => ConvertToHtmlTransform(
                                                                      wordDoc, settings, e, imageHandler))
                                        ));
                }

                // Transform the w:body element to the XHTML h:body element.
                if (element.Name == W.body)
                {
                    return(new XElement(Xhtml.body,
                                        element.Elements().Select(e => ConvertToHtmlTransform(
                                                                      wordDoc, settings, e, imageHandler))));
                }

                // Transform every paragraph with a style that has paragraph properties
                // that has an outline level into the same level of heading.  This takes
                // care of transforming headings of every level.
                if (element.Name == W.p)
                {
                    string styleId = (string)element.Elements(W.pPr).Elements(W.pStyle)
                                     .Attributes(W.val).FirstOrDefault();
                    XElement style = wordDoc.MainDocumentPart.StyleDefinitionsPart
                                     .GetXDocument().Root.Elements(W.style)
                                     .Where(s => (string)s.Attribute(W.styleId) == styleId)
                                     .FirstOrDefault();
                    if (style != null)
                    {
                        int?outlineLevel = (int?)style.Elements(W.pPr)
                                           .Elements(W.outlineLvl).Attributes(W.val).FirstOrDefault();
                        if (outlineLevel != null)
                        {
                            return(new XElement(Xhtml.xhtml + string.Format("h{0}",
                                                                            outlineLevel + 1),
                                                settings.CssClassPrefix != null ?
                                                new XAttribute(HtmlNoNamespace._class,
                                                               settings.CssClassPrefix + styleId) : null,
                                                ConvertEntities(ListItemRetriever.RetrieveListItem(wordDoc,
                                                                                                   element, null)),
                                                element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                                      settings, e, imageHandler))));
                        }
                    }
                }

                // Transform w:p to h:p.
                if (element.Name == W.p)
                {
                    string styleId = (string)element.Elements(W.pPr).Elements(W.pStyle)
                                     .Attributes(W.val).FirstOrDefault();
                    if (styleId == null)
                    {
                        styleId = (string)wordDoc.MainDocumentPart.StyleDefinitionsPart
                                  .GetXDocument().Root.Elements(W.style)
                                  .Where(e => (string)e.Attribute(W.type) == "paragraph" &&
                                         (string)e.Attribute(W._default) == "1")
                                  .FirstOrDefault().Attributes(W.styleId).FirstOrDefault();
                    }
                    XElement z = new XElement(Xhtml.p,
                                              styleId != null ? (
                                                  settings.CssClassPrefix != null ?
                                                  new XAttribute(HtmlNoNamespace._class,
                                                                 settings.CssClassPrefix + styleId) : null
                                                  ) : null,
                                              ConvertEntities(ListItemRetriever.RetrieveListItem(wordDoc,
                                                                                                 element, null)),
                                              element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                                    settings, e, imageHandler)));
                    return(z);
                }

                // Transform every hyperlink in the document to the XHTML h:A element.
                if (element.Name == W.hyperlink && element.Attribute(R.id) != null)
                {
                    try
                    {
                        return(new XElement(Xhtml.A,
                                            new XAttribute(HtmlNoNamespace.href,
                                                           wordDoc.MainDocumentPart
                                                           .HyperlinkRelationships
                                                           .Where(x => x.Id == (string)element.Attribute(R.id))
                                                           .First()
                                                           .Uri
                                                           ),
                                            ConvertEntities(element.Elements(W.r)
                                                            .Elements(W.t)
                                                            .Select(s => (string)s).StringConcatenate())
                                            ));
                    }
                    catch (UriFormatException)
                    {
                        return(element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                     settings, e, imageHandler)));
                    }
                }

                // Transform contents of runs that are part of a hyperlink.
                if (element.Name == W.r &&
                    element.Annotation <FieldInfo>() != null &&
                    element.Annotation <FieldInfo>().Arguments.Length > 0)
                {
                    FieldInfo fieldInfo = element.Annotation <FieldInfo>();
                    return(new XElement(Xhtml.A,
                                        new XAttribute(HtmlNoNamespace.href, fieldInfo.Arguments[0]),
                                        ConvertEntities(element.Elements(W.t)
                                                        .Select(s => (string)s).StringConcatenate())
                                        ));
                }

                // Transform contents of runs.
                if (element.Name == W.r)
                {
                    return(element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                 settings, e, imageHandler)));
                }

                // Transform every w:t element to a text node.
                if (element.Name == W.t)
                {
                    return(ConvertEntities(element.Value));
                }

                // Transform w:br to h:br.
                if (element.Name == W.br || element.Name == W.cr)
                {
                    return(new XElement(Xhtml.br));
                }

                // Transform w:noBreakHyphen to '-'
                if (element.Name == W.noBreakHyphen)
                {
                    return(new XText("-"));
                }

                // Transform w:tbl to h:tbl.
                if (element.Name == W.tbl)
                {
                    return(new XElement(Xhtml.table,
                                        new XAttribute(HtmlNoNamespace.border, 1),
                                        element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                              settings, e, imageHandler))));
                }

                // Transform w:tr to h:tr.
                if (element.Name == W.tr)
                {
                    return(new XElement(Xhtml.tr,
                                        element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                              settings, e, imageHandler))));
                }

                // Transform w:tc to h:td.
                if (element.Name == W.tc)
                {
                    return(new XElement(Xhtml.td,
                                        element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                                                                                              settings, e, imageHandler))));
                }

                // Transform images.
                if (element.Name == W.drawing || element.Name == W.pict)
                {
                    if (imageHandler == null)
                    {
                        return(null);
                    }
                    return(ProcessImage(wordDoc, element, imageHandler));
                }

                // The following removes any nodes that haven't been transformed.
                return(null);
            }
            return(null);
        }