Class: Mint::Document
- Defined in:
- lib/mint/plugins/epub.rb,
lib/mint/document.rb
Overview
Add chapters to document – this is probably not a sustainable pattern for all plugins, but it’s useful here.
Constant Summary collapse
- METADATA_DELIM =
"\n\n"
Instance Attribute Summary collapse
-
#content ⇒ Object
Implicit readers are paired with explicit accessors.
-
#layout ⇒ Object
Implicit readers are paired with explicit accessors.
-
#metadata ⇒ Object
readonly
Implicit readers are paired with explicit accessors.
-
#style ⇒ Object
Implicit readers are paired with explicit accessors.
-
#style_destination ⇒ Object
Explanation of style_destination:.
Attributes inherited from Resource
#destination, #name, #root, #source, #type
Class Method Summary collapse
Instance Method Summary collapse
- #chapters ⇒ Object
-
#initialize(source, opts = {}) {|_self| ... } ⇒ Document
constructor
Creates a new Mint Document object.
- #inline_styles ⇒ Object
-
#publish!(opts = {}) ⇒ Object
Writes all rendered content where a) possible, b) required, and c) specified.
-
#render(args = {}) ⇒ Object
Renders content in the context of layout and returns as a String.
-
#style_destination_directory ⇒ String
Exposes style_destination directory as a String.
-
#style_destination_directory_path ⇒ Pathname
Exposes style_destination directory as a Pathname object.
-
#style_destination_file ⇒ String
Exposes style_destination as a String.
-
#style_destination_file_path ⇒ Pathname
Exposes style_destination as a Pathname object.
-
#stylesheet ⇒ Object
Returns a relative path from the document to its stylesheet.
-
#template=(template) ⇒ Object
Overrides layout and style settings with named template.
Methods inherited from Resource
#destination_directory, #destination_directory_path, #destination_file, #destination_file_path, #equal?, #renderer=, #root_directory, #root_directory_path, #source_directory, #source_directory_path, #source_file, #source_file_path
Constructor Details
#initialize(source, opts = {}) {|_self| ... } ⇒ Document
Creates a new Mint Document object. Can be block initialized. Accepts source and options. Block initialization occurs after all defaults are set, so not all options must be specified.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mint/document.rb', line 12 def initialize(source, opts={}) = Mint..merge opts # Loads source and destination, which will be used for # all source_* and destination_* virtual attributes. super(source, ) self.type = :document # Each of these should invoke explicitly defined method self.content = source self.layout = [:layout] self.style = [:style] self.style_destination = [:style_destination] # The template option will override layout and style choices self.template = [:template] # Yield self to block after all other parameters are loaded, # so we only have to tweak. (We don't have to give up our # defaults or re-test blocks beyond them being tweaked.) yield self if block_given? end |
Instance Attribute Details
#content ⇒ Object
Implicit readers are paired with explicit accessors. This allows for processing variables before storing them.
66 67 68 |
# File 'lib/mint/document.rb', line 66 def content @content end |
#layout ⇒ Object
Implicit readers are paired with explicit accessors. This allows for processing variables before storing them.
66 67 68 |
# File 'lib/mint/document.rb', line 66 def layout @layout end |
#metadata ⇒ Object (readonly)
Implicit readers are paired with explicit accessors. This allows for processing variables before storing them.
66 67 68 |
# File 'lib/mint/document.rb', line 66 def @metadata end |
#style ⇒ Object
Implicit readers are paired with explicit accessors. This allows for processing variables before storing them.
66 67 68 |
# File 'lib/mint/document.rb', line 66 def style @style end |
#style_destination ⇒ Object
Explanation of style_destination:
I’m going to maintain a document’s official style_destination outside of its style object. If a document has no style_destination defined when it needs one, the document will use the original style’s source directory.
This decision eliminates edge cases, including the case where we want to generate, but not move, a document’s style. It also lets us keep style information separate from document-specific information. (Without this separation, funky things happen when you assign a new style template to an existing document – if you had specified a custom style_destination before changing the template, that custom destination would be overridden.)
The style_destination attribute is lazy. It’s exposed via virtual attributes like #style_destination_file.
151 152 153 |
# File 'lib/mint/document.rb', line 151 def style_destination @style_destination end |
Class Method Details
.metadata_chunk(text) ⇒ Object
211 212 213 |
# File 'lib/mint/document.rb', line 211 def (text) text.split(METADATA_DELIM).first end |
.metadata_from(text) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/mint/document.rb', line 215 def (text) = YAML.load (text) case when String {} when false {} else end rescue {} end |
.parse_metadata_from(text) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mint/document.rb', line 230 def (text) = text new_text = if !.empty? text.sub (text) + METADATA_DELIM, "" else text end [, new_text] end |
Instance Method Details
#chapters ⇒ Object
19 20 21 22 |
# File 'lib/mint/plugins/epub.rb', line 19 def chapters html_document = Nokogiri::HTML::Document.parse render EPub.split_on(html_document, "h2").map &:to_s end |
#inline_styles ⇒ Object
204 205 206 |
# File 'lib/mint/document.rb', line 204 def inline_styles CSS.parse() end |
#publish!(opts = {}) ⇒ Object
Writes all rendered content where a) possible, b) required, and c) specified. Outputs to specified file.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mint/document.rb', line 43 def publish!(opts={}) = { :render_style => true }.merge(opts) FileUtils.mkdir_p self.destination_directory File.open(self.destination_file, "w+") do |f| f << self.render end # Only renders style if a) it's specified by the options path and # b) it actually needs rendering (i.e., it's in template form and # not raw, browser-parseable CSS) or it if it doesn't need # rendering but there is an explicit style_destination. if [:render_style] FileUtils.mkdir_p style_destination_directory File.open(self.style_destination_file, "w+") do |f| f << self.style.render end end Mint.after_publish(self, opts) end |
#render(args = {}) ⇒ Object
Renders content in the context of layout and returns as a String.
36 37 38 39 |
# File 'lib/mint/document.rb', line 36 def render(args={}) intermediate_content = layout.render self, args Mint.after_render(intermediate_content) end |
#style_destination_directory ⇒ String
Exposes style_destination directory as a String.
191 192 193 |
# File 'lib/mint/document.rb', line 191 def style_destination_directory style_destination_directory_path.to_s end |
#style_destination_directory_path ⇒ Pathname
Exposes style_destination directory as a Pathname object.
184 185 186 |
# File 'lib/mint/document.rb', line 184 def style_destination_directory_path style_destination_file_path.dirname end |
#style_destination_file ⇒ String
Exposes style_destination as a String.
177 178 179 |
# File 'lib/mint/document.rb', line 177 def style_destination_file style_destination_file_path.to_s end |
#style_destination_file_path ⇒ Pathname
Exposes style_destination as a Pathname object.
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/mint/document.rb', line 163 def style_destination_file_path if style_destination path = Pathname.new style_destination dir = path.absolute? ? path : destination_directory_path + path dir + style.name else style.destination_file_path end end |
#stylesheet ⇒ Object
Returns a relative path from the document to its stylesheet. Can be called directly from inside a layout template.
199 200 201 202 |
# File 'lib/mint/document.rb', line 199 def stylesheet Helpers.normalize_path(self.style_destination_file, self.destination_directory).to_s end |
#template=(template) ⇒ Object
Overrides layout and style settings with named template.
127 128 129 130 131 132 |
# File 'lib/mint/document.rb', line 127 def template=(template) if template self.layout = template self.style = template end end |