Creating HTML using the HTML Line Tree gem

The following example uses the HTML Line Tree gem to convert text into HTML. Notice the JavaScript is enclosed within square brackets to associate it with the script element above it.

require 'hlt'

s =<<S
html
  head
    title A fun example
    script
[// ask the user how they are
function askQuestion(){
  var a = "How are you today?";
  answer = prompt(a, "I feel well!");
}]
  body
    input {onClick: 'askQuestion();'}
S

puts Hlt.new(s).to_html

Output

<\?xml

<html>
  <head>
    <title>A fun example</title>
    <script>
    // ask the user how they are
    function askQuestion(){
      var a = "How are you today?";
      answer = prompt(a, "I feel well!");
    }
</script>
  </head>
  <body>
    <input onClick='askQuestion();'></input>
  </body>
</html>

Resources

Tags:
Source:
0837hrs.txt
Published:
08-09-2011 08:37