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
#exclusions, #handled, #logger, #page, #priority, #registers
Instance Method Summary
collapse
Methods inherited from Processor
#after_exclude, #converter, #dispatch, exclude, #ext, #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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 104
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
|
233
234
235
236
237
238
239
240
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 233
def handle_format(data)
cell = data.cell
cvter = self.converter('markdown')
return if cvter.nil?
content = cell.content.gsub(/(?<!\\)\|/, '\\|')
content = cvter.convert(content.strip)
cell.inner_html = Nokogiri::HTML.fragment(content)
end
|
#handle_multi_rows(data) ⇒ Object
133
134
135
136
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
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 133
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.content += " \n#{cells[i].content}"
end
row.remove
end
scope.table.multi_row_start = false if not match
end
|
#handle_rowspan(data) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 167
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.content += " \n#{cell.content}"
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
196
197
198
199
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
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 196
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
cell.content = cell.content.gsub(/\\:/, ':')
return if align == 0
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
45
46
47
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
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 45
def on_handle_html(content)
doc = Nokogiri::HTML(content)
data = self.table_scope_data
doc.css('table').each do |table|
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
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 8
def on_handle_markdown(content)
references = {}
content.scan(/(\[(.*)\]:\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(/.*(?<!\\)\|.*/) do |result|
references.each do |key, val|
replace = result.gsub(/\[(.*)\]\s*\[#{key}\]/, "[\1](#{val})")
next if result == replace
content = content.gsub(result, replace)
end
end
end
content = content.gsub(/(?<!\\)(\|.*\\\s*)\|\s*\n/, "\\1\n")
content = content.gsub(/\|(?=\|)/, '\\|')
.gsub(/\\:(?=.*?(?<!\\)\|)/, '\\\\\\\\:')
.gsub(/((?<!\\)\|.*?)(\\:)/, '\1\\\\\\\\:')
content.scan(/.*(?<!\\)\|.*/) do |result|
replace = result.gsub(
/(?<!(?<!\\)\\)(\*|\$|\[|\(|\"|_)/,
'\\\\\\\\\1')
next if result == replace
content = content.gsub(result, replace)
end
content
end
|
#table_scope_data ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 83
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
|