public void RelinkEx( [Argument("site", "s", Description = "The root http address of the website copy.")] string site, [Argument("expression", "e", Description = "A regular expression to match against the links.")] string expression, [Argument("target", "t", Description = "The new link, use {0} to insert matched capture groups by ordinal.")] string targetLink) { Regex exp = new Regex(expression, RegexOptions.Singleline); using (ContentStorage storage = new ContentStorage(StoragePath(site), false)) { ContentParser parser = new ContentParser(storage, new Uri(site, UriKind.Absolute)); parser.RewriteUri += u => { Match match = exp.Match(u.OriginalString); if (match.Success) { string newLink = String.Format( targetLink, match.Groups.Cast <Group>() .Select(g => g.Value) .Cast <object>().ToArray() ); return(new Uri(newLink, UriKind.Absolute)); } return(u); }; parser.ProcessAll(); } }
public void Relink( [Argument("site", "s", Description = "The root http address of the website copy.")] string site, [Argument("from", "f", Description = "The original link you want to change.")] string sourceLink, [Argument("target", "t", Description = "The new link you want to use instead.")] string targetLink) { Uri sourceUri = new Uri(sourceLink, UriKind.Absolute); Uri targetUri = new Uri(targetLink, UriKind.Absolute); using (ContentStorage storage = new ContentStorage(StoragePath(site), false)) { ContentParser parser = new ContentParser(storage, new Uri(site, UriKind.Absolute)); parser.RewriteUri += u => { if (sourceUri == u) { return(targetUri); } return(u); }; parser.ProcessAll(); } }
private int CountLinks(string directory, Predicate <Uri> test) { using (ContentStorage store = new ContentStorage(directory, true)) { int counter = 0; ContentParser parser = new ContentParser(store, W3ExampleUrl); parser.VisitUri += u => { if (test(u)) { counter++; } }; parser.ProcessAll(); return(counter); } }
public void LinkSource( [Argument("site", "s", Description = "The root http address of the website copy.")] string site, [Argument("link", "l", Description = "The target link to search for.")] string link) { using (ContentStorage storage = new ContentStorage(StoragePath(site), true)) { ContentParser parser = new ContentParser(storage, new Uri(site, UriKind.Absolute)); ContentRecord rec = ContentRecord.DefaultInstance; parser.ContextChanged += r => rec = r; parser.VisitUri += u => { if (StringComparer.Ordinal.Equals(link, u.OriginalString)) { Console.WriteLine(rec.ContentUri); } }; parser.ProcessAll((r, b) => { }); } }
public void Deduplicate( [Argument("site", "s", Description = "The root http address of the website copy.")] string site, [Argument("remove", "r", DefaultValue = false, Description = "True to remove the page and modify source links, otherwise inserts a redirect.")] bool remove, [Argument("noprompt", "q", DefaultValue = false, Description = "True to stop prompt for confirmation before changing content.")] bool noPrompt) { using (ContentStorage store = new ContentStorage(StoragePath(site), false)) { Dictionary <string, string> replacements = new Dictionary <string, string>(StringComparer.Ordinal); Dictionary <string, string> hashes = new Dictionary <string, string>(StringComparer.Ordinal); foreach (KeyValuePair <string, ContentRecord> item in store) { if (item.Value.HasHashContents) { string original; if (hashes.TryGetValue(item.Value.HashContents, out original)) { replacements[item.Key] = original; Console.WriteLine("{0,-38} => {1,-38}", item.Key, original); } else { hashes.Add(item.Value.HashContents, item.Key); } } } if (replacements.Count > 0 && (noPrompt || new ConfirmPrompt().Continue("Replace all of the above links"))) { Uri baseUri = new Uri(site, UriKind.Absolute); if (remove) { ContentParser parser = new ContentParser(store, baseUri); parser.RewriteUri += u => { string target; if (u.IsSameHost(baseUri) && replacements.TryGetValue(u.NormalizedPathAndQuery(), out target)) { return(new Uri(baseUri, target)); } return(u); }; parser.ProcessAll(); } foreach (string removed in replacements.Keys) { ContentRecord rec = store[removed]; store.Remove(removed); if (!remove) { ContentRecord.Builder builder = rec.ToBuilder(); builder .ClearCompressedLength() .ClearContentLength() .ClearContentStoreId() .ClearContentType() .ClearHashContents() .SetHttpStatus((uint)HttpStatusCode.Redirect) .SetContentRedirect(replacements[removed]) ; store.Add(removed, builder.Build()); } } } } }
public void Links( [Argument("site", "s", Description = "The root http address of the website copy.")] string site, [Argument("verbose", "v", DefaultValue = true, Description = "Display detailed information about each link.")] bool details, [Argument("validate", DefaultValue = false, Description = "Check each link and print the status information.")] bool validate, [Argument("internal", "i", DefaultValue = true, Description = "Set to false or 0 to omit internal links.")] bool showInternal, [Argument("external", "e", DefaultValue = true, Description = "Set to false or 0 to omit external links.")] bool showExternal) { if (details) { Console.WriteLine("Count Target"); } using (ContentStorage storage = new ContentStorage(StoragePath(site), true)) { Uri baseUri = new Uri(site, UriKind.Absolute); Dictionary <string, int> links = new Dictionary <string, int>(StringComparer.Ordinal); ContentParser parser = new ContentParser(storage, baseUri); parser.VisitUri += u => { int count; if ((!showInternal && u.IsSameHost(baseUri)) || (!showExternal && !u.IsSameHost(baseUri))) { return; } links.TryGetValue(u.AbsoluteUri, out count); links[u.AbsoluteUri] = count + 1; }; parser.ProcessAll((r, b) => { }); List <string> keys = new List <string>(links.Keys); keys.Sort(); foreach (string key in keys) { if (validate) { Uri fetch = new Uri(key, UriKind.Absolute); HttpStatusCode result; if (fetch.IsSameHost(baseUri)) { ContentRecord rec; result = storage.TryGetValue(fetch.NormalizedPathAndQuery(), out rec) ? (HttpStatusCode)rec.HttpStatus : HttpStatusCode.NotFound; } else { result = new HttpRequestUtil(fetch).Head(fetch.PathAndQuery); } Console.WriteLine("{0,3} {1,-20} {2}", (int)result, result, fetch); } else if (details) { Console.WriteLine("{0,5:n0} {1}", links[key], key); } else { Console.WriteLine("{0}", key); } } } }
private int CountLinks(string directory, Predicate<Uri> test) { using (ContentStorage store = new ContentStorage(directory, true)) { int counter = 0; ContentParser parser = new ContentParser(store, W3ExampleUrl); parser.VisitUri += u => { if (test(u)) counter++; }; parser.ProcessAll(); return counter; } }
public void Export(string directory) { Dictionary<string, string> renamed = GetFriendlyNames(); Uri location = new Uri(directory, UriKind.Absolute); ContentParser parser = new ContentParser(_content, _baseUri); parser.RewriteElement += e => { return e; }; parser.RewriteUri += uri => { string rename; if (uri.IsSameHost(_baseUri)) { if (renamed.TryGetValue(uri.NormalizedPathAndQuery(), out rename)) { if (RebaseLinks) return new Uri(location, rename.TrimStart('/', '\\')); else return new Uri(_baseUri, rename.TrimStart('/', '\\')); } } return uri; }; parser.RewriteAll = true; parser.RelativeUri = RebaseLinks; parser.Reformat = Reformat; parser.IndentChars = " "; parser.ProcessAll( (r, b) => { string path; if (renamed.TryGetValue(r.ContentUri, out path)) { string file = Path.Combine(directory, path.Replace('/', '\\').TrimStart('\\')); if (!Directory.Exists(Path.GetDirectoryName(file))) Directory.CreateDirectory(Path.GetDirectoryName(file)); File.WriteAllBytes(file, b); } } ); }