Class: SoupCMS::CLI::Model::Markdown

Inherits:
Base
  • Object
show all
Defined in:
lib/soupcms/cli/model/markdown.rb

Direct Known Subclasses

Chapter, Page, Post

Constant Summary

Constants inherited from Base

Base::SEVERITY_COLOR_MAP

Instance Attribute Summary

Attributes inherited from Base

#file

Instance Method Summary collapse

Methods inherited from Base

#app_name, #coll, #conn, #create, create_model, #db, #doc, #doc_name, #hero_image, #initialize, #model, #old_doc, #publish_in_future?, #slug, #type, #update_old_doc

Constructor Details

This class inherits a constructor from SoupCMS::CLI::Model::Base

Instance Method Details

#buildObject



17
18
19
20
21
# File 'lib/soupcms/cli/model/markdown.rb', line 17

def build
  super
  doc['title'] = title unless doc['title']
  doc['description'] = description unless doc['description']
end

#content_flavorObject



7
8
9
# File 'lib/soupcms/cli/model/markdown.rb', line 7

def content_flavor;
  File.basename(file).split('.').size > 2 ? File.basename(file).split('.')[1] : 'kramdown'
end

#descriptionObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/soupcms/cli/model/markdown.rb', line 30

def description
  post_description = ''
  content_lines = doc['content']['value'].lines
  index = 0
  while post_description.length < 300 && content_lines[index] do
    post_description.concat(content_lines[index].chomp.gsub(/\A[\d_\W]+|[\d_\W]+\Z/, ''))
    index += 1
  end
  post_description + '...'
end

#parse_fileObject



11
12
13
14
15
# File 'lib/soupcms/cli/model/markdown.rb', line 11

def parse_file
  @attributes, @content = SoupCMS::CLI::FrontMatterParser.new.parse(file.read)
  doc = {'content' => {'type' => 'markdown', 'flavor' => content_flavor, 'value' => @content}}
  doc.merge @attributes
end

#titleObject



23
24
25
26
27
28
# File 'lib/soupcms/cli/model/markdown.rb', line 23

def title
  content_lines = doc['content']['value'].lines
  doc_title = content_lines.first.chomp
  doc['content']['value'] = content_lines[2] ? content_lines[2..-1].join("\n") : ''
  doc_title.gsub('_', ' ').gsub('#', '').strip
end