Module: Dryml

Extended by:
Dryml
Included in:
Dryml
Defined in:
lib/dryml.rb,
lib/dryml/taglib.rb,
lib/dryml/railtie.rb,
lib/dryml/template.rb,
lib/dryml/dryml_doc.rb,
lib/dryml/part_context.rb,
lib/dryml/dryml_builder.rb,
lib/dryml/tag_parameters.rb,
lib/dryml/dryml_generator.rb,
lib/dryml/parser/document.rb,
lib/dryml/scoped_variables.rb,
lib/dryml/template_environment.rb,
lib/dryml/railtie/template_handler.rb,
lib/dryml/railtie/page_tag_resolver.rb

Overview

The Don’t Repeat Yourself Markup Language

Defined Under Namespace

Modules: DrymlDoc, Helper, Parser Classes: DRYMLBuilder, DrymlException, DrymlGenerator, DrymlSyntaxError, NoParameterError, PartContext, Railtie, ScopedVariables, TagDef, TagParameters, Taglib, Template, TemplateEnvironment

Constant Summary collapse

VERSION =
File.read(File.expand_path('../../VERSION', __FILE__)).strip
"https://github.com/Hobo/hobodoc/edit/master/dryml"
RESERVED_WORDS =
%w{if for while do class else elsif unless case when module in}
ID_SEPARATOR =
'; dryml-tag: '
APPLICATION_TAGLIB =
{ :src => "taglibs/application" }
CORE_TAGLIB =
{ :src => "core", :plugin => "dryml" }
@@root =
Pathname.new File.expand_path('../..', __FILE__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_ifObject

Returns the value of attribute last_if.



41
42
43
# File 'lib/dryml.rb', line 41

def last_if
  @last_if
end

#static_tagsObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/dryml.rb', line 141

def static_tags
  @static_tags ||= begin
                     path = if Object.const_defined?(:Rails) && FileTest.exists?("#{Rails.root}/config/dryml_static_tags.txt")
                                "#{Rails.root}/config/dryml_static_tags.txt"
                            else
                                File.join(File.dirname(__FILE__), "dryml/static_tags")
                            end
                     File.read(path).split
                   end
end

Class Method Details

.rootObject



19
# File 'lib/dryml.rb', line 19

def self.root; @@root; end

Instance Method Details

#call_render(view, local_assigns, identifier) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dryml.rb', line 68

def call_render(view, local_assigns, identifier)
  this = view.controller.send(:dryml_context) || local_assigns[:this]
  view.instance_variable_set("@this", this)
  # do this last, as TemplateEnvironment copies instance variables in initalize
  renderer = page_renderer(view, identifier, local_assigns.keys)
  if identifier =~ /#{ID_SEPARATOR}/
    tag_name = identifier.split(ID_SEPARATOR).last
    renderer.render_tag(tag_name, {:with => this} )
  else
    renderer.render_page(this, local_assigns).strip
  end
end

#clear_cacheObject



51
52
53
# File 'lib/dryml.rb', line 51

def clear_cache
  @cache = {}
end

#empty_page_renderer(view) ⇒ Object



60
61
62
# File 'lib/dryml.rb', line 60

def empty_page_renderer(view)
  page_renderer(view, page_tag_identifier(view.controller.controller_path))
end

#get_field(object, field) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dryml.rb', line 111

def get_field(object, field)
  return nil if object.nil?
  field_str = field.to_s
  case
  when object.respond_to?(field_str)
    object.send(field_str)
  when field_str =~ /^\d+$/
    object[field.to_i]
  else
    object[field]
  end
end

#get_field_path(object, path) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/dryml.rb', line 124

def get_field_path(object, path)
  return nil if object.nil?
  path = path.is_a?(String) ? path.split('.') : Array(path)
  parent = nil
  path.each do |field|
    parent = object
    object = get_field(parent, field)
  end
  [parent, path.last, object]
end

#imports_for_view(view) ⇒ Object



81
82
83
84
85
86
# File 'lib/dryml.rb', line 81

def imports_for_view(view)
  imports = []
  imports << Sprockets::Rails::Helper if defined?(Sprockets) && defined?(Rails)
  imports << ActionView::Helpers if defined?(ActionView)
  imports + view.controller.class.modules_for_helpers(view.controller.class.all_helpers_from_path(view.controller.class.helpers_path))
end

#page_renderer(view, identifier, local_names = [], controller_path = nil, imports = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dryml.rb', line 88

def page_renderer(view, identifier, local_names=[], controller_path=nil, imports=nil)
  controller_path ||= view.controller.controller_path
  if identifier =~ /#{ID_SEPARATOR}/
    identifier = identifier.split(ID_SEPARATOR).first
    @cache[identifier] ||=  make_renderer_class("", "", local_names, taglibs_for(controller_path), imports_for_view(view))
    @cache[identifier].new(view)
  else
    mtime = File.mtime(identifier)
    renderer_class = @cache[identifier]
    # do we need to recompile?
    if (!renderer_class ||                                          # nothing cached?
        (local_names - renderer_class.compiled_local_names).any? || # any new local names?
        renderer_class.load_time < mtime)                           # cache out of date?
      renderer_class = make_renderer_class(File.read(identifier), identifier,
                                           local_names, taglibs_for(controller_path),
                                           imports_for_view(view))
      renderer_class.load_time = mtime
      @cache[identifier] = renderer_class
    end
    renderer_class.new(view)
  end
end

#page_tag_identifier(controller_path, tag_name = '') ⇒ Object



64
65
66
# File 'lib/dryml.rb', line 64

def page_tag_identifier(controller_path, tag_name='')
  "controller: #{controller_path}#{ID_SEPARATOR}#{tag_name}"
end

#precompile_taglibsObject



43
44
45
46
47
48
49
# File 'lib/dryml.rb', line 43

def precompile_taglibs
  Dir.chdir(Rails.root) do
    Dir["app/views/taglibs/**/*.dryml"].each do |f|
      Taglib.get(:template_dir => File.dirname(f), :src => File.basename(f).remove(".dryml"), :source_template => "_.dryml")
    end
  end
end

#render(template_src, locals = {}, template_path = nil, included_taglibs = [], view = nil, imports = nil) ⇒ Object

FIXME: This helper seems to be useless, since it does need Rails, and with Rails it is useless since Dryml does not need Hobo

Helper function for use outside Hobo/Rails

Pass the template context in locals

This function caches. If the mtime of template_path is older than the last compilation time, the cached version will be used. If no template_path is given, template_src is used as the key to the cache.

If a local variable is not present when the template is compiled, it will be ignored when the template is used. In other words, the variable values may change, but the names may not.

included_taglibs is only used during template compilation.

Parameters:

  • template_src (String)

    the DRYML source

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

    local variables.

  • template_path (String, nil) (defaults to: nil)

    the filename of the source.

  • included_taglibs (Array) (defaults to: [])

    A list of Taglibs to include. { :src => “core”, :plugin => “dryml” } is automatically added to this list.

  • view (ActionView::Base) (defaults to: nil)

    an ActionView instance

  • A (Array)

    list of helper modules to import. For example,

    ActionView::Base


182
183
184
185
186
187
188
189
190
# File 'lib/dryml.rb', line 182

def render(template_src, locals={}, template_path=nil, included_taglibs=[], view=nil, imports=nil)
  template_path ||= template_src
  view ||= ActionView::Base.new(ActionController::Base.view_paths, {})
  this = locals.delete(:this) || nil

  renderer_class = Dryml::Template.build_cache[template_path]._?.environment ||
    make_renderer_class(template_src, template_path, locals.keys, included_taglibs, imports)
  renderer_class.new(view).render_page(this, locals)
end

#render_tag(view, tag, options = {}) ⇒ Object



55
56
57
58
# File 'lib/dryml.rb', line 55

def render_tag(view, tag, options={})
  renderer = empty_page_renderer(view)
  renderer.render_tag(tag, options)
end

#unreserve(word) ⇒ Object



135
136
137
138
139
# File 'lib/dryml.rb', line 135

def unreserve(word)
  word = word.to_s
  word += '_' if RESERVED_WORDS.include?(word)
  word
end