Module: Aozora2Html::Utils

Defined in:
lib/aozora2html/utils.rb

Overview

ユーティリティ関数モジュール

Constant Summary collapse

KANJI_NUMS =
'一二三四五六七八九〇'.to_sjis
KANJI_TEN =
''.to_sjis
ZENKAKU_NUMS =
'0-9'.to_sjis

Class Method Summary collapse

Class Method Details

.convert_japanese_number(command) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/aozora2html/utils.rb', line 82

def convert_japanese_number(command)
  tmp = command.tr(ZENKAKU_NUMS, '0-9')
  tmp.tr!(KANJI_NUMS, '1234567890')
  tmp.gsub!(/(\d)#{KANJI_TEN}(\d)/o) { "#{$1}#{$2}" }
  tmp.gsub!(/(\d)#{KANJI_TEN}/o) { "#{$1}0" }
  tmp.gsub!(/#{KANJI_TEN}(\d)/o) { "1#{$1}" }
  tmp.gsub!(/#{KANJI_TEN}/o, '10')
  tmp
end

.create_font_size(times, daisho) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/aozora2html/utils.rb', line 12

def create_font_size(times, daisho)
  size = case times
         when 1
           +''
         when 2
           +'x-'
         else
           raise Aozora2Html::Error, I18n.t(:invalid_font_size) unless times >= 3

           +'xx-'
         end

  case daisho
  when :dai
    size << 'large'
  when :sho
    size << 'small'
  else
    raise Aozora2Html::Error, I18n.t(:invalid_font_size)
  end

  size
end

.create_midashi_class(type, tag) ⇒ Object



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
# File 'lib/aozora2html/utils.rb', line 50

def create_midashi_class(type, tag)
  normal_midashi_tag = {
    'h5' => 'ko-midashi',
    'h4' => 'naka-midashi',
    'h3' => 'o-midashi'
  }

  dogyo_midashi_tag = {
    'h5' => 'dogyo-ko-midashi',
    'h4' => 'dogyo-naka-midashi',
    'h3' => 'dogyo-o-midashi'
  }

  mado_midashi_tag = {
    'h5' => 'mado-ko-midashi',
    'h4' => 'mado-naka-midashi',
    'h3' => 'mado-o-midashi'
  }

  case type
  when :normal
    normal_midashi_tag[tag]
  when :dogyo
    dogyo_midashi_tag[tag]
  when :mado
    mado_midashi_tag[tag]
  else
    raise Aozora2Html::Error, I18n.t(:undefined_header)
  end
end

.create_midashi_tag(size) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/aozora2html/utils.rb', line 37

def create_midashi_tag(size)
  if size.match(SIZE_SMALL)
    'h5'
  elsif size.match(SIZE_MIDDLE)
    'h4'
  elsif size.match(SIZE_LARGE)
    'h3'
  else
    raise Aozora2Html::Error, I18n.t(:undefined_header)
  end
end

.illegal_char_check(char, line) ⇒ void

This method returns an undefined value.

使うべきではない文字があるかチェックする

警告を出力するだけで結果には影響を与えない。警告する文字は以下:

  • 1バイト文字

  • ‘#`ではなく`♯`

  • JIS(JIS X 0208)外字



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/aozora2html/utils.rb', line 103

def illegal_char_check(char, line)
  return unless char.is_a?(String)

  code = char.unpack1('H*')
  if (code == '21') ||
     (code == '23') ||
     ((code >= 'a1') && (code <= 'a5')) ||
     ((code >= '28') && (code <= '29')) ||
     (code == '5b') ||
     (code == '5d') ||
     (code == '3d') ||
     (code == '3f') ||
     (code == '2b') ||
     ((code >= '7b') && (code <= '7d'))
    puts I18n.t(:warn_onebyte, line, char)
  end

  if code == '81f2'
    puts I18n.t(:warn_chuki, line, char)
  end

  if ((code >= '81ad') && (code <= '81b7')) ||
     ((code >= '81c0') && (code <= '81c7')) ||
     ((code >= '81cf') && (code <= '81d9')) ||
     ((code >= '81e9') && (code <= '81ef')) ||
     ((code >= '81f8') && (code <= '81fb')) ||
     ((code >= '8240') && (code <= '824e')) ||
     ((code >= '8259') && (code <= '825f')) ||
     ((code >= '827a') && (code <= '8280')) ||
     ((code >= '829b') && (code <= '829e')) ||
     ((code >= '82f2') && (code <= '82fc')) ||
     ((code >= '8397') && (code <= '839e')) ||
     ((code >= '83b7') && (code <= '83be')) ||
     ((code >= '83d7') && (code <= '83fc')) ||
     ((code >= '8461') && (code <= '846f')) ||
     ((code >= '8492') && (code <= '849e')) ||
     ((code >= '84bf') && (code <= '84fc')) ||
     ((code >= '8540') && (code <= '85fc')) ||
     ((code >= '8640') && (code <= '86fc')) ||
     ((code >= '8740') && (code <= '87fc')) ||
     ((code >= '8840') && (code <= '889e')) ||
     ((code >= '9873') && (code <= '989e')) ||
     ((code >= 'eaa5') && (code <= 'eafc')) ||
     ((code >= 'eb40') && (code <= 'ebfc')) ||
     ((code >= 'ec40') && (code <= 'ecfc')) ||
     ((code >= 'ed40') && (code <= 'edfc')) ||
     ((code >= 'ee40') && (code <= 'eefc')) ||
     ((code >= 'ef40') && (code <= 'effc'))
    puts I18n.t(:warn_jis_gaiji, line, char)
  end
end