Class: Keydown::CodeMap
- Inherits:
-
Object
- Object
- Keydown::CodeMap
- Defined in:
- lib/keydown/code_map.rb
Instance Attribute Summary collapse
-
#mapped_text ⇒ Object
readonly
Returns the value of attribute mapped_text.
Class Method Summary collapse
Instance Method Summary collapse
- #[](id) ⇒ Object
- #add(id, node) ⇒ Object
- #build ⇒ Object
- #each(&blk) ⇒ Object
-
#initialize(text) ⇒ CodeMap
constructor
A new instance of CodeMap.
- #length ⇒ Object
- #nodes ⇒ Object
- #put_code_in(html) ⇒ Object
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_text ⇒ Object (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
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 |
#build ⇒ Object
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 |
#length ⇒ Object
23 24 25 |
# File 'lib/keydown/code_map.rb', line 23 def length @map.keys.length end |
#nodes ⇒ Object
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 |