Example #1
0
        /// <summary>
        /// Before calling this, ConfigurationData has to be loaded. E.g., by running ShowConfigurationDialog()
        /// </summary>
        /// <param name="bookPath"></param>
        public void ConfigureBook(string bookPath)
        {
            /* setup jquery in chrome console (first open a local file):
             * script = document.createElement("script");
                script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");

             *
             * Other snippets
             *
             * document.body.appendChild(script);
             *
             * alert(jQuery.parseJSON('{\"message\": \"triscuit\"}').message)
             *
             *
             * alert($().jquery)
             */

            var dom = XmlHtmlConverter.GetXmlDomFromHtmlFile(bookPath, false);
            XmlHtmlConverter.MakeXmlishTagsSafeForInterpretationAsHtml(dom);
            XmlHtmlConverter.SaveDOMAsHtml5(dom, bookPath);

            var b = new GeckoWebBrowser();
            var neededToMakeThingsWork = b.Handle;
            b.Navigate(bookPath);
            Application.DoEvents();

            //Now we call the method which takes that confuration data and adds/removes/updates pages.
            //We have the data as json string, so first we turn it into object for the updateDom's convenience.
            RunJavaScript(b,"updateDom(jQuery.parseJSON('"+GetAllData()+"'))");
            Application.DoEvents();

            //Ok, so we should have a modified DOM now, which we can save back over the top.

            //nice non-ascii paths kill this, so let's go to a temp file first
            var temp = TempFile.CreateAndGetPathButDontMakeTheFile(); //we don't want to wrap this in using
            b.SaveDocument(temp.Path);
            File.Delete(bookPath);
            File.Move(temp.Path, bookPath);
        }
Example #2
0
        private void ConfigureBookInternal(string bookPath)
        {
            /* setup jquery in chrome console (first open a local file):
             * script = document.createElement("script");
                script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");

             *
             * Other snippets
             *
             * document.body.appendChild(script);
             *
             * alert(jQuery.parseJSON('{\"message\": \"triscuit\"}').message)
             *
             *
             * alert($().jquery)
             */

            var dom = XmlHtmlConverter.GetXmlDomFromHtmlFile(bookPath, false);
            XmlHtmlConverter.MakeXmlishTagsSafeForInterpretationAsHtml(dom);
            XmlHtmlConverter.SaveDOMAsHtml5(dom, bookPath);

            var b = new GeckoWebBrowser();
            var neededToMakeThingsWork = b.Handle;
            NavigateAndWait(b, bookPath);

            //Now we call the method which takes that confuration data and adds/removes/updates pages.
            //We have the data as json string, so first we turn it into object for the updateDom's convenience.
            RunJavaScript(b, "runUpdate(" + GetAllData() + ")");

            //Ok, so we should have a modified DOM now, which we can save back over the top.

            //nice non-ascii paths kill this, so let's go to a temp file first
            var temp = TempFile.CreateAndGetPathButDontMakeTheFile(); //we don't want to wrap this in using
            b.SaveDocument(temp.Path);
            RobustFile.Delete(bookPath);
            RobustFile.Move(temp.Path, bookPath);

            var sanityCheckDom = XmlHtmlConverter.GetXmlDomFromHtmlFile(bookPath, false);

            // Because the Mozilla code loaded the document from a filename initially, and we later save to a
            // different directory, Geckofx45's SaveDocument writes out the stylesheet links as absolute paths
            // using the file:// protocol markup. When we try to open the new file, Mozilla then complains
            // vociferously about security issues, and refuses to access the stylesheets as far as I can tell.
            // Eventually, several of the stylesheets are cleaned up by being added in again, but a couple of
            // them end up with invalid relative paths because they never get re-added.  So let's go through
            // all the stylesheet links here and remove everything except the bare filenames.
            // See https://silbloom.myjetbrains.com/youtrack/issue/BL-3573 for what happens without this fix.
            foreach (System.Xml.XmlElement link in sanityCheckDom.SafeSelectNodes("//link[@rel='stylesheet']"))
            {
                var href = link.GetAttribute("href");
                if (href.StartsWith("file://"))
                    link.SetAttribute("href", Path.GetFileName(href.Replace("file:///", "").Replace("file://", "")));
            }
            XmlHtmlConverter.SaveDOMAsHtml5(sanityCheckDom, bookPath);

            //NB: this check only makes sense for the calendar, which is the only template we've create that
            // uses this class, and there are no other templates on the drawing board that would use it.
            // If/when we use this for something else, this
            //won't work. But by then, we should be using a version of geckofx that can reliably tell us
            //when it is done with the previous navigation.
            if (sanityCheckDom.SafeSelectNodes("//div[contains(@class,'bloom-page')]").Count < 24) //should be 24 pages
            {
                Logger.WriteMinorEvent(RobustFile.ReadAllText(bookPath)); //this will come to us if they report it
                throw new ApplicationException("Malformed Calendar (code assumes only calendar uses the Configurator, and they have at least 24 pages)");
            }

            //NB: we *want* exceptions thrown from the above to make it out.
        }