Class: Zine::Page

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

Overview

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

Direct Known Subclasses

DataPage, Post

Defined Under Namespace

Classes: FormattedData, TagData

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(md_file_name, dest, templates, site_options) ⇒ Page

Returns a new instance of Page.



61
62
63
64
65
66
67
68
# File 'lib/zine/page.rb', line 61

def initialize(md_file_name, dest, templates, site_options)
  file_parts = File.open(md_file_name, 'r').read.split('---')
  @formatted_data = FormattedData.new(parse_yaml(file_parts[1]),
                                      site_options)
  @dest_path = dest
  @raw_text = file_parts[2]
  init_templates(templates)
end

Instance Attribute Details

#formatted_dataObject (readonly)

Returns the value of attribute formatted_data.



15
16
17
# File 'lib/zine/page.rb', line 15

def formatted_data
  @formatted_data
end

Class Method Details

.slug(text) ⇒ Object



106
107
108
109
110
# File 'lib/zine/page.rb', line 106

def self.slug(text)
  text.downcase
      .gsub(/[^a-z0-9]+/, '-')
      .gsub(/^-|-$/, '')
end

Instance Method Details

#init_templates(templates) ⇒ Object



70
71
72
73
74
# File 'lib/zine/page.rb', line 70

def init_templates(templates)
  @header_partial = templates.header
  @footer_partial = templates.footer
  @template = templates.body
end

#parse_markdownObject



76
77
78
79
80
81
82
83
84
# File 'lib/zine/page.rb', line 76

def parse_markdown
  @formatted_data.html = Kramdown::Document.new(
    @raw_text,
    input: 'GFM',
    auto_ids: false,
    smart_quotes: %w(apos apos quot quot),
    syntax_highlighter: 'rouge'
  ).to_html
end

#parse_yaml(text) ⇒ Object



86
87
88
89
90
91
# File 'lib/zine/page.rb', line 86

def parse_yaml(text)
  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

#processObject



98
99
100
101
102
103
104
# File 'lib/zine/page.rb', line 98

def process
  parse_markdown
  html = template_the_html

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

#rel_path_from_build_dir(path) ⇒ Object



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

def rel_path_from_build_dir(path)
  full = Pathname(path)
  full.relative_path_from(Pathname(@build_dir))
end

#template_the_htmlObject



112
113
114
115
116
117
118
# File 'lib/zine/page.rb', line 112

def template_the_html
  @formatted_data.header_partial = @header_partial.result(@formatted_data
    .public_binding)
  @formatted_data.footer_partial = @footer_partial.result(@formatted_data
    .public_binding)
  @template.result(@formatted_data.public_binding)
end