ruby regex on html file

E

eggie5

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?

Below is a snippet form my file management.rhtml, I would like to get
paths to the script files from all the script tags inside the <!--
scripts--> HTML comment tags.

Expected results are:



/javascripts/prototype.js,
management/javascripts/management.js,
/javascripts/scriptaculous.js,
/javascripts/effects.js,
/javascripts/controls.js



Snippet:

<!--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-->
 
A

Ari Brown

<sigh>
I have no shame....

For something as large and (maybe) complex as this, you might want to
try generating your regexp through TextualRegexp.

gem install TextualRegexp

Good luck
ari

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?

Below is a snippet form my file management.rhtml, I would like to get
paths to the script files from all the script tags inside the <!--
scripts--> HTML comment tags.
---------------------------------------------------------------|
~Ari
"I don't suffer from insanity. I enjoy every minute of it" --1337est
man alive
 
U

Une Bévue

eggie5 said:
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)
["/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 :[
 
E

eggie5

eggie5 said:
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)

["/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 :[

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
 
W

William James

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?

Below is a snippet form my file management.rhtml, I would like to get
paths to the script files from all the script tags inside the <!--
scripts--> HTML comment tags.

Expected results are:

/javascripts/prototype.js,
management/javascripts/management.js,
/javascripts/scriptaculous.js,
/javascripts/effects.js,
/javascripts/controls.js

Snippet:

<!--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-->

puts DATA.read.scan( /<script\s+[^>]*src="(.*?)"/m ).flatten

__END__
<!--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-->
 
U

Une Bévue

eggie5 said:
Thank you so must for your effort. This is much more succinct than
what I came up with!

I found it with only one gsub :

#! /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(/^\s+<script\s+[^>]*src="([^ "]*).*/, '\1') if
/<script / === l
}
p js

gives :

["/javascripts/prototype.js", "management/javascripts/management.js",
"/javascripts/scriptaculous.js", "/javascripts/effects.js",
"/javascripts/controls.js"]

best,
 
U

Une Bévue

William James said:
puts DATA.read.scan( /<script\s+[^>]*src="(.*?)"/m ).flatten
i don't understand your "?" here --------------^

what is his meaning after * ???
 
K

Konrad Meyer

--nextPart1595087.hTo1LGE6MD
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Quoth Une B=E9vue:
William James said:
puts DATA.read.scan( /<script\s+[^>]*src=3D"(.*?)"/m ).flatten
i don't understand your "?" here --------------^
=20
what is his meaning after * ???
--=20
Une B=E9vue

Non-greedy match. Find as few characters as possible to match, which in thi=
s=20
case means don't match quote characters.

HTH,
=2D-=20
Konrad Meyer <[email protected]> http://konrad.sobertillnoon.com/

--nextPart1595087.hTo1LGE6MD
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBG+dzXCHB0oCiR2cwRAk7CAKDA+CL2xgZ/DlNWD2IeC7yFwGyXAgCgty0v
QjoBIBGnzB/c08j7+AtcojE=
=kxlF
-----END PGP SIGNATURE-----

--nextPart1595087.hTo1LGE6MD--
 
D

Daniel Sheppard

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?

Your subject says regex, but your request says Hpricot:

require 'hpricot'
doc =3D Hpricot(input)
scripts =3D (doc/'script').map {|x| x['src']}.compact
 
E

eggie5

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?

Your subject says regex, but your request says Hpricot:

require 'hpricot'
doc = Hpricot(input)
scripts = (doc/'script').map {|x| x['src']}.compact

Ahh, that looks beautiful right there! But will hpricot work on
a .rhtml file?
 
D

Daniel Sheppard

Your subject says regex, but your request says Hpricot:
require 'hpricot'
doc =3D Hpricot(input)
scripts =3D (doc/'script').map {|x| x['src']}.compact
=20
Ahh, that looks beautiful right there! But will hpricot work on
a .rhtml file?

Probably - Hpricot should treat all the rhtml guff as if you're just=20
really really bad at writing html and treat the rhtml bits as just raw.

Hpricot('<%=3D <script src=3D"monkey"> %>').at('script')['src']
=3D> "monkey"

The rhtml will get in the way of Hpricot seeing your tree correctly, so
finding script tags only within the head section or something like that
might not work, but for simple finds it should be fine.

Dan.
 
U

Une Bévue

Konrad Meyer said:
Non-greedy match. Find as few characters as possible to match, which in this
case means don't match quote characters.

OK, fine, thanks a lot to remaind me...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,331
Latest member
ElaneLyttl

Latest Threads

Top