/lib/shada/shada_app/templator.rb
https://github.com/bairdb/shada · Ruby · 252 lines · 208 code · 36 blank · 8 comment · 17 complexity · 0e3e57116181fde2aafe653b9a8e6e36 MD5 · raw file
- #!/usr/bin/env ruby -wKU
- #require "iconv"
- require 'shada/shada_logger'
- require 'shada/shada_utils'
- module Shada
- class Templator
-
- include Shada::Logger
-
- attr_accessor :registry, :html
-
- def initialize
- #@ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
-
- @pattern = /\{\$([^\r\n]*?)\}/m
- @alt_pattern = /\[\$(.*?)\]/m
- @include_pattern = /\{\include file\=\"(.*?)\"\}/
- @result_pattern = /\{results for \$(.*?)}(?:(.*?))\{\/results\}/m
- @result_var_pattern = /\[\$(.*)\]/
- @function_pattern = /\{\$(.*?)\->(.*?)\}/
- @array_pattern = /\{\$(.*?)\>(.*?)\}/
- @file_pattern = /\{file\=\"(.*)\"\}/
- @block_pattern = /\{block\=\"(.*)\"\}/
- @param_pattern = /\[\$(.*?)\]/
-
- @tag_arr = []
- @rep_arr = []
-
- @html = ''
- @tags = []
-
- @flow = {:include => 'includes', :file => 'files', :value => 'values', :image => 'images', :array => 'arrays'}
- @flow2 = {:result => 'results', :function => 'functions'}
- @parse_arr = {:result => '->', :function => '->', :array => '>'}
-
- @content_arr = []
- @result_arr = []
-
- @registry = {}
-
- register 'exclude', ''
- end
-
- #register
- def register key, val, type="value"
- @registry[key] = {:value => val, :type => type}
- end
-
- def unregister key
- @registry.delete key
- end
-
- def open_template file
- unless File.directory?(file)
- File.read file
- else
- ''
- end
- end
-
- def init template
- @html = open_template template
- @html = @html.encode('utf-8')
- includes
- preprocess_results
- parse 1
- parse 2
- render
- results
- parse 1
- parse 2
- #files
- #block
- cleanup
- end
-
- def gettags
- @html = @html.encode('utf-8')
- @tags = @html.scan @pattern
- end
-
- def parse pass
- gettags
- @registry.each do |key, val|
- type = val[:type]
- parse_val = @parse_arr[type]
- @tags.each do |tag|
- tag_parse = parse_val ? tag[0].split(parse_val) : ''
- tag_clean = parse_val ? tag_parse[0] : tag[0]
- if tag_clean == key.to_s
- arr = pass == 1 ? @flow : @flow2
- if arr.has_key? type.to_sym
- value = val[:value]
- self.send arr[type.to_sym], {:value => value, :key => key, :tag => tag[0], :parse_val => parse_val}
- end
- end
- end
- end
- end
-
- def values hash
- @tag_arr.push /\{\$#{hash[:key].to_s}\}/
- if hash[:value].is_a?(String)
- @rep_arr.push hash[:value].encode('utf-8')
- else
- @rep_arr.push hash[:value]
- end
- @html = @html.force_encoding('UTF-8')
-
- @tag_arr.zip(@rep_arr).each do |key, val|
- begin
- @html.gsub! key, val.force_encoding('UTF-8')
- rescue => e
- @html.gsub! key, ''
- end
- end
- end
-
- def includes
- @html = @html.encode('utf-8')
- inc = @html.scan @include_pattern
- tag_arr = []
- rep_arr = []
- inc.each do |val|
- pattern = /{include file="#{Regexp.quote(val[0])}"}/
- begin
- html = open_template val[0]
- @html.gsub! pattern, html
- rescue => e
- #puts e.message
- @html.gsub! pattern, ''
- end
- end
-
- inc2 = @html.scan @include_pattern
- unless inc2.empty?
- includes
- end
- end
-
- def arrays hash
- tag = hash[:tag].split hash[:parse_val]
- hash[:value].each do |key, val|
- arr_val = key == tag[1].to_sym ? val : ''
- unless arr_val.nil?
- @tag_arr.push /\{\$#{Regexp.quote(hash[:tag])}\}/
- @rep_arr.push arr_val
- end
- end
- end
-
- def functions hash, result=false
- value = hash[:tag].split hash[:parse_val]
- val = @registry[value[0]]
- klass = val[:value].class == String ? Object.const_get(val[:value]).new : val[:value]
- klass_name = val[:value].class == String ? val[:value].to_s : val[:value].class.to_s
- function = value[1]
- function_pieces = function.scan /(.*)\((.*)\)/ || function
- function_name = function.gsub /\((.*)\)/, ''
- oparam_arr = function_pieces[0][1].split(',').map do |val|
- m = val.strip.scan(@param_pattern)
- if m.count > 0
- begin
- @registry[(m[0][0]).to_sym][:value]
- rescue => e
- ""
- end
- else
- val.strip
- end
- end
- res = klass.send function_name.to_sym, *oparam_arr
-
- unless result
- reg_str = Regexp.quote "{$#{value[0]}->#{function}}"
- begin
- @html.gsub! /#{reg_str}/, res
- rescue => e
- ''
- end
- else
- res
- end
- end
-
- def preprocess_results
- @html = @html.encode('utf-8')
- @html.scan(@result_pattern).inject(0) do |i, result|
- @content_arr.push result[1]
- @html = @html.gsub /\{results for \$#{Regexp.quote(result[0])}\}(.*?)\{\/results\}/m, "{results for $#{result[0]}}%%replacement_#{i}%%{/results}"
- i + 1
- end
- end
-
- def results
- tags = []
- rep = []
-
- @html = @html.encode('utf-8')
- @html.scan(@result_pattern).inject(0) do |i, result|
- @rep_pattern = @content_arr[i].to_s.strip
- @tmp = ""
- begin
- @html = @html.gsub "%%replacement_#{i}%%", @content_arr[i]
- #@content_arr.delete_at(i)
- rescue => e
- end
-
- hash = {:tag => result[0], :parse_val => @parse_arr[:function]}
- arr = functions hash, true
- if arr.class == Array
- arr.each do |row|
- lrep = @rep_pattern
- if row.class == Hash
- row.each do |k,v|
- puts "#{k} - #{v}"
- lrep = lrep.gsub /\{\$#{k.to_s}\}/, v
- lrep = lrep.gsub /\[\$#{k.to_s}\]/, v
- end
- @tmp.insert -1, lrep
- lrep = ""
- else
- row.fields.each do |f|
- lrep = lrep.gsub /\{\$#{f.to_s}\}/, "#{row.instance_variable_get("@#{f.to_s}")}"
- lrep = lrep.gsub /\[\$#{f.to_s}\]/, "#{row.instance_variable_get("@#{f.to_s}")}"
- end
- @tmp.insert -1, lrep
- lrep = ""
- end
- end
- end
-
- preprocess_results
-
- @html.gsub! /\{results for \$#{Regexp.quote(result[0])}\}(.*?)\{\/results\}/m, @tmp
-
-
- i + 1
- end
- end
-
- def cleanup
- @html.gsub! @pattern, ''
- end
-
- def render
- end
- end
- end