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.18 spec/fixtures/githubblob.response
onebox-1.8.17 spec/fixtures/githubblob.response
onebox-1.8.16 spec/fixtures/githubblob.response
onebox-1.8.15 spec/fixtures/githubblob.response
onebox-1.8.14 spec/fixtures/githubblob.response
onebox-1.8.13 spec/fixtures/githubblob.response
onebox-1.8.12 spec/fixtures/githubblob.response
onebox-1.8.11 spec/fixtures/githubblob.response
onebox-1.8.10 spec/fixtures/githubblob.response
onebox-1.8.9 spec/fixtures/githubblob.response
onebox-1.8.8 spec/fixtures/githubblob.response
onebox-1.8.7 spec/fixtures/githubblob.response
onebox-1.8.6 spec/fixtures/githubblob.response
onebox-1.8.5 spec/fixtures/githubblob.response
onebox-1.8.4 spec/fixtures/githubblob.response
onebox-1.8.3 spec/fixtures/githubblob.response
onebox-1.8.2 spec/fixtures/githubblob.response
onebox-1.8.1 spec/fixtures/githubblob.response
onebox-1.8.0 spec/fixtures/githubblob.response
onebox-1.7.9 spec/fixtures/githubblob.response