Module: SemiStatic::Convertable
- Includes:
- ERB::Util
- Defined in:
- lib/semi-static/convertable.rb
Overview
Support conversion of the source file into another format.
Instance Method Summary collapse
-
#content(options = {}) ⇒ Object
Format the source file.
-
#layout ⇒ Object
:nodoc:.
-
#layout_name ⇒ Object
:nodoc:.
-
#load ⇒ Object
:nodoc:.
-
#object_ref(object) ⇒ Object
This method is adapted from Haml::Buffer#parse_object_ref – it’s used to make it easier for Haml and ERB layouts to generate the same output so I can use the same test output for both.
-
#render(options = {}) ⇒ Object
Wrap the formatted source file in the Layout (if any).
-
#snippet(name) ⇒ Object
Helper method used to insert a Snippet into the formatted output.
-
#source_mtime ⇒ Object
Add the layout used (if any) to the list of files used to determine the objects modification time.
-
#underscore(camel_cased_word) ⇒ Object
Changes a word from camel case to underscores.
Instance Method Details
#content(options = {}) ⇒ Object
Format the source file.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/semi-static/convertable.rb', line 17 def content(={}) return @content unless @content.nil? = { :page => self }.merge() for name, value in eval "#{name.to_s} = options[name]" end site.stats.record(self.class.name.split('::').last, source_path) do case self.source_ext when '.md', '.markdown' @content = Maruku.new(self.source_content).to_html when '.haml' Haml::Engine.new(self.source_content, :filename => source_path).render(binding) when '.erb' erb = ERB.new self.source_content, nil, '-' erb.filename = source_path erb.result(binding) when '.html' self.source_content else raise ArgumentError, "Unsupported format: #{self.source_path}" end end end |
#layout ⇒ Object
:nodoc:
49 50 51 52 53 54 55 |
# File 'lib/semi-static/convertable.rb', line 49 def layout #:nodoc: if layout_name.nil? return nil else return site.layouts[layout_name.to_sym] end end |
#layout_name ⇒ Object
:nodoc:
43 44 45 46 47 |
# File 'lib/semi-static/convertable.rb', line 43 def layout_name #:nodoc: unless .nil? || !.include?(:layout) [:layout].to_sym end end |
#load ⇒ Object
:nodoc:
7 8 9 10 11 12 13 |
# File 'lib/semi-static/convertable.rb', line 7 def load #:nodoc: @content = nil if layout layout.load end super end |
#object_ref(object) ⇒ Object
This method is adapted from Haml::Buffer#parse_object_ref – it’s used to make it easier for Haml and ERB layouts to generate the same output so I can use the same test output for both.
89 90 91 92 93 94 |
# File 'lib/semi-static/convertable.rb', line 89 def object_ref(object) #:nodoc: return '' if object.nil? name = underscore(object.class) id = "#{name}_#{object.id || 'new'}" return "class='#{name}' id='#{id}'" end |
#render(options = {}) ⇒ Object
Wrap the formatted source file in the Layout (if any).
70 71 72 73 74 75 76 77 |
# File 'lib/semi-static/convertable.rb', line 70 def render(={}) content = self.content() if layout = { :page => self }.merge content = layout.render(.merge( :content => content )) end return content end |
#snippet(name) ⇒ Object
Helper method used to insert a Snippet into the formatted output.
81 82 83 84 |
# File 'lib/semi-static/convertable.rb', line 81 def snippet(name) name = name.to_s site.snippets[name].render :page => self end |
#source_mtime ⇒ Object
Add the layout used (if any) to the list of files used to determine the objects modification time.
60 61 62 63 64 65 66 |
# File 'lib/semi-static/convertable.rb', line 60 def source_mtime mtime = super if layout && layout.source_mtime > mtime mtime = layout.source_mtime end return mtime end |
#underscore(camel_cased_word) ⇒ Object
Changes a word from camel case to underscores. Based on the method of the same name in Rails’ Inflector, but copied here so it’ll run properly without Rails.
99 100 101 102 103 104 105 |
# File 'lib/semi-static/convertable.rb', line 99 def underscore(camel_cased_word) #:nodoc: camel_cased_word.to_s.gsub(/::/, '_'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |