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!(.*?)!.freeze
FRAGMENT_REGEX =
%r!(#.+?)?!.freeze
%r!\[#{LINK_TEXT_REGEX}\]\(([^\)]+?)#{FRAGMENT_REGEX}\)!.freeze
%r!^\s*?\[#{LINK_TEXT_REGEX}\]: (.+?)#{FRAGMENT_REGEX}\s*?$!.freeze
%r!(#{INLINE_LINK_REGEX}|#{REFERENCE_LINK_REGEX})!.freeze
CONVERTER_CLASS =
Jekyll::Converters::Markdown
CONFIG_KEY =
"relative_links"
ENABLED_KEY =
"enabled"
COLLECTIONS_KEY =
"collections"
LOG_KEY =
"Relative Links:"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Generator

Returns a new instance of Generator.



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

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#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
42
43
44
# File 'lib/jekyll-relative-links/generator.rb', line 28

def generate(site)
  return if disabled?

  @site    = site
  @context = context

  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)
    next if excluded?(document)

    replace_relative_links!(document)
  end
end

#replace_relative_links!(document) ⇒ Object



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

def replace_relative_links!(document)
  url_base = File.dirname(document.relative_path)
  return document if document.content.nil?

  document.content = document.content.dup.gsub(LINK_REGEX) do |original|
    link_type, link_text, relative_path, fragment = link_parts(Regexp.last_match)
    next original unless replaceable_link?(relative_path)

    path = path_from_root(relative_path, url_base)
    url  = url_for_path(path)
    next original unless url

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