Class: Brief::Document
- Inherits:
-
Object
- Object
- Brief::Document
- Includes:
- FrontMatter, Rendering, Templating
- Defined in:
- lib/brief/document.rb,
lib/brief/document/rendering.rb,
lib/brief/document/front_matter.rb
Defined Under Namespace
Modules: FrontMatter, Rendering, Templating Classes: ContentExtractor, Section, Structure
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#frontmatter ⇒ Object
Returns the value of attribute frontmatter.
-
#path ⇒ Object
Returns the value of attribute path.
-
#raw_content ⇒ Object
Returns the value of attribute raw_content.
Instance Method Summary collapse
-
#at(*args, &block) ⇒ Object
Returns a Nokogiri::HTML::Element.
-
#css(*args, &block) ⇒ Object
Shortcut for querying the rendered HTML by css selectors.
- #data ⇒ Object
- #extension ⇒ Object
- #extract_content(*args) ⇒ Object
- #fragment ⇒ Object
-
#initialize(path, options = {}) ⇒ Document
constructor
A new instance of Document.
- #method_missing(meth, *args, &block) ⇒ Object
- #model_class ⇒ Object
-
#model_instance_registered? ⇒ Boolean
Each model class tracks the instances of the models created and ensures that there is a 1-1 relationship between a document path and the model.
- #parser ⇒ Object
- #relative_path_identifier ⇒ Object
- #respond_to?(method) ⇒ Boolean
- #sections ⇒ Object
- #structure ⇒ Object
- #to_model ⇒ Object
Methods included from Templating
Methods included from FrontMatter
Methods included from Rendering
Constructor Details
#initialize(path, options = {}) ⇒ Document
Returns a new instance of Document.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/brief/document.rb', line 9 def initialize(path, = {}) if path.respond_to?(:key?) && .empty? @frontmatter = path.to_mash else @path = Pathname(path) end @options = .to_mash if @path && self.path.exist? @raw_content = path.read load_frontmatter elsif [:contents] @raw_content = [:contents] end model_class.try(:models).try(:<<, to_model) unless model_instance_registered? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/brief/document.rb', line 126 def method_missing(meth, *args, &block) if data.respond_to?(meth) data.send(meth, *args, &block) else super end end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
7 8 9 |
# File 'lib/brief/document.rb', line 7 def content @content end |
#frontmatter ⇒ Object
Returns the value of attribute frontmatter.
7 8 9 |
# File 'lib/brief/document.rb', line 7 def frontmatter @frontmatter end |
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/brief/document.rb', line 7 def path @path end |
#raw_content ⇒ Object
Returns the value of attribute raw_content.
7 8 9 |
# File 'lib/brief/document.rb', line 7 def raw_content @raw_content end |
Instance Method Details
#at(*args, &block) ⇒ Object
Returns a Nokogiri::HTML::Element
60 61 62 |
# File 'lib/brief/document.rb', line 60 def at(*args, &block) parser.send(:at, *args, &block) end |
#css(*args, &block) ⇒ Object
Shortcut for querying the rendered HTML by css selectors.
This will allow for model data attributes to be pulled from the document contents.
Returns a Nokogiri::HTML::Element
55 56 57 |
# File 'lib/brief/document.rb', line 55 def css(*args, &block) parser.send(:css, *args, &block) end |
#data ⇒ Object
28 29 30 |
# File 'lib/brief/document.rb', line 28 def data frontmatter end |
#extension ⇒ Object
86 87 88 |
# File 'lib/brief/document.rb', line 86 def extension path.extname end |
#extract_content(*args) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/brief/document.rb', line 64 def extract_content(*args) = args. args = .delete(:args) if .is_a?(Hash) && .key?(:args) case when .empty? && args.length == 1 && args.first.is_a?(String) results = css(args.first) results = results.first if results.length > 1 && args.first.match(/:first-of-type/) results.try(:text).to_s else binding.pry end end |
#fragment ⇒ Object
122 123 124 |
# File 'lib/brief/document.rb', line 122 def fragment @fragment ||= Nokogiri::HTML.fragment(to_raw_html) end |
#model_class ⇒ Object
94 95 96 |
# File 'lib/brief/document.rb', line 94 def model_class @model_class || ((data && data.type) && Brief::Model.for_type(data.type)) end |
#model_instance_registered? ⇒ Boolean
Each model class tracks the instances of the models created and ensures that there is a 1-1 relationship between a document path and the model.
101 102 103 104 105 |
# File 'lib/brief/document.rb', line 101 def model_instance_registered? model_class && model_class.models.any? do |model| model.path == path end end |
#parser ⇒ Object
115 116 117 118 119 120 |
# File 'lib/brief/document.rb', line 115 def parser @parser ||= begin structure.prescan structure.create_wrappers end end |
#relative_path_identifier ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/brief/document.rb', line 78 def relative_path_identifier if Brief.case path.relative_path_from(Brief.case.root) else path.to_s end end |
#respond_to?(method) ⇒ Boolean
107 108 109 |
# File 'lib/brief/document.rb', line 107 def respond_to?(method) super || data.respond_to?(method) || data.key?(method) end |
#sections ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/brief/document.rb', line 32 def sections mappings = model_class.section_mappings @sections = {}.to_mash mappings.each do |name, mapping| fragment = css("section[data-heading='#{name}']").first @sections[name.parameterize.downcase.underscore] = Brief::Document::Section.new(name, fragment, mapping) end @sections end |
#structure ⇒ Object
111 112 113 |
# File 'lib/brief/document.rb', line 111 def structure @structure_analyzer ||= Brief::Document::Structure.new(fragment, raw_content.lines.to_a) end |
#to_model ⇒ Object
90 91 92 |
# File 'lib/brief/document.rb', line 90 def to_model model_class.new(data.to_hash.merge(path: path, document: self)) if model_class end |