Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Brad Phelan http://xtargets.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Running erb templates from the command line

The following script takes the name of a template as it's argument. Within the
template the command erb is in scope to call more templates. I wrote this for a friend who was having hell with NVU templates and I suggested to write the code by hand and use this script to build his static pages.

#!/usr/bin/ruby
# Quick and dirty template processing script. It takes
# as an argument the name of the first template script
# and then executes it to standard output.

require "erb"


class QuickTemplate
   attr_reader :args, :text
   def initialize(file)
      @text = File.read(file)
   end
   def exec(args={})
      b = binding
      template = ERB.new(@text, 0, "%<>")
      result = template.result(b)
      # Chomp the trailing newline
      result.gsub(/\n$/,'')
   end
end

def erb(file, args={})
   QuickTemplate.new(file).exec(args)
end

puts erb(ARGV[0])


The erb command itself takes an optional argument of a hash which is passed to the template as the
variable name args. Thus you can parameterize your sub templates.

Call the command as

ruby erb_run.rb main.thtml


The main template

<html>
   <head>
   </head>
   <div>
    <%= erb("title.thtml") %>
   </div>
</html>


and the sub template title.thtml

<title> This is Alex's cool restraunt</title>

Using VIM as a syntax highlighting engine from Ruby

You can use VIM as a syntax highlighting engine for code fragments in your blogs etc. The below code snippet works sort of, but it is slow for me. Not sure at the moment whether it is VIM or something else. Perhapps others can try it out and suggest speed or other improvments.

       def  vimsyn(text, filetype)
           synfile = Tempfile.new('synfile')
           synfile.close
           codefile = Tempfile.new('codefile')
           codefile << text
           codefile.close

           # filetype="matlab"

           expr = %Q%/usr/local/bin/vim -f -n -X -e -s -c \
               "set filetype=#{filetpe}" \
               -c "syntax on" \
               -c "set background=dark" \
               -c "let html_use_css=1" \
               -c "run syntax/2html.vim" \
               -c "wq! #{synfile.path}" -c "q" \ 
               #{codefile.path}%

           `#{expr}`
           html = IO.readlines(synfile.path).join
           body = html.match(/<body.*<\/body>/m)[0]
           css  = html.match(/<style.*<\/style>/m)[0]
           css.gsub!(/pre/,'pre.code')
           css.gsub!(/^body.*$/,'')
           body.gsub!(/^<body/,'<div')
           body.gsub!(/<\/body>/,'<div>')                                        
           body.gsub!(/<pre>/,'<pre class=code>')

           return css+body
       end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS