Replacing values in some files: line endings etc

Hi,

on Windows, I need to replace some values in a file; see the program below.

  1. ff

The file is in Unix fileformat; but when Ruby wrote to it, there are
DOS line endings instead of Unix one, AFAICS.
That’s what I think what happens.

What I see is:
I open the file in Vim (which is ff=unix); everything looks fine. I run
the Ruby program, then Vim prompts me to relaod the file; after this,
every line ends in ^M.
When I reopen the file in Vim, everything looks OK again; I think Vim
converts the dos line endings automatically since I have
set ff=unix
in my vimrc.

I really would prefer if Ruby would only gsub the stuff I ask it to,
not the line endings.

  1. gsub

How could I match a longer string, but replace only a part of it?
eg ~
string.gsub! /<(embed|object).+?width="\d+"/, “width=”#{w}""
… but the <object etc should stay.

  1. Golf

What’s a shorter / more elegant version of the program?
s.th. in the style of: grep /dimensions/, sed s/old/new/

Tobi

···

########################################
SVG = 'links.svg’
d = Dir.new ‘./‘
HTMLs = d.grep /^svg.*.html$/
def get_new_dimensions
open(SVG) do |svg|
svg.read.scan
/<svg\s+width="(.+?)"\s+height="(.+?)"/m
[($1.delete’px’),$2.delete’px’]
end
end
w,h = get_new_dimensions
HTMLs.each do |html_file_name|
string = open(html_file_name) do |html_file|
html_file.read
end

how to write these in one (gsub) line?

string.gsub! /width="\d+"/, “width=”#{w}"“
string.gsub! /height=”\d+"/, “width=”#{h}""
open(html_file_name,‘w’) do |html_file|

changes line endings :frowning:

 html_file.write string

end
end
########################################


http://www.pinkjuice.com/

P.S.

The lines in the HTMLs where to dimension values are to be replaced are

Well, I don't like golf but

w,h = get_new_dimensions

   rep = {}
   rep['width'], rep['height'] = get_new_dimensions

HTMLs.each do |html_file_name|
   string = open(html_file_name) do |html_file|
     html_file.read
   end

      string = IO.readlines(html_file_name, nil)[0]

   # how to write these in one (gsub) line?
   string.gsub! /width="\d+"/, "width=\"#{w}\""
   string.gsub! /height="\d+"/, "width=\"#{h}\""

      You really want to replace height with width ?

      string.gsub!(/\b(width|height)="\d+"/) { "width=\"#{rep[$1]}\"" }

      or

      string.gsub!(/\b(width|height)="\d+"/) { "#$1=\"#{rep[$1]}\"" }

      but because html is a stupid format

      string.gsub!(/\b(width|height)=(['"])\d+\\2/) { "#$1=#$2#{rep[$1]}#$2" }

      to handle the case

       width='12' height="24"

      probably you need \s* for the case width = '12'

Guy Decoux

Hi,

on Windows, I need to replace some values in a file; see the program below.
[…]
2. gsub

How could I match a longer string, but replace only a part of it?
eg ~
string.gsub! /<(embed|object).+?width="\d+"/, “width=”#{w}""
… but the <object etc should stay.

(untested)

string.gsub! /(<(?:embed|object).+?width=)"\d+"/, “\1”#{w}""

···

On Mon, Aug 12, 2002 at 06:30:22PM +0900, Tobias Reif wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
’_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Actually, typing random strings in the Finder does the equivalent of
filename completion.
– Discussion on file completion vs. the Mac Finder

regexp are stupid :slight_smile:

string.gsub!(/\b(width|height)=(['"])\d+\\2/) { "#$1=#$2#{rep[$1]}#$2" }

   string.gsub!(/\b(width|height)=(['"])\d+\2/) { "#$1=#$2#{rep[$1]}#$2" }
                               
\2 rather than \\2

Guy Decoux

ts wrote:

thanks for your tips

how to write these in one (gsub) line?

string.gsub! /width="\d+"/, “width=”#{w}"“
string.gsub! /height=”\d+"/, “width=”#{h}""

  You really want to replace height with width ?

No. It should do the same as the above.

Perhaps a gsub with block, then
case $1
when ‘width’ then #height
when ‘height’ then # …

Tobi

···


http://www.pinkjuice.com/

Tobias Reif wrote:

how to write these in one (gsub) line?

string.gsub! /width="\d+"/, “width=”#{w}"“
string.gsub! /height=”\d+"/, “width=”#{h}""

  You really want to replace height with width ?

No. It should do the same as the above.

Oops, sorry; should read

string.gsub! /width="\d+"/, “width=”#{w}"“
string.gsub! /height=”\d+"/, “height=”#{h}""

Tobi

···


http://www.pinkjuice.com/