class Zine::Page

A page on the site where the content comes from a file's markdown, and the destination's location mirrors its own

Constants

TagData

the Tags on a Post

Attributes

formatted_data[R]
source_file[RW]
template_bundle[R]

Public Class Methods

new(md_file_name, dest, templates, site_options) click to toggle source
# File lib/zine/page.rb, line 69
def initialize(md_file_name, dest, templates, site_options)
  @source_file = md_file_name
  file_parts = File.open(md_file_name, 'r').read.split('---', 3)
  @formatted_data = FormattedData.new(parse_yaml(file_parts[1],
                                                 md_file_name),
                                      site_options)
  @dest_path = dest
  @raw_text = file_parts[2]
  init_templates(templates)
end
slug(text) click to toggle source
# File lib/zine/page.rb, line 88
def self.slug(text)
  text.downcase
      .gsub(/[^a-z0-9]+/, '-')
      .gsub(/^-|-$/, '')
end

Public Instance Methods

process(string_or_file_writer) click to toggle source
# File lib/zine/page.rb, line 80
def process(string_or_file_writer)
  parse_markdown
  html = template_the_html

  compressor = HtmlCompressor::Compressor.new
  string_or_file_writer.write(@dest_path, compressor.compress(html))
end

Private Instance Methods

init_templates(templates) click to toggle source
# File lib/zine/page.rb, line 96
def init_templates(templates)
  @header_partial = templates.header
  @footer_partial = templates.footer
  @template = templates.body
  @template_bundle = templates
end
parse_markdown() click to toggle source
# File lib/zine/page.rb, line 103
def parse_markdown
  @formatted_data.html = Kramdown::Document.new(
    @raw_text,
    input: 'GFM',
    auto_ids: false,
    smart_quotes: %w[apos apos quot quot],
  ).to_html
  @raw_text = nil
end
parse_yaml(text, md_file_name) click to toggle source
# File lib/zine/page.rb, line 113
def parse_yaml(text, md_file_name)
  YAML.safe_load text
rescue Psych::Exception
  puts Rainbow("Could not parse front matter for: #{md_file_name}").red
  { 'date' => DateTime.now.to_s, 'title' => md_file_name, 'tags' => [] }
end
rel_path_from_build_dir(path) click to toggle source
# File lib/zine/page.rb, line 120
def rel_path_from_build_dir(path)
  full = Pathname(path)
  full.relative_path_from(Pathname(@build_dir))
end
template_the_html() click to toggle source
# File lib/zine/page.rb, line 125
def template_the_html
  data_binding = @formatted_data.public_binding
  @formatted_data.header_partial = @header_partial.result data_binding
  @formatted_data.footer_partial = @footer_partial.result data_binding
  @template.result data_binding
end