Class: JekyllRelativeLinks::Generator

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

Defined Under Namespace

Classes: Link

Constant Summary collapse

%r!(.*?)!.freeze
FRAGMENT_REGEX =
%r!(#.+?|)?!.freeze
TITLE_REGEX =
%r{(\s+"(?:\\"|[^"])*(?<!\\)"|\s+"(?:\\'|[^'])*(?<!\\)')?}.freeze
FRAG_AND_TITLE_REGEX =
%r!#{FRAGMENT_REGEX}#{TITLE_REGEX}!.freeze
%r!\[#{LINK_TEXT_REGEX}\]\(([^)]+?)#{FRAG_AND_TITLE_REGEX}\)!.freeze
%r!^\s*?\[#{LINK_TEXT_REGEX}\]: (.+?)#{FRAG_AND_TITLE_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.



26
27
28
# File 'lib/jekyll-relative-links/generator.rb', line 26

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jekyll-relative-links/generator.rb', line 30

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



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

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 = link_parts(Regexp.last_match)
    next original unless replaceable_link?(link.path)

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

    link.path = url
    replacement_text(link)
  end

  replace_relative_links_excerpt!(document)
rescue ArgumentError => e
  raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
end