Module: ReVIEW::TextUtils

Instance Method Summary collapse

Instance Method Details

#add_space?(line1, line2, lang, lazy = nil) ⇒ Boolean



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
# File 'lib/review/textutils.rb', line 47

def add_space?(line1, line2, lang, lazy = nil)
  # https://drafts.csswg.org/css-text-3/#line-break-transform
  tail = line1[-1]
  head = line2[0]
  if tail.nil? || head.nil?
    return nil
  end

  space = true
  # rule 2
  if i[F W H].include?(Unicode::Eaw.property(tail)) &&
     i[F W H].include?(Unicode::Eaw.property(head)) &&
     tail !~ /\p{Hangul}/ && head !~ /\p{Hangul}/
    space = nil
  end

  if %w[ja zh zh_CN zh_TW yi].include?(lang)
    # rule 3
    if (i[F W H].include?(Unicode::Eaw.property(tail)) &&
        tail !~ /\p{Hangul}/ &&
        (head =~ /\p{P}/ || head =~ /\p{S}/ || Unicode::Eaw.property(head) == :A)) ||
       (i[F W H].include?(Unicode::Eaw.property(head)) &&
        head !~ /\p{Hangul}/ &&
        (tail =~ /\p{P}/ || head =~ /\p{S}/ || Unicode::Eaw.property(tail) == :A))
      space = nil
    end

    # lazy than rule 3, but it looks better
    if lazy &&
       (i[F W H].include?(Unicode::Eaw.property(tail)) &&
        tail !~ /\p{Hangul}/) ||
       (i[F W H].include?(Unicode::Eaw.property(head)) &&
        head !~ /\p{Hangul}/)
      space = nil
    end
  end
  space
end

#detab(str, ts = 8) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/review/textutils.rb', line 14

def detab(str, ts = 8)
  add = 0
  len = nil
  str.gsub("\t") do
    len = ts - ($`.size + add) % ts
    add += len - 1
    ' ' * len
  end
end

#join_lines_to_paragraph(lines) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/review/textutils.rb', line 86

def join_lines_to_paragraph(lines)
  unless @book.config['join_lines_by_lang']
    return lines.join
  end

  lazy = true
  lang = 'ja'
  0.upto(lines.size - 2) do |n|
    if add_space?(lines[n], lines[n + 1], lang, lazy)
      lines[n] += ' '
    end
  end
  lines.join
end

#split_paragraph(lines) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/review/textutils.rb', line 24

def split_paragraph(lines)
  pre = pre_paragraph
  post = post_paragraph
  trimmed_lines = trim_lines(lines)

  blocked_lines = [[]]
  trimmed_lines.each do |element|
    if element.empty?
      blocked_lines << [] if blocked_lines.last != []
    else
      blocked_lines.last << element
    end
  end

  blocked_lines.map! { |i| join_lines_to_paragraph(i) }

  if pre && post
    blocked_lines.map! { |i| pre + i + post }
  end

  blocked_lines
end