Class: Archaeo::LocalRewriter
- Inherits:
-
Object
- Object
- Archaeo::LocalRewriter
- Defined in:
- lib/archaeo/local_rewriter.rb
Overview
Rewrites previously downloaded files by converting archive URLs to local paths. Operates on files already on disk without fetching.
Instance Method Summary collapse
-
#initialize(prefix: "local", rewrite_js: false, rewrite_absolute: false) ⇒ LocalRewriter
constructor
A new instance of LocalRewriter.
- #rewrite_directory(input_dir, output_dir) ⇒ Object
- #rewrite_file(input_path, output_path) ⇒ Object
Constructor Details
#initialize(prefix: "local", rewrite_js: false, rewrite_absolute: false) ⇒ LocalRewriter
Returns a new instance of LocalRewriter.
14 15 16 17 18 19 |
# File 'lib/archaeo/local_rewriter.rb', line 14 def initialize(prefix: "local", rewrite_js: false, rewrite_absolute: false) @prefix = prefix @rewrite_js = rewrite_js @rewrite_absolute = rewrite_absolute end |
Instance Method Details
#rewrite_directory(input_dir, output_dir) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/archaeo/local_rewriter.rb', line 21 def rewrite_directory(input_dir, output_dir) start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) files = gather_files(input_dir) rewritten = 0 skipped = 0 files.each do |path| rel = path.sub(%r{\A#{Regexp.escape(input_dir)}/?}, "") out_path = File.join(output_dir, rel) result = rewrite_file(path, out_path) result ? rewritten += 1 : skipped += 1 end elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time LocalRewriteSummary.new( total: files.size, rewritten: rewritten, skipped: skipped, elapsed: elapsed ) end |
#rewrite_file(input_path, output_path) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/archaeo/local_rewriter.rb', line 42 def rewrite_file(input_path, output_path) content = File.read(input_path) return nil unless rewrite_candidate?(content) FileUtils.mkdir_p(File.dirname(output_path)) rewriter = build_rewriter rewritten = apply_rewriting(rewriter, content, input_path) File.write(output_path, rewritten) true end |