Class: SM::Fragment

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc/markup/simple_markup/fragments.rb

Overview

A Fragment is a chunk of text, subclassed as a paragraph, a list entry, or verbatim text

Direct Known Subclasses

Paragraph, Rule, Verbatim

Constant Summary collapse

TYPE_MAP =

This is a simple factory system that lets us associate fragement types (a string) with a subclass of fragment

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level, param, type, txt) ⇒ Fragment

Returns a new instance of Fragment.



14
15
16
17
18
19
20
# File 'lib/rdoc/markup/simple_markup/fragments.rb', line 14

def initialize(level, param, type, txt)
  @level = level
  @param = param
  @type  = type
  @txt   = ""
  add_text(txt) if txt
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level



11
12
13
# File 'lib/rdoc/markup/simple_markup/fragments.rb', line 11

def level
  @level
end

#paramObject (readonly)

Returns the value of attribute param



11
12
13
# File 'lib/rdoc/markup/simple_markup/fragments.rb', line 11

def param
  @param
end

#txtObject (readonly)

Returns the value of attribute txt



11
12
13
# File 'lib/rdoc/markup/simple_markup/fragments.rb', line 11

def txt
  @txt
end

#typeObject

Returns the value of attribute type



12
13
14
# File 'lib/rdoc/markup/simple_markup/fragments.rb', line 12

def type
  @type
end

Class Method Details

.for(line) ⇒ Object



41
42
43
44
45
# File 'lib/rdoc/markup/simple_markup/fragments.rb', line 41

def Fragment.for(line)
  klass =  TYPE_MAP[line.type] ||
    raise("Unknown line type: '#{line.type.inspect}:' '#{line.text}'")
  return klass.new(line.level, line.param, line.flag, line.text)
end

.type_name(name) ⇒ Object



37
38
39
# File 'lib/rdoc/markup/simple_markup/fragments.rb', line 37

def Fragment.type_name(name)
  TYPE_MAP[name] = self
end

Instance Method Details

#add_text(txt) ⇒ Object



22
23
24
25
# File 'lib/rdoc/markup/simple_markup/fragments.rb', line 22

def add_text(txt)
  @txt << " " if @txt.length > 0
  @txt << txt.tr_s("\n ", "  ").strip
end

#to_sObject



27
28
29
# File 'lib/rdoc/markup/simple_markup/fragments.rb', line 27

def to_s
  "L#@level: #{self.class.name.split('::')[-1]}\n#@txt"
end