Class: Coradoc::Docx::Transform::Rules::SimpleFieldRule

Inherits:
Coradoc::Docx::Transform::Rule show all
Defined in:
lib/coradoc/docx/transform/rules/simple_field_rule.rb

Overview

Transforms w:fldSimple (simple field) elements.

Simple fields include page numbers, dates, document properties, and other computed content. This rule extracts the field’s text content when available, otherwise produces the instruction text.

Common field types:

  • PAGE → current page number

  • NUMPAGES → total page count

  • DATE → current date

  • TIME → current time

  • DOCPROPERTY → document property value

  • TITLE → document title

  • AUTHOR → document author

Instance Method Summary collapse

Methods inherited from Coradoc::Docx::Transform::Rule

#priority

Instance Method Details

#apply(field, _context) ⇒ Object



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
# File 'lib/coradoc/docx/transform/rules/simple_field_rule.rb', line 27

def apply(field, _context)
  # Try to get the resolved text content first
  text = field_text(field)
  return nil if text.nil? || text.empty?

  # Check if this is a semantic field we should preserve
  instr = field_instruction(field)
  case instr
  when /\A(TITLE|AUTHOR|SUBJECT|KEYWORDS|DOCPROPERTY)\b/i
    # Document metadata — embed as plain text (already resolved)
    text
  when /\A(PAGE|NUMPAGES)\b/i
    # Page layout fields — skip (not semantic)
    nil
  when /\A(HYPERLINK)\b/i
    # Hyperlink field — extract URL and text
    url = extract_hyperlink_url(instr)
    if url
      CoreModel::InlineElement.new(
        format_type: 'link',
        content: text,
        target: url
      )
    else
      text
    end
  when /\A(TOC|PAGEREF|REF|NOTEREF)\b/i
    # TOC / cross-reference fields — skip (print layout)
    nil
  else
    # Generic field — pass through as text
    text
  end
end

#matches?(element) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/coradoc/docx/transform/rules/simple_field_rule.rb', line 22

def matches?(element)
  defined?(Uniword::Wordprocessingml::SimpleField) &&
    element.is_a?(Uniword::Wordprocessingml::SimpleField)
end