class Curldown::Render

Attributes

bitbucket[RW]
github[RW]
html[RW]
lexers[RW]
raw[RW]
repo[RW]
url[RW]
user[RW]

Public Class Methods

new(user: false, repo: false, url: false, github: false, bitbucket: false) click to toggle source
# File lib/curldown.rb, line 29
def initialize(user: false, repo: false, url: false, github: false, bitbucket: false)
  @lexers = []
  Pygments.lexers.each{|k,v| @lexers << v[:aliases] }
  @lexers.flatten!
  if url
    @raw = get(url, json: false)
  else
    if github
      req = GitHub.new(user, repo)
    elsif bitbucket
      req = BitBucket.new(user, repo)
    else
      raise StandardError.new("Must specify either: github, bitbucket or url parameters.")
    end
    req.perform
    @raw= req.readme_md
  end
end

Public Instance Methods

highlight!() click to toggle source
# File lib/curldown.rb, line 51
def highlight!
  doc = Nokogiri::HTML(@html)
  doc.css("pre").each do |pre|
    attrs = pre.children[0].attributes['class']
    if attrs
      lang = validate_lexer(attrs.value)
    else
      lang = "sh"
    end
    highlight = Pygments.highlight(pre.content, :lexer => lang)
    parsed = Nokogiri::HTML(highlight).css(".highlight")
    pre.add_next_sibling(parsed)
    pre.remove
  end
  @html = doc.css("body").children.to_html
end
render() click to toggle source
# File lib/curldown.rb, line 47
def render
  md = Redcarpet::Markdown.new(Redcarpet::Render::HTML, fenced_code_blocks: true)
  @html= md.render(@raw)
end
validate_lexer(string) click to toggle source
# File lib/curldown.rb, line 67
def validate_lexer(string)
  @lexers.include?(string) ? string : "sh"
end