private async Task <AssetPathCollection> ExtractAssets(string source, string suffix, string pagePath, AssetPathCollection assets)
        {
            if (suffix.ToLower().StartsWith("htm"))
            {
                var parser = new AngleSharp.Parser.Html.HtmlParser();
                var doc    = parser.Parse(source);
                var images = doc.Images
                             .Where(x => x.HasAttribute("src"));
                var styles = doc.GetElementsByTagName("link")
                             .Where(l => l.Attributes["rel"].Value.Trim().ToLower() == "stylesheet")
                             .Where(c => c.HasAttribute("href"));
                var scripts = doc.GetElementsByTagName("script")
                              .Where(x => x.HasAttribute("src"));
                assets.AddSerializedAssets(images, "src");
                assets.AddSerializedAssets(scripts, "src");
                assets.AddSerializedAssets(styles, "href");
                foreach (var asset in assets.Assets.Where(a => a.AssetType == "css"))
                {
                    var content = await Zipper.GetStringAsset(asset.OriginalPath, mapPathResolver, webRoot, pagePath);

                    var binaryContent = Encoding.UTF8.GetBytes(content);
                    asset.Content = binaryContent;
                    await ExtractAssets(content, "css", asset.OriginalPath, assets);
                }

                foreach (var asset in assets.Assets.Where(a => a.AssetType != "css"))
                {
                    var binaryContent = await Zipper.GetBinaryAsset(asset.OriginalPath, mapPathResolver, webRoot, pagePath);

                    asset.Content = binaryContent;
                }
            }
            else if (suffix.ToLower() == "css")
            {
                var urls = Zipper.ExtaxtUrlsFromStyle(source);
                foreach (var url in urls)
                {
                    var csslocalPath = await Zipper.ReturnLocalPath(url);

                    var csssuffix = csslocalPath.Split('.').Last();
                    var newUrl    = Guid.NewGuid().ToString().Replace("-", "") + "." + suffix;
                    source = source.Replace(url, newUrl);
                    //assets.AddSerializedAssets()
                    //if (!doneAssets.Contains(newUrl))
                    //{
                    //    zipArchive.AddBinaryAssetToArchive(newUrl, localPath, mapPathResolver, webRoot, serialStyle.Key);
                    //    doneAssets.Add(newUrl);
                    //}
                }
            }
            return(assets);
        }
示例#2
0
        public List <Asset> GetCssAssets(string css)
        {
            var assets = new List <Asset>();
            var urls   = Zipper.ExtaxtUrlsFromStyle(css);

            foreach (var src in urls.Where(s => IsLocalPath(s).Result))
            {
                var suffix = src.Split('.').Last().Split('?').First().Split('#').First();
                var asset  = new Asset
                {
                    Uri    = src,
                    Suffix = suffix,
                    NewUri = Guid.NewGuid().ToString().Replace("-", "")
                };
                assets.Add(asset);
            }

            return(assets);
        }