Module: Asciidoctor::Html::CacheBuster
- Defined in:
- lib/asciidoctor/html/cache_buster.rb
Overview
Postprocess site with cachebusting paths
Defined Under Namespace
Classes: AssetData
Class Method Summary collapse
Class Method Details
.process(outdir) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/asciidoctor/html/cache_buster.rb', line 13 def self.process(outdir) puts "-- Begin cache busting --" puts cache = {} # assetpath => {:filehash,:newpath} glob = ["**/*.html", "*.webmanifest"] Pathname(outdir).glob(glob) do |filepath| puts "Processing" puts " #{filepath}" scanner = StringScanner.new(filepath.read) rgx = %r{"((?<prefix>[\./]*?#{ASSETS_PATH}/.+?)(?<hash>\.|\.[a-f0-9]{8}\.)(?<ext>[a-z]+?))"} buffer = [] while (scanned = scanner.scan_until(rgx)) matched = scanner.matched buffer << scanned[0, scanned.size - matched.size] matchedpath = Pathname(matched[1..-2]) assetpath = filepath.dirname / matchedpath if assetpath.file? assetpath = assetpath.realpath captures = scanner.named_captures cache[assetpath] ||= AssetData.new assetdata = cache[assetpath] assetdata.filehash ||= format("%08x", Zlib.crc32(assetpath.read)) new_hash = assetdata.filehash found_hash = captures["hash"][1..-2] unless captures["hash"] == "." if new_hash == found_hash buffer << matched else new_name = "#{captures["prefix"]}.#{new_hash}.#{captures["ext"]}" buffer << %("#{new_name}") assetdata.newpath = filepath.dirname / Pathname(new_name) end else buffer << matched end end buffer << scanner.rest filepath.write buffer.join end cache.each_pair do |assetpath, assetdata| next unless assetdata.newpath puts puts "Renaming:" puts " #{assetpath}" puts "->#{assetdata.newpath}" assetpath.rename(assetdata.newpath) end puts puts "-- End cache busting --" end |