/// <summary>
 /// Detect the background color of a post body from a URI where
 /// the post body element contains BlogEditingTemplate.POST_BODY_MARKER.
 /// This must be done in a browser (as opposed to a simple DOM) because
 /// the page elements don't layout relative to each other unless they
 /// are being displayed in a browser.
 /// </summary>
 public static Color?DetectColor(string uri, Color?defaultColor)
 {
     return(BrowserOperationInvoker.InvokeAfterDocumentComplete(uri, "BackgroundColorDetector", 700, 700, defaultColor,
                                                                delegate(ExplorerBrowserControl browser)
     {
         IHTMLDocument2 document = browser.Document as IHTMLDocument2;
         IHTMLElement[] elements = HTMLDocumentHelper.FindElementsContainingText(document, BlogEditingTemplate.POST_BODY_MARKER);
         if (elements.Length == 1)
         {
             IHTMLElement postBody = elements[0];
             if (postBody.offsetHeight < 300)
             {
                 postBody.style.height = 300;
             }
             return HTMLColorHelper.GetBackgroundColor(postBody, true, null, Color.White);
         }
         return defaultColor;
     }));
 }
Esempio n. 2
0
        private BlogPostRegions ParseBlogPostIntoTemplate(Stream stream, string postSourceUrl, IProgressHost progress)
        {
            progress.UpdateProgress(Res.Get(StringId.ProgressCreatingEditingTemplate));

            //parse the document to create the blog template
            IHTMLDocument2 doc2 = HTMLDocumentHelper.GetHTMLDocumentFromStream(stream, postSourceUrl);
            IHTMLDocument3 doc  = (IHTMLDocument3)doc2;

            IHTMLElement[] titleElements = HTMLDocumentHelper.FindElementsContainingText(doc2, TEMPORARY_POST_TITLE_GUID);

            IHTMLElement bodyElement = HTMLDocumentHelper.FindElementContainingText(doc2, TEMPORARY_POST_BODY_GUID);

            if (bodyElement != null && bodyElement.tagName == "P")
            {
                //the body element is the <p> we planted, so replace it with a DIV since that will be the safest
                //element to have a as parent to all post content.
                IHTMLElement div = doc2.createElement("div");
                (bodyElement.parentElement as IHTMLDOMNode).replaceChild(div as IHTMLDOMNode, bodyElement as IHTMLDOMNode);
                bodyElement = div;
            }

            //locate the title element.  Note that is there are more than 1 copies of the title text detected, we use the one
            //that is anchored closest to the left or the body element.
            if (titleElements.Length > 0)
            {
                BlogPostRegions regions = new BlogPostRegions();
                regions.Document     = (IHTMLDocument)doc;
                regions.TitleRegions = titleElements;
                regions.BodyRegion   = bodyElement;

                progress.UpdateProgress(100, 100);
                return(regions);
            }
            else
            {
                throw new Exception("unable to access test post.");
            }
        }