Module: R2Doc::TemplateUtil
- Included in:
- Generators::R2DOCGenerator, RDoc::Generator::R2DOC
- Defined in:
- lib/r2doc/template_util.rb
Overview
Utility functions available for use in templates
Class Method Summary collapse
-
.gen_url(path, target) ⇒ Object
Convert a target url to one that is relative to a given path.
Instance Method Summary collapse
-
#content_tag(tagname, content, html_options = {}) ⇒ Object
Return an html tag with content an attributed defined by the provided hash.
-
#link_to(content, url, html_options = nil) ⇒ Object
A much simplified and altered version of the link_to method in ActionView::Helpers::UrlHelper, creates a link of the given
contentto the givenurl. -
#path_to(url) ⇒ Object
Return the relative path to
urlfor the current file. -
#render_partial(partial, locals = nil) ⇒ Object
Runs a partial template and return the contents.
-
#stripe_class(index, class_prefix = '') ⇒ Object
Returns an even or odd class name for striping rows using a zero-based index.
Class Method Details
.gen_url(path, target) ⇒ Object
Convert a target url to one that is relative to a given path. This method is updated to account for paths to files in the root directory.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/r2doc/template_util.rb', line 8 def TemplateUtil.gen_url(path, target) from = File.dirname(path) to, to_file = File.split(target) from = from.split("/") to = to.split("/") from.shift if from.first == '.' to.shift if to.first == '.' while from.size > 0 and to.size > 0 and from[0] == to[0] from.shift to.shift end from.fill("..") from.concat(to) from << to_file File.join(*from) end |
Instance Method Details
#content_tag(tagname, content, html_options = {}) ⇒ Object
Return an html tag with content an attributed defined by the provided hash
71 72 73 74 |
# File 'lib/r2doc/template_util.rb', line 71 def content_tag(tagname, content, = {}) attr_string = .collect{|k,v| " #{h(k.to_s)}=\"#{h(v)}\""}.join('') "<#{tagname}#{attr_string}>#{content}</#{tagname}>" end |
#link_to(content, url, html_options = nil) ⇒ Object
A much simplified and altered version of the link_to method in ActionView::Helpers::UrlHelper, creates a link of the given content to the given url. If url is a string it is assumed to be relative to the top of the documentation directory. If url is a ContextUser object, its path is used. The final URL is adjusted to match the path of the current file being output. If html_options are provided, they become attributes of the returned tag.
55 56 57 58 59 60 |
# File 'lib/r2doc/template_util.rb', line 55 def link_to(content, url, = nil) url = url.path if url.respond_to?(:path) attrs = .nil? ? {} : attrs[:href] = R2Doc::TemplateUtil.gen_url(@current_path, url) content_tag 'a', content, attrs end |
#path_to(url) ⇒ Object
Return the relative path to url for the current file. Assumes that url is a ContextUser or path relative to the top of the documentation directory.
65 66 67 68 |
# File 'lib/r2doc/template_util.rb', line 65 def path_to(url) url = url.path if url.respond_to?(:path) R2Doc::TemplateUtil.gen_url(@current_path, url) end |
#render_partial(partial, locals = nil) ⇒ Object
Runs a partial template and return the contents. This is similar to, but much lighter weight than the notion of partials in Rails. Example usage:
render_partial :foo # renders the partial in '_foo.html.erb'
render_partial :foo, {:bar=>1} # renders the partial and sets local variable bar to 1
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/r2doc/template_util.rb', line 34 def render_partial(partial, locals = nil) local_assign_code = locals.nil? ? '' : locals.keys.collect{|k| "#{k} = locals[:#{k}];"}.join template = "#{template_name}/_#{partial.to_s}" proc_code = " Proc.new {\n \#{local_assign_code}\n R2Doc::TemplateManager.load_template('\#{template}').result(binding)\n }\n end_src\n p = eval proc_code\n p.call\nend\n" |
#stripe_class(index, class_prefix = '') ⇒ Object
Returns an even or odd class name for striping rows using a zero-based index
77 78 79 |
# File 'lib/r2doc/template_util.rb', line 77 def stripe_class(index, class_prefix = '') "#{class_prefix}#{index % 2 == 0 ? 'odd' : 'even'}" end |