#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Weaverville, NC
    Posts
    2
    Rep Power
    0

    Question generating HTML tables


    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. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    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>"
    )
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Weaverville, NC
    Posts
    2
    Rep Power
    0

    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.

IMN logo majestic logo threadwatch logo seochat tools logo