public void Test_Analyze() { var pageLinkAnalyzer = new PageLinkAnalyzer(); var pageHtml = @"<html> <head> </head> <body> <div> <a href=""page1"">page1</a> <a href=""/page2"">page2</a> <a href=""http://www.kooboo.com"">outsidelink</a> <a href=""#"">emptyclick</a> </div> </body> </html>"; SiteDownloadContext siteDownloadContext = new SiteDownloadContext(null, new DownloadOptions() { SiteName = "Test_Analyze", Url = "localhost", Pages = 20, Deep = 1 }, null, null); PageDownloadContext pageDownloadContext = new PageDownloadContext(siteDownloadContext, new PageLevel("http://localhost", 1), pageHtml); pageLinkAnalyzer.Analyze(pageDownloadContext); Assert.AreEqual(2, siteDownloadContext.DownloadQueue.Count); }
public void Test_Analyze() { var httpClientMock = new Mock<IHttpClient>(); httpClientMock.Setup(it => it.DownloadString(It.IsAny<string>())) .Returns<string>((url) => { return "script"; }); var siteFileProviderMock = new Mock<ISiteFileProvider>(); Dictionary<string, string> styles = new Dictionary<string, string>(); siteFileProviderMock.Setup(it => it.AddFile(It.IsAny<Site>(), It.IsAny<string>(), It.IsAny<string>())) .Callback<Site, string, string>((site1, path, content) => { styles.Add(path, content); }); var scriptAnalyzer = new ScriptAnalyzer(httpClientMock.Object, siteFileProviderMock.Object); var pageHtml = @"<html> <head> <script src=""/script1.js"" type=""text/javascript""></script> <script src=""/script2.js"" type=""text/javascript""></script> </head> <body> </body> </html>"; SiteDownloadContext siteDownloadContext = new SiteDownloadContext(null, new DownloadOptions() { SiteName = "Test_Analyze", Url = "localhost", Pages = 20, Deep = 1 }, null, null); PageDownloadContext pageDownloadContext = new PageDownloadContext(siteDownloadContext, new PageLevel("http://localhost", 1), pageHtml); scriptAnalyzer.Analyze(pageDownloadContext); Assert.AreEqual(2, styles.Count()); Assert.AreEqual("script", styles["\\Scripts\\script1.js"]);
private void ApplyCredentials(PageDownloadContext downloadContext, string url) { WinInetCredentialsContext credentialsContext = CreateCredentialsContext(url); if (credentialsContext != null && credentialsContext.CookieString != null) { downloadContext.CookieString = credentialsContext.CookieString.Cookies; } }
private string DownloadTemplateFiles(string templateContents, string templateUrl, IProgressHost progress) { progress.UpdateProgress(Res.Get(StringId.ProgressDownloadingSupportingFiles)); FileBasedSiteStorage files = new FileBasedSiteStorage(_blogTemplateDir); // convert the string to a stream MemoryStream templateStream = new MemoryStream(); StreamWriter writer = new StreamWriter(templateStream, Encoding.UTF8); writer.Write(templateContents); writer.Flush(); templateStream.Seek(0, SeekOrigin.Begin); //read the stream into a lightweight HTML. Note that we use from LightWeightHTMLDocument.FromIHTMLDocument2 //instead of LightWeightHTMLDocument.FromStream because from stream improperly shoves a saveFrom declaration //above the docType (bug 289357) IHTMLDocument2 doc = HTMLDocumentHelper.StreamToHTMLDoc(templateStream, templateUrl, true); LightWeightHTMLDocument ldoc = LightWeightHTMLDocument.FromIHTMLDocument2(doc, templateUrl, true, false); PageDownloadContext downloadContext = new PageDownloadContext(0); ApplyCredentials(downloadContext, templateUrl); using (PageToDownloadFactory downloadFactory = new PageToDownloadFactory(ldoc, downloadContext, _parentControl)) { //calculate the dependent styles and resources ProgressTick tick = new ProgressTick(progress, 50, 100); downloadFactory.CreatePagesToDownload(tick); tick.UpdateProgress(100, 100); //download the dependent styles and resources tick = new ProgressTick(progress, 50, 100); PageAndReferenceDownloader downloader = new PageAndReferenceDownloader(downloadFactory.PagesToDownload, files); this.ApplyCredentials(downloader, templateUrl); downloader.Download(tick); tick.UpdateProgress(100, 100); //Expand out the relative paths in the downloaded HTML file with absolute paths. //Note: this is necessary so that template resources are not improperly resolved relative // to the location of the file the editor is editing. string blogTemplateFile = Path.Combine(_blogTemplateDir, files.RootFile); string origFile = blogTemplateFile + ".token"; File.Move(blogTemplateFile, origFile); string absPath = String.Format(CultureInfo.InvariantCulture, "file:///{0}/{1}", _blogTemplateDir.Replace('\\', '/'), downloader.PathToken); TextHelper.ReplaceInFile(origFile, downloader.PathToken, blogTemplateFile, absPath); File.Delete(origFile); //fix up the files FixupDownloadedFiles(blogTemplateFile, files, downloader.PathToken); //complete the progress. progress.UpdateProgress(100, 100); File.WriteAllText(blogTemplateFile + ".path", absPath); return(blogTemplateFile); } }
public void Test_Analyze() { var httpClientMock = new Mock <IHttpClient>(); httpClientMock.Setup(it => it.DownloadString(It.IsAny <string>())) .Returns <string>((url) => { return("stylesheet"); }); var siteFileProviderMock = new Mock <ISiteFileProvider>(); Dictionary <string, string> styles = new Dictionary <string, string>(); siteFileProviderMock.Setup(it => it.AddFile(It.IsAny <Site>(), It.IsAny <string>(), It.IsAny <string>())) .Callback <Site, string, string>((site1, path, content) => { styles.Add(path, content); }); var styleSheetAnalyzer = new StyleSheetAnalyzer(httpClientMock.Object, siteFileProviderMock.Object); var pageHtml = @"<html> <head> <link rel=""Stylesheet"" href=""/style1.css"" type=""text/css"" /> <link rel=""Stylesheet"" href=""/style2"" type=""text/css"" /> </head> <body> </body> </html>"; SiteDownloadContext siteDownloadContext = new SiteDownloadContext(null, new DownloadOptions() { SiteName = "Test_Analyze", Url = "localhost", Pages = 20, Deep = 1 }, null, null); PageDownloadContext pageDownloadContext = new PageDownloadContext(siteDownloadContext, new PageLevel("http://localhost", 1), pageHtml); styleSheetAnalyzer.Analyze(pageDownloadContext); Assert.AreEqual(2, styles.Count()); Assert.AreEqual("stylesheet", styles["\\Styles\\style1.css"]); Assert.AreEqual("stylesheet", styles["\\Styles\\style2"]); }
public void Analyze(PageDownloadContext context) { var links = context.HtmlDocument.DocumentNode.Descendants() .Where(lnks => lnks.Name == "a" && lnks.Attributes["href"] != null && lnks.InnerText.Trim().Length > 0); //.Select(lnks => UriHelper.GetInsideAbsoluteUrl(context.PageUrl, lnks.Attributes["href"].Value)) //.Where(it => !string.IsNullOrEmpty(it)); if (context.PageLevel.Level < context.SiteDownloadContext.Options.Deep) { int count = 0; foreach (var link in links) { var insideUrl = UriHelper.GetInsideAbsoluteUrl(context.PageLevel.Url, link.Attributes["href"].Value); if (!string.IsNullOrEmpty(insideUrl)) { if (count < context.SiteDownloadContext.Options.Pages) { var absolutePath = new Uri(insideUrl).AbsolutePath; link.Attributes["href"].Value = "/" + SiteExtensions.PREFIX_FRONT_PREVIEW_URL + context.SiteDownloadContext.Site.AbsoluteName + absolutePath; var nextPageLevel = new PageLevel(insideUrl, context.PageLevel.Level + 1); if (!new PageLevelComparer().Equals(context.PageLevel, nextPageLevel) && !context.SiteDownloadContext.DownloadedPages.Contains(nextPageLevel, new PageLevelComparer()) && !context.SiteDownloadContext.DownloadQueue.Contains(nextPageLevel, new PageLevelComparer())) { context.SiteDownloadContext.DownloadQueue.Enqueue(nextPageLevel); count++; } } else { link.Attributes["href"].Value = insideUrl; } } } } }