Class: Arrolio::GenericAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/generic_adapter.rb

Overview

Generic, configuration-driven XML adapter. Reads a set of declarative rules (YAML) that describe how to parse a specific document format's presentation XML into a Content::Document.

A flavor is defined entirely by:

adapter_rules.yml  — element selectors, mappings, style resolution
layout_spec.yml    — styles, page templates, flows
flow_rules.yml     — page sequence structure, cover layout

No Ruby code is needed for a new flavor. The adapter has ZERO hard-coded element names — every XML element/attribute the adapter touches is named in the rules' selectors block.

Constant Summary collapse

DEFAULT_SELECTORS =
{
  'heading' => 'fmt-title',
  'heading_depth_attribute' => 'depth',
  'list_item' => 'li',
  'list_item_label' => 'fmt-name',
  'paragraph' => 'p',
  'term_number' => 'fmt-name',
  'term_preferred' => 'fmt-preferred',
  'term_definition' => 'fmt-definition',
  'term_source' => 'fmt-termsource',
  'note_label' => 'fmt-name',
  'figure_image' => 'image',
  'figure_caption' => 'fmt-name',
  'figure_caption_fallback' => 'name',
  'table_row' => 'tr',
  'table_cell' => ['td', 'th'].freeze,
  'table_header' => 'thead',
  'table_body' => 'tbody',
  'biblio_tag' => 'biblio-tag',
  'biblio_formattedref' => 'formattedref',
  'image_src_attribute' => 'src',
  'image_alt_attribute' => 'alt',
  'id_attribute' => 'id',
  'autonum_class' => 'fmt-autonum-delim',
  'stem' => 'stem',
  'stem_formatted' => 'fmt-stem',
  'math' => 'math',
  'preface_container' => 'preface',
  'preface_children' => ['clause', 'foreword'].freeze,
  'bibliography_container' => 'bibliography',
  'bibliography_reference' => 'references',
  'bibliography_item' => 'bibitem',
  'fallback_title' => 'title',
  'span' => 'span',
  'tab_inline' => 'tab',
  'break_inline' => 'br'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules:, layout_spec: nil) ⇒ GenericAdapter

Returns a new instance of GenericAdapter.



61
62
63
64
65
# File 'lib/arrolio/generic_adapter.rb', line 61

def initialize(rules:, layout_spec: nil)
  @rules = rules.is_a?(String) ? YAML.safe_load_file(rules) : rules
  @layout_spec = layout_spec
  @selectors = DEFAULT_SELECTORS.merge(@rules['selectors'] || {})
end

Instance Attribute Details

#layout_specObject (readonly)

Returns the value of attribute layout_spec.



59
60
61
# File 'lib/arrolio/generic_adapter.rb', line 59

def layout_spec
  @layout_spec
end

#rulesObject (readonly)

Returns the value of attribute rules.



59
60
61
# File 'lib/arrolio/generic_adapter.rb', line 59

def rules
  @rules
end

#selectorsObject (readonly)

Returns the value of attribute selectors.



59
60
61
# File 'lib/arrolio/generic_adapter.rb', line 59

def selectors
  @selectors
end

Instance Method Details

#convert(xml) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/arrolio/generic_adapter.rb', line 67

def convert(xml)
  xml = xml.read if xml.is_a?(IO) || xml.is_a?(StringIO)
  xml = xml.to_s.dup.force_encoding('UTF-8').scrub
  rexml = xml.is_a?(REXML::Document) ? xml : REXML::Document.new(xml)
  root = rexml.root

  Content::Document.new(
    metadata: (root),
    sections: extract_sections(root),
    preface: extract_preface(root),
    bibliography: extract_bibliography(root),
    cover: extract_cover(root),
    footnotes: extract_footnotes(root)
  )
end