Class: Neapolitan::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/neapolitan/factory.rb

Overview

Controls rendering to a variety of back-end templating and markup systems via Malt.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Factory

Returns a new instance of Factory.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :types (Array)


14
15
16
17
# File 'lib/neapolitan/factory.rb', line 14

def initialize(options={})
  @types  = options[:types]
  @system = options[:system] || :malt
end

Instance Attribute Details

#types ⇒ Object (readonly)

Returns the value of attribute types.



8
9
10
# File 'lib/neapolitan/factory.rb', line 8

def types
  @types
end

Class Method Details

.render(text, format, data, &content) ⇒ Object



117
118
119
# File 'lib/neapolitan/factory.rb', line 117

def self.render(text, format, data, &content) 
  new.render(text, format, data, &content)
end

Instance Method Details

#coderay(input, format) ⇒ Object

Apply coderay syntax highlighting. This is a psuedo-format.



77
78
79
80
81
82
# File 'lib/neapolitan/factory.rb', line 77

def coderay(input, format)
  require 'coderay'
  format = format.split('.')[1] || :ruby #:plaintext
  tokens = CodeRay.scan(input, format.to_sym) #:ruby
  tokens.div()
end

#malt ⇒ Malt::Machine

Get cached instance of Malt controller.

Returns:

  • (Malt::Machine) —

    mutli-format renderer



66
67
68
69
70
71
72
73
74
# File 'lib/neapolitan/factory.rb', line 66

def malt
  @malt ||= (
    if types && !types.empty?
      Malt::Machine.new(:types=>types)
    else
      Malt::Machine.new
    end
  )
end

#render(text, format, scope, locals, &content) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/neapolitan/factory.rb', line 20

def render(text, format, scope, locals, &content) 
  case format
  when /^coderay/
    coderay(text, format)
  when /^syntax/
    syntax(text, format)
  when /^rubypants/
    rubypants(text, format)
  else
    if @system == :tilt
      render_via_tilt(text, format, scope, locals, &content)
    else
      render_via_malt(text, format, scope, locals, &content)
    end
  end
end

#render_via_malt(text, format, scope, locals, &content) ⇒ Object

Render via Malt.



38
39
40
41
# File 'lib/neapolitan/factory.rb', line 38

def render_via_malt(text, format, scope, locals, &content)
  doc = malt.text(text, :type=>format.to_sym)
  doc.render(scope, locals, &content)
end

#render_via_tilt(text, format, scope, locals, &content) ⇒ Object

Render via Tilt.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/neapolitan/factory.rb', line 44

def render_via_tilt(text, format, scope, locals, &content)
  if engine = Tilt[format]
    #case data
    #when Hash
    #  scope = Object.new
    #  table = data
    #when Binding
    #  scope = data.eval('self')
    #  table = {}
    #else # object scope
    #  scope = data
    #  table = {}
    #end
    engine.new{text}.render(scope, locals, &content)
  else
    text
  end
end

#rubypants(input_html, format) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/neapolitan/factory.rb', line 94

def rubypants(input_html, format)
  require 'rubypants'
  format, flag = format.split('.')
  flag  = (flag || 2).to_i
  pants = RubyPants.new(input_html, flag)
  pantts.to_html
end

#syntax(input, format) ⇒ Object

Apply syntax syntax highlighting. This is a psuedo-format.



85
86
87
88
89
90
91
# File 'lib/neapolitan/factory.rb', line 85

def syntax(input, format)
  require 'syntax/convertors/html'
  format = format.split('.')[1] || 'ruby' #:plaintext
  lines  = true
  conv   = Syntax::Convertors::HTML.for_syntax(format)
  conv.convert(input,lines)
end