class CodeBlock::Block
This is the class that does the work of filtering which lines should be displayed and which shouldn't. Generally you won't have to work directly with this class.
Attributes
lines[R]
The array of CodeBlock::Line
objects.
named[R]
A hash of named lines.
Public Class Methods
new(p_code, opts={})
click to toggle source
# File lib/codeblock.rb, line 385 def initialize(p_code, opts={}) @code = p_code @collapse = opts['collapse'] @tab = opts['tab'] || ' ' @start = opts['start'] @end = opts['end'] # build lines array build_lines() end
Public Instance Methods
line_nums_to_s()
click to toggle source
Builds the string that is returned by CodeBlock#line_nums_to_s
.
# File lib/codeblock.rb, line 467 def line_nums_to_s # $tm.hrm rv = [] # get maximum width width = @lines[-1].number.to_s.length # loop through lines @lines.each do |line| rv.push line.number.to_s.rjust(width) + '.' end # return return rv.join("\n") end
notes_to_s()
click to toggle source
Builds the string that is returned by CodeBlock#notes_to_s
.
# File lib/codeblock.rb, line 492 def notes_to_s rv = [] # loop through lines @lines.each do |line| rv.push line.notes || '' end # return return rv.join("\n") end
to_s()
click to toggle source
Builds the string that is returned by CodeBlock#to_s
.
# File lib/codeblock.rb, line 421 def to_s() # $tm.hrm rv = [] indent = @code.indent || '' line_nums = @code.line_nums # get maximum width if line_nums max_width = @lines[-1].number.to_s.length end # build return array @lines.each do |line| # get string v = line.to_s # substitute tabs if @tab v = v.gsub(/\t/mu, @tab) end # line numbers if line_nums v = line.number.to_s.rjust(max_width) + '. ' + v end # indent v = indent + v # add to return array rv.push v end # return return rv.join("\n") end
Private Instance Methods
build_lines()
click to toggle source
# File lib/codeblock.rb, line 515 def build_lines # $tm.hrm @lines = [] # starting line name started = @start ? false : true ended = false # get visible lines @code.lines_full.each do |line| # start if necessary if (not started) and @start started = line.atts['name'] == @start end # add line if started and (not ended) if line.to_s @lines.push line.clone end end # if ending line if (not ended) and @end ended = line.atts['name'] == @end end end # strip leading empty lines while @lines.any? and (not @lines[0].to_s.match(/\S/mu)) @lines.shift end # strip trailing empty lines while @lines.any? and (not @lines[-1].to_s.match(/\S/mu)) @lines.pop end # collapse if @collapse while collapse_find() end end # number lines @lines.each_with_index() do |line, idx| line.number = idx + 1 end # build hash of named lines @named = {} # get named lines @lines.each do |line| if line.name if @named[line.name] raise 'https://motley.uno/messages/code/init/redundant-name: ' + line.name end @named[line.name] = line end end end
collapse_find()
click to toggle source
# File lib/codeblock.rb, line 586 def collapse_find() # loop through lines @lines.each_with_index do|line, idx| if idx > 0 if line.no_content? and @lines[idx-1].no_content? @lines.delete_at(idx-1) return true end end end # didn't find anymore lines to collapse return false end