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.5.64 spec/fixtures/githubblob.response
onebox-1.5.63 spec/fixtures/githubblob.response
onebox-1.5.62 spec/fixtures/githubblob.response
onebox-1.5.61 spec/fixtures/githubblob.response
onebox-1.5.60 spec/fixtures/githubblob.response
onebox-1.5.50 spec/fixtures/githubblob.response
onebox-1.5.49 spec/fixtures/githubblob.response
onebox-1.5.48 spec/fixtures/githubblob.response
onebox-1.5.47 spec/fixtures/githubblob.response
onebox-1.5.45 spec/fixtures/githubblob.response
onebox-1.5.44 spec/fixtures/githubblob.response
onebox-1.5.43 spec/fixtures/githubblob.response
onebox-1.5.42 spec/fixtures/githubblob.response
onebox-1.5.41 spec/fixtures/githubblob.response
onebox-1.5.40 spec/fixtures/githubblob.response
onebox-1.5.39 spec/fixtures/githubblob.response
onebox-1.5.38 spec/fixtures/githubblob.response
onebox-1.5.37 spec/fixtures/githubblob.response
onebox-1.5.36 spec/fixtures/githubblob.response
onebox-1.5.35 spec/fixtures/githubblob.response