Class: ParaBlock

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, content = "", target = nil) ⇒ ParaBlock

Default contructor



4
5
6
7
8
9
10
# File 'lib/bayeux/para_block.rb', line 4

def initialize(type, content = "", target = nil)
  @orig_type = type
  @type = normalise_type(type)
  
  @content = content
  @target = target
end

Class Method Details

.from_s(string) ⇒ Object

Type conversions



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/bayeux/para_block.rb', line 127

def self.from_s(string)
  
  unless string =~ /\s+/ then
    header = string.split(':')
    
    if header[0].nil? then
      
      # If we can't work out the type, set everything
      # to null
      type = :none
      target = nil
    
    else
      
      # Attempt to coerce the content to a type   
      block_type = header[0].to_sym
      unless block_type.nil? then
        type = block_type
      else
        type = :none
      end
      
      target = header[1]
    
    end
    
  else
    type = :none
  end
  
  return ParaBlock.new(type, "", target)
end

.json_create(json_hash) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/bayeux/para_block.rb', line 190

def self.json_create(json_hash)
  begin
    require 'json'

    block = new(json_hash["type"], json_hash["content"], json_hash["target"])
    return block

  rescue LoadError
    warn "The JSON gem couldn't be loaded, and so the JSON representation could not be generated"
  end
end

Instance Method Details

#<<(value) ⇒ Object

Convenience functions



41
42
43
# File 'lib/bayeux/para_block.rb', line 41

def <<(value)
  @content << value
end

#clearObject



49
50
51
# File 'lib/bayeux/para_block.rb', line 49

def clear
  @content.clear
end

#contentObject



26
27
28
# File 'lib/bayeux/para_block.rb', line 26

def content
  @content
end

#content=(content) ⇒ Object



23
24
25
# File 'lib/bayeux/para_block.rb', line 23

def content=(content)
  @content = content
end

#content_to_type!Object

Take the current contents as a block type, and reset the content



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bayeux/para_block.rb', line 69

def content_to_type!
  
  unless @content =~ /\s+/ then
    header = @content.split(':')
    
    if header[0].nil? then
      
      # If we can't work out the type, set everything
      # to null
      @type = :none
      @target = nil
    
    else
      
      # Attempt to coerce the content to a type   
      block_type = header[0].to_sym
      unless block_type.nil? then
        @orig_type = block_type
        @type = normalise_type(block_type)
      else
        @orig_type = :none
        @type = :none
      end
      
      @target = header[1]
      
    end
    
  else
    @type = :none
  end
  
  @content.clear
end

#empty?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/bayeux/para_block.rb', line 45

def empty?
  return (@content.empty? or (/\S/ !~ @content))
end

#normalise_type(type) ⇒ Object

Do type mapping if needed, otherwise pass on the contents as the type



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bayeux/para_block.rb', line 110

def normalise_type(type)
  case type
    when :e
      return :emph
    when :s
      return :strong
    when :quote
      return :block_quote
    else
      return type
  end
end

#orig_type_lengthObject

Returns the length of the type and target, as they were written out _in the original document_. For the current type length, see type_length



56
57
58
# File 'lib/bayeux/para_block.rb', line 56

def orig_type_length
  @orig_type.to_s.length + @target.to_s.length + 1
end

#targetObject



33
34
35
# File 'lib/bayeux/para_block.rb', line 33

def target
  @target
end

#target=(target) ⇒ Object



30
31
32
# File 'lib/bayeux/para_block.rb', line 30

def target=(target)
  @target = target
end

#to_debug_sObject



164
165
166
# File 'lib/bayeux/para_block.rb', line 164

def to_debug_s
  return "block type: #{type}, content: #{content}"
end

#to_json(*a) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/bayeux/para_block.rb', line 172

def to_json(*a)
  begin
    require 'json'

    json_hash = {
      "type"    => @name,
      "content" => @content,
      "target"  => @target,
      JSON.create_id => self.class.name
    }

    return json_hash.to_json

  rescue LoadError
    warn "The JSON gem couldn't be loaded, and so the JSON representation could not be generated"
  end
end

#to_sObject



160
161
162
# File 'lib/bayeux/para_block.rb', line 160

def to_s
  @content.to_s
end

#to_symObject



168
169
170
# File 'lib/bayeux/para_block.rb', line 168

def to_sym
  @content.to_sym
end

#typeObject



19
20
21
# File 'lib/bayeux/para_block.rb', line 19

def type
  @type
end

#type=(type) ⇒ Object

Accessors



16
17
18
# File 'lib/bayeux/para_block.rb', line 16

def type=(type)
  @type = normalise_type(type)
end

#type_lengthObject

Returns the length of the type and target, as they would be written out (including the separator), e.g as ‘figure:L3’.length



63
64
65
# File 'lib/bayeux/para_block.rb', line 63

def type_length
  @type.to_s.length + @target.to_s.length + 1
end