Class: Brief::Document

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Templating

#generate_content

Methods included from FrontMatter

#data=, #frontmatter_line_count

Methods included from Rendering

#to_html, #unwrapped_html

Constructor Details

#initialize(path, options = {}) ⇒ Document

Returns a new instance of Document.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/brief/document.rb', line 13

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

  register_model_instance if self.path && self.path.exist?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



225
226
227
228
229
230
231
# File 'lib/brief/document.rb', line 225

def method_missing(meth, *args, &block)
  if data.respond_to?(meth)
    data.send(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



7
8
9
# File 'lib/brief/document.rb', line 7

def content
  @content
end

#frontmatterObject

Returns the value of attribute frontmatter.



7
8
9
# File 'lib/brief/document.rb', line 7

def frontmatter
  @frontmatter
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/brief/document.rb', line 7

def path
  @path
end

#raw_contentObject

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



132
133
134
# File 'lib/brief/document.rb', line 132

def at(*args, &block)
  parser.send(:at, *args, &block)
end

#briefcaseObject



93
94
95
# File 'lib/brief/document.rb', line 93

def briefcase
  (@briefcase_root && Brief.cases[@briefcase_root]) || Brief.case
end

#combined_data_and_contentObject



79
80
81
82
# File 'lib/brief/document.rb', line 79

def combined_data_and_content
  return content if data.nil? || data.empty?
  frontmatter.to_hash.to_yaml + "---\n\n#{ content }"
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



127
128
129
# File 'lib/brief/document.rb', line 127

def css(*args, &block)
  parser.send(:css, *args, &block)
end

#dataObject



84
85
86
# File 'lib/brief/document.rb', line 84

def data
  frontmatter
end

#documentObject



9
10
11
# File 'lib/brief/document.rb', line 9

def document
  self
end

#document_typeObject



183
184
185
186
187
# File 'lib/brief/document.rb', line 183

def document_type
  existing = data && data.type
  return existing if existing
  parent_folder_name.try(:singularize)
end

#exist?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/brief/document.rb', line 166

def exist?
  path && path.exist?
end

#extensionObject



158
159
160
# File 'lib/brief/document.rb', line 158

def extension
  path.extname
end

#extract_content(*args) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/brief/document.rb', line 136

def extract_content(*args)
  options = args.extract_options!
  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

#fragmentObject



217
218
219
# File 'lib/brief/document.rb', line 217

def fragment
  @fragment ||= Nokogiri::HTML.fragment(to_raw_html)
end

#in_briefcase(briefcase) ⇒ Object



88
89
90
91
# File 'lib/brief/document.rb', line 88

def in_briefcase(briefcase)
  @briefcase_root = briefcase.root
  self
end

#model_classObject



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/brief/document.rb', line 170

def model_class
  case
  when @model_class
    @model_class
  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.

Returns:

  • (Boolean)


196
197
198
199
200
# File 'lib/brief/document.rb', line 196

def model_instance_registered?
  model_class && model_class.models.any? do |model|
    model.path == path
  end
end

#parent_folder_nameObject



189
190
191
# File 'lib/brief/document.rb', line 189

def parent_folder_name
  path.parent.basename.to_s.downcase
end

#parserObject



210
211
212
213
214
215
# File 'lib/brief/document.rb', line 210

def parser
  @parser ||= begin
                structure.prescan
                structure.create_wrappers
              end
end

#raw=(val) ⇒ Object



36
37
38
39
40
41
# File 'lib/brief/document.rb', line 36

def raw= val
  @raw_set = true
  @raw_content = val
  #document.load_frontmatter
  @raw_content
end

#refresh!Object



58
59
60
61
62
63
64
65
66
# File 'lib/brief/document.rb', line 58

def refresh!
  @content = nil
  @raw_content = path.read
  @frontmatter = nil
  @raw_frontmatter = nil
  @refreshing = true
  load_frontmatter
  true
end

#register_model_instanceObject



32
33
34
# File 'lib/brief/document.rb', line 32

def register_model_instance
  model_class.try(:models).try(:<<, to_model) unless model_instance_registered?
end

#relative_path_identifierObject



150
151
152
153
154
155
156
# File 'lib/brief/document.rb', line 150

def relative_path_identifier
  if Brief.case
    path.relative_path_from(Brief.case.root)
  else
    path.to_s
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/brief/document.rb', line 202

def respond_to?(method)
  super || (data && data.respond_to?(method)) || (data && data.key?(method))
end

#saveObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/brief/document.rb', line 47

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



68
69
70
71
72
73
74
75
76
77
# File 'lib/brief/document.rb', line 68

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

#sectionsObject



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/brief/document.rb', line 97

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

Returns:

  • (Boolean)


43
44
45
# File 'lib/brief/document.rb', line 43

def set_raw?
  !!@raw_set
end

#structureObject



206
207
208
# File 'lib/brief/document.rb', line 206

def structure
  @structure_analyzer ||= Brief::Document::Structure.new(fragment, raw_content.lines.to_a)
end

#to_modelObject



162
163
164
# File 'lib/brief/document.rb', line 162

def to_model
  model_class.new((data || {}).to_hash.merge(path: path, document: self).reverse_merge(:type=>document_type)) if model_class
end

#typeObject



221
222
223
# File 'lib/brief/document.rb', line 221

def type
  document_type
end