Module: ExtractFiles
Constant Summary collapse
- VERSION =
"0.0.3"
- DEFAULT_EXTENSIONS =
%w(png jpeg jpg gif css js)
Instance Method Summary collapse
-
#extract!(io, options = {}) ⇒ Object
Process the input io, or anything that respond to __read__ and return a string.
-
#get!(io, options) ⇒ Object
get! is a wrapper for extract! that returns and array of the matches.
Instance Method Details
#extract!(io, options = {}) ⇒ Object
Process the input io, or anything that respond to __read__ and return a string. options are:
* **extensions**: Array of extensions to look for. Default is DEFAULT_EXTENSIONS
* **base**: Base directory. Image a relative path for a image **bg.png** in a css file inside the **styles** folder. The default output
will say "../images/bg.png". If you say that the base directory is "styles" the output will be "styles/../images/bg.png". Is unset by default.
* ****: If the output is somehing like "sytles/../images/bg.png" will convert that in "images/bg.png". Is enabled by false.
* **full_path**: Show the full_path
If no block is passed, it will output to the stdout throught puts. If a block is given, it will yield each founded entry.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/extract_files.rb', line 23 def extract!(io, = {}) raise "Incorrect format for IO" unless io.respond_to?(:read) = {:extensions => DEFAULT_EXTENSIONS}.merge() ext = "(" + [:extensions].join("|") + ")" re = /([a-z0-9\-_\/\.]*[a-z0-9]*\.#{ext})/i io.read.scan(re).each do |match| path = match[0] path = File.join([:base], path) if [:base] path = simplify_path(path) if [:expand] path = File.(path) if [:full_path] if block_given? yield path else puts path end end end |
#get!(io, options) ⇒ Object
get! is a wrapper for extract! that returns and array of the matches.
46 47 48 49 50 |
# File 'lib/extract_files.rb', line 46 def get!(io,) matches = [] extract!(io,){|match| matches<<match} matches end |