/// <summary> /// 处理html中的资源src /// </summary> /// <param name="html">html</param> /// <param name="page">page</param> /// <returns>html</returns> public string RePathJsCssImg(string html, BasePage page) { Matches = JsCssImageTest.Matches(html); html = ReplaceLinks(html, page); Matches = CssBgTest.Matches(html); html = ReplaceLinks(html, page); return html; }
private string ReplaceLinks(string html, BasePage page) { GroupCollection gc = null; string src = "", s = ""; int n = 0, d = 0; List<string> r = new List<string>(); for (int i = 0; i < Matches.Count; i += 1) { gc = Matches[i].Groups; src = gc["src"].Value; if (string.IsNullOrEmpty(src) || r.Contains(src)) { continue; } if (src[0] == '/') { continue; } srcDirs = src.Split('/').Where(x => x.Length > 0).ToList(); if (srcDirs[0] == "http:" || srcDirs[0] == "https:") { continue; } n = srcDirs.Where(x => x == "..").Count(); if (n == 0) { html = html.Replace(src, string.Format("{0}{1}/{2}", inputPath, page is LabelPage ? "label" : page is StaticPage ? "static" : page is SubListPage ? "item" : "page" , srcDirs[0].Trim() == "." ? src.Trim().Substring(2) : src.Trim())); } else { if (n == baseDirs.Count()) { html = html.Replace(src, string.Format("/{0}/{1}", baseDirs[0], Regex.Replace(src.Trim(), @"(\.\.\/)+", string.Empty))); } else if (n < baseDirs.Count()) { d = baseDirs.Count() - n; s = "/"; for (int j = 0; j <= n; j += 1) { s += baseDirs[j] + "/"; } html = html.Replace(src, string.Format("{0}{1}", s, Regex.Replace(src.Trim(), @"(\.\.\/)+", string.Empty))); } } r.Add(src); } return html; }