Class: Octopress::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/octopress/page.rb

Direct Known Subclasses

Post

Constant Summary collapse

DEFAULT_OPTIONS =
{
  'post_ext' => 'markdown',
  'page_ext' => 'html',
  'post_layout' => 'post',
  'page_layout' => 'page',
  'titlecase' => true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, options) ⇒ Page

Returns a new instance of Page.



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

def initialize(site, options)
  @site = site
  @config = DEFAULT_OPTIONS.merge(site.config)
  @options = options
  set_default_options

  @front_matter = %w{title date}

  # Ensure title
  #
  @options['title'] ||= ''

  # Ensure a quoted title to avoid YAML parsing issues.
  #
  @options['title'] = "#{@options['title']}"

  @content = options['content'] || content
end

Instance Attribute Details

#siteObject

Returns the value of attribute site.



12
13
14
# File 'lib/octopress/page.rb', line 12

def site
  @site
end

Instance Method Details

#contentObject

Load the user provided or default template for a new post or page.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/octopress/page.rb', line 126

def content

  # Handle case where user passes the full path
  #
  file = @options['template'] || default_template

  if file
    file.sub!(/^_templates\//, '')
    file = File.join(site.source, '_templates', file) if file
    if File.exist? file
      parse_template File.open(file).read
    elsif @options['template']
      abort "No #{@options['type']} template found at #{file}"
    else
      parse_template default_front_matter
    end
  else
    parse_template default_front_matter
  end
end

#convert_date(date) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/octopress/page.rb', line 109

def convert_date(date)
  date ||= 'now'
  if date == 'now'
    date = Time.now.iso8601
  else
    begin
      date = Time.parse(date.to_s, Time.now).iso8601
    rescue => error
      puts 'Could not parse date. Try formatting it like YYYY-MM-DD HH:MM'
      abort error.message
    end
  end
  date
end

#date_slugObject



200
201
202
# File 'lib/octopress/page.rb', line 200

def date_slug
  @options['date'].split('T')[0]
end

#default_front_matter(template = '') ⇒ Object

Ensures front-matter is set with optional arguments



192
193
194
195
196
197
198
# File 'lib/octopress/page.rb', line 192

def default_front_matter(template='')
  @front_matter.dup.map do |k|
    if @options[k] && !(template =~ /#{k}:/)
      "\n#{k}: #{@options[k]}"
    end
  end.join('')
end

#default_templateObject



147
148
149
# File 'lib/octopress/page.rb', line 147

def default_template
  'page'
end

#extensionObject



94
95
96
# File 'lib/octopress/page.rb', line 94

def extension
  @options['extension'].sub(/^\./, '')
end

#parse_template(input) ⇒ Object

Render Liquid vars in YAML front-matter.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/octopress/page.rb', line 152

def parse_template(input)

  if @config['titlecase']
    @options['title'].titlecase!
  end

  vars = @options.dup

  # Allow templates to use slug
  #
  vars['slug'] = title_slug

  # Allow templates to use date fragments
  #
  date = Time.parse(vars['date'] || Time.now.iso8601)
  vars['date'] = date.iso8601
  vars['year'] = date.year
  vars['month'] = date.strftime('%m')
  vars['day'] = date.strftime('%d')
  vars['ymd'] = date.strftime('%Y-%m-%d')

  # If possible only parse the YAML front matter.
  # If YAML front-matter dashes aren't present parse the whole 
  # template and add dashes.
  #

  parsed = if input =~ /\A-{3}\s+(.+?)\s+-{3}(.+)?/m
    input = $1
    content = $2
    input << default_front_matter(input)
  else
    content = ''
  end

  template = Liquid::Template.parse(input)
  "---\n#{template.render(vars).strip}\n---\n#{content}"
end

#pathObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/octopress/page.rb', line 78

def path
  return @path if @path
  file = @options['path']
  raise "You must specify a path." unless file

  # If path ends with a slash, make it an index
  #
  file += "index" if file =~ /\/$/

  # if path has no extension, add the default extension
  #
  file += ".#{extension}" unless file =~ /\.\w+$/

  @path = File.join(site.source, file)
end

#path_slug(path) ⇒ Object

Returns a slug extracted from a path



206
207
208
# File 'lib/octopress/page.rb', line 206

def path_slug(path)
  File.basename(path, '.*').scan(/((\d+-){3})?(\S+)/).flatten[2]
end

Print instructions for setting up a new collection



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/octopress/page.rb', line 61

def print_collection_tip(collection)
  # If Jekyll is not already configurated for this collection, print instructions
  #
  if !@config['collections'] || !@config['collections'][collection]
    msg = "\nTIP: To create a new '#{collection}' collection, add this to your Jekyll configuration\n"
    msg += "----------------\n"
    msg += "collections:\n  #{collection}:\n    output: true"
    msg += "\n----------------"
    puts msg
  end
end

#relative_path(path) ⇒ Object



73
74
75
76
# File 'lib/octopress/page.rb', line 73

def relative_path(path)
  local = Dir.pwd + '/'
  path.sub(local, '')
end

#set_default_optionsObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/octopress/page.rb', line 98

def set_default_options
  @options['type']      ||= 'page'
  @options['write_message'] ||= 'New page:'
  @options['layout']      = @config['page_layout']
  if @options['date']
    @options['date']        = convert_date @options['date']
  end
  @options['extension'] ||= @config['page_ext']
  @options['template']  ||= @config['page_template']
end

#title_slugObject

Returns a string which is url compatible.



212
213
214
215
216
217
218
219
220
221
# File 'lib/octopress/page.rb', line 212

def title_slug
  value = (@options['slug'] || @options['title']).downcase
  value.gsub!(/[^\x00-\x7F]/u, '')
  value.gsub!(/(&amp;|&)+/, 'and')
  value.gsub!(/[']+/, '')
  value.gsub!(/\W+/, ' ')
  value.strip!
  value.gsub!(' ', '-')
  value
end

#writeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/octopress/page.rb', line 37

def write
  if File.exist?(path) && !@options['force']
    abort "File #{relative_path(path)} already exists. Use --force to overwrite."
  end

  dir = File.dirname(path)

  FileUtils.mkdir_p(dir)
  File.open(path, 'w') { |f| f.write(@content) }
  if STDOUT.tty?
    puts "#{@options['write_message']} #{relative_path(path)}"
    
    # If path begins with an underscore the page is probably being added to a collection
    #
    if @options['type'] == 'page'
      print_collection_tip($1) if dir =~ /#{site.source}\/_([^\/]+)/
    end
  else
    puts path
  end
end