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
core_link(text)
click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 48 def core_link(text) constants = ruby_constants(text).join('/') method = ruby_method(text) "#{CORE_BASE_URL}/#{constants}.html#{method}" end
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
stdlib_link(text)
click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 55 def stdlib_link(text) constants = ruby_constants(text).join('/') method = ruby_method(text) libdoc_part = "libdoc/#{ruby_stdlib(text)}/rdoc" "#{STDLIB_BASE_URL}/#{libdoc_part}/#{constants}.html#{method}" end
sub_core_links(text)
click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 26 def sub_core_links(text) match = text.match(CORE_REGEX) return text if match.nil? match.captures.reduce(text) do |result, capture| result.sub!(doc_regex(:core, capture), core_link(capture)) end end
sub_stdlib_links(text)
click to toggle source
# File lib/belajar/markdown/ruby_doc.rb, line 35 def sub_stdlib_links(text) match = text.match(STDLIB_REGEX) return text if match.nil? match.captures.reduce(text) do |result, capture| result.sub!(doc_regex(:stdlib, capture), stdlib_link(capture)) end end