Class: Aozora2Html::RubyBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/aozora2html/ruby_buffer.rb

Overview

ルビ文字列解析用バッファ

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRubyBuffer

Returns a new instance of RubyBuffer.



11
12
13
# File 'lib/aozora2html/ruby_buffer.rb', line 11

def initialize
  clear
end

Instance Attribute Details

#protectedObject

‘|`が来た時に真にする。ルビの親文字のガード用。



9
10
11
# File 'lib/aozora2html/ruby_buffer.rb', line 9

def protected
  @protected
end

Instance Method Details

#clearObject

バッファの初期化。‘“”`の1要素のバッファにする。



16
17
18
19
20
# File 'lib/aozora2html/ruby_buffer.rb', line 16

def clear
  @ruby_buf = [+'']
  @protected = nil
  @char_type = nil
end

#create_ruby(parser, ruby) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aozora2html/ruby_buffer.rb', line 34

def create_ruby(parser, ruby)
  ans = +''
  notes = []

  @ruby_buf.each do |token|
    if token.is_a?(Aozora2Html::Tag::UnEmbedGaiji)
      ans.concat(GAIJI_MARK)
      token.escape!
      notes.push(token)
    else
      ans.concat(token.to_s)
    end
  end

  notes.unshift(Aozora2Html::Tag::Ruby.new(parser, ans, ruby))
  clear

  notes
end

#dump_into(buffer) ⇒ Object

buffer management



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aozora2html/ruby_buffer.rb', line 75

def dump_into(buffer)
  if @protected
    @ruby_buf.unshift(RUBY_PREFIX)
    @protected = nil
  end
  top = @ruby_buf[0]
  if top.is_a?(String) && buffer.last.is_a?(String)
    buffer.last.concat(top)
    buffer.concat(@ruby_buf[1, @ruby_buf.length])
  else
    buffer.concat(@ruby_buf)
  end
  clear
  buffer
end

#empty?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/aozora2html/ruby_buffer.rb', line 22

def empty?
  @ruby_buf.empty?
end

#lastObject



54
55
56
# File 'lib/aozora2html/ruby_buffer.rb', line 54

def last
  @ruby_buf.last
end

#lengthObject



70
71
72
# File 'lib/aozora2html/ruby_buffer.rb', line 70

def length
  @ruby_buf.length
end

#present?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/aozora2html/ruby_buffer.rb', line 26

def present?
  !empty?
end

#push(item) ⇒ Object

バッファ末尾にitemを追加する

itemとバッファの最後尾がどちらもStringであれば連結したStringにし、そうでなければバッファの末尾に新しい要素として追加する



62
63
64
65
66
67
68
# File 'lib/aozora2html/ruby_buffer.rb', line 62

def push(item)
  if last.is_a?(String) && item.is_a?(String)
    @ruby_buf.last.concat(item)
  else
    @ruby_buf.push(item)
  end
end

#push_char(char, buffer) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/aozora2html/ruby_buffer.rb', line 91

def push_char(char, buffer)
  ctype = char_type(char)
  if (ctype == :hankaku_terminate) && (@char_type == :hankaku)
    push(char)
    @char_type = :else
  elsif @protected || ((ctype != :else) && (ctype == @char_type))
    push(char)
  else
    dump_into(buffer)
    push(char)
    @char_type = ctype
  end
end

#to_aObject



30
31
32
# File 'lib/aozora2html/ruby_buffer.rb', line 30

def to_a
  @ruby_buf
end