eggie5
9/26/2007 2:09:00 AM
On Sep 25, 6:35 pm, unbewusst.s...@weltanschauung.com.invalid (Une
B?vue) wrote:
> eggie5 <egg...@gmail.com> wrote:
> > I'm trying to write a rake task to extract all the script tags out of
> > my html file and save them to an array. How can I do this?
>
> Is that a solution 4 u ??? :
>
> #! /usr/bin/env ruby
>
> html = ' <!--scripts-->
>
> <script type="text/javascript"
> src="/javascripts/prototype.js">
> </script>
>
> <script type="text/javascript"
> src="management/javascripts/management.js">
> </script>
>
> <script src="/javascripts/scriptaculous.js"
> type="text/javascript"></script>
>
> <script src="/javascripts/effects.js"
> type="text/javascript"></script>
>
> <script src="/javascripts/controls.js"
> type="text/javascript"></script>
>
> <!--endscripts-->
> '
> js = []
> html.each {|l|
> js << l.chomp.gsub(/.* src="(.*[^ ])"[ >].*/, '\1').gsub(/(.*)"
> type=.*/, '\1') if /<script / === l}
>
> p js
>
> gives :
> RubyMate r6354 running Ruby r1.8.6 (/opt/local/bin/ruby)
>
> >>> extract_js.rb
>
> ["/javascripts/prototype.js", "management/javascripts/management.js",
> "/javascripts/scriptaculous.js", "/javascripts/effects.js",
> "/javascripts/controls.js"]
>
> on Mac OS X 10.4.10
>
> i didn't found a solution with only one gsub...
> sure it exits :[
> --
> Une B?vue
Thank you so must for your effort. This is much more succinct than
what I came up with!
File.open("app/views/layouts/management.rhtml", "r") do |infile|
file_text=""
while (line = infile.gets)
file_text << line
end
script_block=file_text.match("<!--scripts-->[\\S\\s]*?<!--
endscripts-->")
script_block=script_block.to_s
script_refs=script_block.scan(/[^\"]+.js/)
script_refs.length
script_refs.each do |ref|
base_path = "public/"
puts "#{base_path}#{ref}"
end
end