Sha256: 593dbf62a5a75c0a8c02bfe052a6fbc431b6ef9624569b450cb46bcbe8f38e5d

Contents?: true

Size: 1.23 KB

Versions: 34

Compression:

Stored size: 1.23 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 = case extension
                when "rb" then "ruby"
                when "js" then "javascript"
                else extension
             end

      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

34 entries across 34 versions & 1 rubygems

Version Path
onebox-1.5.5 spec/fixtures/githubblob.response
onebox-1.5.3 spec/fixtures/githubblob.response
onebox-1.5.2 spec/fixtures/githubblob.response
onebox-1.5.1 spec/fixtures/githubblob.response
onebox-1.5.0 spec/fixtures/githubblob.response
onebox-1.4.9 spec/fixtures/githubblob.response
onebox-1.4.8 spec/fixtures/githubblob.response
onebox-1.4.7 spec/fixtures/githubblob.response
onebox-1.4.5 spec/fixtures/githubblob.response
onebox-1.4.4 spec/fixtures/githubblob.response
onebox-1.4.3 spec/fixtures/githubblob.response
onebox-1.4.2 spec/fixtures/githubblob.response
onebox-1.4.1 spec/fixtures/githubblob.response
onebox-1.4.0 spec/fixtures/githubblob.response
onebox-1.3.9 spec/fixtures/githubblob.response
onebox-1.3.8 spec/fixtures/githubblob.response
onebox-1.3.7 spec/fixtures/githubblob.response
onebox-1.3.6 spec/fixtures/githubblob.response
onebox-1.3.5 spec/fixtures/githubblob.response
onebox-1.3.4 spec/fixtures/githubblob.response