Class: Aozora2Html::Tag::Ruby

Inherits:
ReferenceMentioned show all
Defined in:
lib/aozora2html/tag/ruby.rb

Overview

ルビ用

現状、under_rubyは無視しているのに注意

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ReferenceMentioned

#block_element?, #target_string

Methods inherited from Aozora2Html::Tag

#char_type, #inspect, #syntax_error

Constructor Details

#initialize(parser, string, ruby, under_ruby = '') ⇒ Ruby

Returns a new instance of Ruby.



15
16
17
18
19
20
# File 'lib/aozora2html/tag/ruby.rb', line 15

def initialize(parser, string, ruby, under_ruby = '')
  @target = string
  @ruby = ruby
  @under_ruby = under_ruby
  super
end

Instance Attribute Details

#rubyObject

Returns the value of attribute ruby.



12
13
14
# File 'lib/aozora2html/tag/ruby.rb', line 12

def ruby
  @ruby
end

#targetObject (readonly)

Returns the value of attribute target.



13
14
15
# File 'lib/aozora2html/tag/ruby.rb', line 13

def target
  @target
end

#under_rubyObject

Returns the value of attribute under_ruby.



12
13
14
# File 'lib/aozora2html/tag/ruby.rb', line 12

def under_ruby
  @under_ruby
end

Class Method Details

.include_ruby?(array) ⇒ Integer?

arrayがルビを含んでいればそのインデックスを返す

Returns:

  • (Integer, nil)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/aozora2html/tag/ruby.rb', line 110

def self.include_ruby?(array)
  array.index do |elt|
    case elt
    when Aozora2Html::Tag::Ruby
      true
    when Aozora2Html::Tag::ReferenceMentioned
      if elt.target.is_a?(Array)
        include_ruby?(elt.target)
      else
        elt.target.is_a?(Aozora2Html::Tag::Ruby)
      end
    end
  end
end

.rearrange_ruby(parser, targets, upper_ruby, under_ruby) ⇒ Object

rubyタグの再割り当て



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
103
104
# File 'lib/aozora2html/tag/ruby.rb', line 27

def self.rearrange_ruby(parser, targets, upper_ruby, under_ruby)
  unless include_ruby?(targets)
    return Aozora2Html::Tag::Ruby.new(parser, targets, upper_ruby, under_ruby)
  end

  new_targets = []
  new_upper = if upper_ruby == ''
                []
              else
                upper_ruby
              end
  new_under = if under_ruby == ''
                []
              else
                under_ruby
              end
  if (new_upper.length > 1) && (new_under.length > 1)
    raise Aozora2Html::Error, I18n.t(:dont_allow_triple_ruby)
  end

  targets.each do |x|
    case x
    when Aozora2Html::Tag::Ruby
      raise Aozora2Html::Error, I18n.t(:dont_use_double_ruby) if x.target.is_a?(Array)

      if x.ruby == ''
        raise Aozora2Html::Error, I18n.t(:dont_use_double_ruby) unless new_under.is_a?(Array)

        new_under.push(x.under_ruby)
      else
        raise Aozora2Html::Error, I18n.t(:dont_use_double_ruby) unless new_upper.is_a?(Array)

        new_upper.push(x.ruby)
      end
      new_targets.push(x.target)
    when Aozora2Html::Tag::ReferenceMentioned
      if x.target.is_a?(Array)
        # recursive
        ruby2 = rearrange_ruby(parser, x.target, '', '')
        target2, upper_ruby2, under_ruby2 = ruby2.target, ruby2.ruby, ruby2.under_ruby
        # rotation!!
        target2.each do |y|
          tmp = x.dup
          tmp.target = y
          new_targets.push(tmp)
        end
        if new_under.is_a?(Array)
          new_under.concat(under_ruby2)
        elsif under_ruby2.to_s.length > 0
          raise Aozora2Html::Error, I18n.t(:dont_use_double_ruby)
        end
        if new_upper.is_a?(Array)
          new_upper.concat(upper_ruby2)
        elsif upper_ruby2.to_s.length > 0
          raise Aozora2Html::Error, I18n.t(:dont_use_double_ruby)
        end
      else
        new_targets.push(x)
        if new_under.is_a?(Array)
          new_under.push('')
        end
        if new_upper.is_a?(Array)
          new_upper.push('')
        end
      end
    else
      new_targets.push(x)
      if new_under.is_a?(Array)
        new_under.push('')
      end
      if new_upper.is_a?(Array)
        new_upper.push('')
      end
    end
  end

  Aozora2Html::Tag::Ruby.new(parser, new_targets, new_upper, new_under)
end

Instance Method Details

#to_sObject



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

def to_s
  "<ruby><rb>#{@target}</rb><rp>#{PAREN_BEGIN_MARK}</rp><rt>#{@ruby}</rt><rp>#{PAREN_END_MARK}</rp></ruby>"
end