class CodeBlock

CodeBlock

Constants

VERSION

Version

Attributes

collapse[R]

If true, then contiguous lines that contain just spaces are collapsed to a single space-only line.

end[R]

The name of the line that ends the block. Leave nil for no ending line. If an ending line is given but that line is not found then an exception is raised.

indent[R]

If set, indicates a string to put at the beginning of each line. For Markdown, a value of four spaces is a good choice. Defaults to nil.

keep_meta[R]

If true, the line meta information (i.e. the ## and everything after it) is not stripped from the output lines. Defaults to false.

line_nums[R]

If true, line numbers are output with the block of code. Defaults to false.

lines_full[R]

The full array of lines in the code.

skip_skips[R]

If true, then lines that are marked to be skipped are not skipped. Defaults to false.

start[R]

The name of the line that starts the block. Leave nil to start at the beginning of the file. If an ending line is given but that line is not found then an exception is raised.

tab[R]

The string to convert tab characters to. Defaults to four spaces.

Public Class Methods

new(raw, opts={}) click to toggle source

First param should be the code that should be parsed. The remaining options follow the names of the various properties that can be set. So, for example, to create a block with named, starting and ending lines, you could do this:

block = CodeBlock.new(src, 'start'=>'a', 'end'=>'b')

You might find that you are often naming the starting line “start” and the ending line “end”. To simplify setting those common names, you can use the se (“start/end”) option:

block = CodeBlock.new(src, 'se'=>true)

That is exactly equivelant to:

block = CodeBlock.new(src, 'start'=>'start', 'end'=>'end')
# File lib/codeblock.rb, line 32
def initialize(raw, opts={})
        @lines_org = raw.lines
        @keep_meta = opts['keep_meta'] || false
        @tab = opts['tab'] || '    '
        @indent = opts['indent']
        @line_nums = opts['line_nums']
        @skip_skips = opts['skip_skips'] ? true : false
        
        # if 'se' param, set start and end to "start" and "end"
        if opts['se']
                @start = 'start'
                @end = 'end'
        else
                @start = opts['start']
                @end = opts['end']
        end
        
        # default collapse to true
        if opts.has_key?('collapse')
                @collapse = opts['collapse']
        else
                @collapse = true
        end
        
        # initialize block object
        @block_ob = nil
end

Public Instance Methods

collapse=(new_val) click to toggle source

Sets the collapse property.

# File lib/codeblock.rb, line 139
def collapse=(new_val)
        @block_ob = nil
        @collapse = new_val
end
end=(new_val) click to toggle source

Sets the end property.

# File lib/codeblock.rb, line 121
def end=(new_val)
        @block_ob = nil
        @end = new_val
end
indent=(new_val) click to toggle source

Sets the indent property.

# File lib/codeblock.rb, line 133
def indent=(new_val)
        @block_ob = nil
        @indent = new_val
end
keep_meta=(new_val) click to toggle source

Sets the keep_meta property.

# File lib/codeblock.rb, line 145
def keep_meta=(new_val)
        @block_ob = nil
        @keep_meta = new_val
end
line_nums=(new_val) click to toggle source

Sets the line_nums property.

# File lib/codeblock.rb, line 157
def line_nums=(new_val)
        @block_ob = nil
        @line_nums = new_val
end
line_nums_to_s() click to toggle source

Returns a string of the line numbers, one number per line.

# File lib/codeblock.rb, line 185
def line_nums_to_s
        return block.line_nums_to_s
end
lines() click to toggle source

Returns an array of the lines in the code block. Each element of the array is a CodeBlock::Line object.

# File lib/codeblock.rb, line 175
def lines
        return block.lines
end
named() click to toggle source

A hash of named lines.

# File lib/codeblock.rb, line 201
def named
        return block.named
end
notes_to_s() click to toggle source

Returns a string of the notes for each each line. Notes are set with the `notes` attribute in the meta section of the line, like this:

  db['hero'] = 'Thor' ## notes="Set the value in the database"

There will be as many lines in the string as lines in the code block.
Lines with no notes will be empty lines.
# File lib/codeblock.rb, line 196
def notes_to_s
        return block.notes_to_s
end
skip_skips=(new_val) click to toggle source

Sets the skip_skips property.

# File lib/codeblock.rb, line 151
def skip_skips=(new_val)
        @block_ob = nil
        @skip_skips = new_val
end
start=(new_val) click to toggle source

Sets the start property.

# File lib/codeblock.rb, line 115
def start=(new_val)
        @block_ob = nil
        @start = new_val
end
tab=(new_val) click to toggle source

Sets the tab property.

# File lib/codeblock.rb, line 127
def tab=(new_val)
        @block_ob = nil
        @tab = new_val
end
to_s() click to toggle source

Returns a string of the code block.

# File lib/codeblock.rb, line 180
def to_s
        return block.to_s
end

Private Instance Methods

block() click to toggle source
# File lib/codeblock.rb, line 217
def block()
        # build block if necessary
        if not @block_ob
                @lines_full = @lines_org.map {|line| CodeBlock::Line.new(self, line)}
                opts = {}
                opts['start'] = @start
                opts['end'] = @end
                opts['tab'] = @tab
                opts['collapse'] = @collapse
                @block_ob = CodeBlock::Block.new(self, opts)
        end
        
        # return
        return @block_ob
end