HTML generator script (in Ruby) - what do yout think about it?

Hi folks,
since I have a lot of time recently, I decided to create a little (non-dynamic) website with HTML and CSS.
So, when I began writing I noticed that there is a lot of "typing overhead" what really went on my nerves.
Be it as this may... I was thinking about how I could shorten the whole process of writing HTML-code.
I began writing a little ruby-script, wich reads a HTML file line by line, searches for a certain pattern, interprets this pattern and generates HTML-code out of it.
I hope you get what I mean. Here is the code (it's still experimental... )
# Command class - contains the "template"-code
class Commands
# generate a list
def Commands.list(args)
s = "<ul>\n"
args.each { |arg| s += "<li>#{arg}</li>\n" }
s += "</ul>\n"
end
# generate a headline
def Commands.h2 (args)
"<h2>#{args.join(" ")}</h2>"
end
# generate an image
def Commands.img(args)
"<img src='#{args[0]}' width='#{args[1]}' height='#{args[2]}'/>"
end
end
# Searches for pattern and executes them
def execute_rubyfoo(content)
content = content.map { |line| line.strip }
new_content = content
success = 0
errors = 0
puts "Generating..."
new_content.each_with_index { |line, i|
# line matches pattern
if line =~ /^\$\$\s*rfoo.*/
command = new_content[i].sub(/\$\$rfoo\s*/, "")
command = command.sub(/\s*\$\$/, "")
command = command.split(" ")
begin
method = Commands.method(command[0])
new_content[i] = method.call command[1..-1]
success += 1
rescue NameError => e
puts "--> Line #{i + 1}: Command not found: #{command[0]}"
errors += 1
end
end
puts "\n---"
puts "Finished: #{success} successfully, #{errors} errors."
new_content
end
file = File.new("foo.html", "r+")
new_content = execute_rubyfoo(file.readlines)
file.close
new_file = File.new("foo.html.bla", "w+")
new_file.puts new_content
new_file.close
An example HTML-file could look like that:
<html>
<body>
<h1>Hallo</h1>
$$rfoo list 1 2 3 4 5$$
$$rfoo h2$$
$$rfoo img bla.png 100 100$$
</body>
</html>
And a generated HTML-file like that:
<html>
<body>
<h1>Hallo</h1>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<h2>foo</h2>
<img src='bla.png' width='100' height='100'/>
</body>
</html>
So, now what I want to know is:
What do you think about this idea in general and the way I implemented it? Is there already something like that?
Thanks,
g0ju

ERB is even part of the standard ruby libraries. See the Notes for other alternatives.
I have to admit that I'm guilty of reinventing that wheel, too
Last edited by hbekel (2009-09-22 09:04:57)

Similar Messages

  • What do you think about the html5?

    I've been seeing developers philosophizing about the future, and I want to know what do you think about the brand new html5?
    mainly because the html5 can take out of the market technology ( flex ).
    thanks for the opinion!!!
    [email protected]

    Yep this is most definitely not just a rumor, I've found plenty of sources talking about this.
    Here's someone's take on HTML5, a non-Adobe perspective, from a Silverlight developer.  I thought this would be interesting to mention.  Not sure I agree 100% with everything said (IE may not be most used browser and I don't believe in DRM), but it's an interesting read anyway:
    Yes, you can do a LOT of stuff with HTML5 + JS that Silverlight is good for. But HTML5 will only reach Candidate Recommendation status in 2012 - if Silverlight keeps the current pace, it will be at V7 by then.
    HTML5 will only get you approximately what Silverlight had at V1.0. A Canvas element, some video playback capabilites, and a Javascript programming model. Can you imagine how further advanced Silverlight 4 is at the moment?
    Actually, scratch that - the video quality and availability of HTML5 is a lot worse than what Silverlight has to offer. There is no DRM, no Smooth Streaming, not even full screen! No GPU acceleration either. Even the codec HTML5 has to support is not standardized! This results in Firefox 3.6 having only Theora decoder, and Youtube experimenting with HTML in H.264 (on the same day FF3.6 launched), only playable in approx. 4-5% of the world's browsers.
    Internet Explorer is still the most widely used browser on the web, and does not have HTML5. Currently, there are more Silverlight capable browsers browsing the web than HTML5 compatible ones. I don't think that in the next 3 years you will be able to create an HTML5 app and hope that at least 50% of the world's population can view it without switching browsers. (and remember: installing a plugin is a lot less hassle than switching to a new browser!)
    HTML5 will not be truly cross-browser standard for quite a long time, if ever. There are too many things that the browser developer can do as they wish - just think about the aforementioned video codec issue. There are too many little differences in each browser's Javascript implementation to make it really portable.
    The developer story: nothing in the HTML + JS world comes close to the awesome Visual Studio and Expression Blend tools. Javascript is cool for small apps, but cannot hold a candle to C# when it comes to serious development. Fighting with browser and OS incompatilities takes up a huge amount of time for any HTML website or web app - with Silverlight you don't have this.
    Just compare what the best HTML / Ajax company in the world (Google) did with maps, and what MS did to see the difference. Go tohttp://maps.google.com/ (watch out - the Streetview part is in Flash, not Ajax), and compare it to http://www.bing.com/maps/explore/. That is the difference I am talking about.
    Of course he means (or should have meant) Flex/Flash rather than just Flash.  As for the GWT and other AJAX fanboys who I guess think browser compatibility problems are going to be a thing of the past (or won't be a drag on corporate bottom lines - haha), it makes you wonder what other wild fantasies they're envisioning for the future.  Next they'll come and tell us XML-based protocols are going to be as fast as AMF...

  • What do you think about my site?

    What do you think about my site?
    www.linkx.ro
    This is my first site in flash8 so please be
    understanding:)

    I think it is pretty good. I like it. Although I have to
    agree that if you are looking for some serious business looks you
    have to go with more traditional design (HTML/CSS) and incorporate
    small flash bits that will make it look more dinamic. But that is
    just me. I have one suggestion - too much stuff man. I mean lots of
    links all over, moving things around. I understand you wanted to go
    for something dynamic and flashy but it is overwhelming to me! You
    might want to consider developing some sort of consistency and keep
    information/links/graphics bundled together so when one wants to
    find a link they would move to the link area and so on. Anyway
    looks great though for your first flash web.

  • What do you think about using exceptions for something more than errors

    if you look the java.lang.Exception description at the JDK javadoc, you can see the following text:
    "The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch."
    ... we can�t see "error" word anywhere!!
    What do you think about using exceptions for something more than errors? Can be possible use them as a way for send information to upper layers?
    Thankx in regards...

    it seems that what you say is a functional way of achiveing that, yes
    but Exceptions are generally reserverd for "Exceptional" situations ie program messing up or invalid data
    it does require a fair bit of processor time to actually generate and throw an Exception.
    so, all in all its "better" to use "normal" condition flow control to achive what you want.. you can always return early, break loops, call methods to pass information

  • What do you think about this product ?

    Hi,
    I want to have access to SAP ECC 6.0 for personnal use, so I find a hard drive in the following web site :
    http://www.erpselftraining.com/product/SAP-server-MCOD.html
    Please can you tell me what do you think about this hard drive ? has anyone already tried it ? do you advise it ?
    Your answers will be appreciated.
    Kind regards.

    is it for learning purpose ? then why dont you just download the demo versions of SAP!!

  • Adobe, what do you think about that ?

    - one day I was demoing DPS to 35 people in Paris. The whole day the servers were barely accessible. For the reputation and reliability of DPS that was wonderful.
    - another day I was doing a little DPS demo for Adobe. Again the same problem.
    - another day, the demo was ruined because the servers were so slow.
    - this Monday morning, I open my iPad and I can see my latest Folio in ACV.
    In the afternoon I join Adobe reps and partners and other VIPs in London for the launch of CS6. There I go with my iPad and what a surprise when I want to show the Folio to an Adobe representative ?
    Despite being signed in, all my folios where archived and all in Download state. I could not access a WiFi and even if I could I think that downloading 500 Mb would be impossible.
    Many people where asking me :
    —"And are you teaching DPS ?"
    – I said "Yes, I'm even working on it for real projects."
    — "Do you have one to show us ?"
    – "No, because Adobe's software, for a reason that I don't understand, deleted all my folios a few moments ago and I would have to redownload them again. ACV sucks".
    People to who I have talked to where Adobe people, chiefs of graphic studios (newspaper, agency,...), potential clients for DPS,...
    There was even of Adobe person that told me : "We are receiving a lot of complaints about the technical model of DPS, and you're right, it's terrible".
    There are so many problems with this app and with the servers.
    DPS's reputation will may be better when one day we just play offline with the folios like a PDF in GoodReader for instance.
    DPS is exploding my bandwidth.

    Peter, that is sad. If a publisher decides to pull the issue from the
    server, that's like burning down their own library of things.
    Apple wanted adobe to stop downloading such large data into the storage
    that will be backed up. So issues now go into the cache, that will not be
    uploaded to the Icloud storage.
    Because you buy a 64gb iPad, but get only 2Gb of online storage.
    —Johannes
    (mobil gesendet)
    Am 26.04.2012 18:35 schrieb "Peter Villevoye" <[email protected]>:
       Re: Adobe, what do you think about that ?  created by Peter Villevoye<http://forums.adobe.com/people/Peter+Villevoye>in
    Digital Publishing Suite - View the full discussion<http://forums.adobe.com/message/4363125#4363125

  • I would like to have a solar panel on my iPhone so I can have a constant state of charge what do you think about that?

    I would like to have a solar panel on my iPhone so I can have a constant state of charge what do you think about that?

    I am among this community since I invested much into this corporation. I rather go to the consumers just to discuss an idea I would like for this corporation to achieve. I do not like to purchase third party accessories as they seem to fail my expectations or the fact that it is not worth it when i finally see what they have made. Yes they are good products but there is much disappointment when I look to third-party products that say they are engineering products for the iPhone or other Apple device I have purchase. Just a thought no need to be so hostile.

  • What do you think about IPlanet 6.x Application server?

    Hi,
    Right now our company is developing an important project with J2EE. Our customer has IPlanet 6.5 application server. So far, we have had lots of problems with it.
    What do you think about it?.
    Would we consider to change the application server?.

    We also have had a lot of problems, although to be fair we have got it running reasonably reliabily now but only after disabling some of its features such as clustering. Make sure you have the maintence pack 1 installed. Ask Sun about the problems you are having often we would get a reply saying it was a known bug etc, unfortunatly no bug list is made available by sun...

  • What do you think about asset group?

    Hi,
    My client ask me to configure asset group functionality. It's well explained in SAP documentation but there is many SAP OSS note to correct some bugs.
    What do you think about this functionality? Do you have any suggestion regarding asset group?
    Thanks and regards
    Karim

    Hi,
    What do you think about the forum rules?
    Before you post: Rules of Engagement
    Thanks,
    Srinu

  • What do you think about online IT degree?

    I would like to take an online degree since i cannot afored the time to go to on campus school to hold a masters degree. What do you think about online IT degree, Do you have one? If yes, where did you get it? Would you recommend a good school? 
    Thanks 
    This topic first appeared in the Spiceworks Community

    I would like to take an online degree since i cannot afford the time to go to on campus school to hold a master's degree. What do you think about online IT degree, Do you have one? If yes, where did you get it? Does it help get a job? Would you recommend a good school? 
    Thanks 
    This topic first appeared in the Spiceworks Community

  • What do you think about our design

    Dear sir, madame,
    What do you think about our design:
    Mini Paarden shop
    Looking forward hearing your opinion.
    Kind regards,
    Mariska

    Hello Mariska,
    Browsing on an iPad, the search bar + cart Infos are hidden on the right hand side of the page (I need to scroll to see it). Depending on your target audience and the devices they uses, it might be an issue if they can't complete the order process because they don't see where the cart is...
    Apart from that, the design overall strikes me as clear enough to be browsable, IMO it does the job!

  • What do you think about BIOS 1.8 for KT4V

    What do you think about BIOS 1.8 for KT4V?
    Is this version really better?
    CRC error- still  be occured?

    Hi
    This bios is catastrophic(k).I flashed bios and boot to win 98 and no one of my programs wont too work.Every time I try to start anything there is a note "this program performed illigal op...".So I re-installed win but problem remains.Now I'm back on bios 1.7.

  • What do you think about EFI

    What do you think about EFI, is it good or bad? For Linux or generally
    Last edited by fk (2007-03-27 22:36:03)

    EFI is? This? http://en.wikipedia.org/wiki/Extensible … _Interface

  • What do you think about the iPhone 4 for my 11 year old daughter

    What do you think about the iPhone 4 for my 11 year old daughter

    It should be a good choice. I recently got the 5C which typically is $99 with supported carriers. Verizon, AT&T, Sprint provide attractive pricing with a two year contract or it you are adding a line to your current service. I have several friends using the 4S, and they have been very pleased with them.

  • What do you think about the value of MacKeeper?

    What do you think about the value of the free download MacKeeper?

    See below.
    https://discussions.apple.com/docs/DOC-3036

Maybe you are looking for

  • Local network computer name keeps changing

    I haven't been able to work out why my iMac has bouts of telling me that my computer's name is already in use on the network and adding a number to it. I keep having to go into System Preferences/Sharing and change it back again so that my husband ca

  • New Iphone / Old Imac

    Please Help! I just bought an IP4 which is my third iPhone. I have a vintage Imac running 10.4.11 which runs perfectly and does what we've always needed it to do. We also have a house full of PC's. I have always used our Imac to activate and sync my

  • Camera connector, nikon 3200, 0 pictures

    I have a fifth generation video ipod, 30 GB, software version 1.1.2 and tried syncing pictures from my Nikon 3200 using the camera connector. It has worked fine for two imports of a few (50) pictures but on the third import, it seemed to stop during

  • SQLParseException occurred while composing Offline Database View definition

    While importing view definitions from CASE120 to offline database for some views I am getting following error. ERROR: An SQLParseException occurred while composing Offline Database View definitions: Error(s) parsing SQL: Unexpected token near *!* in

  • All new clips disabled

    Hi, I am new to FCP X but not FCP.  I am working on a project trying to learn this program and I have done something (stupid) and now the clips that existed in my timeline are all active, but anything new I bring down from the event library is disabl