class Aozora2Html::RubyBuffer
ルビ文字列解析用バッファ
Attributes
protected[RW]
‘|`が来た時に真にする。ルビの親文字のガード用。
Public Class Methods
new()
click to toggle source
# File lib/aozora2html/ruby_buffer.rb, line 11 def initialize clear end
Public Instance Methods
clear()
click to toggle source
バッファの初期化。‘“”`の1要素のバッファにする。
# File lib/aozora2html/ruby_buffer.rb, line 16 def clear @ruby_buf = [+''] @protected = nil @char_type = nil end
create_ruby(parser, ruby)
click to toggle source
# 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)
click to toggle source
buffer management
# 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?()
click to toggle source
# File lib/aozora2html/ruby_buffer.rb, line 22 def empty? @ruby_buf.empty? end
last()
click to toggle source
# File lib/aozora2html/ruby_buffer.rb, line 54 def last @ruby_buf.last end
length()
click to toggle source
# File lib/aozora2html/ruby_buffer.rb, line 70 def length @ruby_buf.length end
present?()
click to toggle source
# File lib/aozora2html/ruby_buffer.rb, line 26 def present? !empty? end
push(item)
click to toggle source
バッファ末尾にitemを追加する
itemとバッファの最後尾がどちらもStringであれば連結したStringにし、 そうでなければバッファの末尾に新しい要素として追加する
# 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)
click to toggle source
# 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_a()
click to toggle source
# File lib/aozora2html/ruby_buffer.rb, line 30 def to_a @ruby_buf end
Private Instance Methods
char_type(char)
click to toggle source
# File lib/aozora2html/ruby_buffer.rb, line 109 def char_type(char) ## `String#char_type`も定義されているのに注意 char.char_type rescue StandardError :else end