class Belajar::Markdown::RubyDoc

Constants

CORE_BASE_URL
CORE_REGEX
RUBY_DOC_URL
STDLIB_BASE_URL
STDLIB_REGEX

Public Class Methods

parse(text) click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 14
def parse(text)
  new.parse(text)
end

Public Instance Methods

parse(text) click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 19
def parse(text)
  parsed_text = sub_stdlib_links(text)
  sub_core_links(parsed_text)
end

Private Instance Methods

doc_regex(type, capture) click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 44
def doc_regex(type, capture)
  Regexp.new(Regexp.escape("(ruby-doc #{type}: #{capture})"))
end
downcased?(text) click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 99
def downcased?(text)
  text == text.downcase
end
ruby_constants(text) click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 79
def ruby_constants(text)
  parts = text.split.last.split(/::|#/)
  select_capitalized(parts)
end
ruby_method(text) click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 84
def ruby_method(text)
  method = text.split(/::|#/).last
  return '' unless downcased?(method)

  method_type = text =~ /#/ ? 'i' : 'c'
  method_name = CGI.escape(method.strip).tr('%', '-').gsub(/\A-/, '')
  "#method-#{method_type}-#{method_name}"
end
ruby_stdlib(text) click to toggle source

Returns the stdlib part of the url. If an explicit stdlib name is defined in markdown, e.g.

(ruby-doc stdlib: net/http Net::HTTP) => 'net/http'

then this lib name is used. Else the lib is created from the constants, e.g.

(ruby-doc stdlib: Time) => 'time'
# File lib/belajar/markdown/ruby_doc.rb, line 69
def ruby_stdlib(text)
  parts = text.split

  if parts.length > 1
    parts.first.strip.downcase
  else
    ruby_constants(text).join('/').downcase
  end
end
select_capitalized(parts) click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 93
def select_capitalized(parts)
  parts.select do |part|
    part[0].match(/\w/) && part[0] == part[0].upcase
  end
end