Web Programming Languages

Ruby Source Code: images.rb

#!/usr/bin/ruby
require 'cgi'
require './common'

#----------------------------------------------------------
#   The Web Language Project
#   Mark Brautigam
#   May-June 2015
#   http://www.mixed-up.com/markb/
#----------------------------------------------------------

cgi = CGI.new
puts cgi.header
headers1()
headers2()
print sidebarX("ruby", "images", "rb")

print %q(
  <div id="content">
    <h2>Ruby: Directory Read</h2>
    <p>Read the contents of a directory of images. Exclude the non-image directory entries.
       Create a caption by deleting the file extension and capitalizing the file name.</p>
    <p>The Ruby <tt><b>Dir</b></tt> function lets you specify exactly the files you want
       included in the list, such as <tt><b>Dir["../birth/*.png"]</b></tt>. So, there was
       no need to manually exclude invisible files, dot files, or directories.</p>
    <p class='choose'>Choose a directory:
      <a href='images.rb?dir=comics'>Comics</a> • 
      <a href='images.rb?dir=embroidery'>Embroidery</a> • 
      <a href='images.rb?dir=stone'>Carving</a> • 
      <a href='images.rb?dir=tech'>Tech</a>
    </p>
)

#----------------------------------------------------
def getCaption (s)
#----------------------------------------------------
  caption = File.basename(s, File.extname(s)).capitalize

  if (caption.index("_") != 0)
    if (caption.index("_") != nil)
      captions = caption.split ("_");
      for j in 0..captions.length-1
        captions[j] = captions[j].capitalize
      end
      caption = captions[1..100].join("<br />")
      return caption
    else
      return caption
    end
  else
    return ""
  end
end

#----------------------------------------------------
#  The main Ruby code is here.
#----------------------------------------------------
dir = "../images/comics"
askdir = cgi['dir']
if (askdir != '') 
  dir = "../images/" + askdir
end

images = Dir[dir + "/*"] # no space in Dir[
nimages = images.count
for i in 0..nimages-1
  caption = getCaption (images[i])

  if (caption != "")
    print "    <figure>\n"
    printf("      <img src='%s' />\n", images[i]) # again no space in print(
    printf("      <figcaption>%s</figcaption>\n", caption)
    print "    </figure>\n"
  end
end

print %q(
    <p>We can change the order of the files (for example, birth order instead of alphabetical
       order) by prepending each file name with a number and removing the number from the
       caption string.</p>
    <p>Images courtesy of:<br />
    <a target='_blank'
       href='http://www.worldofjudaica.com/jewish-home/stationery/p_jerusalem_stone_paperweight_with_twelve_tribes_and_menorah'>
       Stone Paperweight at World of Judaica</a><br />
     <a target='_blank'
        href='http://4-hobby.com/store/product.php?productid=17686&cat=320&page=1'>
        Embroidery Designs at 4-Hobby.com</a><br />
     <a target='_blank' href='http://www.dumbingofage.com'>Dumbing of Age</a><br />
     <a target='_blank' href='http://www.scarygoround.com'>Scary Go Round</a><br />
     <a target='_blank' href='http://www.questionablecontent.net'>Questionable Content</a>
    </p>
    <p><a href='source.rb?f=5'>Show Ruby source code »</a>
  </div>
)

footers()