Class: Curldown::Render

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user: false, repo: false, url: false, github: false, bitbucket: false) ⇒ Render

Returns a new instance of Render.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#bitbucketObject

Returns the value of attribute bitbucket.



28
29
30
# File 'lib/curldown.rb', line 28

def bitbucket
  @bitbucket
end

#githubObject

Returns the value of attribute github.



28
29
30
# File 'lib/curldown.rb', line 28

def github
  @github
end

#htmlObject

Returns the value of attribute html.



28
29
30
# File 'lib/curldown.rb', line 28

def html
  @html
end

#lexersObject

Returns the value of attribute lexers.



28
29
30
# File 'lib/curldown.rb', line 28

def lexers
  @lexers
end

#rawObject

Returns the value of attribute raw.



28
29
30
# File 'lib/curldown.rb', line 28

def raw
  @raw
end

#repoObject

Returns the value of attribute repo.



28
29
30
# File 'lib/curldown.rb', line 28

def repo
  @repo
end

#urlObject

Returns the value of attribute url.



28
29
30
# File 'lib/curldown.rb', line 28

def url
  @url
end

#userObject

Returns the value of attribute user.



28
29
30
# File 'lib/curldown.rb', line 28

def user
  @user
end

Instance Method Details

#highlight!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

#renderObject



47
48
49
50
# 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) ⇒ Object



67
68
69
# File 'lib/curldown.rb', line 67

def validate_lexer(string)
  @lexers.include?(string) ? string : "sh"
end