Ejemplo n.º 1
0
 public RhqPackageBuilder(IMapPathResolver mapPathResolver, string webRoot)
 {
     this.mapPathResolver = mapPathResolver;
     this.webRoot         = webRoot;
     ms          = new MemoryStream();
     zipArchive  = new ZipArchive(ms, ZipArchiveMode.Create, true);
     Assets      = new AssetPathCollection();
     StyleAssets = new AssetPathCollection();
 }
Ejemplo n.º 2
0
        private 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       = Zipper.GetStringAsset(asset.OriginalPath, mapPathResolver, webRoot, pagePath);
                    var binaryContent = Encoding.UTF8.GetBytes(content);
                    asset.Content = binaryContent;
                    ExtractAssets(content, "css", asset.OriginalPath, assets);
                }

                foreach (var asset in assets.Assets.Where(a => a.AssetType != "css"))
                {
                    var binaryContent = 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 = 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);
        }