Class: Keydown::CodeMap

Inherits:
Object
  • Object
show all
Defined in:
lib/keydown/code_map.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ CodeMap

Returns a new instance of CodeMap.



6
7
8
9
# File 'lib/keydown/code_map.rb', line 6

def initialize(text)
  @text = text
  @map = {}
end

Instance Attribute Details

#mapped_textObject (readonly)

Returns the value of attribute mapped_text.



4
5
6
# File 'lib/keydown/code_map.rb', line 4

def mapped_text
  @mapped_text
end

Class Method Details

.build_from(text) ⇒ Object



50
51
52
53
54
# File 'lib/keydown/code_map.rb', line 50

def self.build_from(text)
  map = CodeMap.new(text)
  map.build
  map
end

Instance Method Details

#[](id) ⇒ Object



15
16
17
# File 'lib/keydown/code_map.rb', line 15

def [](id)
  @map[id]
end

#add(id, node) ⇒ Object



11
12
13
# File 'lib/keydown/code_map.rb', line 11

def add(id, node)
  @map[id] = node
end

#buildObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/keydown/code_map.rb', line 31

def build
  @mapped_text = @text.gsub(/^(```|@@@) ?(.+?)\r?\n(.+?)\r?\n(```|@@@)\r?$/m) do
    language = $2
    code = $3
    id = Digest::SHA1.hexdigest(code)

    add(id, highlight(code, language))
    id
  end
end

#each(&blk) ⇒ Object



19
20
21
# File 'lib/keydown/code_map.rb', line 19

def each(&blk)
  @map.send(:each, &blk)
end

#lengthObject



23
24
25
# File 'lib/keydown/code_map.rb', line 23

def length
  @map.keys.length
end

#nodesObject



27
28
29
# File 'lib/keydown/code_map.rb', line 27

def nodes
  @map
end

#put_code_in(html) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/keydown/code_map.rb', line 42

def put_code_in(html)
  each do |id, code|
    html.sub!(id, code)
  end

  html
end