Show text from text file in cgi script

Discussion in 'Ruby' started by Ryan C., Feb 23, 2011.

  1. Ryan C.

    Ryan C. Guest

    I am writing a cgi script to pull the contents of a .txt file in the
    cgi-bin directory into the body of an html file and can't seem to figure
    it out.

    eruby seems to be a solution, but I can't tell if it's to be used with
    Rails or not and the documentation is lacking for a newbie.

    Here's what I've got:

    Code:
    #!/usr/local/bin/ruby
    # SCRIPT hello_world.cgi
    # CREATOR: Ryan Clark
    puts "Content-type: text/html"
    puts
    
    #This line left intentionally blank
    
    puts <<HTML
    <!DOCTYPE html>
    <html>
    <head>
    <title>Oliver Twist</title>
    <style type ="text/css">
    body {
    background-color:#335;
    color:#fff;
    font-family:Helvetica,Arial,Verana,sans-serif;
    font-size:12px;}
    h1 {color:#c00}
    </style>
    </head>
    <body>
    <% File.open("oliver_twist.txt").each { |line| puts line } %>
    </body>
    </html>
    HTML
    
    --
    Posted via http://www.ruby-forum.com/.
     
    Ryan C., Feb 23, 2011
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    Assuming that you have set your web server properly (presumably apache with
    mod_ruby?)
    I think you should check the safeness level as you are trying to use
    "tainted" data, in this case file content.
    By default, you are not allowed to do so. So try with different safeness
    level.
    The following links might be helpful.

    http://www.modruby.net/en/index.rbx?mode=search&word=shugo#label-17
    http://ruby-doc.org/docs/ProgrammingRuby/html/taint.html

    Regards,
    Junegunn Choi.
     
    junegunn choi, Feb 23, 2011
    #2
    1. Advertising

  3. Ryan C.

    Ryan C. Guest

    Thanks for the links, but I do not set up the web server and this is for
    a class.

    The assignment is just to get the contents of oliver_twist.txt to show
    in an html doc using a CGI script.

    Here's my site:

    hills.ccsf.edu/~rclark4/oliver_twist.cgi

    --
    Posted via http://www.ruby-forum.com/.
     
    Ryan C., Feb 23, 2011
    #3
  4. On 02/22/2011 06:27 PM, Ryan C. wrote:
    > I am writing a cgi script to pull the contents of a .txt file in the
    > cgi-bin directory into the body of an html file and can't seem to figure
    > it out.
    >
    > eruby seems to be a solution, but I can't tell if it's to be used with
    > Rails or not and the documentation is lacking for a newbie.
    >
    > Here's what I've got:
    >
    >
    Code:
    > #!/usr/local/bin/ruby
    > # SCRIPT hello_world.cgi
    > # CREATOR: Ryan Clark
    > puts "Content-type: text/html"
    > puts
    >
    > #This line left intentionally blank
    >
    > puts<<HTML
    > <!DOCTYPE html>
    > <html>
    > <head>
    > <title>Oliver Twist</title>
    > <style type ="text/css">
    > body {
    > background-color:#335;
    > color:#fff;
    > font-family:Helvetica,Arial,Verana,sans-serif;
    > font-size:12px;}
    > h1 {color:#c00}
    > </style>
    > </head>
    > <body>
    > <% File.open("oliver_twist.txt").each { |line| puts line } %>
    > </body>
    > </html>
    > HTML
    > [/code[/color]
    
    You are not actually using eruby in your script, but you are trying to
    use the eruby syntax, which is not needed. When you are using "here
    documents," you are just using big multiline strings, meaning you can
    use string interpolation to insert the contents of the file into the
    middle of the string.
    
    For example:
    
    x = "world"
    
    puts <<HERE
    hello #{x}
    HERE
    
    
    But you need to concentrate on reading the contents of the file into a
    string first. Then you can insert it into the output.
    
    http://rdoc.info/stdlib/core/1.9.2/IO.read
    
    -Justin
     
    Justin Collins, Feb 23, 2011
    #4
  5. Ryan C.

    Ryan C. Guest

    Justin,

    Thanks! I was looking at this from the wrong angle, obviously.

    Also, this is weird to me, but I'm new to this... I didn't need the #{}.

    Here's the code:


    #!/usr/local/bin/ruby
    # SCRIPT oliver_twist.cgi
    # CREATOR: Ryan Clark
    puts "Content-type: text/html"
    puts
    oliver = File.open("oliver_twist.txt").each { |line| puts line }

    #This line left intentionally blank

    puts <<HTML
    <!DOCTYPE html>
    <html>
    <head>
    <title>Oliver Twist</title>
    <style type ="text/css">
    body {
    background-color:#335;
    color:#fff;
    font-family:Helvetica,Arial,Verana,sans-serif;
    font-size:12px;}
    </style>
    </head>
    <body>

    </body>
    </html>
    HTML

    If I put the line #{oliver} between the body tags, it shows in the
    result.

    Here's the page:

    http://hills.ccsf.edu/~rclark4/oliver_twist.cgi

    Is that bizarre or normal? Can anyone explain?

    --
    Posted via http://www.ruby-forum.com/.
     
    Ryan C., Feb 23, 2011
    #5
  6. Ryan C. wrote in post #983261:
    > Here's what I've got:

    ...
    > puts <<HTML
    > <!DOCTYPE html>
    > <html>
    > <head>
    > <title>Oliver Twist</title>
    > <style type ="text/css">
    > body {
    > background-color:#335;
    > color:#fff;
    > font-family:Helvetica,Arial,Verana,sans-serif;
    > font-size:12px;}
    > h1 {color:#c00}
    > </style>
    > </head>
    > <body>
    > <% File.open("oliver_twist.txt").each { |line| puts line } %>
    > </body>
    > </html>
    > HTML


    puts inside puts is bad.

    Look, what you're doing is simply this:

    puts "A very big string indeed"

    The only difference is that you've written the string in the multiline
    form, also known as a "here-doc":

    puts <<HTML
    A very big string indeed
    HTML

    So, move your other puts outside this.

    puts <<HTML
    My header goes here
    HTML

    File.open("oliver_twist.txt").each { |line| puts line }

    puts <<HTML
    My footer goes here
    HTML

    As others have pointed out, if you get the contents of the file as a
    *value* then you can insert it directly into a string. This is called
    string interpolation.

    puts <<HTML
    My header goes here
    #{File.read("oliver_twist.txt")}
    My footer goes here
    HTML

    It looks like you would rather be using erb syntax, which is in any case
    a much easier way to assemble a page.

    It is possible to do this within a standalone cgi. However, you *really*
    should look at Sinatra. Honestly. I promise you'll be glad you did. For
    one thing it has support for erb templates built in; for another, it
    makes it easier to separate your app logic from your view generation.

    Here is your CGI as a sinatra app, complete with a layout and inline
    templates in a single file.

    ----------------------------------------------------------------
    require "rubygems" # ruby 1.8.x only
    require "sinatra"

    get "/" do
    @title = "Oliver Twist"
    @content = File.read("oliver_twist.txt")
    erb :book
    end

    __END__

    @@ layout
    <!DOCTYPE html>
    <html>
    <head>
    <title><%= @title %></title>
    <style type ="text/css">
    body {
    background-color:#335;
    color:#fff;
    font-family:Helvetica,Arial,Verana,sans-serif;
    font-size:12px;}
    h1 {color:#c00}
    </style>
    </head>
    <body>
    <%= yield %>
    </body>
    </html>

    @@ book
    <pre>
    <%= @content %>
    </pre>
    ----------------------------------------------------------------

    Do "gem install sinatra", then just run this code from the command line
    (ruby myapp.rb). It will start a webserver listening on port 4567. There
    are other options for when you want to deploy the app for real.

    HTH, Brian.

    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Feb 23, 2011
    #6
  7. Ryan C.

    Ryan C. Guest

    Brian,

    Thanks for helping to clear up the interpolation problem. I appreciate
    the sinatra suggestion and am definitely interested in becoming more
    familiar. I just attended a talk on sinatra and am eager to learn
    more. This project was part of an assignment for a programming class -
    I'll have to find out just how creative in our solutions we are allowed
    to get.

    Thanks again,

    Ryan

    --
    Posted via http://www.ruby-forum.com/.
     
    Ryan C., Feb 23, 2011
    #7
  8. I see. Well, if it has to be CGI, you can still use erb templates like
    this:

    #!/usr/bin/ruby -w
    require "erb"

    template = ERB.new(<<HTML)
    <!DOCTYPE html>
    <html>
    <head>
    <title><%= title %></title>
    <style type ="text/css">
    body {
    background-color:#335;
    color:#fff;
    font-family:Helvetica,Arial,Verana,sans-serif;
    font-size:12px;}
    h1 {color:#c00}
    </style>
    </head>
    <body>
    <pre>
    <%= content %>
    </pre>
    </body>
    </html>
    HTML

    title = "Oliver Twist"
    content = File.read("oliver_twist.txt")
    puts template.result(binding)

    # Kernel#binding is some magic which gives the template
    # access to your local variables

    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Feb 24, 2011
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. praba kar

    Python-cgi or Perl-cgi script doubt

    praba kar, Jul 30, 2005, in forum: Python
    Replies:
    1
    Views:
    812
    Michael Sparks
    Jul 30, 2005
  2. Amir  Michail

    cgi relay for python cgi script

    Amir Michail, Oct 4, 2005, in forum: Python
    Replies:
    7
    Views:
    498
    Michael Ekstrand
    Oct 4, 2005
  3. Muzammil
    Replies:
    1
    Views:
    1,285
    red floyd
    Aug 28, 2008
  4. fatted
    Replies:
    1
    Views:
    182
    Steve Grazzini
    Jul 25, 2003
  5. kath
    Replies:
    4
    Views:
    814
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page