Module: Asciidoctor::Rhrev::CellTextMarks
- Defined in:
- lib/asciidoctor/rhrev/cell_text_marks.rb
Overview
Lifts an rhrev attribute list written as the first line of a table cell's content onto the cell itself, then removes that line from the cell's raw text, so the mark reaches the cell for every cell style, including cells in a header row.
Asciidoctor parses a header-row cell without any style at all: Table::Cell#initialize forces the style to nil whenever the table's header option is in effect, so the a| prefix is consumed and ignored there, and the cell's content is never parsed as blocks. Rows promoted into the header after parsing (the hrows tree processor from asciidoctor-extensions-lab) keep their parsed style, but asciidoctor-pdf and Asciidoctor's own HTML5 converter render every head cell through the plain-text path, which returns an AsciiDoc-style cell's raw source, attribute line included. Both cases leave the attribute list in the cell's raw text. Reading that raw text directly, before any converter touches it, covers every style, every row kind, and every promotion mechanism with one rule: an rhrev attribute list alone on the first line of the cell's content marks the cell.
Reads and writes only the cell's raw @text. Cell#text applies substitutions, and the PDF converter's prescan runs with attribute substitution deliberately suppressed because counter attributes increment on every evaluation; table_contains_pagerhref? already takes the same care. Idempotent: once the line is stripped a second pass finds nothing to lift, which matters for asciidoctor-pdf's scratch passes and rhrev's own deferred pagerhref re-render, both of which reuse the same node objects.
For an AsciiDoc-style cell the inner document was already parsed from the same raw text, with the attribute list attached to its first nested block; propagate_cell_content_marks still moves that mark onto the cell and strips it from the nested block, so the two mechanisms agree on the cell's attributes and the strip here only affects the raw text a head cell renders from.
An attribute list without any rhrev-prefixed key is left untouched: it belongs to the writer's own content.
Constant Summary collapse
- LEADING_ATTRIBUTE_LIST_RX =
/\A\[([^\[\]\n]+)\]\n/
Class Method Summary collapse
-
.lift(doc, prefix) ⇒ Object
Lifts marks from every cell of every table in doc.
-
.lift_cell(cell, prefix) ⇒ Object
Lifts a mark from one cell.
Class Method Details
.lift(doc, prefix) ⇒ Object
Lifts marks from every cell of every table in doc. Returns the number of cells changed.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/asciidoctor/rhrev/cell_text_marks.rb', line 49 def lift doc, prefix lifted = 0 doc.find_by(context: :table).each do |table| rows = table.rows (rows.head + rows.body + rows.foot).flatten.each do |cell| lifted += 1 if lift_cell cell, prefix end end lifted end |
.lift_cell(cell, prefix) ⇒ Object
Lifts a mark from one cell. Returns true when the cell changed.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/asciidoctor/rhrev/cell_text_marks.rb', line 61 def lift_cell cell, prefix raw = cell.instance_variable_get :@text return false unless raw.is_a?(::String) && (m = LEADING_ATTRIBUTE_LIST_RX.match raw) attrs = (::Asciidoctor::AttributeList.new m[1]).parse marks = attrs.select { |key, _| key.to_s.start_with? prefix } return false if marks.empty? marks.each { |key, value| cell.set_attr key.to_s, value } cell.instance_variable_set :@text, (raw.slice m[0].length, raw.length) true end |