Class: Curldown::Render
- Inherits:
-
Object
- Object
- Curldown::Render
- Defined in:
- lib/curldown.rb
Instance Attribute Summary collapse
-
#bitbucket ⇒ Object
Returns the value of attribute bitbucket.
-
#github ⇒ Object
Returns the value of attribute github.
-
#html ⇒ Object
Returns the value of attribute html.
-
#lexers ⇒ Object
Returns the value of attribute lexers.
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#repo ⇒ Object
Returns the value of attribute repo.
-
#url ⇒ Object
Returns the value of attribute url.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #highlight! ⇒ Object
-
#initialize(user: false, repo: false, url: false, github: false, bitbucket: false) ⇒ Render
constructor
A new instance of Render.
- #render ⇒ Object
- #validate_lexer(string) ⇒ Object
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
#bitbucket ⇒ Object
Returns the value of attribute bitbucket.
28 29 30 |
# File 'lib/curldown.rb', line 28 def bitbucket @bitbucket end |
#github ⇒ Object
Returns the value of attribute github.
28 29 30 |
# File 'lib/curldown.rb', line 28 def github @github end |
#html ⇒ Object
Returns the value of attribute html.
28 29 30 |
# File 'lib/curldown.rb', line 28 def html @html end |
#lexers ⇒ Object
Returns the value of attribute lexers.
28 29 30 |
# File 'lib/curldown.rb', line 28 def lexers @lexers end |
#raw ⇒ Object
Returns the value of attribute raw.
28 29 30 |
# File 'lib/curldown.rb', line 28 def raw @raw end |
#repo ⇒ Object
Returns the value of attribute repo.
28 29 30 |
# File 'lib/curldown.rb', line 28 def repo @repo end |
#url ⇒ Object
Returns the value of attribute url.
28 29 30 |
# File 'lib/curldown.rb', line 28 def url @url end |
#user ⇒ Object
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 |
#render ⇒ Object
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 |