ruby html (or xhtml) forms class...

S

Skeets

does one exist? i use an excellent php based forms class when i code
in php. it includes javascriput validation, server side validation,
regex validation and a lot more.

does ruby have something similar, or will i have to 1. code my own (not
gonna happen anytime soon) or 2. just write the html out every time
(not good)?

tia...
 
I

Ian Whitney

After not finding anything that met my needs, I'm writing one. But
it's a couple months away from being ready.

Ian
 
A

ara.t.howard

After not finding anything that met my needs, I'm writing one. But it's a
couple months away from being ready.

try this out

http://rubyforge.org/frs/download.php/8234/xx-0.1.0.tgz


info about it


NAME

xx - twice as dirty

SYNOPSIS
gem install "double x"

require "xx"

include XX::XHTML

doc = xhtml_{
html_{
head_{ title_{ " go xx! " } }
body_{ " one more and it would be illegal " }
}
}


URI

http://rubyforge.org/frs/?group_id=1024


DESCRIPTION

xx is a library designed to extend ruby objects with html, xhtml, and xml
generation methods. the syntax provided by xx aims to make the generation of
xml or xhtml as clean looking and natural as ruby it self.

the approach taken, that of extending objects, allows natural document
generation while preserving access to instance data. in essence it provides
ruby objects (including the top level 'main' object) an intuitive means to
generate various markup views of their data in a way that is correct and
elegant.

xx is brought to you by the good folks at http://eparklabs.com.


SAMPLES

<========< sample/a.rb >========>

~ > cat sample/a.rb

require "xx"
include XX::XHTML
#
# xx modules extend the current object to allow natural document markup
#
doc = xhtml_{
html_{
head_{ title_{ " go xx! " } }
body_{ " one more and it would be illegal " }
}
}
puts doc.pretty


~ > ruby sample/a.rb

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title> go xx! </title>
</head>
<body> one more and it would be illegal </body>
</html>


<========< sample/b.rb >========>

~ > cat sample/b.rb

require "xx"
#
# xml is as easy as html. xx extends your object very carefully, adding an
# one method that is not prefaced with 'xx_' : 'method_missing'. the
# method_missing defined is conservatively, recognizing only methods that end
# with underscore ('_') as 'tag' methods intended to generate markup. as with
# html, attributes may be passed to any tag method as either symbol or string.
#

class Table < ::Array
include XX::XML
attr "fields"
def initialize *a, &b
@fields = a.shift
replace a
end
def self::[] *a, &b
new *a, &b
end
def to_xml
xml_{
table_{
each do |row|
row_{
fields.zip(row) do |field, value|
field_:)name => field, 'width' => value.size){ value }
end
}
end
}
}
end
end

table = Table[
%w( first_name last_name ssn ),
%w( jane doe 424-24-2424 ),
%w( john buck 574-86-4242 ),
]

puts table.to_xml.pretty

~ > ruby sample/b.rb

<?xml version='1.0'?>
<table>
<row>
<field name='first_name' width='4'>jane</field>
<field name='last_name' width='3'>doe</field>
<field name='ssn' width='11'>424-24-2424</field>
</row>
<row>
<field name='first_name' width='4'>john</field>
<field name='last_name' width='4'>buck</field>
<field name='ssn' width='11'>574-86-4242</field>
</row>
</table>


<========< sample/c.rb >========>

~ > cat sample/c.rb

require "xx"
#
# xx makes it impossible to generate invalid (syntactically) invalid documents
# - unless to instruct it in insert raw html or xml using the 'h_' or 'x_'
# methods. text inserted with 't_' is automatically escaped. like all xx
# methods these can have one or more underscores after them in case there is a
# collision with another method or the tag 'h', 'x', or 't' needs to be
# generated.
#
include XX::XML

doc = xml_{
root_{
div_{ t_ "this is escaped < > & text" }
div_{ h_ "this is raw <html>. & is not escaped" }
div_{ x_ "<raw> xml </raw>" }
div_{ x_{ even_{ entire_{ documents_{ "nest" } } } } }
}
}
puts doc.pretty

~ > ruby sample/c.rb

<?xml version='1.0'?>
<root>
<div>this is escaped &lt; &gt; &amp; text</div>
<div>this is raw <html>. & is not escaped</div>
<div><raw> xml </raw></div>
<div><even><entire><documents>nest</documents></entire></even></div>
</root>


<========< sample/d.rb >========>

~ > cat sample/d.rb

require "xx"
#
# xx has only a few methods which end in '_'. these methods, therefore, cannot
# be used in conjuction with method_missing to auto-generate tags. for those
# methods a tag of the same method can be generated using and escaped form,
# namely two or more underscores always mean 'generate a tag'. those methods
# are:
#
# - g_
# - text_
# - t_
# - h_
# - x_
# - c_
# - at_
# - att_
# - yat_
#
include XX::XML

doc = xml_{
root_{

t_{ "this is a text element" }
t__{ "this is not text, but a __tag__ called t" }

x_{ "this un-escaped & < > stuff" }
x__{ "this is not un-escaped & < > stuff but a tag called x" }
}
}
puts doc.pretty


~ > ruby sample/d.rb

<?xml version='1.0'?>
<root>this is a text element<t>this is not text, but a __tag__ called t</t>this un-escaped & < > stuff<x>this is not un-escaped &amp; &lt; &gt; stuff but a tag called x</x>
</root>


HISTORY

0.1.0:
- added the "g_" method, which generates any tag
^
g_("anytag", "key" => "value"){ b_{ "bold" } }

- added at_ and att_ methods to parse yaml and k=v strings as hashes.

at_("src : image.jpg, width : 100%")

