Class: Bunto::Document
- Inherits:
-
Object
- Object
- Bunto::Document
- Includes:
- Comparable
- Defined in:
- lib/bunto/document.rb
Constant Summary collapse
- YAML_FRONT_MATTER_REGEXP =
%r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m
- DATELESS_FILENAME_MATCHER =
%r!^(?:.+/)*(.*)(\.[^.]+)$!
- DATE_FILENAME_MATCHER =
%r!^(?:.+/)*(\d{2,4}-\d{1,2}-\d{1,2})-(.*)(\.[^.]+)$!
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#content ⇒ Object
Returns the value of attribute content.
-
#extname ⇒ Object
readonly
Returns the value of attribute extname.
-
#output ⇒ Object
Returns the value of attribute output.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare this document against another document.
- #[](key) ⇒ Object
-
#asset_file? ⇒ Boolean
Determine whether the document is an asset file.
-
#basename ⇒ Object
The base filename of the document.
-
#basename_without_ext ⇒ Object
The base filename of the document, without the file extname.
-
#cleaned_relative_path ⇒ Object
Produces a “cleaned” relative path.
-
#coffeescript_file? ⇒ Boolean
Determine whether the document is a CoffeeScript file.
-
#data ⇒ Object
Fetch the Document’s data.
- #date ⇒ Object
-
#destination(base_directory) ⇒ Object
The full path to the output file.
-
#draft? ⇒ Boolean
Returns whether the document is a draft.
-
#excerpt_separator ⇒ Object
The Document excerpt_separator, from the YAML Front-Matter or site default excerpt_separator value.
-
#generate_excerpt? ⇒ Boolean
Whether to generate an excerpt.
- #id ⇒ Object
-
#initialize(path, relations = {}) ⇒ Document
constructor
Create a new Document.
-
#inspect ⇒ Object
The inspect string for this document.
-
#merge_data!(other, source: "YAML front matter") ⇒ Object
Merge some data in with this document’s data.
-
#method_missing(method, *args, &blck) ⇒ Object
Override of method_missing to check in @data for the key.
- #next_doc ⇒ Object
-
#output_ext ⇒ Object
The output extension of the document.
-
#permalink ⇒ Object
The permalink for this Document.
-
#place_in_layout? ⇒ Boolean
Determine whether the file should be placed into layouts.
- #previous_doc ⇒ Object
-
#published? ⇒ Boolean
Whether the file is published or not, as indicated in YAML front-matter.
-
#read(opts = {}) ⇒ Object
Read in the file and assign the content and data based on the file contents.
-
#related_posts ⇒ Object
Calculate related posts.
-
#relative_path ⇒ Object
The path to the document, relative to the site source.
-
#render_with_liquid? ⇒ Boolean
Determine whether the file should be rendered with Liquid.
-
#respond_to?(method, include_private = false) ⇒ Boolean
Override of normal respond_to? to match method_missing’s logic for looking in @data.
- #respond_to_missing?(method) ⇒ Boolean
-
#sass_file? ⇒ Boolean
Determine whether the document is a Sass file.
- #source_file_mtime ⇒ Object
-
#to_liquid ⇒ Object
Create a Liquid-understandable version of this Document.
-
#to_s ⇒ Object
The string representation for this document.
- #trigger_hooks(hook_name, *args) ⇒ Object
-
#url ⇒ Object
The computed URL for the document.
-
#url_placeholders ⇒ Object
Construct a Hash of key-value pairs which contain a mapping between a key in the URL template and the corresponding value for this document.
-
#url_template ⇒ Object
The URL template where the document would be accessible.
-
#write(dest) ⇒ Object
Write the generated Document file to the destination directory.
-
#write? ⇒ Boolean
Determine whether this document should be written.
-
#yaml_file? ⇒ Boolean
Determine whether the document is a YAML file.
Constructor Details
#initialize(path, relations = {}) ⇒ Document
Create a new Document.
path - the path to the file relations - a hash with keys :site and :collection, the values of which
are the Bunto::Site and Bunto::Collection to which this
Document belong.
Returns nothing.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bunto/document.rb', line 22 def initialize(path, relations = {}) @site = relations[:site] @path = path @extname = File.extname(path) @collection = relations[:collection] @has_yaml_header = nil if draft? categories_from_path("_drafts") else categories_from_path(collection.relative_directory) end data.default_proc = proc do |_, key| site.frontmatter_defaults.find(relative_path, collection.label, key) end trigger_hooks(:post_init) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blck) ⇒ Object
Override of method_missing to check in @data for the key.
363 364 365 366 367 368 369 370 371 372 |
# File 'lib/bunto/document.rb', line 363 def method_missing(method, *args, &blck) if data.key?(method.to_s) Bunto::Deprecator. "Document##{method} is now a key "\ "in the #data hash." Bunto::Deprecator. "Called by #{caller.first}." data[method.to_s] else super end end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
7 8 9 |
# File 'lib/bunto/document.rb', line 7 def collection @collection end |
#content ⇒ Object
Returns the value of attribute content.
8 9 10 |
# File 'lib/bunto/document.rb', line 8 def content @content end |
#extname ⇒ Object (readonly)
Returns the value of attribute extname.
7 8 9 |
# File 'lib/bunto/document.rb', line 7 def extname @extname end |
#output ⇒ Object
Returns the value of attribute output.
8 9 10 |
# File 'lib/bunto/document.rb', line 8 def output @output end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/bunto/document.rb', line 7 def path @path end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
7 8 9 |
# File 'lib/bunto/document.rb', line 7 def site @site end |
Instance Method Details
#<=>(other) ⇒ Object
Compare this document against another document. Comparison is a comparison between the 2 paths of the documents.
Returns -1, 0, +1 or nil depending on whether this doc’s path is less than,
equal or greater than the other doc's path. See String#<=> for more details.
295 296 297 298 299 300 |
# File 'lib/bunto/document.rb', line 295 def <=>(other) return nil unless other.respond_to?(:data) cmp = data["date"] <=> other.data["date"] cmp = path <=> other.path if cmp.nil? || cmp.zero? cmp end |
#[](key) ⇒ Object
203 204 205 |
# File 'lib/bunto/document.rb', line 203 def [](key) data[key] end |
#asset_file? ⇒ Boolean
Determine whether the document is an asset file. Asset files include CoffeeScript files and Sass/SCSS files.
Returns true if the extname belongs to the set of extensions
that asset files use.
135 136 137 |
# File 'lib/bunto/document.rb', line 135 def asset_file? sass_file? || coffeescript_file? end |
#basename ⇒ Object
The base filename of the document.
Returns the base filename of the document.
103 104 105 |
# File 'lib/bunto/document.rb', line 103 def basename @basename ||= File.basename(path) end |
#basename_without_ext ⇒ Object
The base filename of the document, without the file extname.
Returns the basename without the file extname.
96 97 98 |
# File 'lib/bunto/document.rb', line 96 def basename_without_ext @basename_without_ext ||= File.basename(path, ".*") end |
#cleaned_relative_path ⇒ Object
Produces a “cleaned” relative path. The “cleaned” relative path is the relative path without the extname
and with the collection's directory removed as well.
This method is useful when building the URL of the document.
Examples:
When relative_path is "_methods/site/generate.md":
cleaned_relative_path
# => "/site/generate"
Returns the cleaned relative path of the document.
118 119 120 121 |
# File 'lib/bunto/document.rb', line 118 def cleaned_relative_path @cleaned_relative_path ||= relative_path[0..-extname.length - 1].sub(collection.relative_directory, "") end |
#coffeescript_file? ⇒ Boolean
Determine whether the document is a CoffeeScript file.
Returns true if extname == .coffee, false otherwise.
149 150 151 |
# File 'lib/bunto/document.rb', line 149 def coffeescript_file? ".coffee" == extname end |
#data ⇒ Object
Fetch the Document’s data.
Returns a Hash containing the data. An empty hash is returned if
no data was read.
46 47 48 |
# File 'lib/bunto/document.rb', line 46 def data @data ||= {} end |
#date ⇒ Object
60 61 62 |
# File 'lib/bunto/document.rb', line 60 def date data["date"] ||= (draft? ? source_file_mtime : site.time) end |
#destination(base_directory) ⇒ Object
The full path to the output file.
base_directory - the base path of the output directory
Returns the full path to the output file of this document.
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/bunto/document.rb', line 212 def destination(base_directory) dest = site.in_dest_dir(base_directory) path = site.in_dest_dir(dest, URL.unescape_path(url)) if url.end_with? "/" path = File.join(path, "index.html") else path << output_ext unless path.end_with? output_ext end path end |
#draft? ⇒ Boolean
Returns whether the document is a draft. This is only the case if the document is in the ‘posts’ collection but in a different directory than ‘_posts’.
Returns whether the document is a draft.
73 74 75 76 |
# File 'lib/bunto/document.rb', line 73 def draft? data["draft"] ||= relative_path.index(collection.relative_directory).nil? && collection.label == "posts" end |
#excerpt_separator ⇒ Object
The Document excerpt_separator, from the YAML Front-Matter or site default excerpt_separator value
Returns the document excerpt_separator
315 316 317 |
# File 'lib/bunto/document.rb', line 315 def excerpt_separator (data["excerpt_separator"] || site.config["excerpt_separator"]).to_s end |
#generate_excerpt? ⇒ Boolean
Whether to generate an excerpt
Returns true if the excerpt separator is configured.
322 323 324 |
# File 'lib/bunto/document.rb', line 322 def generate_excerpt? !excerpt_separator.empty? end |
#id ⇒ Object
345 346 347 |
# File 'lib/bunto/document.rb', line 345 def id @id ||= File.join(File.dirname(url), (data["slug"] || basename_without_ext).to_s) end |
#inspect ⇒ Object
The inspect string for this document. Includes the relative path and the collection label.
Returns the inspect string for this document.
279 280 281 |
# File 'lib/bunto/document.rb', line 279 def inspect "#<Bunto::Document #{relative_path} collection=#{collection.label}>" end |
#merge_data!(other, source: "YAML front matter") ⇒ Object
Merge some data in with this document’s data.
Returns the merged data.
53 54 55 56 57 58 |
# File 'lib/bunto/document.rb', line 53 def merge_data!(other, source: "YAML front matter") merge_categories!(other) Utils.deep_merge_hashes!(data, other) merge_date!(source) data end |
#next_doc ⇒ Object
326 327 328 329 330 331 |
# File 'lib/bunto/document.rb', line 326 def next_doc pos = collection.docs.index { |post| post.equal?(self) } if pos && pos < collection.docs.length - 1 collection.docs[pos + 1] end end |
#output_ext ⇒ Object
The output extension of the document.
Returns the output extension
89 90 91 |
# File 'lib/bunto/document.rb', line 89 def output_ext Bunto::Renderer.new(site, self).output_ext end |
#permalink ⇒ Object
The permalink for this Document. Permalink is set via the data Hash.
Returns the permalink or nil if no permalink was set in the data.
188 189 190 |
# File 'lib/bunto/document.rb', line 188 def permalink data && data.is_a?(Hash) && data["permalink"] end |
#place_in_layout? ⇒ Boolean
Determine whether the file should be placed into layouts.
Returns false if the document is either an asset file or a yaml file,
true otherwise.
165 166 167 |
# File 'lib/bunto/document.rb', line 165 def place_in_layout? !(asset_file? || yaml_file?) end |
#previous_doc ⇒ Object
333 334 335 336 337 338 |
# File 'lib/bunto/document.rb', line 333 def previous_doc pos = collection.docs.index { |post| post.equal?(self) } if pos && pos > 0 collection.docs[pos - 1] end end |
#published? ⇒ Boolean
Whether the file is published or not, as indicated in YAML front-matter
Returns ‘false’ if the ‘published’ key is specified in the YAML front-matter and is ‘false’. Otherwise returns ‘true’.
240 241 242 |
# File 'lib/bunto/document.rb', line 240 def published? !(data.key?("published") && data["published"] == false) end |
#read(opts = {}) ⇒ Object
Read in the file and assign the content and data based on the file contents. Merge the frontmatter of the file with the frontmatter default values
Returns nothing.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/bunto/document.rb', line 249 def read(opts = {}) Bunto.logger.debug "Reading:", relative_path if yaml_file? @data = SafeYAML.load_file(path) else begin merge_defaults read_content(opts) read_post_data rescue SyntaxError => e Bunto.logger.error "Error:", "YAML Exception reading #{path}: #{e.}" rescue => e raise e if e.is_a? Bunto::Errors::FatalException Bunto.logger.error "Error:", "could not read file #{path}: #{e.}" end end end |
#related_posts ⇒ Object
Calculate related posts.
Returns an Array of related Posts.
352 353 354 |
# File 'lib/bunto/document.rb', line 352 def Bunto::RelatedPosts.new(self).build end |
#relative_path ⇒ Object
The path to the document, relative to the site source.
Returns a String path which represents the relative path
from the site source to this document
82 83 84 |
# File 'lib/bunto/document.rb', line 82 def relative_path @relative_path ||= Pathutil.new(path).relative_path_from(site.source).to_s end |
#render_with_liquid? ⇒ Boolean
Determine whether the file should be rendered with Liquid.
Returns false if the document is either an asset file or a yaml file,
true otherwise.
157 158 159 |
# File 'lib/bunto/document.rb', line 157 def render_with_liquid? !(coffeescript_file? || yaml_file?) end |
#respond_to?(method, include_private = false) ⇒ Boolean
Override of normal respond_to? to match method_missing’s logic for looking in @data.
358 359 360 |
# File 'lib/bunto/document.rb', line 358 def respond_to?(method, include_private = false) data.key?(method.to_s) || super end |
#respond_to_missing?(method) ⇒ Boolean
374 375 376 |
# File 'lib/bunto/document.rb', line 374 def respond_to_missing?(method, *) data.key?(method.to_s) || super end |
#sass_file? ⇒ Boolean
Determine whether the document is a Sass file.
Returns true if extname == .sass or .scss, false otherwise.
142 143 144 |
# File 'lib/bunto/document.rb', line 142 def sass_file? %w(.sass .scss).include?(extname) end |
#source_file_mtime ⇒ Object
64 65 66 |
# File 'lib/bunto/document.rb', line 64 def source_file_mtime @source_file_mtime ||= File.mtime(path) end |
#to_liquid ⇒ Object
Create a Liquid-understandable version of this Document.
Returns a Hash representing this Document’s data.
271 272 273 |
# File 'lib/bunto/document.rb', line 271 def to_liquid @to_liquid ||= Drops::DocumentDrop.new(self) end |
#to_s ⇒ Object
The string representation for this document.
Returns the content of the document
286 287 288 |
# File 'lib/bunto/document.rb', line 286 def to_s output || content || "NO CONTENT" end |
#trigger_hooks(hook_name, *args) ⇒ Object
340 341 342 343 |
# File 'lib/bunto/document.rb', line 340 def trigger_hooks(hook_name, *args) Bunto::Hooks.trigger collection.label.to_sym, hook_name, self, *args if collection Bunto::Hooks.trigger :documents, hook_name, self, *args end |
#url ⇒ Object
The computed URL for the document. See ‘Bunto::URL#to_s` for more details.
Returns the computed URL for the document.
195 196 197 198 199 200 201 |
# File 'lib/bunto/document.rb', line 195 def url @url = URL.new({ :template => url_template, :placeholders => url_placeholders, :permalink => permalink, }).to_s end |
#url_placeholders ⇒ Object
Construct a Hash of key-value pairs which contain a mapping between
a key in the URL template and the corresponding value for this document.
Returns the Hash of key-value pairs for replacement in the URL.
180 181 182 |
# File 'lib/bunto/document.rb', line 180 def url_placeholders @url_placeholders ||= Drops::UrlDrop.new(self) end |
#url_template ⇒ Object
The URL template where the document would be accessible.
Returns the URL template for the document.
172 173 174 |
# File 'lib/bunto/document.rb', line 172 def url_template collection.url_template end |
#write(dest) ⇒ Object
Write the generated Document file to the destination directory.
dest - The String path to the destination dir.
Returns nothing.
228 229 230 231 232 233 234 |
# File 'lib/bunto/document.rb', line 228 def write(dest) path = destination(dest) FileUtils.mkdir_p(File.dirname(path)) File.write(path, output, :mode => "wb") trigger_hooks(:post_write) end |
#write? ⇒ Boolean
Determine whether this document should be written. Based on the Collection to which it belongs.
True if the document has a collection and if that collection’s #write?
method returns true, otherwise false.
307 308 309 |
# File 'lib/bunto/document.rb', line 307 def write? collection && collection.write? end |
#yaml_file? ⇒ Boolean
Determine whether the document is a YAML file.
Returns true if the extname is either .yml or .yaml, false otherwise.
126 127 128 |
# File 'lib/bunto/document.rb', line 126 def yaml_file? %w(.yaml .yml).include?(extname) end |