|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am writing a Ruby script that outputs a table of pictures in its current directory. So far it goes like this:
Code:
#!/usr/bin/env ruby -w
html_file = File.open "pictures.html", "w"
html_file.puts(
"<html>",
"<head><title>The Cross Family Cross-Country Trip</title></head>",
"<!--",
"**************************************",
"This file was generated by pictures.rb",
"**************************************",
"-->",
"<body>",
"<div align=center>",
"<h2>.: Pictures :.</h2>",
"<table border=0>",
"<tr>"
)
# Sort pictures by modified date
pictures = Dir["*.{jpg,jpeg}"].sort do |a,b|
File.mtime(a) <=> File.mtime(b)
end
for picture in pictures
html_file.puts(
"<td height=110 width=150>",
"<a href=\"#{picture}\">",
"<img height=100 length=140 src=\"#{picture}\" border=0></a>",
"</td>"
)
end
html_file.puts(
"</table>",
"</div><div align=right>",
"This page was generated by a <a href=\"http://ruby-lang.org\">",
"Ruby</a> script available <a href=\"pictures.rb\">here</a>.",
"</div>",
"</body>",
"</html>"
)
My problem is that I need it to make a new row every 4 pictures or so, for it to fit in the browser. HOW?! This has been giving me some trouble for the past couple of hours.Thanks in Advance |
|
#2
|
||||
|
||||
|
This is an algorithm forum more than a ruby forum, so I guess I'll try to explain an algorithm for how to do this first. How about using a variable (say count) to keep track of the number of files output so far. When the value is a multiple of 4, then print <tr> and </tr> at the beginning and end of the loop. Something like this, I guess (since I really can't program in ruby):
Code:
#!/usr/bin/env ruby -w
html_file = File.open "pictures.html", "w"
html_file.puts(
"<html>",
"<head><title>The Cross Family Cross-Country Trip</title></head>",
"<!--",
"**************************************",
"This file was generated by pictures.rb",
"**************************************",
"-->",
"<body>",
"<div align=center>",
"<h2>.: Pictures :.</h2>",
"<table border=0>",
"<tr>"
)
# Sort pictures by modified date
pictures = Dir["*.{jpg,jpeg}"].sort do |a,b|
File.mtime(a) <=> File.mtime(b)
end
count = 1
for picture in pictures
if (count % 4 == 0)
html_file.puts(
"<tr>"
)
html_file.puts(
"<td height=110 width=150>",
"<a href=\"#{picture}\">",
"<img height=100 length=140 src=\"#{picture}\" border=0></a>",
"</td>"
)
if (count % 4 == 0)
html_file.puts(
"</tr>"
)
count += 1
end
html_file.puts(
"</tr>"
)
html_file.puts(
"</table>",
"</div><div align=right>",
"This page was generated by a <a href=\"http://ruby-lang.org\">",
"Ruby</a> script available <a href=\"pictures.rb\">here</a>.",
"</div>",
"</body>",
"</html>"
)
|
|
#3
|
|||
|
|||
|
Thanks
Thanks, I ended up using that same consept in a hackish way of counting by the array index:
Code:
for picture in pictures
html_file.puts(
"<td height=110 width=150>",
"<a href=\"#{picture}\">",
"<img height=100 length=140 src=\"#{picture}\" border=0></a>",
"</td>"
)
# Create new row every four pictures
while (pictures.index(picture)+1).divmod(4).last == 0
html_file.puts "</tr>"
html_file.puts "<tr>" until picture = pictures.last
end
end
That is a lot uglier than most Ruby has to be, but a least my mind can rest now. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > generating HTML tables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|