#=> {"src"=>"image.jpg", "width"=> "100%"}

0.0.0:
- initial version


AUTHORS

dan fitzpatrick <[email protected]>
ara.t.howard <[email protected]>


BUGS

please send bug reports to /dev/null. patches to addresses above. ;-)


LICENSE

ePark Labs Public License version 1 Copyright (c) 2005, ePark Labs, Inc. and
contributors All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of ePark Labs nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-a
 
P

Phil Tomson

does one exist? i use an excellent php based forms class when i code
in php. it includes javascriput validation, server side validation,
regex validation and a lot more.

does ruby have something similar, or will i have to 1. code my own (not
gonna happen anytime soon) or 2. just write the html out every time
(not good)?

What about _why's Markaby:
http://markaby.rubyforge.org/

Phil
 
A

ara.t.howard

hmmm. i'm not sure what to do - but i'm open to suggestion! maybe i
should
just reloate the samples directory into the test directory?

for now you can just grab the tarball - sorry for inconvenience.

cheers.

-a

Tnx! tests pass now.

However, the example at the end of xx.rb seems to be failing:

$ ruby lib/xx.rb
------------------------------------------
------------------------------------------
lib/xx.rb:200:in `method_missing': undefined method `cdata_' for [["0",
"1", "2"], ["a", "b", "c"]]:Table (NoMethodError)
from lib/xx.rb:704:in `doc'
from lib/xx.rb:72:in `create'
from lib/xx.rb:48:in `tracking_additions'
from lib/xx.rb:71:in `create'
from (eval):10:in `script_'
from lib/xx.rb:208:in `method_missing'
from lib/xx.rb:704:in `doc'
from lib/xx.rb:72:in `create'
... 10 levels...
from lib/xx.rb:578:in `xhtml_'
from lib/xx.rb:708:in `to_xhtml'
from lib/xx.rb:724
from lib/xx.rb:722

oops! typo. here's a fix


harp:~/xx-0.1.0/xx-0.1.0 > diff -u lib/xx.rb.org lib/xx.rb
--- lib/xx.rb.org 2006-02-13 16:50:38.000000000 -0700
+++ lib/xx.rb 2006-02-13 16:50:52.000000000 -0700
@@ -356,6 +356,7 @@
#--}}}
end
alias_method "c_", "xx_cdata_"
+ alias_method "cdata_", "xx_cdata_"
def xx_parse_attributes string
#--{{{
string = string.to_s


i'll incorporate the fix into the next release - and hopefully figure out the
test/sample bit too. cheers.


-a
 
S

Skeets

the only reason i haven't jumped into rails head first is the forms
class... regex, js and server validation, simple js commands
(background color, etc...), length, width and a ton more.

it will be tough to let go - since so much of good web development is
tied up in forms generation and validation.

decisions, decisions.
 
J

John N. Alegre

On a similar note ....

Is there a class to parse a form and send the results in email? I am
looking for something as "plug and play" as possible.

All comments welcome
john
 
D

David Vallner

D=C5=88a =C5=A0tvrtok 16 Febru=C3=A1r 2006 23:28 John N. Alegre nap=C3=ADsa=
l:
On a similar note ....

Is there a class to parse a form and send the results in email? I am
looking for something as "plug and play" as possible.

All comments welcome
john

If very desperate, you can put a YAML dump of the form data in an e-mail.

Personally, I don't think I'd use such a magic solution - I prefer a good H=
TTP=20
request parser and a good mailer as separate components, than a half-baked=
=20
mix and match of both.

David Vallner
 
J

John N. Alegre

David (and list) ...

The parser is no issue that would be pretty much unique to the form and I
have lots of experience with parser code. What I really need is a mailer
that will take SMTP host name or IP and the parsed form info and send it of
to the correct (and sometimes different) email addy.

Any suggestions?
john
 
D

David Vallner

D=C5=88a Piatok 17 Febru=C3=A1r 2006 01:13 John N. Alegre nap=C3=ADsal:
David (and list) ...

The parser is no issue that would be pretty much unique to the form and I
have lots of experience with parser code. What I really need is a mailer
that will take SMTP host name or IP and the parsed form info and send it = of
to the correct (and sometimes different) email addy.

Any suggestions?
john

Net::SMTP? Not really a mailer though. I know Rails includes a Mailer=20
component. If I have my facts right, it uses a library called TMail to crea=
te=20
the mail messages... The Net::SMTP documentation also recommends RubyMail a=
s=20
an option for mail generation.

So, you get the data from the form, compose the mail object to send using=20
TMail or RubyMail, then use Net::SMTP to send the mail object. If what you'=
re=20
doing is simple enough to go without a full blown webapp framework, this=20
should work perfectly.

David Vallner
 
E

Ezra Zygmuntowicz

Jon-


Here is a little snippet that might help for sending emails from a
ruby script:


require 'net/smtp'



def send_mail(to, from, subject, body)
msgstr = <<EOM
From: #{from}
To: #{to)
Subject: #{ subject }
MIME-Version: 1.0
Content-Type: text/html;

<html>
<style type='text/css'>
#m_b div { border: 1px solid #aaa; }
</style>
<body>
The following email is about...:<br />
<hr />
<div id='m_b'>
<div>#{ body }</div></div>
</body></html>
EOM
Net::SMTP.start('mail.example.com', 25, 'mail.com', 'username',
'password', :login) do |smtp|
smtp.send_message msgstr, "#{from}", "#{to}"
end
puts "Mail sent."
puts "*"*30
puts msgstr
end


heers-
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
http://yakimaherald.com
(e-mail address removed)
blog: http://brainspl.at
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top