string sitemapHtml()
        {
            string sitemap          = "";
            string baseDirectory    = PathInfo.EliminateFileName(htmlFileList[0].Uri.AbsoluteUri);
            string currentDirectory = PathInfo.EliminateFileName(htmlFileList[0].Uri.AbsoluteUri);
            int    baseDepth        = htmlFileList[0].CountSlashesInAbsoluteUrl();
            int    currentDepth     = baseDepth;

            sitemap += "<ul>" + Environment.NewLine + "<li>" + currentDirectory + "<br />";

            sitemap += GenerateAnchorElement(htmlFileList[0], PageTitle) + "<br />" + Environment.NewLine;

            for (int i = 1; i < htmlFileList.Count; i++)
            {
                if (PathInfo.EliminateFileName(htmlFileList[i].Uri.AbsoluteUri) != currentDirectory)
                {
                    currentDirectory = PathInfo.EliminateFileName(htmlFileList[i].Uri.AbsoluteUri);

                    int diff;
                    if ((diff = htmlFileList[i].CountSlashesInAbsoluteUrl() - currentDepth) > 0)
                    {
                        for (int j = 0; j < diff; j++)
                        {
                            sitemap += "<ul>" + Environment.NewLine;
                            currentDepth++;
                        }
                        sitemap += "<li>" + HttpUtility.HtmlEncode(currentDirectory.Replace(baseDirectory, "")) + "<br />";
                    }
                    else if ((diff = htmlFileList[i].CountSlashesInAbsoluteUrl() - currentDepth) < 0)
                    {
                        sitemap += "</li>" + Environment.NewLine;
                        for (int j = 0; j > diff; j--)
                        {
                            sitemap += "</ul>" + Environment.NewLine + "</li>" + Environment.NewLine;
                            currentDepth--;
                        }
                        sitemap += "<li>" + HttpUtility.HtmlEncode(currentDirectory.Replace(baseDirectory, "")) + "<br />";
                    }
                    else
                    {
                        sitemap += "</li>" + Environment.NewLine;
                        sitemap += "</ul>" + Environment.NewLine;
                        sitemap += "<ul>" + Environment.NewLine;
                        sitemap += "<li>" + HttpUtility.HtmlEncode(currentDirectory.Replace(baseDirectory, "")) + "<br />" + Environment.NewLine;
                    }
                }

                sitemap += GenerateAnchorElement(htmlFileList[i], PageTitle) + "<br />" + Environment.NewLine;
            }

            for (; currentDepth >= baseDepth; currentDepth--)
            {
                sitemap += "</li>" + Environment.NewLine + "</ul>" + Environment.NewLine;
            }

            return(sitemap);
        }
        private void ListUpUrls(Uri baseUri)
        {
            Queue <Uri> pending = new Queue <Uri>();

            pending.Enqueue(baseUri);

            while (pending.Count > 0)
            {
                Uri      currentUri = pending.Dequeue();
                HtmlFile file       = null;

                bw.ReportProgress(0, Resources.String3 + Environment.NewLine + currentUri.AbsolutePath);
                CheckCanceledOrNot();

                try
                {
                    file = new HtmlFile(currentUri);
                    file.Fetch(FileEncoding, LastModification == LastModification.Server && GenerateXmlFile, FromLocalFiles, LocalPathInfo);

                    if (LastModification == LastModification.Specified)
                    {
                        file.SavedDate = this.SpecifiedDate;
                    }
                    htmlFileList.Add(file);

                    foreach (Uri newUri in file.GetLinkUrls())
                    {
                        if (!htmlFileList.Exists(f => f.Uri.AbsoluteUri == newUri.AbsoluteUri) &&
                            !erroredFileUris.Exists(u => u.AbsoluteUri == newUri.AbsoluteUri) &&
                            !pending.Contains(newUri) &&
                            !MatchPattern(newUri.AbsolutePath, this.ExcludedPatternList) &&
                            newUri.AbsoluteUri.Contains(PathInfo.EliminateFileName(this.baseUrl.AbsoluteUri)))
                        {
                            pending.Enqueue(newUri);
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    if (IncludeNotFoundFiles && file != null)
                    {
                        htmlFileList.Add(file);
                    }
                    erroredFileUris.Add(currentUri);
                    Logger.AddLog(e, currentUri.AbsolutePath);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Sorts elements of the current object according to URLs.
        /// </summary>
        /// <param name="exceptFirstItem">If <c>true</c>, the first element will not be moved.</param>
        public void SortByUrl(bool exceptFirstItem = false)
        {
            HtmlFile firstItem = null;

            if (exceptFirstItem && this.Count > 0)
            {
                firstItem = this[0];
                this.RemoveAt(0);
            }

            this.Sort((HtmlFile a, HtmlFile b) =>
            {
                // compare directory first so that, e.g.,
                // ["/c.html", "/b/b.html", "/a.html"] is sorted into
                // ["/a.html", "/c.html", "/b/b.html"]
                // instead of ["/a.html", "/b/b.html", "/c.html"]

                string _a = PathInfo.EliminateFileName(a.Uri.AbsoluteUri);
                string _b = PathInfo.EliminateFileName(b.Uri.AbsoluteUri);
                int diff  = string.Compare(_a, _b);

                if (diff != 0)
                {
                    return(diff);
                }
                else
                {
                    return(string.Compare(a.Uri.AbsoluteUri, b.Uri.AbsoluteUri));
                }
            });

            if (exceptFirstItem && firstItem != null)
            {
                this.Insert(0, firstItem);
            }
        }