class JekyllRelativeLinks::Generator
Constants
- COLLECTIONS_KEY
- CONFIG_KEY
- CONVERTER_CLASS
- ENABLED_KEY
- FRAGMENT_REGEX
- INLINE_LINK_REGEX
- LINK_REGEX
- LINK_TEXT_REGEX
- LOG_KEY
- REFERENCE_LINK_REGEX
Attributes
config[RW]
site[RW]
Public Class Methods
new(config)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 24 def initialize(config) @config = config end
Public Instance Methods
generate(site)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
absolute_url?(string)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 111 def absolute_url?(string) return unless string Addressable::URI.parse(string).absolute? rescue Addressable::URI::InvalidURIError nil end
collections?()
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 135 def collections? option(COLLECTIONS_KEY) == true end
context()
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 74 def context @context ||= JekyllRelativeLinks::Context.new(site) end
disabled?()
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 131 def disabled? option(ENABLED_KEY) == false end
excluded?(document)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 139 def excluded?(document) return false unless option("exclude") entry_filter = if document.respond_to?(:collection) document.collection.entry_filter else global_entry_filter end entry_filter.glob_include?(option("exclude"), document.relative_path).tap do |excluded| Jekyll.logger.debug(LOG_KEY, "excluded #{document.relative_path}") if excluded end end
fragment?(string)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 119 def fragment?(string) string&.start_with?("#") end
global_entry_filter()
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 153 def global_entry_filter @global_entry_filter ||= Jekyll::EntryFilter.new(site) end
link_parts(matches)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 66 def link_parts(matches) link_type = matches[2] ? :inline : :reference link_text = matches[link_type == :inline ? 2 : 5] relative_path = matches[link_type == :inline ? 3 : 6] fragment = matches[link_type == :inline ? 4 : 7] [link_type, link_text, relative_path, fragment] end
markdown_converter()
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 82 def markdown_converter @markdown_converter ||= site.find_converter_instance(CONVERTER_CLASS) end
markdown_extension?(extension)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 78 def markdown_extension?(extension) markdown_converter.matches(extension) end
option(key)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 127 def option(key) config[CONFIG_KEY] && config[CONFIG_KEY][key] end
path_from_root(relative_path, url_base)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 95 def path_from_root(relative_path, url_base) relative_path.sub!(%r!\A/!, "") absolute_path = File.expand_path(relative_path, url_base) absolute_path.sub(%r!\A#{Regexp.escape(Dir.pwd)}/!, "") end
potential_targets()
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 91 def potential_targets @potential_targets ||= site.pages + site.static_files + site.docs_to_write end
replaceable_link?(string)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 123 def replaceable_link?(string) !fragment?(string) && !absolute_url?(string) end
replacement_text(type, text, url, fragment = nil)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 101 def replacement_text(type, text, url, fragment = nil) url << fragment if fragment if type == :inline "[#{text}](#{url})" else "\n[#{text}]: #{url}" end end
url_for_path(path)
click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 86 def url_for_path(path) target = potential_targets.find { |p| p.relative_path.sub(%r!\A/!, "") == path } relative_url(target.url) if target&.url end