Class: Asciidoctor::Table::Cell

Inherits:
AbstractBlock show all
Defined in:
lib/asciidoctor/table.rb

Overview

Methods for managing the a cell in an AsciiDoc table.

Constant Summary collapse

DOUBLE_LF =
LF * 2

Constants included from Substitutors

Substitutors::CAN, Substitutors::CGI, Substitutors::DEL, Substitutors::ESC_R_SB, Substitutors::HighlightedPassSlotRx, Substitutors::PASS_END, Substitutors::PASS_START, Substitutors::PLUS, Substitutors::PassSlotRx, Substitutors::QuotedTextSniffRx, Substitutors::RS, Substitutors::R_SB, Substitutors::SUB_GROUPS, Substitutors::SUB_HINTS, Substitutors::SUB_OPTIONS, Substitutors::SpecialCharsRx, Substitutors::SpecialCharsTr

Instance Attribute Summary collapse

Attributes inherited from AbstractBlock

#blocks, #caption, #content_model, #level, #numeral, #source_location, #style, #subs

Attributes inherited from AbstractNode

#attributes, #context, #document, #id, #node_name, #parent

Instance Method Summary collapse

Methods inherited from AbstractBlock

#<<, #alt, #assign_caption, #block?, #blocks?, #captioned_title, #context=, #convert, #find_by, #inline?, #list_marker_keyword, #next_adjacent_block, #number, #number=, #remove_sub, #sections, #sections?, #sub?, #title, #title=, #title?, #xreftext

Methods inherited from AbstractNode

#add_role, #attr, #attr?, #block?, #converter, #enabled_options, #generate_data_uri, #generate_data_uri_from_uri, #has_role?, #icon_uri, #image_uri, #inline?, #is_uri?, #media_uri, #normalize_asset_path, #normalize_system_path, #normalize_web_path, #option?, #read_asset, #read_contents, #reftext, #reftext?, #remove_attr, #remove_role, #role, #role=, #role?, #roles, #set_attr, #set_option, #update_attributes

Methods included from Substitutors

#apply_header_subs, #apply_normal_subs, #apply_reftext_subs, #apply_subs, #expand_subs, #extract_passthroughs, #highlight_source, #resolve_block_subs, #resolve_lines_to_highlight, #resolve_pass_subs, #resolve_subs, #restore_passthroughs, #sub_attributes, #sub_callouts, #sub_macros, #sub_post_replacements, #sub_quotes, #sub_replacements, #sub_source, #sub_specialchars

Methods included from Logging

#logger, #message_with_context

Constructor Details

#initialize(column, cell_text, attributes = {}, opts = {}) ⇒ Cell

Returns a new instance of Cell.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/asciidoctor/table.rb', line 235

def initialize column, cell_text, attributes = {}, opts = {}
  super column, :table_cell
  @cursor = @reinitialize_args = nil
  @source_location = opts[:cursor].dup if @document.sourcemap
  # NOTE: column is always set when parsing; may not be set when building table from the API
  if column
    if (in_header_row = column.table.header_row?)
      if in_header_row == :implicit && (cell_style = column.style || (attributes && attributes['style']))
        @reinitialize_args = [column, cell_text, attributes && attributes.merge, opts] if cell_style == :asciidoc || cell_style == :literal
        cell_style = nil
      end
    else
      cell_style = column.style
    end
    # REVIEW feels hacky to inherit all attributes from column
    update_attributes column.attributes
  end
  # NOTE if attributes is defined, we know this is a psv cell; implies text needs to be stripped
  if attributes
    if attributes.empty?
      @colspan = @rowspan = nil
    else
      @colspan, @rowspan = (attributes.delete 'colspan'), (attributes.delete 'rowspan')
      # TODO delete style attribute from @attributes if set
      cell_style = attributes['style'] || cell_style unless in_header_row
      update_attributes attributes
    end
    case cell_style
    when :asciidoc
      asciidoc = true
      inner_document_cursor = opts[:cursor]
      if (cell_text = cell_text.rstrip).start_with? LF
        lines_advanced = 1
        lines_advanced += 1 while (cell_text = cell_text.slice 1, cell_text.length).start_with? LF
        # NOTE this only works if we remain in the same file
        inner_document_cursor.advance lines_advanced
      else
        cell_text = cell_text.lstrip
      end
    when :literal
      literal = true
      cell_text = cell_text.rstrip
      # QUESTION should we use same logic as :asciidoc cell? strip leading space if text doesn't start with newline?
      cell_text = cell_text.slice 1, cell_text.length while cell_text.start_with? LF
    else
      normal_psv = true
      # NOTE AsciidoctorJ uses nil cell_text to create an empty cell
      cell_text = cell_text ? cell_text.strip : ''
    end
  else
    @colspan = @rowspan = nil
    if cell_style == :asciidoc
      asciidoc = true
      inner_document_cursor = opts[:cursor]
    end
  end
  # NOTE only true for non-header rows
  if asciidoc
    # FIXME hide doctitle from nested document; temporary workaround to fix
    # nested document seeing doctitle and assuming it has its own document title
    parent_doctitle = @document.attributes.delete('doctitle')
    # NOTE we need to process the first line of content as it may not have been processed
    # the included content cannot expect to match conditional terminators in the remaining
    # lines of table cell content, it must be self-contained logic
    # QUESTION should we reset cell_text to nil?
    # QUESTION is is faster to check for :: before splitting?
    inner_document_lines = cell_text.split LF, -1
    if (unprocessed_line1 = inner_document_lines[0]).include? '::'
      preprocessed_lines = (PreprocessorReader.new @document, [unprocessed_line1], inner_document_cursor).readlines
      unless unprocessed_line1 == preprocessed_lines[0] && preprocessed_lines.size < 2
        inner_document_lines.shift
        inner_document_lines.unshift(*preprocessed_lines) unless preprocessed_lines.empty?
      end
    end unless inner_document_lines.empty?
    @inner_document = Document.new inner_document_lines, standalone: false, parent: @document, cursor: inner_document_cursor
    @document.attributes['doctitle'] = parent_doctitle unless parent_doctitle.nil?
    @subs = nil
  elsif literal
    @content_model = :verbatim
    @subs = BASIC_SUBS
  else
    if normal_psv
      if in_header_row
        @cursor = opts[:cursor] # used in deferred catalog_inline_anchor call
      else
        catalog_inline_anchor cell_text, opts[:cursor]
      end
    end
    @content_model = :simple
    @subs = NORMAL_SUBS
  end
  @text = cell_text
  @style = cell_style
