Class: CodeBlock::Line
- Inherits:
-
Object
- Object
- CodeBlock::Line
- Defined in:
- lib/codeblock.rb
Overview
A CodeBlock::Line represents a single line in the block of code. Generally you will not need to instantiate a CodeBlock::Line object yourself.
Instance Attribute Summary collapse
-
#atts ⇒ Object
readonly
Returns a hash of the name=value pairs set in the line’s meta section.
-
#number ⇒ Object
Returns the line’s number in the code block.
Instance Method Summary collapse
-
#has_content? ⇒ Boolean
Returns true if the line has any non-space characters.
-
#initialize(code, raw) ⇒ Line
constructor
Accepts a CodeBlock object and the raw string from the code.
-
#name ⇒ Object
Returns the value of ‘name’ in the line’s attributes.
-
#no_content? ⇒ Boolean
Returns false if the line has any non-space characters.
-
#notes ⇒ Object
Returns the value of ‘notes’ in the line’s attributes.
-
#skip ⇒ Object
Returns the value of ‘skip’ in the line’s attributes.
-
#to_s ⇒ Object
Returns the line as it should be displayed, taking into account tabs, indentation, line number, etc.
Constructor Details
#initialize(code, raw) ⇒ Line
Accepts a CodeBlock object and the raw string from the code.
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/codeblock.rb', line 253 def initialize(code, raw) @code = code @raw = raw @number = nil @display = @raw # unless keep meta, else strip ## and everything after if @code. @display = @raw else @display = @raw.sub(/\s*\#\#.*/mu, '') end # remove trailing spaces @display = @display.rstrip # attributes if @raw.match(/\#\#/mu) raw_atts = @raw.sub(/\A.*\#\#\s*/mu, '') raw_atts = raw_atts.rstrip # if raw atts are a single string without =, use that as the name if raw_atts.match(/\A\S+\z/mu) and (not raw_atts.match(/\=/mu)) @atts = {'name'=>raw_atts} else @atts = AttParser.parse(raw_atts, 'infer'=>true) end else @atts = {} end end |
Instance Attribute Details
#atts ⇒ Object (readonly)
Returns a hash of the name=value pairs set in the line’s meta section.
294 295 296 |
# File 'lib/codeblock.rb', line 294 def atts @atts end |
#number ⇒ Object
Returns the line’s number in the code block.
297 298 299 |
# File 'lib/codeblock.rb', line 297 def number @number end |
Instance Method Details
#has_content? ⇒ Boolean
Returns true if the line has any non-space characters.
355 356 357 |
# File 'lib/codeblock.rb', line 355 def has_content? return @display.match(/\S/mu) end |
#name ⇒ Object
Returns the value of ‘name’ in the line’s attributes. Returns nil if no name was set.
310 311 312 |
# File 'lib/codeblock.rb', line 310 def name return @atts['name'] end |
#no_content? ⇒ Boolean
Returns false if the line has any non-space characters.
360 361 362 |
# File 'lib/codeblock.rb', line 360 def no_content? return !has_content? end |
#notes ⇒ Object
Returns the value of ‘notes’ in the line’s attributes. Returns nil if no notes were set.
322 323 324 |
# File 'lib/codeblock.rb', line 322 def notes return @atts['notes'] end |
#skip ⇒ Object
Returns the value of ‘skip’ in the line’s attributes. Returns nil if no skip was set.
316 317 318 |
# File 'lib/codeblock.rb', line 316 def skip return @atts['skip'] end |
#to_s ⇒ Object
Returns the line as it should be displayed, taking into account tabs, indentation, line number, etc. If the line is to be skipped then nil is returned.
338 339 340 341 342 343 344 |
# File 'lib/codeblock.rb', line 338 def to_s if @atts['skip'] and (not @code.skip_skips) return nil else return @display end end |