init: engine + first engine (kramdown)

This commit is contained in:
Denis Knauf 2013-01-20 15:02:38 +01:00
commit bbc0375243
8 changed files with 190 additions and 0 deletions

6
.gitignore vendored Normal file
View file

@ -0,0 +1,6 @@
*.swp
log
*.gem
*.gemspec
tmp
pkg

1
AUTHORS Normal file
View file

@ -0,0 +1 @@
Denis Knauf <DenisDotKnaufPlusUteAtGmailDotCom>

1
README.md Normal file
View file

@ -0,0 +1 @@
Ups, war bisher zu faul. :/

57
Rakefile Normal file
View file

@ -0,0 +1,57 @@
require 'rubygems'
require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "Ute"
gem.summary = %Q{Universal Template Engines Collection}
gem.description = %Q{Many template engines in one collection with one universal API}
gem.email = "Denis.Knauf@gmail.com"
gem.homepage = "http://github.com/DenisKnauf/Ute"
gem.authors = ["Denis Knauf"]
gem.files = %w[AUTHORS README.md VERSION lib/**/*.rb test/**/*.rb]
gem.require_paths = %w[lib]
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test' << 'ext'
test.pattern = 'test/**/*_test.rb'
test.verbose = true
end
begin
require 'rcov/rcovtask'
Rcov::RcovTask.new do |test|
test.libs << 'test'
test.pattern = 'test/**/*_test.rb'
test.verbose = true
end
rescue LoadError
task :rcov do
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
end
end
#task :test => :check_dependencies
#task :default => :test
require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
if File.exist?('VERSION')
version = File.read('VERSION')
else
version = ""
end
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "Ute #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end

1
VERSION Normal file
View file

@ -0,0 +1 @@
0.0.1

39
lib/templates.rb Normal file
View file

@ -0,0 +1,39 @@
module Templates
class ImplementationExpected < Exception
end
@templates = Hash.new {|h,k| h[k] = [] }
@preferred = {}
class <<self
def normalize ext
ext.to_s.downcase.sub( /^\./, '').to_sym
end
def [] filename
%r<\.([^./]+)$> =~ filename
ext = normalize $1
@preferred[ext] || @templates[ext][0]
end
def new filename, options = nil
options[:filename] = filename
engine = self[filename]
engine && engine.new( options)
end
def register klass, *exts
exts.each do |ext|
@templates[normalize ext].unshift( klass).uniq!
end
end
def prefer klass, *exts
if exts.empty?
@templates.each {|ext, klasses| @preferred[ext] = klass if klasses.include? klass }
else
register klass, *exts
exts.each {|ext| @preferred[normalize ext] = klass }
end
end
end
end

58
lib/templates/engine.rb Normal file
View file

@ -0,0 +1,58 @@
class Templates::Engine
class <<self
# don't forget to implement initialize for your engine.
# @initialized must be set to true or false like return:
# return: true: engine can be provieded
# false: engine can not be provieded (lib not found for example)
def initialize
raise ImplementationExpected, "Implementation for #{name}.initialize needed"
end
def require_template_library *libs
libs.each {|lib| require lib }
true
rescue LoadError
return false
end
def initialized?() @initialized end
def register *exts
@extentions = exts
Templates.register self, *exts
end
end
attr_reader :options, :filename, :line, :data
def initialize options = nil
@options = options.nil? ? {} : options.dup
@options[:filename] ||= @options[:file].nil? ? '`(_}-{_)`' : @options[:file].path
@filename = @options[:filename]
@line = @options[:line] || 1
@data = if @options[:content]
@options[:content]
elsif @options[:file]
@options[:file].read
elsif @options[:filename]
File.read @options[:filename]
end
end
def evaluate
raise ImplementationExpected, "Implementation for #{name}#evaluate needed"
end
def prepare
raise ImplementationExpected, "Implementation for #{name}#prepare needed"
end
def output
@output ||= evaluate
end
def render
prepare
output
end
end

View file

@ -0,0 +1,27 @@
class Templates::Engines::Kramdown < Templates::Engine
DUMB_QUOTES = [39, 39, 34, 34]
class <<self
def initialized?
defined? ::Kramdown
end
def initialize
require_template_library 'kramdown'
end
end
def prepare
options = @options[:options]
options = options.nil? ? {} : options.dup
options[:smart_quotes] = DUMB_QUOTES unless options[:smartypants]
@engine = ::Kramdown::Document.new @data, options
@output = nil
end
def evaluate
@engine.to_html
end
register :markdown, :mkd, :md
end