Class: Brief::Document
Defined Under Namespace
Modules: FrontMatter, Rendering, Templating
Classes: ContentExtractor, Section, Structure
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Templating
#generate_content
#data=, #frontmatter_line_count
Methods included from Rendering
#script_contents, #script_preamble, #to_html, #unwrapped_html
Constructor Details
#initialize(path, options = {}) ⇒ Document
Returns a new instance of Document.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/brief/document.rb', line 25
def initialize(path, options = {})
if path.respond_to?(:key?) && options.empty?
@frontmatter = path.to_mash
else
@path = Pathname(path)
end
@options = options.to_mash
if @path && self.path.exist?
@raw_content = self.path.read
load_frontmatter
elsif options[:contents]
@raw_content = options[:contents]
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
262
263
264
265
266
267
268
|
# File 'lib/brief/document.rb', line 262
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
|
#options ⇒ Object
Returns the value of attribute options.
7
8
9
|
# File 'lib/brief/document.rb', line 7
def options
@options
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
157
158
159
|
# File 'lib/brief/document.rb', line 157
def at(*args, &block)
parser.send(:at, *args, &block)
end
|
#briefcase ⇒ Object
117
118
119
|
# File 'lib/brief/document.rb', line 117
def briefcase
(@briefcase_root && Brief.cases[@briefcase_root]) || Brief.case
end
|
#combined_data_and_content ⇒ Object
98
99
100
101
|
# File 'lib/brief/document.rb', line 98
def combined_data_and_content
return content if data.nil? || data.empty?
frontmatter.to_hash.to_yaml + "---\n\n#{ content }"
end
|
#content_hash ⇒ Object
42
43
44
|
# File 'lib/brief/document.rb', line 42
def content_hash
Digest::MD5.hexdigest(@content.to_s)
end
|
#content_stale? ⇒ Boolean
50
51
52
|
# File 'lib/brief/document.rb', line 50
def content_stale?
content_hash != file_hash
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
152
153
154
|
# File 'lib/brief/document.rb', line 152
def css(*args, &block)
parser.send(:css, *args, &block)
end
|
#data ⇒ Object
103
104
105
|
# File 'lib/brief/document.rb', line 103
def data
frontmatter
end
|
#document ⇒ Object
9
10
11
|
# File 'lib/brief/document.rb', line 9
def document
self
end
|
#document_type ⇒ Object
216
217
218
|
# File 'lib/brief/document.rb', line 216
def document_type
options.fetch(:type) { document_type! }
end
|
#document_type! ⇒ Object
220
221
222
223
224
|
# File 'lib/brief/document.rb', line 220
def document_type!
existing = data && data.type
return existing if existing
parent_folder_name.try(:singularize)
end
|
#exist? ⇒ Boolean
197
198
199
|
# File 'lib/brief/document.rb', line 197
def exist?
path && path.exist?
end
|
#extension ⇒ Object
183
184
185
|
# File 'lib/brief/document.rb', line 183
def extension
path.extname
end
|
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/brief/document.rb', line 161
def (*args)
options = args.
args = options.delete(:args) if options.is_a?(Hash) && options.key?(:args)
case
when options.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
|
#file_hash ⇒ Object
46
47
48
|
# File 'lib/brief/document.rb', line 46
def file_hash
Digest::MD5.hexdigest(path.read.to_s)
end
|
#fragment ⇒ Object
254
255
256
|
# File 'lib/brief/document.rb', line 254
def fragment
@fragment ||= Nokogiri::HTML.fragment(to_raw_html)
end
|
#in_briefcase(briefcase) ⇒ Object
107
108
109
110
111
112
113
114
115
|
# File 'lib/brief/document.rb', line 107
def in_briefcase(briefcase)
@briefcase_root = briefcase.root
unless Brief::Util.ensure_child_path(briefcase.docs_path, path)
raise 'Invalid document path'
end
self
end
|
#inspect ⇒ Object
17
18
19
|
# File 'lib/brief/document.rb', line 17
def inspect
"#{ model_class }.at_path(#{relative_path})"
end
|
#model_attributes ⇒ Object
187
188
189
190
191
|
# File 'lib/brief/document.rb', line 187
def model_attributes
(data || {}).to_hash
.merge(path: path, document: self)
.reverse_merge(type: document_type)
end
|
#model_class ⇒ Object
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/brief/document.rb', line 201
def model_class
case
when @model_class
@model_class
when briefcase
briefcase.model_class_for(self)
when data && data.type
Brief::Model.for_type(data.type)
when parent_folder_name.length > 0
Brief::Model.for_folder_name(parent_folder_name)
else
raise 'Could not determine the model class to use for this document. Specify the type, or put it in a folder that maps to the correct type.'
end
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.
233
234
235
236
237
|
# File 'lib/brief/document.rb', line 233
def model_instance_registered?
model_class && model_class.models.any? do |model|
model.path == path
end
end
|
#parent_folder_name ⇒ Object
226
227
228
|
# File 'lib/brief/document.rb', line 226
def parent_folder_name
path.parent.basename.to_s.downcase
end
|
#parser ⇒ Object
247
248
249
250
251
252
|
# File 'lib/brief/document.rb', line 247
def parser
@parser ||= begin
structure.prescan
structure.create_wrappers
end
end
|
#raw=(val) ⇒ Object
54
55
56
57
58
59
|
# File 'lib/brief/document.rb', line 54
def raw= val
@raw_set = true
@raw_content = val
@raw_content
end
|
#refresh! ⇒ Object
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/brief/document.rb', line 76
def refresh!
@content = nil
@raw_content = path.read
@frontmatter = nil
@raw_frontmatter = nil
@refreshing = true
@content_hash = nil
load_frontmatter
true
end
|
#relative_path ⇒ Object
21
22
23
|
# File 'lib/brief/document.rb', line 21
def relative_path
briefcase.present? ? path.relative_path_from(briefcase.docs_path) : path
end
|
#relative_path_identifier ⇒ Object
175
176
177
178
179
180
181
|
# File 'lib/brief/document.rb', line 175
def relative_path_identifier
if Brief.case
path.relative_path_from(Brief.case.root)
else
path.to_s
end
end
|
#respond_to?(method) ⇒ Boolean
239
240
241
|
# File 'lib/brief/document.rb', line 239
def respond_to?(method)
super || (data && data.respond_to?(method)) || (data && data.key?(method))
end
|
#save ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/brief/document.rb', line 65
def save
if set_raw?
file_contents = raw_content
else
file_contents = combined_data_and_content
end
path.open('w') {|fh| fh.write(file_contents) }
refresh!
end
|
#save! ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/brief/document.rb', line 87
def save!
if set_raw?
file_contents = raw_content
else
file_contents = combined_data_and_content
end
path.open('w+') {|fh| fh.write(file_contents) }
refresh!
end
|
#sections ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/brief/document.rb', line 121
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
|
#set_raw? ⇒ Boolean
61
62
63
|
# File 'lib/brief/document.rb', line 61
def set_raw?
!!@raw_set
end
|
#structure ⇒ Object
243
244
245
|
# File 'lib/brief/document.rb', line 243
def structure
@structure_analyzer ||= Brief::Document::Structure.new(fragment, raw_content.lines.to_a)
end
|
#to_model ⇒ Object
193
194
195
|
# File 'lib/brief/document.rb', line 193
def to_model
model_class.try(:new, model_attributes)
end
|
#to_s ⇒ Object
13
14
15
|
# File 'lib/brief/document.rb', line 13
def to_s
"#{ model_class }.at_path(#{relative_path})"
end
|
#type ⇒ Object
258
259
260
|
# File 'lib/brief/document.rb', line 258
def type
document_type
end
|