textwrap 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.txt +10 -6
  2. data/lib/textwrap.rb +63 -21
  3. metadata +2 -2
data/README.txt CHANGED
@@ -4,16 +4,20 @@ Copyright (c) 2007 SUGAWARA Genki <sgwr_dts@yahoo.co.jp>
4
4
 
5
5
  == Example
6
6
 
7
+ $KCODE = 'u'
7
8
  require 'textwrap'
8
9
  include Textwrap
9
- puts Textwrap.fill(<<EOS, 40)
10
+
11
+ p fill(<<EOS, 40, :consider_linebreak_whitespace => true)
10
12
  The wrap() method is just like fill() except that it returns
11
13
  a list of strings instead of one big string with newlines to separate
12
14
  the wrapped lines.
13
15
  EOS
16
+ #=> "The wrap() method is just like fill()\nexcept that it returns a list of strings\ninstead of one big with newlines\nto separate the wrapped lines."
14
17
 
15
- # The wrap() method is just like fill()
16
- # except that it returns a list of strings
17
- # instead of one big string with newlines
18
- # to separate the wrapped lines.
19
-
18
+ p fill(<<EOS, 40)
19
+ 拙者親方と申すは御立会の内に御存知の御方も御座りましょうが、御江戸を発って二十里上方、
20
+ 相州小田原一色町を御過ぎなされて、青物町を上りへ御出でなさるれば、欄干橋虎屋藤右衛門、
21
+ 只今では剃髪致して圓斎と名乗りまする。
22
+ EOS
23
+ #=> "拙者親方と申すは御立会の内に御存知の御方\nも御座りましょうが、御江戸を発って二十里\n上方、相州小田原一色町を御過ぎなされて、\n青物町を上りへ御出でなさるれば、欄干橋虎\n屋藤右衛門、只今では剃髪致して圓斎と名乗\nりまする。"
@@ -1,24 +1,22 @@
1
- #
2
- # = testwrap.rb
3
- #
4
- # Copyright (c) 2007 SUGAWARA Genki <sgwr_dts@yahoo.co.jp>
5
- #
6
- # == Example
7
- #
1
+ # $KCODE = 'u'
8
2
  # require 'textwrap'
9
3
  # include Textwrap
10
- # puts Textwrap.fill(<<EOS, 40)
4
+ #
5
+ # p fill(<<EOS, 40, :consider_linebreak_whitespace => true)
11
6
  # The wrap() method is just like fill() except that it returns
12
7
  # a list of strings instead of one big string with newlines to separate
13
8
  # the wrapped lines.
14
9
  # EOS
10
+ # #=> "The wrap() method is just like fill()\nexcept that it returns a list of strings\ninstead of one big with newlines\nto separate the wrapped lines."
11
+ #
12
+ # p fill(<<EOS, 40)
13
+ # 拙者親方と申すは御立会の内に御存知の御方も御座りましょうが、御江戸を発って二十里上方、
14
+ # 相州小田原一色町を御過ぎなされて、青物町を上りへ御出でなさるれば、欄干橋虎屋藤右衛門、
15
+ # 只今では剃髪致して圓斎と名乗りまする。
16
+ # EOS
17
+ # #=> "拙者親方と申すは御立会の内に御存知の御方\nも御座りましょうが、御江戸を発って二十里\n上方、相州小田原一色町を御過ぎなされて、\n青物町を上りへ御出でなさるれば、欄干橋虎\n屋藤右衛門、只今では剃髪致して圓斎と名乗\nりまする。"
15
18
  #
16
- # # The wrap() method is just like fill()
17
- # # except that it returns a list of strings
18
- # # instead of one big string with newlines
19
- # # to separate the wrapped lines.
20
19
  #
21
-
22
20
  require 'strscan'
23
21
 
24
22
  module Textwrap
@@ -30,32 +28,76 @@ module Textwrap
30
28
 
31
29
  initial_indent = options.fetch(:initial_indent, '')
32
30
  subsequent_indent = options.fetch(:subsequent_indent, '')
31
+ break_long_words = options.fetch(:break_long_words, true)
32
+
33
+ code = options.fetch(:code, $KCODE)
34
+
35
+ if (m = /\A[esu]/i.match(code))
36
+ code = m[0].downcase
37
+ else
38
+ code = 'n'
39
+ end
33
40
 
34
41
  if options.fetch(:expand_tabs, true)
35
42
  text = text.gsub(/\t/, ' ') # SP x 8
36
43
  end
37
44
 
38
45
  if options.fetch(:replace_whitespace, true)
39
- text = text.gsub(/\s/, ' ')
46
+ text = text.gsub(/\t\f/, ' ')
47
+ end
48
+
49
+ if options.has_key?(:consider_linebreak_whitespace)
50
+ consider_linebreak_whitespace = options[:consider_linebreak_whitespace]
51
+ elsif code == 'n'
52
+ consider_linebreak_whitespace = true
53
+ else
54
+ consider_linebreak_whitespace = false
40
55
  end
41
56
 
57
+ gauge_width = lambda {|str|
58
+ w = 0
59
+
60
+ str.scan(Regexp.new('.', nil, code)) do |c|
61
+ w += (c[0] <= 0x7f) ? 1 : 2
62
+ end
63
+
64
+ return w
65
+ }
66
+
42
67
  if options.fetch(:break_long_words, true)
43
- regexps = [/\s{1,#{width}}/, /\S{1,#{width}}/]
68
+ break_word = lambda {|src, scnr|
69
+ return [src, scnr] if gauge_width.call(src) <= width
70
+ w = 0; dest = ''
71
+
72
+ src.scan(Regexp.new('.', nil, code)) do |c|
73
+ w += (c[0] <= 0x7f) ? 1 : 2
74
+
75
+ break if w > width
76
+ dest << c
77
+ end
78
+
79
+ new_scnr = StringScanner.new(src.slice(dest.length..-1) + scnr.rest)
80
+ return [dest, new_scnr]
81
+ }
44
82
  else
45
- regexps = [/\s+/, /\S+/]
83
+ break_word = lambda {|str, scnr| [str, scnr]}
46
84
  end
47
85
 
48
- s = StringScanner.new(text.split($/).join)
86
+ regexps = [/\s+/, /\S+/]
87
+ s = StringScanner.new(text.split(/[\r\n]+/).join(consider_linebreak_whitespace ? ' ' : ''))
49
88
  lines = []
50
89
  indent = initial_indent
51
- words = [s.scan(regexps.reverse!.first) || s.scan(regexps.reverse!.first) || '']
90
+ word = s.scan(regexps.reverse!.first) || s.scan(regexps.reverse!.first) || ''
91
+ word, s = break_word.call(word, s)
92
+ words = [word]
52
93
 
53
94
  until s.eos?
54
95
  word = s.scan(regexps.reverse!.first) or next
55
- line = indent + words.join.strip
96
+ word, s = break_word.call(word, s)
97
+ line = indent + words.join.sub(/\A\s+/, '')
56
98
 
57
- if (line + word).length > width
58
- lines << line
99
+ if gauge_width.call(line + word) > width
100
+ lines << line.sub(/\s+\Z/, '')
59
101
  words.clear
60
102
  indent = subsequent_indent
61
103
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: textwrap
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2007-10-20 00:00:00 +09:00
6
+ version: 0.1.1
7
+ date: 2007-10-28 00:00:00 +09:00
8
8
  summary: Text wrapping and filling library like Python textwrap module.
9
9
  require_paths:
10
10
  - lib