Class: Malt::Formats::Abstract
- Inherits:
-
Object
- Object
- Malt::Formats::Abstract
- Includes:
- Kernel
- Defined in:
- lib/malt/formats/abstract.rb
Overview
Abstract format class serves as the base class for all other format classes.
Direct Known Subclasses
CSS, HTML, Haml, LESS, Latex, Markdown, PDF, RDoc, Radius, Rtals, Ruby, Sass, Text, Textile, YAML
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Access to the options given to the initializer.
Class Method Summary collapse
- .engine(set = nil) ⇒ Object
- .extensions ⇒ Object
-
.register(*exts) ⇒ Object
Register the class to an extension type.
Instance Method Summary collapse
-
#default ⇒ Object
Default rendering type is
:html
. -
#engine ⇒ Object
Specified engine to use for rendering.
- #extensions ⇒ Object
-
#file ⇒ Object
File name of document.
- #parse_type_and_data(type_and_data) ⇒ Object
-
#refile(type = nil) ⇒ Object
Produce a new filename replacing old extension with new extension.
-
#render(*type_and_data, &yld) ⇒ Object
Render to default or given format.
- #subtype ⇒ Object
-
#text ⇒ Object
Document source text.
- #to(type, data = nil, &yld) ⇒ Object
- #to_s ⇒ Object
-
#type ⇒ Object
File extension (with prefixed dot).
Instance Attribute Details
#options ⇒ Object (readonly)
Access to the options given to the initializer.
54 55 56 |
# File 'lib/malt/formats/abstract.rb', line 54 def @options end |
Class Method Details
.engine(set = nil) ⇒ Object
32 33 34 35 |
# File 'lib/malt/formats/abstract.rb', line 32 def self.engine(set=nil) @engine = set if set @engine end |
.extensions ⇒ Object
27 28 29 |
# File 'lib/malt/formats/abstract.rb', line 27 def self.extensions @extensions end |
.register(*exts) ⇒ Object
Register the class to an extension type.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/malt/formats/abstract.rb', line 13 def self.register(*exts) @extensions = exts Malt.register(self, *exts) #exts.each do |ext| # Abstract.module_eval %{ # def to_#{ext}(*db,&yld) # convert(:#{ext},*db,&yld) # end # } #end end |
Instance Method Details
#default ⇒ Object
Default rendering type is :html
. Override if it differs for the subclassing format.
163 164 165 |
# File 'lib/malt/formats/abstract.rb', line 163 def default :html end |
#engine ⇒ Object
Specified engine to use for rendering.
Keep in mind that the ability to specify the engine varies based on engine, format and output format.
75 76 77 |
# File 'lib/malt/formats/abstract.rb', line 75 def engine [:engine] || self.class.engine end |
#extensions ⇒ Object
147 148 149 |
# File 'lib/malt/formats/abstract.rb', line 147 def extensions self.class.extensions end |
#file ⇒ Object
File name of document.
62 63 64 |
# File 'lib/malt/formats/abstract.rb', line 62 def file @file ||= [:file].to_s end |
#parse_type_and_data(type_and_data) ⇒ Object
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/malt/formats/abstract.rb', line 168 def parse_type_and_data(type_and_data) if Symbol === type_and_data.first type = type_and_data.first data = type_and_data.last else type = nil data = type_and_data.first end return type, data end |
#refile(type = nil) ⇒ Object
Produce a new filename replacing old extension with new extension.
type - Symbol representation of extension (e.g. :html).
Returns a String of the new file name.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/malt/formats/abstract.rb', line 130 def refile(type=nil) if file if type type = type.to_s.sub(/^\./,'') fext = self.class.extensions.find{|e| file.end_with?(e)} new_file = file.chomp(fext) + type else fext = self.class.extensions.find{|e| file.end_with?(e)} new_file = file.chomp('.'+fext) end else new_file = nil end new_file end |
#render(*type_and_data, &yld) ⇒ Object
Render to default or given format.
If the first argument is a Symbol it is considered the format, otherwise it is taken to be the database for rendering template variables.
88 89 90 91 |
# File 'lib/malt/formats/abstract.rb', line 88 def render(*type_and_data, &yld) type, data = parse_type_and_data(type_and_data) __send__(type || default, data, &yld) end |
#subtype ⇒ Object
152 153 154 |
# File 'lib/malt/formats/abstract.rb', line 152 def subtype File.extname(file.chomp(type)) end |
#text ⇒ Object
Document source text.
57 58 59 |
# File 'lib/malt/formats/abstract.rb', line 57 def text @text ||= [:text] || File.read(file) end |
#to(type, data = nil, &yld) ⇒ Object
80 81 82 |
# File 'lib/malt/formats/abstract.rb', line 80 def to(type, data=nil, &yld) __send__("to_#{type}", data, &yld) end |
#to_s ⇒ Object
157 158 159 |
# File 'lib/malt/formats/abstract.rb', line 157 def to_s text end |
#type ⇒ Object
File extension (with prefixed dot).
67 68 69 |
# File 'lib/malt/formats/abstract.rb', line 67 def type @type ||= File.extname(file) end |