Permalink
Browse files

Initial sorta.

  • Loading branch information...
1 parent 79de468 commit e78bf88173b035a4780564976ac420bdef83a484 @chrislloyd committed Aug 16, 2011
View
@@ -1,5 +1,5 @@
require 'rake/clean'
-require 'bundler/gem_tasks'
+# require 'rake/gem_tasks'
require 'rake/testtask'
Rake::TestTask.new do |t|
View
@@ -0,0 +1,92 @@
+#!/usr/bin/env ruby
+# encoding: utf-8
+
+require 'yaml'
+require 'optparse'
+
+# TODO: Remove
+$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
+
+require 'tuml'
+
+class Tuml
+ class CLI
+ # Return a structure describing the options.
+ def self.parse_options(args)
+ OptionParser.new do |opts|
+ opts.banner = "Usage: tuml [-t] TEMPLATE [DATA] [URL]"
+
+ opts.separator " "
+
+ opts.separator "Examples:"
+ opts.separator " $ tuml data.yml template.tuml"
+ opts.separator " $ tuml data.yml template.tuml posts/231234"
+ opts.separator " $ cat data.yml | tuml - template.tuml"
+
+ # opts.separator " See tuml(1) or " +
+ # "http://rollrapp.com"
+ # opts.separator " for more details."
+
+ opts.separator " "
+ opts.separator "Options:"
+
+ opts.on("-t", "--tokens FILE",
+ "Print the tokenized form of a given template.") do |file|
+ require 'pp'
+ pp Tuml::Template.new(File.read(file)).tokens
+ exit
+ end
+
+
+ opts.separator ""
+ opts.separator "Common Options:"
+
+ opts.on_tail("-v", "--version", "Print the version") do |v|
+ puts "Tuml v#{Tuml::Version}"
+ exit
+ end
+
+ opts.on_tail("-h", "--help", "Show this message") do
+ puts opts
+ exit
+ end
+ end.parse!(args)
+ end
+
+ # Does the dirty work of reading files from STDIN and the command
+ # line then processing them. The meat of this script, if you will.
+ def self.process_files(template, data=:dummy, path='/')
+
+ t = File.read(template)
+ d = YAML.load_file(data)
+
+ ctx = Tuml::IndexPage.new(d)
+
+ puts Tuml::Template.new(t).render(ctx)
+
+ # doc = input_stream.read
+ #
+ # if doc =~ /^(\s*---(.+)---\s*)/m
+ # yaml = $2.strip
+ # template = doc.sub($1, '')
+ #
+ # YAML.each_document(yaml) do |data|
+ # puts Tuml.render(template, data)
+ # end
+ # else
+ # puts Tuml.render(doc)
+ # end
+
+ end
+ end
+end
+
+# Help is the default.
+ARGV << '-h' if ARGV.empty? && $stdin.tty?
+
+# Process options
+Tuml::CLI.parse_options(ARGV)
+
+# Still here - process ARGV
+Tuml::CLI.process_files(*ARGV)
+
View
@@ -1,4 +1,18 @@
require 'tuml/version'
-require 'tuml/parser'
-require 'tuml/compiler'
require 'tuml/template'
+require 'tuml/context'
+require 'tuml/page'
+require 'tuml/post'
+
+require 'tuml/pages'
+require 'tuml/posts'
+
+class Tuml
+
+ ROOT_PATH = '/'
+
+ # def self.render(template, data)
+ # new(template, data)
+ # end
+
+end
View
@@ -1,99 +0,0 @@
-require 'json'
-
-module Tuml
- class Compiler
-
- def initialize(context)
- @context = context
- end
-
- def compile node
- type = node.shift
- send("on_#{type}", *node)
- end
-
- def on_multi *parts
- parts.map {|part| compile(part)}.join
- end
-
- def on_static text
- text
- end
-
- def on_noop
- ''
- end
-
- def on_block name, body
- var = @context.lookup(name)
- return unless var
-
- [*var].map do |ctx|
- @context.push ctx
- result = compile(body)
- @context.pop
- result
- end.join
- end
-
- def on_var name, attrs={}
- @context.lookup name, attrs
- end
-
- def on_esc type, tag
- send "esc_#{type}", compile(tag)
- end
-
- def on_cond type, name, body
- var = @context.lookup name
- if (type == :if && var) || (type == :ifnot && !var)
- compile(body)
- end
- ''
- end
- end
-
- def on_text label
- @context.lookup_text label
- end
-
- def on_color label
- @context.lookup_color label
- end
-
- def on_font label
- @context.lookup_font label
- end
-
- def on_image label
- @context.lookup_image label
- end
-
- def on_lang label
- @context.lookup_lang label
- end
-
-
- # Escapes
-
- def esc_js str
- str.to_json
- end
-
- # TODO
- def esc_jsplaintext str
- str
- end
-
- # TODO
- def esc_plaintext str
- str
- end
-
- # TODO
- def esc_urlencoded str
- str
- end
-
- end
-end
View
@@ -1,5 +1,40 @@
-module Tuml
+class Tuml
class Context
+ attr_reader :data
+
+ attr_accessor :parent
+
+ def self.prototype
+ @prototype ||= {}
+ end
+
+ def self.tag(name, &blk)
+ prototype[name] = block_given? ? blk : -> { data[name]}
+ end
+
+ def self.block(name, klass=nil, &blk)
+ prototype["block:#{name}"] = blk
+ end
+
+
+ def initialize(data)
+ @data = data
+ end
+
+ def find(name)
+ method = self.class.prototype[name]
+ if method
+ instance_exec &method
+ elsif parent
+ parent.find(name)
+ end
+ end
+
+ # TODO: Rename this method
+ def raw_block(name)
+ data["block:#{name}"] || []
+ end
+
end
end
View
@@ -0,0 +1,5 @@
+class Tuml
+ class Frame < Hash
+
+ end
+end
View
@@ -0,0 +1,108 @@
+require 'json'
+require 'colored'
+
+class Tuml
+ class Generator
+
+ def initialize(ctx)
+ @ctx = ctx
+ end
+
+ def compile(node)
+ type = node.first
+ send("on_#{type}", *node[1..-1])
+ end
+
+ def on_multi *parts
+ parts.map {|part| compile(part)}.join
+ end
+
+ def on_static text
+ text
+ end
+
+ def on_noop
+ ''
+ end
+
+ def on_block name, body
+ if var = @ctx.find("block:#{name}")
+ if var.is_a?(Array)
+ var.map do |ctx|
+ ctx.parent = @ctx
+ @ctx = ctx
+
+ result = compile(body)
+
+ @ctx = ctx.parent
+
+ result
+ end.join
+ else
+ compile(body)
+ end
+ end
+ end
+
+ def on_tag name, attrs={}
+ @ctx.find(name)
+ end
+
+ def on_esc type, tag
+ send "esc_#{type}", compile(tag)
+ end
+
+ def on_cond type, name, body
+ ''
+ # var = @ctx.find(name)
+ # if (type == :if && var) || (type == :ifnot && !var)
+ # compile(body)
+ # else
+ # ''
+ # end
+ end
+
+ def on_text label
+ @ctx.find(label)
+ end
+
+ def on_color label
+ @ctx.find(label)
+ end
+
+ def on_font label
+ @ctx.find(label)
+ end
+
+ def on_image label
+ @ctx.find(label)
+ end
+
+ def on_lang label
+ @ctx.find(label)
+ end
+
+
+ # Escapes
+
+ def esc_js str
+ str.to_json
+ end
+
+ # TODO
+ def esc_jsplaintext str
+ str
+ end
+
+ # TODO
+ def esc_plaintext str
+ str
+ end
+
+ # TODO
+ def esc_urlencoded str
+ str
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit e78bf88

Please sign in to comment.