mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-12-20 12:32:39 -05:00
1221c393a3
Conflicts: lib/oneboxer.rb lib/oneboxer/whitelist.rb spec/controllers/robots_txt_controller_spec.rb
49 lines
1.2 KiB
Ruby
49 lines
1.2 KiB
Ruby
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
|