Class: Uniword::Builder::CommentAnchorer

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/builder/comment_anchorer.rb

Overview

Anchors a comment to a body paragraph.

Word anchors a comment with three pieces of markup around the commented content:

<w:commentRangeStart w:id="0"/>
... commented runs ...
<w:commentRangeEnd w:id="0"/>
<w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr>
  <w:commentReference w:id="0"/></w:r>

Paragraph uses mixed-content serialization, so the anchor elements must also be recorded in the paragraph's element_order — fresh paragraphs get a complete order built from their current content, parsed paragraphs get the new entries inserted in place (a second anchor on the same paragraph nests inside the first, matching Word's canonical output).

Examples:

Anchor a comment on the last paragraph of a document

CommentAnchorer.anchor(document.body.paragraphs.last, "1")

Constant Summary collapse

REFERENCE_STYLE =

Character style Word applies to comment reference runs.

"CommentReference"
CONTENT_ELEMENTS =

Paragraph content element names in mapping declaration order, excluding pPr and the comment anchor elements (handled separately). Mirrors Paragraph's xml do block; used to rebuild element_order for programmatically anchored paragraphs.

%w[
  hyperlink bookmarkStart bookmarkEnd fldChar instrText
  commentReference AlternateContent sdt oMathPara oMath proofErr
  fldSimple
].freeze
COLLECTION_READERS =

Element name → collection reader, used to size element_order entries without dynamic dispatch.

{
  "hyperlink" => :hyperlinks.to_proc,
  "bookmarkStart" => :bookmark_starts.to_proc,
  "bookmarkEnd" => :bookmark_ends.to_proc,
  "fldChar" => :field_chars.to_proc,
  "instrText" => :instr_text.to_proc,
  "commentReference" => :comment_references.to_proc,
  "AlternateContent" => :alternate_content.to_proc,
  "sdt" => :sdts.to_proc,
  "oMathPara" => :o_math_paras.to_proc,
  "oMath" => :o_maths.to_proc,
  "proofErr" => :proof_errors.to_proc,
  "fldSimple" => :simple_fields.to_proc,
}.freeze

Class Method Summary collapse

Class Method Details

.anchor(paragraph, comment_id) ⇒ void

This method returns an undefined value.

Anchor a comment around the paragraph's current content. No-op when the paragraph is nil (comment stays unanchored but remains valid in word/comments.xml).

Parameters:



64
65
66
67
68
69
70
71
72
# File 'lib/uniword/builder/comment_anchorer.rb', line 64

def anchor(paragraph, comment_id)
  return unless paragraph

  id = comment_id.to_s
  paragraph.comment_range_starts << range_start(id)
  paragraph.comment_range_ends << range_end(id)
  paragraph.runs << reference_run(id)
  sync_element_order(paragraph)
end