end

Instance Attribute Details

#colspanObject

An Integer of the number of columns this cell will span (default: nil)



224
225
226
# File 'lib/asciidoctor/table.rb', line 224

def colspan
  @colspan
end

#inner_documentObject (readonly)

Returns the nested Document in an AsciiDoc table cell (only set when style is :asciidoc)



233
234
235
# File 'lib/asciidoctor/table.rb', line 233

def inner_document
  @inner_document
end

#rowspanObject

An Integer of the number of rows this cell will span (default: nil)



227
228
229
# File 'lib/asciidoctor/table.rb', line 227

def rowspan
  @rowspan
end

#textObject

Get the String text of this cell with substitutions applied.

Used for cells in the head row as well as text-only (non-AsciiDoc) cells in the foot row and body.

This method shouldn’t be used for cells that have the AsciiDoc style.

Returns:

  • the converted String text for this Cell



357
358
359
# File 'lib/asciidoctor/table.rb', line 357

def text
  apply_subs @text, @subs
end

Instance Method Details

#catalog_inline_anchor(cell_text = @text, cursor = nil) ⇒ Object



342
343
344
345
346
347
# File 'lib/asciidoctor/table.rb', line 342

def catalog_inline_anchor cell_text = @text, cursor = nil
  cursor, @cursor = @cursor, nil unless cursor
  if (cell_text.start_with? '[[') && LeadingInlineAnchorRx =~ cell_text
    Parser.catalog_inline_anchor $1, $2, self, cursor, @document
  end
end

#contentObject

Handles the body data (tbody, tfoot), applying styles and partitioning into paragraphs

This method should not be used for cells in the head row or that have the literal style.

Returns:

  • the converted String for this Cell



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/asciidoctor/table.rb', line 371

def content
  if (cell_style = @style) == :asciidoc
    @inner_document.convert
  elsif @text.include? DOUBLE_LF
    (text.split BlankLineRx).map do |para|
      cell_style && cell_style != :header ? (Inline.new parent, :quoted, para, type: cell_style).convert : para
    end
  elsif (subbed_text = text).empty?
    []
  elsif cell_style && cell_style != :header
    [(Inline.new parent, :quoted, subbed_text, type: cell_style).convert]
  else
    [subbed_text]
  end
end

#fileObject

Get the source file where this block started



396
397
398
# File 'lib/asciidoctor/table.rb', line 396

def file
  @source_location && @source_location.file
end

#linenoObject

Get the source line number where this block started



401
402
403
# File 'lib/asciidoctor/table.rb', line 401

def lineno
  @source_location && @source_location.lineno
end

#linesObject



387
388
389
# File 'lib/asciidoctor/table.rb', line 387

def lines
  @text.split LF
end

#reinitialize(has_header) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/asciidoctor/table.rb', line 330

def reinitialize has_header
  if has_header
    @reinitialize_args = nil
  elsif @reinitialize_args
    return Table::Cell.new(*@reinitialize_args)
  else
    @style = @attributes['style']
  end
  catalog_inline_anchor if @cursor
  self
end

#sourceObject



391
392
393
# File 'lib/asciidoctor/table.rb', line 391

def source
  @text
end

#to_sObject



405
406
407
# File 'lib/asciidoctor/table.rb', line 405

def to_s
  %(#{super} - [text: #{@text}, colspan: #{@colspan || 1}, rowspan: #{@rowspan || 1}, attributes: #{@attributes}])
end