Class: Sawsge::Page

Inherits:
Resource show all
Defined in:
lib/page.rb

Overview

An HTML page

Direct Known Subclasses

Home, Post

Instance Attribute Summary collapse

Attributes inherited from Resource

#path

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Page

Returns a new instance of Page.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/page.rb', line 8

def initialize(path)
  super(path)
  html_body_fragment = PandocRuby.convert(File.new(@path, 'r').read, from: :markdown, to: :html)
  @document = Nokogiri::HTML5(HEADER + FOOTER)
  @body = Nokogiri::HTML5.fragment(html_body_fragment)

  # Place body fragment after header (and before footer)
  header = @document.at_css('header')
  @body = header.add_next_sibling(@body)

  # Parse the body fragment instead of the whole document,
  # as the header may have another h1 within
  @title = begin
    h1 = @body.at_css('h1')
    h1 ? h1.content : ''
  end
  @document.at_css('title').content = @title

  if EXTERNAL_LINKS_TARGET_BLANK
    # For any `a` tag where the href attribute has no
    # hostname (external link) and no existing `target`
    # attribute, add an attribute `target` with value
    # blank. mailto links are also ignored, as common
    # obfuscation techniques can interfere with Nokogiri.
    external_links = @document.css('a').reject do |link|
      uri = URI(link['href'])
      # If a link is malformed, it's not sawsge's problem
      # to fix.
    rescue URI::InvalidComponentError
      false
    else
      host = uri.host
      scheme = uri.scheme
      host.nil? or host.empty? or scheme == 'mailto' or !link['target'].nil?
    end
    external_links.each { |link| link['target'] = '_blank' }
  end
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/page.rb', line 6

def title
  @title
end

Instance Method Details

#buildObject



47
48
49
50
51
52
53
54
# File 'lib/page.rb', line 47

def build
  serialized_html = @document.serialize
  out_path = File.join(OUT_DIRNAME, @path.sub('index.md', 'index.html'))
  out_dir = File.join(OUT_DIRNAME, File.dirname(@path))

  FileUtils.mkpath out_dir
  File.new(out_path, 'w').syswrite(serialized_html)
end