Class: Ntxt::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/ntxt/block.rb

Overview

Block constructor ntxtObj the root Ntxt object. parentBlock the parent Block object. startTxt the starting offset in ntxtObj.text where this starts stopTxt the offset in ntxtObje.text after the startTxt position. The block of text submitted must be a valid block, meaning, it may ONLY contain subblocks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ntxtObj, parentBlock = nil, startTxt = 0, stopTxt = 0) ⇒ Block

Create a new Block. Typically you will never need to do this. Blocks are created by Parser.

ntxtObj

The Ntxt object that this block belongs to. The Ntxt object holds the text this block will reference.

parentBlock

The parent block. Nil by default.

startTxt

The staring character in Ntxt.text.

stopTxt

The initial offset. If nil this is set to ntxtObj.text.length.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ntxt/block.rb', line 45

def initialize(ntxtObj, parentBlock=nil, startTxt=0, stopTxt=0)
  @children = []
  @tags = Hash.new(0)
  @start = startTxt
  @offset = stopTxt || ntxtObj.text.length
  @ntxt = ntxtObj
  @parent = parentBlock
  @indent = 0
  @header = 0
  
  if @parent
    re = /\s*\S/m
    #m = re.match( ntxtObj.text, @start )
    m = Block::blockReMatch(re, ntxtObj.text, @start)
    if m
      @indent = m[0].length
    else
      @indent = 0
    end
    @parent.children.push(self)
  else
    @indent = 0
  end
end

Instance Attribute Details

#childrenObject

A list of child Blcoks.



14
15
16
# File 'lib/ntxt/block.rb', line 14

def children
  @children
end

#headerObject

The current header level. 0 for no header. 1-6 otherwise.



33
34
35
# File 'lib/ntxt/block.rb', line 33

def header
  @header
end

#indentObject

The current indent level. If this is a header block, ident=0.



36
37
38
# File 'lib/ntxt/block.rb', line 36

def indent
  @indent
end

#ntxtObject

The Ntxt object.



27
28
29
# File 'lib/ntxt/block.rb', line 27

def ntxt
  @ntxt
end

#offsetObject

The offset from the start field.



24
25
26
# File 'lib/ntxt/block.rb', line 24

def offset
  @offset
end

#parentObject

The parent Block or nil if this is a root Block.



30
31
32
# File 'lib/ntxt/block.rb', line 30

def parent
  @parent
end

#startObject

The start index in the text string held in the Ntxt parent object. See the ntxt field.



21
22
23
# File 'lib/ntxt/block.rb', line 21

def start
  @start
end

#tagsObject

A hash of all tags of this block and its children.



17
18
19
# File 'lib/ntxt/block.rb', line 17

def tags
  @tags
end

Instance Method Details

#addTag(tag, inc = 0) ⇒ Object

Add a tag to this block and all ancestor blocks.

A tag added to a block does not increment that block’s tag count for the added tag. A tag added to the parent of a tagged block has it’s tag count incremented by 1.

Thus, if a tag count = 0, then this block owns the tag.

If a tag count = 1. there is 1 child block with the given tag.

tag

The tag to add.

inc

The increment value. This should always be 0 for client code.



83
84
85
86
# File 'lib/ntxt/block.rb', line 83

def addTag(tag, inc=0)
  @tags[tag] += inc
  @parent.addTag(tag, 1) if @parent
end

#root?Boolean

Return true if the parent object is nil.

Returns:

  • (Boolean)


95
96
97
# File 'lib/ntxt/block.rb', line 95

def root?
  @parent.nil?
end

#textObject

Return the text slice that this block refers to. Note that parent blocks include their child blocks’ text.



90
91
92
# File 'lib/ntxt/block.rb', line 90

def text
  @ntxt.text[@start, @offset]
end

#walk {|_self| ... } ⇒ Object

Given a block this will first call that block with this Block as the only argument. Then walk is recusively called on all child Blocks.

Yields:

  • (_self)

Yield Parameters:

  • _self (Ntxt::Block)

    the object that the method was called on



101
102
103
104
# File 'lib/ntxt/block.rb', line 101

def walk(&y)
  yield self
  @children.each { |c| c.walk(&y) }
end

#walkText(printFunc, enterChild, exitChild) ⇒ Object

This method handles the complexity of handing the user the text immediately handled by each block.

If you call Block.text you will get a contiguous block of text that covers this Block and all its children. Essentially the start to the offset substring of Ntxt.text.

What this method does is pass each text that belongs only to the particular Block in question and the children. The text is passed to the user in order, so concatinating it would result in equivalent output to Block.text.

This method is useful for visualizing the text structure or filtering out blocks that aren’t interesting to the user.

printFunc

A lambda that takes the text, depth, and the Block.

enterChild

A lambda that takes depth and the Block.

exitChild

A lambda that takes depth and the Block.

For example:

printBlock = lambda { |text, depth, block| ... }
enterBlock = lambda { |depth, block| ... }
exitBlock  = lambda { |depth, block| ... }

block.walkText( printBlock, enterBlock, exitBlock )


132
133
134
# File 'lib/ntxt/block.rb', line 132

def walkText(printFunc, enterChild, exitChild)
  walkTextHelper(printFunc, enterChild, exitChild, 0)
end