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.8.39 spec/fixtures/githubblob.response
onebox-1.8.38 spec/fixtures/githubblob.response
onebox-1.8.36 spec/fixtures/githubblob.response
onebox-1.8.35 spec/fixtures/githubblob.response
onebox-1.8.34 spec/fixtures/githubblob.response
onebox-1.8.33 spec/fixtures/githubblob.response
onebox-1.8.32 spec/fixtures/githubblob.response
onebox-1.8.31 spec/fixtures/githubblob.response
onebox-1.8.30 spec/fixtures/githubblob.response
onebox-1.8.29 spec/fixtures/githubblob.response
onebox-1.8.28 spec/fixtures/githubblob.response
onebox-1.8.27 spec/fixtures/githubblob.response
onebox-1.8.26 spec/fixtures/githubblob.response
onebox-1.8.25 spec/fixtures/githubblob.response
onebox-1.8.24 spec/fixtures/githubblob.response
onebox-1.8.23 spec/fixtures/githubblob.response
onebox-1.8.22 spec/fixtures/githubblob.response
onebox-1.8.21 spec/fixtures/githubblob.response
onebox-1.8.20 spec/fixtures/githubblob.response
onebox-1.8.19 spec/fixtures/githubblob.response