<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Bradphelan's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 29 Aug 2008 22:44:45 GMT</pubDate>
    <description>DZone Snippets: Bradphelan's Code Snippets</description>
    <item>
      <title>Running erb templates from the command line</title>
      <link>http://snippets.dzone.com/posts/show/1723</link>
      <description>The following script takes the name of a template as it's argument. Within the&lt;br /&gt;template the command &lt;b&gt;erb&lt;/b&gt; 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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;# Quick and dirty template processing script. It takes&lt;br /&gt;# as an argument the name of the first template script&lt;br /&gt;# and then executes it to standard output.&lt;br /&gt;&lt;br /&gt;require "erb"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class QuickTemplate&lt;br /&gt;   attr_reader :args, :text&lt;br /&gt;   def initialize(file)&lt;br /&gt;      @text = File.read(file)&lt;br /&gt;   end&lt;br /&gt;   def exec(args={})&lt;br /&gt;      b = binding&lt;br /&gt;      template = ERB.new(@text, 0, "%&lt;&gt;")&lt;br /&gt;      result = template.result(b)&lt;br /&gt;      # Chomp the trailing newline&lt;br /&gt;      result.gsub(/\n$/,'')&lt;br /&gt;   end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def erb(file, args={})&lt;br /&gt;   QuickTemplate.new(file).exec(args)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts erb(ARGV[0])&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The erb command itself takes an optional argument of a hash which is passed to the template as the&lt;br /&gt;variable name &lt;b&gt;args&lt;/b&gt;. Thus you can parameterize your sub templates. &lt;br /&gt;&lt;br /&gt;Call the command as&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby erb_run.rb main.thtml&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The main template&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt;   &lt;head&gt;&lt;br /&gt;   &lt;/head&gt;&lt;br /&gt;   &lt;div&gt;&lt;br /&gt;    &lt;%= erb("title.thtml") %&gt;&lt;br /&gt;   &lt;/div&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and the sub template &lt;b&gt;title.thtml&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;title&gt; This is Alex's cool restraunt&lt;/title&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 21 Mar 2006 00:30:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1723</guid>
      <author>bradphelan (Brad Phelan)</author>
    </item>
    <item>
      <title>Using VIM as a syntax highlighting engine from Ruby</title>
      <link>http://snippets.dzone.com/posts/show/990</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;       def  vimsyn(text, filetype)&lt;br /&gt;           synfile = Tempfile.new('synfile')&lt;br /&gt;           synfile.close&lt;br /&gt;           codefile = Tempfile.new('codefile')&lt;br /&gt;           codefile &lt;&lt; text&lt;br /&gt;           codefile.close&lt;br /&gt;&lt;br /&gt;           # filetype="matlab"&lt;br /&gt;&lt;br /&gt;           expr = %Q%/usr/local/bin/vim -f -n -X -e -s -c \&lt;br /&gt;               "set filetype=#{filetpe}" \&lt;br /&gt;               -c "syntax on" \&lt;br /&gt;               -c "set background=dark" \&lt;br /&gt;               -c "let html_use_css=1" \&lt;br /&gt;               -c "run syntax/2html.vim" \&lt;br /&gt;               -c "wq! #{synfile.path}" -c "q" \ &lt;br /&gt;               #{codefile.path}%&lt;br /&gt;&lt;br /&gt;           `#{expr}`&lt;br /&gt;           html = IO.readlines(synfile.path).join&lt;br /&gt;           body = html.match(/&lt;body.*&lt;\/body&gt;/m)[0]&lt;br /&gt;           css  = html.match(/&lt;style.*&lt;\/style&gt;/m)[0]&lt;br /&gt;           css.gsub!(/pre/,'pre.code')&lt;br /&gt;           css.gsub!(/^body.*$/,'')&lt;br /&gt;           body.gsub!(/^&lt;body/,'&lt;div')&lt;br /&gt;           body.gsub!(/&lt;\/body&gt;/,'&lt;div&gt;')                                        &lt;br /&gt;           body.gsub!(/&lt;pre&gt;/,'&lt;pre class=code&gt;')&lt;br /&gt;&lt;br /&gt;           return css+body&lt;br /&gt;       end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 20 Dec 2005 18:28:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/990</guid>
      <author>bradphelan (Brad Phelan)</author>
    </item>
  </channel>
</rss>
