Class: PointBlank::DOM::DOMObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mmmd/blankshell.rb

Overview

DOM Object

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDOMObject

Returns a new instance of DOMObject.



1553
1554
1555
1556
1557
1558
# File 'lib/mmmd/blankshell.rb', line 1553

def initialize
  @children = []
  @temp_children = []
  @properties = {}
  @content = ""
end

Class Attribute Details

.parserObject

Returns the value of attribute parser.



1546
1547
1548
# File 'lib/mmmd/blankshell.rb', line 1546

def parser
  @parser
end

.scannerObject

Returns the value of attribute scanner.



1546
1547
1548
# File 'lib/mmmd/blankshell.rb', line 1546

def scanner
  @scanner
end

.unsorted_childrenObject

Returns the value of attribute unsorted_children.



1546
1547
1548
# File 'lib/mmmd/blankshell.rb', line 1546

def unsorted_children
  @unsorted_children
end

.unsorted_overlaysObject

Returns the value of attribute unsorted_overlays.



1546
1547
1548
# File 'lib/mmmd/blankshell.rb', line 1546

def unsorted_overlays
  @unsorted_overlays
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



1617
1618
1619
# File 'lib/mmmd/blankshell.rb', line 1617

def content
  @content
end

#parentObject

Returns the value of attribute parent.



1617
1618
1619
# File 'lib/mmmd/blankshell.rb', line 1617

def parent
  @parent
end

#parserObject

Returns the value of attribute parser.



1617
1618
1619
# File 'lib/mmmd/blankshell.rb', line 1617

def parser
  @parser
end

#positionObject

Returns the value of attribute position.



1617
1618
1619
# File 'lib/mmmd/blankshell.rb', line 1617

def position
  @position
end

#propertiesObject

Returns the value of attribute properties.



1617
1618
1619
# File 'lib/mmmd/blankshell.rb', line 1617

def properties
  @properties
end

#temp_childrenObject (readonly)

Returns the value of attribute temp_children.



1618
1619
1620
# File 'lib/mmmd/blankshell.rb', line 1618

def temp_children
  @temp_children
end

Class Method Details

.define_child(child, priority = 9999) ⇒ void

This method returns an undefined value.

Define valid child for this DOMObject class

Parameters:

  • child (Class)


1475
1476
1477
1478
# File 'lib/mmmd/blankshell.rb', line 1475

def define_child(child, priority = 9999)
  @unsorted_children ||= []
  @unsorted_children.append([child, priority])
end

.define_overlay(overlay, priority = 9999) ⇒ void

This method returns an undefined value.

Define an overlay - a parsing strategy that occurs once a block is closed. May transform block if #process method of the overlay class returns a class.

Parameters:



1500
1501
1502
1503
# File 'lib/mmmd/blankshell.rb', line 1500

def define_overlay(overlay, priority = 9999)
  @unsorted_overlays ||= []
  @unsorted_overlays.append([overlay, priority])
end

.define_parser(parser) ⇒ void

This method returns an undefined value.

Define self parser for this DOMObject class

Parameters:



1490
1491
1492
1493
# File 'lib/mmmd/blankshell.rb', line 1490

def define_parser(parser)
  parser.parser_for = self
  @parser = parser
end

.define_scanner(scanner) ⇒ void

This method returns an undefined value.

Define child element scanner for this DOMObject class

Parameters:

  • child (Class)


1483
1484
1485
# File 'lib/mmmd/blankshell.rb', line 1483

def define_scanner(scanner)
  @scanner = scanner
end

.inherited(subclass) ⇒ Object

Make subclasses inherit scanner and valid children



1458
1459
1460
1461
1462
1463
# File 'lib/mmmd/blankshell.rb', line 1458

def inherited(subclass)
  subclass.parser ||= @parser
  subclass.scanner ||= @scanner
  subclass.unsorted_children ||= @unsorted_children.dup || []
  super(subclass)
end

.parse(doc) ⇒ self

