Class: Jekyll::Spaceship::TableProcessor
- Inherits:
-
Processor
- Object
- Processor
- Jekyll::Spaceship::TableProcessor
show all
- Defined in:
- lib/jekyll-spaceship/processors/table-processor.rb
Constant Summary
Constants inherited
from Processor
Processor::DEFAULT_PRIORITY, Processor::PRIORITY_MAP
Instance Attribute Summary
Attributes inherited from Processor
#config, #exclusions, #handled, #logger, #page, #priority, #registers
Instance Method Summary
collapse
Methods inherited from Processor
#after_exclude, config, #converter, #dispatch, escape_html, exclude, #ext, #filename, #initialize, #initialize_exclusions, #initialize_priority, #initialize_register, #name, #on_handle_html_block, #on_handled, #output_ext, #pre_exclude, priority, #process?, register
Instance Method Details
#handle_colspan(data) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 108
def handle_colspan(data)
scope = data.scope.call __method__
cells = data.cells
cell = data.cell
if scope.table.row != data.row
scope.table.row = data.row
scope.row.colspan = 0
end
if cell == cells.last and scope.row.colspan > 0
cells.count.downto(cells.count - scope.row.colspan + 1) do |i|
c = cells[i - 1]
return unless c.get_attribute('colspan').nil?
c.remove
end
end
result = cell.content.match(/(\|)+$/)
return if result.nil?
cell.content = cell.content.gsub(/(\|)+$/, '')
result = result[0]
colspan = result.scan(/\|/).count
scope.row.colspan += colspan
cell.set_attribute('colspan', colspan + 1)
end
|
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 238
def handle_format(data)
cell = data.cell
cvter = self.converter('markdown')
return if cvter.nil?
content = cell.inner_html
.gsub(/(?<!\\)\|/, '\\|')
.gsub(/^\s+|\s+$/, '')
content = cvter.convert(content)
content = Nokogiri::HTML.fragment(content)
if content.children.first&.name == 'p'
content = content.children
end
cell.inner_html = content.inner_html
end
|
#handle_multi_rows(data) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 137
def handle_multi_rows(data)
scope = data.scope.call __method__
cells = data.cells
row = data.row
cell = data.cell
if scope.table.table != data.table
scope.table.table = data.table
scope.table.multi_row_cells = nil
scope.table.multi_row_start = false
end
return if cell != cells.last
match = cell.content.match(/(?<!\\)\\\s*$/)
if match
cell.content = cell.content.gsub(/(?<!\\)\\\s*$/, '')
if not scope.table.multi_row_start
scope.table.multi_row_cells = cells
scope.table.multi_row_start = true
end
end
if scope.table.multi_row_cells != cells and scope.table.multi_row_start
for i in 0...scope.table.multi_row_cells.count do
multi_row_cell = scope.table.multi_row_cells[i]
multi_row_cell.inner_html += "<br>#{cells[i].inner_html}"
end
row.remove
end
scope.table.multi_row_start = false if not match
end
|
#handle_rowspan(data) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 171
def handle_rowspan(data)
scope = data.scope.call __method__
cell = data.cell
cells = data.cells
if scope.table.table != data.table
scope.table.table = data.table
scope.table.span_row_cells = []
end
if scope.row.row != data.row
scope.row.row = data.row
scope.row.col_index = 0
end
span_cell = scope.table.span_row_cells[scope.row.col_index]
if span_cell and cell.content.match(/^\s*\^{2}/)
cell.content = cell.content.gsub(/^\s*\^{2}/, '')
span_cell.inner_html += "<br>#{cell.inner_html}"
rowspan = span_cell.get_attribute('rowspan') || 1
rowspan = rowspan.to_i + 1
span_cell.set_attribute('rowspan', "#{rowspan}")
cell.remove
else
scope.table.span_row_cells[scope.row.col_index] = cell
end
scope.row.col_index += [cell.get_attribute('colspan').to_i, 1].max
end
|
#handle_text_align(data) ⇒ Object
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 200
def handle_text_align(data)
cell = data.cell
align = 0
if cell.content.match(/^\s*:(?!:)/)
cell.content = cell.content.gsub(/^\s*:/, '')
align += 1
end
if cell.content.match(/(?<!\\):\s*$/)
cell.content = cell.content.gsub(/:\s*$/, '')
align += 2
end
return if align == 0
cell.content = cell.content.gsub(/\\:/, ':')
style = cell.get_attribute('style')
if align == 1
align = 'text-align: left'
elsif align == 2
align = 'text-align: right'
elsif align == 3
align = 'text-align: center'
end
if style&.match(/text-align:.+/)
style = style.gsub(/text-align:.+/, align)
else
style = align
end
cell.set_attribute('style', style)
end
|
#on_handle_html(content) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 48
def on_handle_html(content)
doc = Nokogiri::HTML(content)
data = self.table_scope_data
doc.css('table').each do |table|
next if table.ancestors('code, pre').size > 0
rows = table.css('tr')
data.table = table
data.rows = rows
data.reset.call :table
rows.each do |row|
cells = row.css('th, td')
data.row = row
data.cells = cells
data.reset.call :row
cells.each do |cell|
data.cell = cell
handle_colspan(data)
handle_multi_rows(data)
handle_text_align(data)
handle_rowspan(data)
end
end
rows.each do |row|
cells = row.css('th, td')
cells.each do |cell|
data.cell = cell
handle_format(data)
end
end
self.handled = true
end
doc.to_html
end
|
#on_handle_markdown(content) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 8
def on_handle_markdown(content)
references = {}
content.scan(/\n\s*(\[(.*)\]:\s*(\S+(\s+".*?")?))/) do |match_data|
ref_name = match_data[1]
ref_value = match_data[2]
references[ref_name] = ref_value
end
if references.size > 0
content.scan(/[^\n]*(?<!\\)\|[^\n]*/) do |result|
references.each do |key, val|
replace = result.gsub(/\[([^\n]*)\]\s*\[#{key}\]/, "[\1](#{val})")
next if result == replace
content = content.gsub(result, replace)
end
end
end
content = content.gsub(/(?<!\\)(\|[^\n]*\\\s*)\|\s*\n/, "\\1\n")
content = content.gsub(/\|(?=\|)/, '\\|')
.gsub(/\\:(?=[^\n]*?(?<!\\)\|)/, '\\\\\\\\:')
.gsub(/((?<!\\)\|[^\n]*?)(\\:)/, '\1\\\\\\\\:')
content.scan(/[^\n]*(?<!\\)\|[^\n]*/) do |result|
next unless result
.gsub(/((?<!\\)\${1,2})[^\n]*?\1/, '')
.match(/(?<!\\)\|/)
replace = result.gsub(
/(?<!(?<!\\)\\)(\*|\$|\[|\(|\"|_)/, '\\\\\\\\\1')
next if result == replace
content = content.gsub(result, replace)
end
content
end
|
#table_scope_data ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 87
def table_scope_data
data = OpenStruct.new(_: OpenStruct.new)
data.reset = ->(scope, namespace = nil) {
data._.marshal_dump.each do |key, val|
if namespace == key or namespace.nil?
data._[key][scope] = OpenStruct.new
end
end
}
data.scope = ->(namespace) {
if not data._[namespace]
data._[namespace] = OpenStruct.new(
table: OpenStruct.new,
row: OpenStruct.new
)
end
data._[namespace]
}
data
end
|