Permalink
Browse files

Add wlang command line + example

  • Loading branch information...
blambeau committed Jan 23, 2012
1 parent ef8b62c commit 1efb1134d0988e4ec4788f411b1ed425f7409863
Showing with 123 additions and 0 deletions.
  1. +4 −0 bin/wlang
  2. +25 −0 examples/mustang/html.tpl
  3. +94 −0 lib/wlang/command.rb
View
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+$:.unshift File.expand_path('../../lib', __FILE__)
+require 'wlang/command'
+WLang::Command.run(ARGV)
View
@@ -0,0 +1,25 @@
+---
+people:
+ - name: Bernard Lambeau
+ hobbies:
+ - name: Programming
+ - name: Guitar
+ - name: Louis Lambeau
+ hobbies:
+ - name: Programming
+ - name: Bass
+---
+<html>
+<body>
+ <ul>
+ *{people}{
+ <li>${name}</li>
+ <ul>
+ *{hobbies}{
+ <li>${name}</li>
+ }
+ </ul>
+ }
+ </ul>
+</body>
+</html>
View
@@ -0,0 +1,94 @@
+require 'wlang'
+require 'quickl'
+module WLang
+ #
+ # wlang -- templating engine
+ #
+ # SYNOPSIS
+ # Usage: wlang [options] TEMPLATE
+ #
+ # OPTIONS:
+ # #{summarized_options}
+ #
+ # DESCRIPTION
+ # This command invokes the wlang templating engine on TEMPLATE
+ # and flushes the rendering result on standard output.
+ #
+ class Command < Quickl::Command(__FILE__, __LINE__)
+
+ options do |opt|
+ @output = nil
+ opt.on('--output=FILE', 'Render output in FILE') do |file|
+ @output = file
+ end
+ @yaml_front_matter = true
+ opt.on("--[no-]yaml-front-matter",
+ "Enable/disable YAML front mater (defaults to true)") do |val|
+ @yaml_front_matter = val
+ end
+ @ast = false
+ opt.on('--ast', "Debugs the AST") do
+ @ast = true
+ end
+ @compiling_options = {}
+ opt.on('--[no-]auto-spacing') do |val|
+ @compiling_options[:autospacing] = val
+ end
+ opt.on_tail("--help", "Show help") do
+ raise Quickl::Help
+ end
+ opt.on_tail("--version", "Show version") do
+ raise Quickl::Exit, "#{Quickl.program_name} #{WLang::VERSION} (c) 2009-2012, Bernard Lambeau"
+ end
+ end
+
+ def execute(argv)
+ install(argv)
+
+ compiler = @dialect.compiler(@compiling_options)
+ if @ast
+ require 'awesome_print'
+ ap compiler.ast(@template)
+ end
+
+ with_output do |output|
+ compiler.compile(@template).render(@context, output)
+ end
+ end
+
+ private
+
+ def install(argv)
+ raise Quickl::Help unless argv.size == 1
+
+ # template file
+ unless File.file?(@tpl_file = argv.first)
+ raise Quickl::Exit, "No such template #{tpl_file}"
+ end
+
+ # dialect
+ require 'wlang/mustang'
+ @dialect = WLang::Mustang
+
+ # template and context
+ @template = File.read(@tpl_file)
+ @context = {}
+
+ if @yaml_front_matter and
+ @template =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
+ require 'yaml'
+ @context.merge! YAML::load($1)
+ @template = $'
+ end
+ end
+
+ def with_output(&proc)
+ if @output
+ File.open(@output, 'w', &proc)
+ else
+ proc.call($stdout)
+ end
+ end
+
+ end # class Command
+end # module WLang

0 comments on commit 1efb113

Please sign in to comment.