Esempio n. 1
0
        public XDocument Get(WordprocessingDocument wpdocument, string mediaFile)
        {
            XDocument result = null;;

            lock (this)
            {
                //#warning Plaese, enable cache
                var cacheDisabled = false;
                if (!_innerCache.ContainsKey(mediaFile) || cacheDisabled)
                {
                    result = DocumentFacade.Transform(wpdocument);
                    var stringBuilder = new StringBuilder();
                    using (var writer = new StringWriter(stringBuilder))
                    {
                        result.Save(writer, SaveOptions.DisableFormatting);
                    }
                    _innerCache[mediaFile] = stringBuilder.ToString();
                }
                else
                {
                    using (var reader = new StringReader(_innerCache[mediaFile]))
                    {
                        result = XDocument.Load(reader, LoadOptions.PreserveWhitespace);
                    }
                }
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Return rendered html of media file(docs)
        /// </summary>
        /// <param name="mediaFile"></param>
        /// <returns>XDocument</returns>
        public static XDocument RenderAsDocument(string mediaFile, bool split)
        {
            XDocument document;

            try
            {
                LogTime("Empty");
                using (var stream = DocumentFacade.GetMediaFileStream(mediaFile))
                {
                    using (var wpdocument = WordprocessingDocument.Open(stream, false))
                    {
                        LogTime("using (WordprocessingDocument wpdocument = DocumentFacade.GetWordprocessingDocument(mediaFile))");
                        var documentStyles = new DocumentStyles(wpdocument);
                        LogTime("var documentStyles = new DocumentStyles(wpdocument);");
                        document = DocumentFacade.TransformCached(wpdocument, mediaFile);
                        LogTime("document = DocumentFacade.Transform(wpdocument);");
                        if (split)
                        {
                            document = SplitRenderer.Render(document, documentStyles);
                        }
                        LogTime("document = SplitRenderer.Render(document, documentStyles);");
                        document = ImageRenderer.ResolveImagePath(document, mediaFile);
                        LogTime("document = ImageRenderer.ResolveImagePath(document, mediaFile);");
                        document = ListRenderer.RenderLists(document, documentStyles);
                        LogTime("document = ListRenderer.RenderLists(document, documentStyles);");
                        document = CleanRenderer.Render(document);
                        LogTime("document = CleanRenderer.Render(document);");
                        document = MarkupRenderer.Render(document);
                        LogTime("document = MarkupRenderer.Render(document);");
                        document = LinkRenderer.RenderLinks(document, wpdocument.MainDocumentPart.ExternalRelationships);
                        document = YoutubeRenderer.RenderYoutubeLinks(document);
                    }
                }
            }
            catch (Exception e)
            {
                document = new XDocument(
                    new XElement(Namespaces.Xhtml + "span",
                                 new XAttribute("class", "Error"),
                                 e.Message
                                 )
                    );
                Log.LogError(nameof(DocumentRenderer), e);
            }

            return(document);
        }