Class: JekyllRelativeLinks::Generator

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

Constant Summary collapse

%r!([^\]]+)!
FRAGMENT_REGEX =
%r!(#.+?)?!
%r!\[#{LINK_TEXT_REGEX}\]\(([^\)]+?)#{FRAGMENT_REGEX}\)!
%r!^\s*?\[#{LINK_TEXT_REGEX}\]: (.+?)#{FRAGMENT_REGEX}\s*?$!
%r!(#{INLINE_LINK_REGEX}|#{REFERENCE_LINK_REGEX})!
CONVERTER_CLASS =
Jekyll::Converters::Markdown
CONFIG_KEY =
"relative_links".freeze
ENABLED_KEY =
"enabled".freeze
COLLECTIONS_KEY =
"collections".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Generator

Returns a new instance of Generator.



23
24
25
26
# File 'lib/jekyll-relative-links/generator.rb', line 23

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

Instance Attribute Details

#siteObject

Returns the value of attribute site.



5
6
7
# File 'lib/jekyll-relative-links/generator.rb', line 5

def site
  @site
end

Instance Method Details

#generate(site) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jekyll-relative-links/generator.rb', line 28

def generate(site)
  @site    = site
  @context = context
  return if disabled?

  documents = site.pages
  documents = site.pages + site.docs_to_write if collections?

  documents.each do |document|
    next unless markdown_extension?(document.extname)
    next if document.is_a?(Jekyll::StaticFile)
    replace_relative_links!(document)
  end
end

#replace_relative_links!(document) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jekyll-relative-links/generator.rb', line 43

def replace_relative_links!(document)
  url_base = File.dirname(document.relative_path)

  document.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