Reverse Engineering with Amrita2

Amrita2 can accept a user friendly template.

'user friendly' means that you see what will be generated by your programs when you open the template.

In other words, if your customer show a plain HTML document and say 'The system that you are goint develope should generate a page like this' , you can use the HTML document as your template file with little effort.

This is good at this situation: Your customer change his mind after you converted his HTML document to a code and say 'I want to change the page but it will not cost so much because the dynamic element in the page is same'

This is not true with most WEB Development System but is true with Amrita2. With Amrita2 you can say 'Change the template by yourself and I will use it with no extra effort' because the template really running is almost same as he made at first step.

Here's how this can be done.

A simple story

Let's begin with a simple story

The requested page image

This is the desired result.

<html>
  <body>
     <h1>hello world</h1>
     <p>Amrita2 is a html template libraly for Ruby</p>
  </body>
</html>

Ask ' What is the dynamic part of this ?' and mark it with id attributes.

<html>
  <body>
     <h1 id='title'>hello world</h1>
     <p id='body'>Amrita2 is a html template libraly for Ruby</p>
  </body>
</html>

The use the tool bin/amrita_r

$ ruby -I~/amrita2/lib ~/amrita2/bin/amrita_r a.html 

'a.html.rb' will be generated like this

require 'amrita2/template'
include Amrita2
t = TemplateFile.new("a.html") do
  Amrita2::define_template_spec(:key_attr_name=> :id) do
    dynamic_element(:title) # hello world 
    dynamic_element(:body) # Amrita2 is a html tem 
  end
end
data = 
{:title=>Amrita2::SanitizedString["hello world"],
 :body=>
 Amrita2::SanitizedString["Amrita2 is a html template libraly for Ruby"]}

 t.expand(STDOUT, data)

This code makes original page with no modification. And it is a Amrita2 based code.

You can copy this template spec and data to your codes. Then change the data related code so that it will be generated runtime.

print "Enter title: "; title = gets
print "Enter body: ";  body = gets
data = {
  :title=>title,
  :body=>body
}
t.expand(STDOUT, data)

The important point is you can modify a.html after writing this code. Only if the dynamic elements matches the modified template, your code produce an output acording to it.

a more complex real story

The directory 'sample/reverse' contains a code that produce a page very similar to http://www.ruby-lang.org/

step1: get contents

$ wget -pk http://www.ruby-lang.org/en/

'sample/reverse/rubysite/' contains the result of this command.

step2: convert it to xhtml

$ tidy -m -i -asxml index.html

'sample/reverse/rubysite/en/step2.html' is the result.

step3: use amrita_r

You can mark it with id attribute by hand. But this HTML result uses 'class' attribute

$ ruby -I../../../lib ../../../bin/amrita_r -k class en/step2.html 

-k option is make the 'class' key attribute.

'sample/reverse/rubysite/en/step2.html.rb' is the result of this command.

step4: change it by hand

I changed the step3 to 'sample/reverse/reverse.rb' by hand.

There are too many template specs so I deleted many dynamic_element statement.

The file 'sample/reverse/reverse.html' contains its result.