onebox 1.8.48 → 1.8.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/lib/onebox/engine.rb +1 -0
- data/lib/onebox/engine/github_blob_onebox.rb +13 -198
- data/lib/onebox/engine/gitlab_blob_onebox.rb +25 -0
- data/lib/onebox/engine/standard_embed.rb +1 -1
- data/lib/onebox/mixins/git_blob_onebox.rb +215 -0
- data/lib/onebox/version.rb +1 -1
- data/templates/git_blob.mustache +37 -0
- data/templates/githubblob.mustache +1 -0
- data/templates/gitlabblob.mustache +1 -0
- metadata +7 -3
- data/templates/githubblob.mustache +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 436b288a464534f29ae523c8c02154bb954073c5e54626a912ca7ff68bea020b
|
4
|
+
data.tar.gz: 3218a25d11cf3e6d6309a8477bfe4276c327e34fae286770c4710533f3ce28e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b333be477f8bfdcd7b0cc9606c72147f7d4e0a872278420f389b000c0f2e6d9473abc31ec9c6ffc406f77c629b45d900dac19cb864d0a01e7da10c7ee0c62af5
|
7
|
+
data.tar.gz: 0d94a3f320e2a206b3ea0a7de43a23dcb336b1690cd3ff06ee56cdf7eea4ec7de93880ad92a86efdc6dd1583b503e948546113ee21b973d9667bf977486d538e
|
data/lib/onebox/engine.rb
CHANGED
@@ -1,210 +1,25 @@
|
|
1
|
+
require_relative '../mixins/git_blob_onebox'
|
2
|
+
|
1
3
|
module Onebox
|
2
4
|
module Engine
|
3
5
|
class GithubBlobOnebox
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
EXPAND_AFTER = 0b001
|
8
|
-
EXPAND_BEFORE = 0b010
|
9
|
-
EXPAND_NONE = 0b0
|
10
|
-
|
11
|
-
DEFAULTS = {
|
12
|
-
EXPAND_ONE_LINER: EXPAND_AFTER | EXPAND_BEFORE, #set how to expand a one liner. user EXPAND_NONE to disable expand
|
13
|
-
LINES_BEFORE: 10,
|
14
|
-
LINES_AFTER: 10,
|
15
|
-
SHOW_LINE_NUMBER: true,
|
16
|
-
MAX_LINES: 20,
|
17
|
-
MAX_CHARS: 5000
|
18
|
-
}
|
19
|
-
|
20
|
-
matches_regexp(/^https?:\/\/(www\.)?github\.com.*\/blob\//)
|
21
|
-
always_https
|
22
|
-
|
23
|
-
def initialize(link, cache = nil, timeout = nil)
|
24
|
-
super link, cache , timeout
|
25
|
-
#merge engine options from global Onebox.options interface
|
26
|
-
# self.options = Onebox.options["GithubBlobOnebox"] # self.class.name.split("::").last.to_s
|
27
|
-
# self.options = Onebox.options[self.class.name.split("::").last.to_s] #We can use this a more generic approach. extract the engine class name automatically
|
28
|
-
|
29
|
-
self.options = DEFAULTS
|
30
|
-
|
31
|
-
# Define constant after merging options set in Onebox.options
|
32
|
-
# We can define constant automatically.
|
33
|
-
options.each_pair do |constant_name, value|
|
34
|
-
constant_name_u = constant_name.to_s.upcase
|
35
|
-
if constant_name_u == constant_name.to_s
|
36
|
-
#define a constant if not already defined
|
37
|
-
self.class.const_set constant_name_u.to_sym , options[constant_name_u.to_sym] unless self.class.const_defined? constant_name_u.to_sym
|
38
|
-
end
|
39
|
-
end
|
6
|
+
def self.git_regexp
|
7
|
+
/^https?:\/\/(www\.)?github\.com.*\/blob\//
|
40
8
|
end
|
41
|
-
|
42
|
-
|
43
|
-
@selected_lines_array = nil
|
44
|
-
@selected_one_liner = 0
|
45
|
-
|
46
|
-
def calc_range(m, contents_lines_size)
|
47
|
-
truncated = false
|
48
|
-
from = /\d+/.match(m[:from]) #get numeric should only match a positive interger
|
49
|
-
to = /\d+/.match(m[:to]) #get numeric should only match a positive interger
|
50
|
-
range_provided = !(from.nil? && to.nil?) #true if "from" or "to" provided in URL
|
51
|
-
from = from.nil? ? 1 : from[0].to_i #if from not provided default to 1st line
|
52
|
-
to = to.nil? ? -1 : to[0].to_i #if to not provided default to undefiend to be handled later in the logic
|
53
|
-
|
54
|
-
if to === -1 && range_provided #case "from" exists but no valid "to". aka ONE_LINER
|
55
|
-
one_liner = true
|
56
|
-
to = from
|
57
|
-
else
|
58
|
-
one_liner = false
|
59
|
-
end
|
60
|
-
|
61
|
-
unless range_provided #case no range provided default to 1..MAX_LINES
|
62
|
-
from = 1
|
63
|
-
to = MAX_LINES
|
64
|
-
truncated = true if contents_lines_size > MAX_LINES
|
65
|
-
#we can technically return here
|
66
|
-
end
|
67
|
-
|
68
|
-
from, to = [from, to].sort #enforce valid range. [from < to]
|
69
|
-
from = 1 if from > contents_lines_size #if "from" out of TOP bound set to 1st line
|
70
|
-
to = contents_lines_size if to > contents_lines_size #if "to" is out of TOP bound set to last line.
|
71
|
-
|
72
|
-
if one_liner
|
73
|
-
@selected_one_liner = from
|
74
|
-
if EXPAND_ONE_LINER != EXPAND_NONE
|
75
|
-
if (EXPAND_ONE_LINER & EXPAND_BEFORE != 0) # check if EXPAND_BEFORE flag is on
|
76
|
-
from = [1, from - LINES_BEFORE].max # make sure expand before does not go out of bound
|
77
|
-
end
|
78
|
-
|
79
|
-
if (EXPAND_ONE_LINER & EXPAND_AFTER != 0) # check if EXPAND_FLAG flag is on
|
80
|
-
to = [to + LINES_AFTER, contents_lines_size].min # make sure expand after does not go out of bound
|
81
|
-
end
|
82
|
-
|
83
|
-
from = contents_lines_size if from > contents_lines_size #if "from" is out of the content top bound
|
84
|
-
# to = contents_lines_size if to > contents_lines_size #if "to" is out of the content top bound
|
85
|
-
else
|
86
|
-
#no expand show the one liner solely
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
if to - from > MAX_LINES && !one_liner #if exceed the MAX_LINES limit correct unless range was produced by one_liner which it expand setting will allow exceeding the line limit
|
91
|
-
truncated = true
|
92
|
-
to = from + MAX_LINES - 1
|
93
|
-
end
|
94
|
-
|
95
|
-
{
|
96
|
-
from: from, #calculated from
|
97
|
-
from_minus_one: from - 1, #used for getting currect ol>li numbering with css used in template
|
98
|
-
to: to, #calculated to
|
99
|
-
one_liner: one_liner, #boolean if a one-liner
|
100
|
-
selected_one_liner: @selected_one_liner, #if a one liner is provided we create a reference for it.
|
101
|
-
range_provided: range_provided, #boolean if range provided
|
102
|
-
truncated: truncated
|
103
|
-
}
|
9
|
+
def self.onebox_name
|
10
|
+
"githubblob"
|
104
11
|
end
|
105
12
|
|
106
|
-
|
107
|
-
def
|
108
|
-
|
109
|
-
a_lines = str.lines
|
110
|
-
a_lines.each do |l|
|
111
|
-
l = l.chomp("\n") # remove new line
|
112
|
-
m = l.match(/^[ ]*/) # find leading spaces 0 or more
|
113
|
-
unless m.nil? || l.size == m[0].size || m[0].size == 0 # no match | only spaces in line | empty line
|
114
|
-
m_str_length = m[0].size
|
115
|
-
if m_str_length <= 1 # minimum space is 1 or nothing we can break we found our minimum
|
116
|
-
min_space = m_str_length
|
117
|
-
break #stop iteration
|
118
|
-
end
|
119
|
-
if m_str_length < min_space
|
120
|
-
min_space = m_str_length
|
121
|
-
end
|
122
|
-
else
|
123
|
-
next # SKIP no match or line is only spaces
|
124
|
-
end
|
125
|
-
end
|
126
|
-
a_lines.each do |l|
|
127
|
-
re = Regexp.new "^[ ]{#{min_space}}" #match the minimum spaces of the line
|
128
|
-
l.gsub!(re, "")
|
129
|
-
end
|
130
|
-
a_lines.join
|
13
|
+
include Onebox::Mixins::GitBlobOnebox
|
14
|
+
def raw_regexp
|
15
|
+
/github\.com\/(?<user>[^\/]+)\/(?<repo>[^\/]+)\/blob\/(?<sha1>[^\/]+)\/(?<file>[^#]+)(#(L(?<from>[^-]*)(-L(?<to>.*))?))?/mi
|
131
16
|
end
|
132
|
-
|
133
|
-
|
134
|
-
lines = removeLeadingIndentation(lines.join).lines # A little ineffeicent we could modify removeLeadingIndentation to accept array and return array, but for now it is only working with a string
|
135
|
-
hash_builder = []
|
136
|
-
output_builder = []
|
137
|
-
lines.map.with_index { |line, i|
|
138
|
-
lnum = (i.to_i + start)
|
139
|
-
hash_builder.push(line_number: lnum, data: line.gsub("\n", ""), selected: (selected == lnum) ? true : false)
|
140
|
-
output_builder.push "#{lnum}: #{line}"
|
141
|
-
}
|
142
|
-
{ output: output_builder.join(), array: hash_builder }
|
17
|
+
def raw_template(m)
|
18
|
+
"https://raw.github.com/#{m[:user]}/#{m[:repo]}/#{m[:sha1]}/#{m[:file]}"
|
143
19
|
end
|
144
|
-
|
145
|
-
|
146
|
-
return @raw if @raw
|
147
|
-
|
148
|
-
m = @url.match(/github\.com\/(?<user>[^\/]+)\/(?<repo>[^\/]+)\/blob\/(?<sha1>[^\/]+)\/(?<file>[^#]+)(#(L(?<from>[^-]*)(-L(?<to>.*))?))?/mi)
|
149
|
-
|
150
|
-
if m
|
151
|
-
from = /\d+/.match(m[:from]) #get numeric should only match a positive interger
|
152
|
-
to = /\d+/.match(m[:to]) #get numeric should only match a positive interger
|
153
|
-
|
154
|
-
@file = m[:file]
|
155
|
-
@lang = Onebox::FileTypeFinder.from_file_name(m[:file])
|
156
|
-
contents = open("https://raw.github.com/#{m[:user]}/#{m[:repo]}/#{m[:sha1]}/#{m[:file]}", read_timeout: timeout).read
|
157
|
-
|
158
|
-
contents_lines = contents.lines #get contents lines
|
159
|
-
contents_lines_size = contents_lines.size #get number of lines
|
160
|
-
|
161
|
-
cr = calc_range(m, contents_lines_size) #calculate the range of lines for output
|
162
|
-
selected_one_liner = cr[:selected_one_liner] #if url is a one-liner calc_range will return it
|
163
|
-
from = cr[:from]
|
164
|
-
to = cr[:to]
|
165
|
-
@truncated = cr[:truncated]
|
166
|
-
range_provided = cr[:range_provided]
|
167
|
-
@cr_results = cr
|
168
|
-
|
169
|
-
if range_provided #if a range provided (single line or more)
|
170
|
-
if SHOW_LINE_NUMBER
|
171
|
-
lines_result = line_number_helper(contents_lines[(from - 1)..(to - 1)], from, selected_one_liner) #print code with prefix line numbers in case range provided
|
172
|
-
contents = lines_result[:output]
|
173
|
-
@selected_lines_array = lines_result[:array]
|
174
|
-
else
|
175
|
-
contents = contents_lines[(from - 1)..(to - 1)].join()
|
176
|
-
end
|
177
|
-
|
178
|
-
else
|
179
|
-
contents = contents_lines[(from - 1)..(to - 1)].join()
|
180
|
-
end
|
181
|
-
|
182
|
-
if contents.length > MAX_CHARS #truncate content chars to limits
|
183
|
-
contents = contents[0..MAX_CHARS]
|
184
|
-
@truncated = true
|
185
|
-
end
|
186
|
-
|
187
|
-
@raw = contents
|
188
|
-
end
|
20
|
+
def title
|
21
|
+
Sanitize.fragment(URI.unescape(link).sub(/^https?\:\/\/github\.com\//, ''))
|
189
22
|
end
|
190
|
-
|
191
|
-
def data
|
192
|
-
@data ||= {
|
193
|
-
title: Sanitize.fragment(URI.unescape(link).sub(/^https?\:\/\/github\.com\//, '')),
|
194
|
-
link: link,
|
195
|
-
# IMPORTANT NOTE: All of the other class variables are populated
|
196
|
-
# as *side effects* of the `raw` method! They must all appear
|
197
|
-
# AFTER the call to `raw`! Don't get bitten by this like I did!
|
198
|
-
content: raw,
|
199
|
-
lang: "lang-#{@lang}",
|
200
|
-
lines: @selected_lines_array ,
|
201
|
-
has_lines: !@selected_lines_array.nil?,
|
202
|
-
selected_one_liner: @selected_one_liner,
|
203
|
-
cr_results: @cr_results,
|
204
|
-
truncated: @truncated
|
205
|
-
}
|
206
|
-
end
|
207
|
-
|
208
23
|
end
|
209
24
|
end
|
210
25
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative '../mixins/git_blob_onebox'
|
2
|
+
|
3
|
+
module Onebox
|
4
|
+
module Engine
|
5
|
+
class GitlabBlobOnebox
|
6
|
+
def self.git_regexp
|
7
|
+
/^https?:\/\/(www\.)?gitlab\.com.*\/blob\//
|
8
|
+
end
|
9
|
+
def self.onebox_name
|
10
|
+
"gitlabblob"
|
11
|
+
end
|
12
|
+
|
13
|
+
include Onebox::Mixins::GitBlobOnebox
|
14
|
+
def raw_regexp
|
15
|
+
/gitlab\.com\/(?<user>[^\/]+)\/(?<repo>[^\/]+)\/blob\/(?<sha1>[^\/]+)\/(?<file>[^#]+)(#(L(?<from>[^-]*)(-L(?<to>.*))?))?/mi
|
16
|
+
end
|
17
|
+
def raw_template(m)
|
18
|
+
"https://gitlab.com/#{m[:user]}/#{m[:repo]}/raw/#{m[:sha1]}/#{m[:file]}"
|
19
|
+
end
|
20
|
+
def title
|
21
|
+
Sanitize.fragment(URI.unescape(link).sub(/^https?\:\/\/gitlab\.com\//, ''))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -127,7 +127,7 @@ module Onebox
|
|
127
127
|
favicon = "#{uri.scheme}:#{favicon}"
|
128
128
|
elsif favicon && favicon.match(/^https?:\/\//i).nil?
|
129
129
|
uri = URI(url)
|
130
|
-
favicon = if !(
|
130
|
+
favicon = if !favicon.start_with?("/") && uri.path.present?
|
131
131
|
"#{uri.scheme}://#{uri.host.sub(/\/$/, '')}#{uri.path.sub(/\/$/, '')}/#{favicon.sub(/^\//, '')}"
|
132
132
|
else
|
133
133
|
"#{uri.scheme}://#{uri.host.sub(/\/$/, '')}/#{favicon.sub(/^\//, '')}"
|
@@ -0,0 +1,215 @@
|
|
1
|
+
module Onebox
|
2
|
+
module Mixins
|
3
|
+
module GitBlobOnebox
|
4
|
+
def self.included(klass)
|
5
|
+
klass.include(Onebox::Engine)
|
6
|
+
klass.include(Onebox::LayoutSupport)
|
7
|
+
klass.matches_regexp(klass.git_regexp)
|
8
|
+
klass.always_https
|
9
|
+
klass.include(InstanceMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
EXPAND_AFTER = 0b001
|
13
|
+
EXPAND_BEFORE = 0b010
|
14
|
+
EXPAND_NONE = 0b0
|
15
|
+
|
16
|
+
DEFAULTS = {
|
17
|
+
EXPAND_ONE_LINER: EXPAND_AFTER | EXPAND_BEFORE, #set how to expand a one liner. user EXPAND_NONE to disable expand
|
18
|
+
LINES_BEFORE: 10,
|
19
|
+
LINES_AFTER: 10,
|
20
|
+
SHOW_LINE_NUMBER: true,
|
21
|
+
MAX_LINES: 20,
|
22
|
+
MAX_CHARS: 5000
|
23
|
+
}
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
def initialize(link, cache = nil, timeout = nil)
|
27
|
+
super link, cache , timeout
|
28
|
+
#merge engine options from global Onebox.options interface
|
29
|
+
# self.options = Onebox.options["GithubBlobOnebox"] # self.class.name.split("::").last.to_s
|
30
|
+
# self.options = Onebox.options[self.class.name.split("::").last.to_s] #We can use this a more generic approach. extract the engine class name automatically
|
31
|
+
|
32
|
+
self.options = DEFAULTS
|
33
|
+
|
34
|
+
# Define constant after merging options set in Onebox.options
|
35
|
+
# We can define constant automatically.
|
36
|
+
options.each_pair do |constant_name, value|
|
37
|
+
constant_name_u = constant_name.to_s.upcase
|
38
|
+
if constant_name_u == constant_name.to_s
|
39
|
+
#define a constant if not already defined
|
40
|
+
unless self.class.const_defined? constant_name_u.to_sym
|
41
|
+
Onebox::Mixins::GitBlobOnebox.const_set constant_name_u.to_sym , options[constant_name_u.to_sym]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
@selected_lines_array = nil
|
49
|
+
@selected_one_liner = 0
|
50
|
+
|
51
|
+
def calc_range(m, contents_lines_size)
|
52
|
+
truncated = false
|
53
|
+
from = /\d+/.match(m[:from]) #get numeric should only match a positive interger
|
54
|
+
to = /\d+/.match(m[:to]) #get numeric should only match a positive interger
|
55
|
+
range_provided = !(from.nil? && to.nil?) #true if "from" or "to" provided in URL
|
56
|
+
from = from.nil? ? 1 : from[0].to_i #if from not provided default to 1st line
|
57
|
+
to = to.nil? ? -1 : to[0].to_i #if to not provided default to undefiend to be handled later in the logic
|
58
|
+
|
59
|
+
if to === -1 && range_provided #case "from" exists but no valid "to". aka ONE_LINER
|
60
|
+
one_liner = true
|
61
|
+
to = from
|
62
|
+
else
|
63
|
+
one_liner = false
|
64
|
+
end
|
65
|
+
|
66
|
+
unless range_provided #case no range provided default to 1..MAX_LINES
|
67
|
+
from = 1
|
68
|
+
to = MAX_LINES
|
69
|
+
truncated = true if contents_lines_size > MAX_LINES
|
70
|
+
#we can technically return here
|
71
|
+
end
|
72
|
+
|
73
|
+
from, to = [from, to].sort #enforce valid range. [from < to]
|
74
|
+
from = 1 if from > contents_lines_size #if "from" out of TOP bound set to 1st line
|
75
|
+
to = contents_lines_size if to > contents_lines_size #if "to" is out of TOP bound set to last line.
|
76
|
+
|
77
|
+
if one_liner
|
78
|
+
@selected_one_liner = from
|
79
|
+
if EXPAND_ONE_LINER != EXPAND_NONE
|
80
|
+
if (EXPAND_ONE_LINER & EXPAND_BEFORE != 0) # check if EXPAND_BEFORE flag is on
|
81
|
+
from = [1, from - LINES_BEFORE].max # make sure expand before does not go out of bound
|
82
|
+
end
|
83
|
+
|
84
|
+
if (EXPAND_ONE_LINER & EXPAND_AFTER != 0) # check if EXPAND_FLAG flag is on
|
85
|
+
to = [to + LINES_AFTER, contents_lines_size].min # make sure expand after does not go out of bound
|
86
|
+
end
|
87
|
+
|
88
|
+
from = contents_lines_size if from > contents_lines_size #if "from" is out of the content top bound
|
89
|
+
# to = contents_lines_size if to > contents_lines_size #if "to" is out of the content top bound
|
90
|
+
else
|
91
|
+
#no expand show the one liner solely
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
if to - from > MAX_LINES && !one_liner #if exceed the MAX_LINES limit correct unless range was produced by one_liner which it expand setting will allow exceeding the line limit
|
96
|
+
truncated = true
|
97
|
+
to = from + MAX_LINES - 1
|
98
|
+
end
|
99
|
+
|
100
|
+
{
|
101
|
+
from: from, #calculated from
|
102
|
+
from_minus_one: from - 1, #used for getting currect ol>li numbering with css used in template
|
103
|
+
to: to, #calculated to
|
104
|
+
one_liner: one_liner, #boolean if a one-liner
|
105
|
+
selected_one_liner: @selected_one_liner, #if a one liner is provided we create a reference for it.
|
106
|
+
range_provided: range_provided, #boolean if range provided
|
107
|
+
truncated: truncated
|
108
|
+
}
|
109
|
+
end
|
110
|
+
|
111
|
+
#minimize/compact leading indentation while preserving overall indentation
|
112
|
+
def removeLeadingIndentation(str)
|
113
|
+
min_space = 100
|
114
|
+
a_lines = str.lines
|
115
|
+
a_lines.each do |l|
|
116
|
+
l = l.chomp("\n") # remove new line
|
117
|
+
m = l.match(/^[ ]*/) # find leading spaces 0 or more
|
118
|
+
unless m.nil? || l.size == m[0].size || m[0].size == 0 # no match | only spaces in line | empty line
|
119
|
+
m_str_length = m[0].size
|
120
|
+
if m_str_length <= 1 # minimum space is 1 or nothing we can break we found our minimum
|
121
|
+
min_space = m_str_length
|
122
|
+
break #stop iteration
|
123
|
+
end
|
124
|
+
if m_str_length < min_space
|
125
|
+
min_space = m_str_length
|
126
|
+
end
|
127
|
+
else
|
128
|
+
next # SKIP no match or line is only spaces
|
129
|
+
end
|
130
|
+
end
|
131
|
+
a_lines.each do |l|
|
132
|
+
re = Regexp.new "^[ ]{#{min_space}}" #match the minimum spaces of the line
|
133
|
+
l.gsub!(re, "")
|
134
|
+
end
|
135
|
+
a_lines.join
|
136
|
+
end
|
137
|
+
|
138
|
+
def line_number_helper(lines, start, selected)
|
139
|
+
lines = removeLeadingIndentation(lines.join).lines # A little ineffeicent we could modify removeLeadingIndentation to accept array and return array, but for now it is only working with a string
|
140
|
+
hash_builder = []
|
141
|
+
output_builder = []
|
142
|
+
lines.map.with_index { |line, i|
|
143
|
+
lnum = (i.to_i + start)
|
144
|
+
hash_builder.push(line_number: lnum, data: line.gsub("\n", ""), selected: (selected == lnum) ? true : false)
|
145
|
+
output_builder.push "#{lnum}: #{line}"
|
146
|
+
}
|
147
|
+
{ output: output_builder.join(), array: hash_builder }
|
148
|
+
end
|
149
|
+
|
150
|
+
def raw
|
151
|
+
return @raw if @raw
|
152
|
+
|
153
|
+
m = @url.match(self.raw_regexp)
|
154
|
+
|
155
|
+
if m
|
156
|
+
from = /\d+/.match(m[:from]) #get numeric should only match a positive interger
|
157
|
+
to = /\d+/.match(m[:to]) #get numeric should only match a positive interger
|
158
|
+
|
159
|
+
@file = m[:file]
|
160
|
+
@lang = Onebox::FileTypeFinder.from_file_name(m[:file])
|
161
|
+
contents = open(self.raw_template(m), read_timeout: timeout).read
|
162
|
+
|
163
|
+
contents_lines = contents.lines #get contents lines
|
164
|
+
contents_lines_size = contents_lines.size #get number of lines
|
165
|
+
|
166
|
+
cr = calc_range(m, contents_lines_size) #calculate the range of lines for output
|
167
|
+
selected_one_liner = cr[:selected_one_liner] #if url is a one-liner calc_range will return it
|
168
|
+
from = cr[:from]
|
169
|
+
to = cr[:to]
|
170
|
+
@truncated = cr[:truncated]
|
171
|
+
range_provided = cr[:range_provided]
|
172
|
+
@cr_results = cr
|
173
|
+
|
174
|
+
if range_provided #if a range provided (single line or more)
|
175
|
+
if SHOW_LINE_NUMBER
|
176
|
+
lines_result = line_number_helper(contents_lines[(from - 1)..(to - 1)], from, selected_one_liner) #print code with prefix line numbers in case range provided
|
177
|
+
contents = lines_result[:output]
|
178
|
+
@selected_lines_array = lines_result[:array]
|
179
|
+
else
|
180
|
+
contents = contents_lines[(from - 1)..(to - 1)].join()
|
181
|
+
end
|
182
|
+
|
183
|
+
else
|
184
|
+
contents = contents_lines[(from - 1)..(to - 1)].join()
|
185
|
+
end
|
186
|
+
|
187
|
+
if contents.length > MAX_CHARS #truncate content chars to limits
|
188
|
+
contents = contents[0..MAX_CHARS]
|
189
|
+
@truncated = true
|
190
|
+
end
|
191
|
+
|
192
|
+
@raw = contents
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def data
|
197
|
+
@data ||= {
|
198
|
+
title: title,
|
199
|
+
link: link,
|
200
|
+
# IMPORTANT NOTE: All of the other class variables are populated
|
201
|
+
# as *side effects* of the `raw` method! They must all appear
|
202
|
+
# AFTER the call to `raw`! Don't get bitten by this like I did!
|
203
|
+
content: raw,
|
204
|
+
lang: "lang-#{@lang}",
|
205
|
+
lines: @selected_lines_array ,
|
206
|
+
has_lines: !@selected_lines_array.nil?,
|
207
|
+
selected_one_liner: @selected_one_liner,
|
208
|
+
cr_results: @cr_results,
|
209
|
+
truncated: @truncated
|
210
|
+
}
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
data/lib/onebox/version.rb
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
<h4><a href="{{link}}" target="_blank">{{title}}</a></h4>
|
2
|
+
{{^has_lines}}
|
3
|
+
<pre><code class='{{lang}}'>{{content}}</code></pre>
|
4
|
+
{{/has_lines}}
|
5
|
+
{{#has_lines}}
|
6
|
+
{{! This is a template comment | Sample rules for this box
|
7
|
+
<style>
|
8
|
+
pre.onebox code ol{
|
9
|
+
margin-left:0px;
|
10
|
+
}
|
11
|
+
pre.onebox code ol .lines{
|
12
|
+
margin-left:30px;
|
13
|
+
}
|
14
|
+
pre.onebox code ol.lines li {
|
15
|
+
list-style-type: decimal;
|
16
|
+
margin-left:45px;
|
17
|
+
}
|
18
|
+
pre.onebox code li{
|
19
|
+
list-style-type: none;
|
20
|
+
background-color:#fff;
|
21
|
+
border-bottom:1px solid #F0F0F0;
|
22
|
+
padding-left:5px;
|
23
|
+
|
24
|
+
}
|
25
|
+
pre.onebox code li.selected{
|
26
|
+
background-color:#cfc
|
27
|
+
}
|
28
|
+
</style>
|
29
|
+
}}
|
30
|
+
<pre class='onebox' ><code class='{{lang}}'><ol class='start lines' start="{{cr_results.from}}" style='counter-reset: li-counter {{cr_results.from_minus_one}} ;'>
|
31
|
+
{{#lines}}<li{{#selected}} class="selected"{{/selected}}>{{data}}</li>{{/lines}}
|
32
|
+
</ol></code></pre>
|
33
|
+
{{/has_lines}}
|
34
|
+
|
35
|
+
{{#truncated}}
|
36
|
+
This file has been truncated. <a href="{{link}}" target="_blank">show original</a>
|
37
|
+
{{/truncated}}
|
@@ -0,0 +1 @@
|
|
1
|
+
templates/git_blob.mustache
|
@@ -0,0 +1 @@
|
|
1
|
+
templates/git_blob.mustache
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: onebox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.49
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joanna Zeta
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2018-
|
13
|
+
date: 2018-06-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: multi_json
|
@@ -318,6 +318,7 @@ files:
|
|
318
318
|
- lib/onebox/engine/github_gist_onebox.rb
|
319
319
|
- lib/onebox/engine/github_issue_onebox.rb
|
320
320
|
- lib/onebox/engine/github_pullrequest_onebox.rb
|
321
|
+
- lib/onebox/engine/gitlab_blob_onebox.rb
|
321
322
|
- lib/onebox/engine/google_calendar_onebox.rb
|
322
323
|
- lib/onebox/engine/google_docs_onebox.rb
|
323
324
|
- lib/onebox/engine/google_maps_onebox.rb
|
@@ -360,6 +361,7 @@ files:
|
|
360
361
|
- lib/onebox/layout.rb
|
361
362
|
- lib/onebox/layout_support.rb
|
362
363
|
- lib/onebox/matcher.rb
|
364
|
+
- lib/onebox/mixins/git_blob_onebox.rb
|
363
365
|
- lib/onebox/mixins/twitch_onebox.rb
|
364
366
|
- lib/onebox/preview.rb
|
365
367
|
- lib/onebox/sanitize_config.rb
|
@@ -373,11 +375,13 @@ files:
|
|
373
375
|
- templates/_layout.mustache
|
374
376
|
- templates/amazon.mustache
|
375
377
|
- templates/douban.mustache
|
378
|
+
- templates/git_blob.mustache
|
376
379
|
- templates/githubblob.mustache
|
377
380
|
- templates/githubcommit.mustache
|
378
381
|
- templates/githubgist.mustache
|
379
382
|
- templates/githubissue.mustache
|
380
383
|
- templates/githubpullrequest.mustache
|
384
|
+
- templates/gitlabblob.mustache
|
381
385
|
- templates/googledocs.mustache
|
382
386
|
- templates/googleplayapp.mustache
|
383
387
|
- templates/instagram.mustache
|
@@ -411,7 +415,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
411
415
|
version: '0'
|
412
416
|
requirements: []
|
413
417
|
rubyforge_project:
|
414
|
-
rubygems_version: 2.6
|
418
|
+
rubygems_version: 2.7.6
|
415
419
|
signing_key:
|
416
420
|
specification_version: 4
|
417
421
|
summary: A gem for generating embeddable HTML previews from URLs.
|
@@ -1,37 +0,0 @@
|
|
1
|
-
<h4><a href="{{link}}" target="_blank">{{title}}</a></h4>
|
2
|
-
{{^has_lines}}
|
3
|
-
<pre><code class='{{lang}}'>{{content}}</code></pre>
|
4
|
-
{{/has_lines}}
|
5
|
-
{{#has_lines}}
|
6
|
-
{{! This is a template comment | Sample rules for this box
|
7
|
-
<style>
|
8
|
-
pre.onebox code ol{
|
9
|
-
margin-left:0px;
|
10
|
-
}
|
11
|
-
pre.onebox code ol .lines{
|
12
|
-
margin-left:30px;
|
13
|
-
}
|
14
|
-
pre.onebox code ol.lines li {
|
15
|
-
list-style-type: decimal;
|
16
|
-
margin-left:45px;
|
17
|
-
}
|
18
|
-
pre.onebox code li{
|
19
|
-
list-style-type: none;
|
20
|
-
background-color:#fff;
|
21
|
-
border-bottom:1px solid #F0F0F0;
|
22
|
-
padding-left:5px;
|
23
|
-
|
24
|
-
}
|
25
|
-
pre.onebox code li.selected{
|
26
|
-
background-color:#cfc
|
27
|
-
}
|
28
|
-
</style>
|
29
|
-
}}
|
30
|
-
<pre class='onebox' ><code class='{{lang}}'><ol class='start lines' start="{{cr_results.from}}" style='counter-reset: li-counter {{cr_results.from_minus_one}} ;'>
|
31
|
-
{{#lines}}<li{{#selected}} class="selected"{{/selected}}>{{data}}</li>{{/lines}}
|
32
|
-
</ol></code></pre>
|
33
|
-
{{/has_lines}}
|
34
|
-
|
35
|
-
{{#truncated}}
|
36
|
-
This file has been truncated. <a href="{{link}}" target="_blank">show original</a>
|
37
|
-
{{/truncated}}
|