Module: Scene7ize
- Defined in:
- lib/scene7ize.rb,
lib/scene7ize/cli.rb,
lib/scene7ize/version.rb
Defined Under Namespace
Classes: CLI
Constant Summary collapse
- DEFAULT_REGEX =
DEFAULT_REGEX = /(?<=[(‘“])(?<dir_and_basename>((?!).)*).(?<ext>gif|jpg|jpeg|png)(?=)/i
/(?<dir_and_basename>((?!['"\)\(]).)*)\.(?<ext>gif|jpg|jpeg|png)(?=['"\)])/i
- VERSION =
"0.2.7"
Class Attribute Summary collapse
-
.scene7prefix ⇒ Object
Returns the value of attribute scene7prefix.
Class Method Summary collapse
- .parse_file(scene7prefix, input_file, output_file = nil, base_path = nil) ⇒ Object
- .replace(content) ⇒ Object
- .scene7url_from(image_filename) ⇒ Object
Class Attribute Details
.scene7prefix ⇒ Object
Returns the value of attribute scene7prefix.
10 11 12 |
# File 'lib/scene7ize.rb', line 10 def scene7prefix @scene7prefix end |
Class Method Details
.parse_file(scene7prefix, input_file, output_file = nil, base_path = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/scene7ize.rb', line 54 def self.parse_file(scene7prefix, input_file, output_file = nil, base_path = nil) @scene7prefix = scene7prefix file_content = File.read(input_file) @input_file_path = File.dirname(input_file) @base_path = File.path(base_path) if base_path replacement = replace(file_content) output_file = input_file if output_file.nil? File.open(output_file, "w") { |file| file.puts replacement } end |
.replace(content) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/scene7ize.rb', line 40 def self.replace(content) replacement = content.gsub(DEFAULT_REGEX) do |match| original_filename = "#{$~[:dir_and_basename]}.#{$~[:ext]}" puts "matching #{original_filename}" # reconstruct image path relative to input file and open image_filename = File.join(@base_path || @input_file_path, original_filename) self.scene7url_from(image_filename) || original_filename end end |
.scene7url_from(image_filename) ⇒ Object
12 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 |
# File 'lib/scene7ize.rb', line 12 def self.scene7url_from(image_filename) # TODO: error handling if !File.exists?(image_filename) puts "ERROR: Could not open #{image_filename}. Leaving unchanged." return false end image = MiniMagick::Image.open(image_filename) suffix = case image[:format].downcase when 'jpg', 'jpeg' '&qlt=100' when 'png' '&fmt=png-alpha' when 'gif' '&fmt=gif-alpha' else "" end basename = File.basename(image_filename, File.extname(image_filename)) # TODO: error handle URI::InvalidURIError URI.join(self.scene7prefix, "#{basename}?wid=#{image[:width]}&hei=#{image[:height]}#{suffix}").to_s end |