静态页面生成技术中,把css,js,以及内容中的img标签放在同一个目录下

/// <summary>
/// 处理css样式表的Link标签--并拷贝css文件
/// </summary>
/// <param name="strText">读取到的Html内容</param>
/// <param name="WuliPath">创建好目录后的物理路径</param>
/// <returns>修改了link标签后的Html内容</returns>
public string DealWithLink(string strText, string path)
{
      Regex regLink = new Regex ("<link(?:.*?)href=[\"'](.*?)[\"'](?:.*?)>", RegexOptions.Singleline);
            MatchCollection mcLinks = regLink.Matches(strText);
            for (int i = 0; i < mcLinks.Count; i++)
            {
                Match mcLink = mcLinks[i];//根据集合的索引得到: 表示单个正则表达式匹配的结果的Match类
                string strLink = mcLink.Value;
                Regex regHref = new Regex ("href=[\"'](.*?)[\"']");
                Match mcHref = regHref.Match(strLink);
                string strHref = mcHref.Value;//得到href=""
                int start = strHref.LastIndexOf("/") + 1;//若不加1,连/也截取了
                int count = strHref.LastIndexOf("\"") - start;
                string strNewHref = strHref.Substring(start, count);//得到不带有路径的样式表名称--只有这样,才能实现同一路径下的访问
                string strNewLink = strLink.Replace(strHref, "href=\"" + strNewHref + "\"");//得到处理了路径的Link标签
                strText = strText.Replace(strLink, strNewLink);//让它本身不断地更新
                int startPath = strHref.IndexOf("/",10);//这儿要/,不加1
                //注意:  /*.WEB/我用的是vs2005,这个是网站的相对根路径(10为第一个/和第二个/的中间一个字母的索引即可)
                int length = strHref.LastIndexOf("\"") - startPath;
                string xuniPath = (strHref.Substring(startPath, length)).Insert(0, "~");
                string basePath = Server.MapPath(xuniPath);//得到html内容中引用的样式表所在的物理路径
                if (!File.Exists(path + strNewHref))
                {
                    File.Copy(basePath, path + strNewHref);
                }
            }
            return strText;
        }

posted on 2011-01-08 10:58  杜军(橘仙)  阅读(254)  评论(0编辑  收藏  举报

导航