Class: Octopress::Gist::Tag
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- Octopress::Gist::Tag
- Defined in:
- lib/octopress-gist/gist.rb
Direct Known Subclasses
Instance Method Summary collapse
- #download_gist(options) ⇒ Object
- #gist_files(gist, filename = nil) ⇒ Object
-
#initialize(tag_name, markup, tokens) ⇒ Tag
constructor
A new instance of Tag.
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ Tag
Returns a new instance of Tag.
5 6 7 8 9 10 |
# File 'lib/octopress-gist/gist.rb', line 5 def initialize(tag_name, markup, tokens) super @cache_disabled = false @tag_name = tag_name @markup = markup end |
Instance Method Details
#download_gist(options) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/octopress-gist/gist.rb', line 79 def download_gist() retried = false begin response = URI.parse("https://api.github.com/gists/#{options[:gist_id]}").read Cache.write_cache(response, ) if [:cache] JSON.parse(response) rescue => e raise if retried retried = true retry end rescue => e puts "Failed to download Gist: #{gist}.".red puts e.extend Error end |
#gist_files(gist, filename = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/octopress-gist/gist.rb', line 63 def gist_files(gist, filename=nil) gists = gist['files'] # Return a single file if specified if filename if file = gists[filename] [file] else # List file names found in the error message. filenames = gists.keys.each { |k| k.to_s } raise "File '#{filename}' not found in gist #{gist['id']}. Did you mean '#{filenames.join('\' or \'')}'?" end else gists.values.each{ |v| v } end end |
#render(context) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/octopress-gist/gist.rb', line 12 def render(context) if CodeHighlighter.clean_markup(@markup) =~ /([\S]*)\s*(\S*)/ gist_id, filename = $1.strip, $2.strip filename = $2.strip == "" ? nil : $2.strip = { gist_id: $1.strip, cache: !@cache_disabled, escape: false } = CodeHighlighter.parse_markup(@markup, ) if @cache_disabled or ! gist = Cache.read_cache() gist = download_gist() end [:url] ||= gist['html_url'] [:link_text] ||= "View Gist" output = [] files = gist_files(gist, filename) if files.length > 1 && ([:start] || [:end] || [:range]) puts "Gist embed warning:" puts " You should probably avoid using start, end or range options on gist embeds with multiple files" puts " Embed files one at a time like {% gist 124151 filename %}" end files.each do |file| opt = { title: file['filename'], lang: (file['language'] || '').downcase }.merge() begin code = CodeHighlighter.select_lines(file['content'], opt) code = CodeHighlighter.highlight(code, opt) code = "<notextile>#{code}</notextile>" if context.registers[:page]['path'].match(/textile/) output << code rescue => e markup = "{% #{@tag_name} #{@markup.strip} %}" CodeHighlighter.highlight_failed(e, "{% #{@tag_name} [gist_id] [file_name] [options...] %}", markup, file['content']) end end output.join "\n" else error_msg = "Syntax Error: {% #{@tag_name} #{@markup.strip} %}" syntax_msg = "Should be: {% #{@tag_name} [gistid] [file_name] [options...] %}" puts error_msg.red puts syntax_msg "<pre>#{error_msg}\n#{syntax_msg}</pre>" end end |