Sha256: d0dcabcab2c4566b7cf415a40b549ae4e1c42c64eaf5265e55815de3d3102574

Contents?: true

Size: 1.1 KB

Versions: 108

Compression:

Stored size: 1.1 KB

Contents

require_dependency 'oneboxer/handlebars_onebox'

module Oneboxer
  class GithubBlobOnebox < HandlebarsOnebox

    matcher /^https?:\/\/(?:www\.)?github\.com\/[^\/]+\/[^\/]+\/blob\/.*/
    favicon 'github.png'

    def translate_url
      m = @url.match(/github\.com\/(?<user>[^\/]+)\/(?<repo>[^\/]+)\/blob\/(?<sha1>[^\/]+)\/(?<file>[^#]+)(#(L(?<from>[^-]*)(-L(?<to>.*))?))?/mi)
      if m
        @from = (m[:from] || -1).to_i
        @to = (m[:to] || -1).to_i
        @file = m[:file]
        return "https://raw.github.com/#{m[:user]}/#{m[:repo]}/#{m[:sha1]}/#{m[:file]}"
      end
      nil
    end

    def parse(data)

      if @from > 0
        if @to < 0
          @from = @from - 10
          @to = @from + 20
        end
        if @to > @from
          data = data.split("\n")[@from..@to].join("\n")
        end
      end

      extension = @file.split(".")[-1]
      @lang = extension

      truncated = false
      if data.length > SiteSetting.onebox_max_chars
        data = data[0..SiteSetting.onebox_max_chars-1]
        truncated = true
      end

      {content: data, truncated: truncated}
    end

  end
end

Version data entries

108 entries across 108 versions & 1 rubygems

Version Path
onebox-1.7.8 spec/fixtures/githubblob.response
onebox-1.7.7 spec/fixtures/githubblob.response
onebox-1.7.6 spec/fixtures/githubblob.response
onebox-1.7.5 spec/fixtures/githubblob.response
onebox-1.7.4 spec/fixtures/githubblob.response
onebox-1.7.3 spec/fixtures/githubblob.response
onebox-1.7.2 spec/fixtures/githubblob.response
onebox-1.7.1 spec/fixtures/githubblob.response
onebox-1.7.0 spec/fixtures/githubblob.response
onebox-1.6.9 spec/fixtures/githubblob.response
onebox-1.6.8 spec/fixtures/githubblob.response
onebox-1.6.7 spec/fixtures/githubblob.response
onebox-1.6.6 spec/fixtures/githubblob.response
onebox-1.6.5 spec/fixtures/githubblob.response
onebox-1.6.4 spec/fixtures/githubblob.response
onebox-1.6.3 spec/fixtures/githubblob.response
onebox-1.6.2 spec/fixtures/githubblob.response
onebox-1.6.1 spec/fixtures/githubblob.response
onebox-1.6.0 spec/fixtures/githubblob.response
onebox-1.5.65 spec/fixtures/githubblob.response