Exemplo n.º 1
0
        public Manifest Process(Manifest manifest, string outputFolder)
        {
            if (manifest == null)
            {
                throw new ArgumentNullException(nameof(manifest));
            }
            if (outputFolder == null)
            {
                throw new ArgumentNullException(nameof(outputFolder));
            }
            var context = HtmlPostProcessContext.Load(PostProcessorHost);

            foreach (var handler in Handlers)
            {
                handler.SetContext(context);
                manifest = handler.PreHandleWithScopeWrapper(manifest);
            }
            foreach (var tuple in from item in manifest.Files ?? Enumerable.Empty <ManifestItem>()
                     from output in item.OutputFiles
                     where output.Key.Equals(".html", StringComparison.OrdinalIgnoreCase)
                     select new
            {
                Item = item,
                InputFile = item.SourceRelativePath,
                OutputFile = output.Value.RelativePath,
            })
            {
                if (!EnvironmentContext.FileAbstractLayer.Exists(tuple.OutputFile))
                {
                    continue;
                }
                var document = new HtmlDocument();
                try
                {
                    using (var stream = EnvironmentContext.FileAbstractLayer.OpenRead(tuple.OutputFile))
                    {
                        document.Load(stream, Encoding.UTF8);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogWarning($"Warning: Can't load content from {tuple.OutputFile}: {ex.Message}");
                    continue;
                }
                foreach (var handler in Handlers)
                {
                    handler.HandleWithScopeWrapper(document, tuple.Item, tuple.InputFile, tuple.OutputFile);
                }
                using (var stream = EnvironmentContext.FileAbstractLayer.Create(tuple.OutputFile))
                {
                    document.Save(stream, Encoding.UTF8);
                }
            }
            foreach (var handler in Handlers)
            {
                manifest = handler.PostHandleWithScopeWrapper(manifest);
            }
            context.Save();
            return(manifest);
        }
Exemplo n.º 2
0
 public static HtmlPostProcessContext Load(IPostProcessorHost host)
 {
     using (var stream = host?.LoadContextInfo())
     {
         if (stream == null || host?.IsIncremental == false)
         {
             var context = new HtmlPostProcessContext();
             context.PostProcessorHost = host;
             return(context);
         }
         using (var sr = new StreamReader(stream))
         {
             var context = JsonUtility.Deserialize <HtmlPostProcessContext>(sr);
             context.PostProcessorHost = host;
             var totalSrcFileSet = new HashSet <string>(host.SourceFileInfos.Select(s => s.SourceRelativePath));
             context.FileMapping = new OSPlatformSensitiveDictionary <string>(
                 context.FileMapping
                 .Where(p => totalSrcFileSet.Contains(p.Value))
                 .ToDictionary(p => p.Key, p => p.Value));
             context.Bookmarks = new OSPlatformSensitiveDictionary <HashSet <string> >(
                 context.Bookmarks
                 .Where(p => context.FileMapping.ContainsKey(p.Key))
                 .ToDictionary(p => p.Key, p => p.Value));
             return(context);
         }
     }
 }
Exemplo n.º 3
0
        public override void LoadContext(HtmlPostProcessContext context)
        {
            if (context.PostProcessorHost?.IsIncremental != true)
            {
                return;
            }
            var fileMapping         = Deserialize <string>(context, nameof(_fileMapping)) ?? new OSPlatformSensitiveDictionary <string>();
            var registeredBookmarks = Deserialize <HashSet <string> >(context, nameof(_registeredBookmarks)) ?? new OSPlatformSensitiveDictionary <HashSet <string> >();
            var set = new HashSet <string>(
                from sfi in context.PostProcessorHost.SourceFileInfos
                where sfi.IsIncremental
                select sfi.SourceRelativePath,
                FilePathComparer.OSPlatformSensitiveStringComparer);

            foreach (var pair in fileMapping)
            {
                if (set.Contains(pair.Value))
                {
                    _fileMapping[pair.Key] = pair.Value;
                }
            }
            foreach (var pair in registeredBookmarks)
            {
                if (set.Contains(fileMapping[pair.Key]))
                {
                    _registeredBookmarks[pair.Key] = pair.Value;
                }
            }
        }
Exemplo n.º 4
0
 private static OSPlatformSensitiveDictionary <T> Deserialize <T>(HtmlPostProcessContext context, string name)
     where T : class
 {
     return(context.Load(
                name,
                stream =>
     {
         using var sr = new StreamReader(stream);
         return JsonUtility.Deserialize <OSPlatformSensitiveDictionary <T> >(sr);
     }));
 }
Exemplo n.º 5
0
        public override void LoadContext(HtmlPostProcessContext context)
        {
            if (context.PostProcessorHost?.IsIncremental != true)
            {
                return;
            }
            var fileMapping = Deserialize <string>(context, nameof(_fileMapping));

            if (fileMapping == null)
            {
                throw new BuildCacheException("File mappings are not found in html post processor.");
            }
            var registeredBookmarks = Deserialize <HashSet <string> >(context, nameof(_registeredBookmarks));

            if (registeredBookmarks == null)
            {
                throw new BuildCacheException("Registered bookmarks are not found in html post processor.");
            }
            var set = new HashSet <string>(
                from sfi in context.PostProcessorHost.SourceFileInfos
                where sfi.IsIncremental
                select sfi.SourceRelativePath,
                FilePathComparer.OSPlatformSensitiveStringComparer);

            foreach (var pair in fileMapping)
            {
                if (set.Contains(pair.Value))
                {
                    _fileMapping[pair.Key] = pair.Value;
                }
            }
            foreach (var pair in registeredBookmarks)
            {
                if (set.Contains(fileMapping[pair.Key]))
                {
                    _registeredBookmarks[pair.Key] = pair.Value;
                }
            }
        }
Exemplo n.º 6
0
 public override void SaveContext(HtmlPostProcessContext context)
 {
     context.Save(nameof(_registeredBookmarks), stream => Serialize(stream, _registeredBookmarks));
     context.Save(nameof(_fileMapping), stream => Serialize(stream, _fileMapping));
 }
Exemplo n.º 7
0
 public void SetContext(HtmlPostProcessContext context)
 {
     Context = context;
 }
Exemplo n.º 8
0
 public virtual void SaveContext(HtmlPostProcessContext context)
 {
 }
Exemplo n.º 9
0
 public virtual void LoadContext(HtmlPostProcessContext context)
 {
 }