Parse a document

Returns:

  • (self)


1514
1515
1516
1517
1518
1519
# File 'lib/mmmd/blankshell.rb', line 1514

def parse(doc)
  newdoc = new
  newdoc.parser = parser.new
  scan = @scanner.new(doc, newdoc)
  scan.scan
end

.sort_childrenvoid

This method returns an undefined value.

Sort children by priority



1467
1468
1469
1470
# File 'lib/mmmd/blankshell.rb', line 1467

def sort_children
  @valid_children = @unsorted_children&.sort_by(&:last)&.map(&:first) ||
                    []
end

.sort_overlaysvoid

This method returns an undefined value.

Sort overlays by priority



1507
1508
1509
1510
# File 'lib/mmmd/blankshell.rb', line 1507

def sort_overlays
  @valid_overlays = @unsorted_overlays&.sort_by(&:last)&.map(&:first) ||
                    []
end

.upsourceObject

Source parameters from parent (fixes recursive dependency)



1522
1523
1524
1525
1526
1527
1528
1529
1530
# File 'lib/mmmd/blankshell.rb', line 1522

def upsource
  superclass&.tap do |sc|
    @scanner = sc.scanner
    @parser = sc.parser
    @unsorted_children = sc.unsorted_children.dup
    @unsorted_overlays = sc.unsorted_overlays.dup
  end
  sort_children
end

.valid_childrenArray<Class>

Get array of valid children sorted by priority

Returns:

  • (Array<Class>)


1541
1542
1543
1544
# File 'lib/mmmd/blankshell.rb', line 1541

def valid_children
  sort_children unless @valid_children
  @valid_children
end

.valid_overlaysArray<::PointBlank::Parsing::NullOverlay>

Get array of valid overlays sorted by priority



1534
1535
1536
1537
# File 'lib/mmmd/blankshell.rb', line 1534

def valid_overlays
  sort_overlays unless @valid_overlays
  @valid_overlays
end

Instance Method Details

#[](index) ⇒ DOMObject

Get element at position

Parameters:

  • index (Integer)

Returns:



1575
1576
1577
# File 'lib/mmmd/blankshell.rb', line 1575

def [](index)
  @children[index]
end

#[]=(index, element) ⇒ DOMObject

Set element at position

Parameters:

Returns:



1564
1565
1566
1567
1568
1569
1570
# File 'lib/mmmd/blankshell.rb', line 1564

def []=(index, element)
  unless element.is_a? ::PointBlank::DOM::DOMObject
    raise DOMError, "invalid DOM class #{element.class}"
  end

  @children[index] = element
end

#append_child(child) ⇒ Object

Append child

Parameters:



1601
1602
1603
1604
1605
1606
1607
1608
1609
# File 'lib/mmmd/blankshell.rb', line 1601

def append_child(child)
  unless child.is_a? ::PointBlank::DOM::DOMObject
    raise DOMError, "invalid DOM class #{child.class}"
  end

  child.parent = self
  child.position = @children.length
  @children.append(child)
end

#append_temp_child(child) ⇒ Object

Append temp child

Parameters:



1613
1614
1615
# File 'lib/mmmd/blankshell.rb', line 1613

def append_temp_child(child)
  @temp_children.append(child)
end

#childrenArray<DOMObject>

Return an array duplicate of all children

Returns:



1587
1588
1589
# File 'lib/mmmd/blankshell.rb', line 1587

def children
  @children.dup
end

#each(&block) ⇒ Object

Iterate over each child of DOMObject

Parameters:

  • block (#call)


1581
1582
1583
# File 'lib/mmmd/blankshell.rb', line 1581

def each(&block)
  @children.each(&block)
end

#root::PointBlank::DOM::DOMObject

Get root element containing this child



1593
1594
1595
1596
1597
# File 'lib/mmmd/blankshell.rb', line 1593

def root
  current_root = self
  current_root = current_root.parent while current_root.parent
  current_root
end