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.1'

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
# 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

Instance Attribute Details

#collapseObject

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



87
88
89
# File 'lib/codeblock.rb', line 87

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.



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

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.



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

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.



91
92
93
# File 'lib/codeblock.rb', line 91

def keep_meta
  @keep_meta
end

#line_numsObject

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



100
101
102
# File 'lib/codeblock.rb', line 100

def line_nums
  @line_nums
end

#lines_fullObject (readonly)

The full array of lines in the code.



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

def lines_full
  @lines_full
end

#skip_skipsObject

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



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

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.



71
72
73
# File 'lib/codeblock.rb', line 71

def start
  @start
end

#tabObject

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



79
80
81
# File 'lib/codeblock.rb', line 79

def tab
  @tab
end

Instance Method Details

#line_nums_to_sObject

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



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

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.



175
176
177
# File 'lib/codeblock.rb', line 175

def lines
	return block.lines
end

#namedObject

A hash of named lines.



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

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.


196
197
198
# File 'lib/codeblock.rb', line 196

def notes_to_s
	return block.notes_to_s
end

#to_sObject

Returns a string of the code block.



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

def to_s
	return block.to_s
end