Class: Ntxt::Block
- Inherits:
-
Object
- Object
- Ntxt::Block
- 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
-
#children ⇒ Object
A list of child Blcoks.
-
#header ⇒ Object
The current header level.
-
#indent ⇒ Object
The current indent level.
-
#ntxt ⇒ Object
The Ntxt object.
-
#offset ⇒ Object
The offset from the
start
field. -
#parent ⇒ Object
The parent Block or nil if this is a root Block.
-
#start ⇒ Object
The
start
index in the text string held in the Ntxt parent object. -
#tags ⇒ Object
A hash of all tags of this block and its children.
Instance Method Summary collapse
-
#addTag(tag, inc = 0) ⇒ Object
Add a tag to this block and all ancestor blocks.
-
#initialize(ntxtObj, parentBlock = nil, startTxt = 0, stopTxt = 0) ⇒ Block
constructor
Create a new Block.
-
#root? ⇒ Boolean
Return true if the parent object is nil.
-
#text ⇒ Object
Return the text slice that this block refers to.
-
#walk {|_self| ... } ⇒ Object
Given a block this will first call that block with this Block as the only argument.
-
#walkText(printFunc, enterChild, exitChild) ⇒ Object
This method handles the complexity of handing the user the text immediately handled by each block.
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
#children ⇒ Object
A list of child Blcoks.
14 15 16 |
# File 'lib/ntxt/block.rb', line 14 def children @children end |
#header ⇒ Object
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 |
#indent ⇒ Object
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 |
#ntxt ⇒ Object
The Ntxt object.
27 28 29 |
# File 'lib/ntxt/block.rb', line 27 def ntxt @ntxt end |
#offset ⇒ Object
The offset from the start
field.
24 25 26 |
# File 'lib/ntxt/block.rb', line 24 def offset @offset end |
#parent ⇒ Object
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 |
#start ⇒ Object
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 |
#tags ⇒ Object
A hash of all tags of this block and its children.
17 18 19 |
# File 'lib/ntxt/block.rb', line 17 def @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.
95 96 97 |
# File 'lib/ntxt/block.rb', line 95 def root? @parent.nil? end |
#text ⇒ Object
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.
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 |