Class: MarkdownRubyDocumentation::RelativeLinkConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_ruby_documentation/relative_link_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject:) ⇒ RelativeLinkConverter

Returns a new instance of RelativeLinkConverter.



5
6
7
8
# File 'lib/markdown_ruby_documentation/relative_link_converter.rb', line 5

def initialize(subject:)
  @root_path = root_path
  @subject   = subject
end

Instance Attribute Details

#root_pathObject (readonly)

Returns the value of attribute root_path.



3
4
5
# File 'lib/markdown_ruby_documentation/relative_link_converter.rb', line 3

def root_path
  @root_path
end

#subjectObject (readonly)

Returns the value of attribute subject.



3
4
5
# File 'lib/markdown_ruby_documentation/relative_link_converter.rb', line 3

def subject
  @subject
end

#textObject (readonly)

Returns the value of attribute text.



3
4
5
# File 'lib/markdown_ruby_documentation/relative_link_converter.rb', line 3

def text
  @text
end

Instance Method Details

#call(text = nil) ⇒ Object



10
11
12
13
# File 'lib/markdown_ruby_documentation/relative_link_converter.rb', line 10

def call(text=nil)
  @text = text
  generate
end


36
37
38
39
40
41
42
# File 'lib/markdown_ruby_documentation/relative_link_converter.rb', line 36

def create_relative_link(link)
  if link.include?("#")
    "#" + link.split("#").last
  else
    link
  end
end

#generateObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/markdown_ruby_documentation/relative_link_converter.rb', line 15

def generate
  text.scan(/\[[\w?\-_!0-9 ]*\]\((.*?)\)/).each do |r|
    link = r.first

    if link.include?(path.to_s)
      @text = text.gsub(link, create_relative_link(link))
    end
  end
  text
end

#pathObject



26
27
28
29
30
31
32
33
34
# File 'lib/markdown_ruby_documentation/relative_link_converter.rb', line 26

def path
  @path ||= begin
    method = MarkdownRubyDocumentation::Method.create(subject.name, null_method: true, context: Kernel)
    parts  = method.context_name.to_s.split("::").reject(&:blank?)
    path   = parts.map { |p| p.underscore }.join("/")
    path   = "#{path}.md#{method.type_symbol}#{method.name}"
    MarkdownRubyDocumentation::GitHubLink::FileUrl.new(file_path: File.join(MarkdownRubyDocumentation::Generate.output_object.relative_dir, path)).to_s
  end
end