Class: BuntoRelativeLinks::Generator

Inherits:
Bunto::Generator
  • Object
show all
Includes:
Bunto::Filters::URLFilters
Defined in:
lib/bunto-relative-links/generator.rb

Constant Summary collapse

%r!([^\]]+)!
FRAGMENT_REGEX =
%r!(#.+?)?!
%r!\[#{LINK_TEXT_REGEX}\]\(([^\)]+?)#{FRAGMENT_REGEX}\)!
%r!^\[#{LINK_TEXT_REGEX}\]: (.+?)#{FRAGMENT_REGEX}$!
%r!(#{INLINE_LINK_REGEX}|#{REFERENCE_LINK_REGEX})!
CONVERTER_CLASS =
Bunto::Converters::Markdown

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Generator

Returns a new instance of Generator.



18
19
20
21
# File 'lib/bunto-relative-links/generator.rb', line 18

def initialize(site)
  @site    = site
  @context = context
end

Instance Attribute Details

#siteObject

Returns the value of attribute site.



3
4
5
# File 'lib/bunto-relative-links/generator.rb', line 3

def site
  @site
end

Instance Method Details

#generate(site) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/bunto-relative-links/generator.rb', line 23

def generate(site)
  @site    = site
  @context = context

  site.pages.each do |page|
    next unless markdown_extension?(page.extname)
    replace_relative_links!(page)
  end
end

#replace_relative_links!(page) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bunto-relative-links/generator.rb', line 33

def replace_relative_links!(page)
  url_base = File.dirname(page.path)

  page.content.gsub!(LINK_REGEX) do |original|
    link_type, link_text, relative_path, fragment = link_parts(Regexp.last_match)
    next original if fragment?(relative_path) || absolute_url?(relative_path)

    path = path_from_root(relative_path, url_base)
    url  = url_for_path(path)

    if url
      replacement_text(link_type, link_text, url, fragment)
    else
      original
    end
  end
rescue ArgumentError => e
  raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
end