Module: Prawn::Accessibility::DocumentExtensions
- Defined in:
- lib/prawn/accessibility/document.rb
Overview
Instance methods included into Document to provide the high-level tagged-PDF (accessibility) API.
Tagging is opt-in: create the document with tagged: true (see the
initializer shim at the bottom of this file). Everything here is built on
Prawn/pdf-core's public API (renderer, state, min_version,
before_render); no core class is patched to support it.
Instance Method Summary collapse
-
#artifact(type: nil) { ... } ⇒ void
Mark content as an artifact (decorative, not read by screen readers).
-
#figure(alt_text:) { ... } ⇒ void
Render an image wrapped in a Figure structure element with alt text.
-
#heading(level, content, options = {}) ⇒ void
Render a heading at the specified level.
-
#paragraph(content = nil, options = {}) { ... } ⇒ void
Render text wrapped in a paragraph structure element.
-
#structure(tag, attributes = {}) { ... } ⇒ void
Wrap content in a structure element.
-
#structure_container(tag, attributes = {}) { ... } ⇒ void
Wrap content in a structure element without marking the content directly.
-
#structure_tree ⇒ Prawn::Accessibility::StructureTree?
The structure tree for this document, or nil if not tagged.
-
#tagged? ⇒ Boolean
Whether this document is tagged for accessibility.
Instance Method Details
#artifact(type: nil) { ... } ⇒ void
This method returns an undefined value.
Mark content as an artifact (decorative, not read by screen readers). Use for page numbers, decorative borders, backgrounds, watermarks.
86 87 88 89 90 |
# File 'lib/prawn/accessibility/document.rb', line 86 def artifact(type: nil, &block) return yield if !tagged? || !block structure_tree.mark_artifact(artifact_type: type, &block) end |
#figure(alt_text:) { ... } ⇒ void
This method returns an undefined value.
Render an image wrapped in a Figure structure element with alt text.
132 133 134 135 136 137 138 |
# File 'lib/prawn/accessibility/document.rb', line 132 def figure(alt_text:, &block) if tagged? structure(:Figure, Alt: alt_text, &block) else yield end end |
#heading(level, content, options = {}) ⇒ void
This method returns an undefined value.
Render a heading at the specified level.
98 99 100 101 102 103 104 105 |
# File 'lib/prawn/accessibility/document.rb', line 98 def heading(level, content, = {}) tag = :"H#{level}" if tagged? structure(tag) { text(content, ) } else text(content, ) end end |
#paragraph(content = nil, options = {}) { ... } ⇒ void
This method returns an undefined value.
Render text wrapped in a paragraph structure element.
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/prawn/accessibility/document.rb', line 113 def paragraph(content = nil, = {}, &block) if tagged? if block structure(:P, &block) else structure(:P) { text(content, ) } end elsif block yield else text(content, ) end end |
#structure(tag, attributes = {}) { ... } ⇒ void
This method returns an undefined value.
Wrap content in a structure element. The block's content will be associated with the given tag in the document's structure tree.
Can be nested — inner structure calls become children of the outer.
53 54 55 56 57 58 59 60 |
# File 'lib/prawn/accessibility/document.rb', line 53 def structure(tag, attributes = {}, &block) return yield if !tagged? || !block tree = structure_tree tree.begin_element(tag, attributes) tree.mark_content(tag, &block) tree.end_element end |
#structure_container(tag, attributes = {}) { ... } ⇒ void
This method returns an undefined value.
Wrap content in a structure element without marking the content directly. Use this for container elements (Table, TR, L, LI) where the children will each have their own marked content.
70 71 72 73 74 75 76 77 |
# File 'lib/prawn/accessibility/document.rb', line 70 def structure_container(tag, attributes = {}, &block) return yield if !tagged? || !block tree = structure_tree tree.begin_element(tag, attributes) yield tree.end_element end |
#structure_tree ⇒ Prawn::Accessibility::StructureTree?
The structure tree for this document, or nil if not tagged.
25 26 27 |
# File 'lib/prawn/accessibility/document.rb', line 25 def structure_tree @accessibility_structure_tree end |
#tagged? ⇒ Boolean
Whether this document is tagged for accessibility.
32 33 34 |
# File 'lib/prawn/accessibility/document.rb', line 32 def tagged? !@accessibility_structure_tree.nil? end |