Class: CodeBlock

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

Overview

CodeBlock

Defined Under Namespace

Classes: Block, Line

Constant Summary collapse

VERSION =

Version

'1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, opts = {}) ⇒ CodeBlock

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')


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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']
	
	# 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
	
	# if collapse is defined
	if opts.has_key?('collapse')
		@collapse = opts['collapse']
	else
		@collapse = true
	end
	
	# if skip_skips is defined
	if opts.has_key?('skip_skips')
		@skip_skips = opts['skip_skips']
	else
		@skip_skips = true
	end
	
	# initialize block object
	@block_ob = nil
end

Instance Attribute Details

#collapseObject

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



93
94
95
# File 'lib/codeblock.rb', line 93

def collapse
  @collapse
end

#endObject

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.



82
83
84
# File 'lib/codeblock.rb', line 82

def end
  @end
end

#indentObject

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.



89
90
91
# File 'lib/codeblock.rb', line 89

def indent
  @indent
end

#keep_metaObject

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



97
98
99
# File 'lib/codeblock.rb', line 97

def keep_meta
  @keep_meta
end

#line_numsObject

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



106
107
108
# File 'lib/codeblock.rb', line 106

def line_nums
  @line_nums
end

#lines_fullObject (readonly)

The full array of lines in the code.



109
110
111
# File 'lib/codeblock.rb', line 109

def lines_full
  @lines_full
end

#skip_skipsObject

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



102
103
104
# File 'lib/codeblock.rb', line 102

def skip_skips
  @skip_skips
end

#startObject

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.



77
78
79
# File 'lib/codeblock.rb', line 77

def start
  @start
end

#tabObject

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



85
86
87
# File 'lib/codeblock.rb', line 85

def tab
  @tab
end

Instance Method Details

#line_nums_to_sObject

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



191
192
193
# File 'lib/codeblock.rb', line 191

def line_nums_to_s
	return block.line_nums_to_s
end

#linesObject

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



181
182
183
# File 'lib/codeblock.rb', line 181

def lines
	return block.lines
end

#namedObject

A hash of named lines.



207
208
209
# File 'lib/codeblock.rb', line 207

def named
	return block.named
end

#notes_to_sObject

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.


202
203
204
# File 'lib/codeblock.rb', line 202

def notes_to_s
	return block.notes_to_s
end

#to_sObject

Returns a string of the code block.



186
187
188
# File 'lib/codeblock.rb', line 186

def to_s
	return block.to_s
end