Class: CodeBlock::Line

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.keep_meta
    @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

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

#numberObject

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.

Returns:

  • (Boolean)


355
356
357
# File 'lib/codeblock.rb', line 355

def has_content?
  return @display.match(/\S/mu)
end

#nameObject

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.

Returns:

  • (Boolean)


360
361
362
# File 'lib/codeblock.rb', line 360

def no_content?
  return !has_content?
end

#notesObject

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

#skipObject

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_sObject

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