Class: Jekyll::Spaceship::TableProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/jekyll-spaceship/processors/table-processor.rb

Constant Summary collapse

ATTR_LIST_PATTERN =
/((?<!\\)\{:(?:([A-Za-z]\S*):)?(.*?)(?<!\\)\})/
ATTR_LIST_REFS =
{}

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, fetch_img_data, #filename, #initialize, #initialize_exclusions, #initialize_priority, #initialize_register, make_img_tag, #name, #on_handle_html_block, #on_handled, #output_ext, #pre_exclude, priority, #process?, register

Constructor Details

This class inherits a constructor from Jekyll::Spaceship::Processor

Instance Method Details

#handle_attr_list(data) ⇒ Object

Examples: {:ref-name: .cls1 title=“hello” } {: #id ref-name data=“world” } {: #id title=“hello” } {: .cls style=“color: #333” }



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 262

def handle_attr_list(data)
  cell = data.cell
  content = cell.inner_html
  # inline attribute list(IAL) handler
  ial_handler = ->(list) do
    list.scan(/(\S+)=("|')(.*?)\2|(\S+)/) do |attr|
      key = attr[0]
      val = attr[2]
      single = attr[3]
      if !key.nil?
        val = (cell.get_attribute(key) || '') + val
        cell.set_attribute(key, val)
      elsif !single.nil?
        if single.start_with? '#'
          key = 'id'
          val = single[1..-1]
        elsif single.start_with? '.'
          key = 'class'
          val = cell.get_attribute(key) || ''
          val += (val.size.zero? ? '' : ' ') + single[1..-1]
        elsif ATTR_LIST_REFS.has_key? single
          ial_handler.call ATTR_LIST_REFS[single]
        end
        unless key.nil?
          cell.set_attribute(key, val)
        end
      end
    end
  end
  # handle attribute list
  content.scan(ATTR_LIST_PATTERN) do |result|
    ref = result[1]
    list = result[2]
    # handle inline attribute list
    ial_handler.call list if ref.nil?
    # remove attr_list
    content = content.sub(result[0], '')
  end
  cell.inner_html = content
end

#handle_colspan(data) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 127

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

  # handle colspan
  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

#handle_format(data) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 303

def handle_format(data)
  cell = data.cell
  cvter = self.converter('markdown')
  return if cvter.nil?
  content = cell.inner_html
    .gsub(/(?<!\\)\|/, '\\|')
    .gsub(/^\s+|\s+$/, '')
    .gsub(/&lt;/, '<')
    .gsub(/&gt;/, '>')
  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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 156

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

  # handle multi-rows
  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 += "\n<br>\n#{cells[i].inner_html}"
    end
    row.remove
  end
  scope.table.multi_row_start = false if not match
end

#handle_rowspan(data) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 190

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

  # handle rowspan
  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 += "\n<br>\n#{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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 219

def handle_text_align(data)
  cell = data.cell

  # pre-handle text align
  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

  # handle text align
  return if align == 0

  # handle escape colon
  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

  # handle existed inline-style
  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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 66

def on_handle_html(content)
  # use nokogiri to parse html content
  doc = Nokogiri::HTML(content)

  data = self.table_scope_data

  # handle each table
  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)
        handle_attr_list(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



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 11

def on_handle_markdown(content)
  # pre-handle reference-style links
  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

  # pre-handle row-span
  content = content.gsub(/(?<!\\)(\|[^\n]*\\\s*)\|\s*\n/, "\\1\n")

  # escape | and :
  content = content.gsub(/\|(?=\|)/, '\\|')
    .gsub(/\\:(?=[^\n]*?(?<!\\)\|)/, '\\\\\\\\:')
    .gsub(/((?<!\\)\|[^\n]*?)(\\:)/, '\1\\\\\\\\:')

  # escape * and _ and $ etc.
  content.scan(/[^\n]*(?<!\\)\|[^\n]*/) do |result|
    # skip for math expression within pipeline
    next unless result
      .gsub(/((?<!\\)\${1,2})[^\n]*?\1/, '')
      .match(/(?<!\\)\|/)
    replace = result.gsub(
      /(?<!(?<!\\)\\)(\*|\$|\[|\(|\"|_)/, '\\\\\\\\\1')
    next if result == replace
    content = content.gsub(result, replace)
  end

  # pre-handle attribute list (AL)
  ATTR_LIST_REFS.clear()
  content.scan(ATTR_LIST_PATTERN) do |result|
    ref = result[1]
    list = result[2]
    next if ref.nil?
    if ATTR_LIST_REFS.has_key? ref
      ATTR_LIST_REFS[ref] += list
    else
      ATTR_LIST_REFS[ref] = list
    end
  end
  content
end

#table_scope_dataObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jekyll-spaceship/processors/table-processor.rb', line 106

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