Skip to content

Commit 0f88031

Browse files
committed
Fixed further locations where output encoding was not preserved. Bug #11 by Vít Ondruch
1 parent d29a9fa commit 0f88031

File tree

6 files changed

+157
-18
lines changed

6 files changed

+157
-18
lines changed

History.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
=== 3.4.1
1+
=== 3.5
22

33
* Minor enhancements
44
* RDoc::Parser::C looks for rb_scan_args and fills in RDoc::AnyMethod#params
55
appropriately. This may provide useful information if the author did not
66
provide a call-seq.
77
* RDoc::Parser::C now records the function name for methods implemented in
88
C.
9-
* RDoc now records file and byte offset inforamtion for methods.
9+
* RDoc now records file and byte offset information for methods.
1010
* Bug fixes
1111
* Locations of module aliases are now recorded.
1212
* RDoc::Parser::C finds method bodies better now.
13+
* Fixed further locations where output encoding was not preserved. Bug #11
14+
by Vít Ondruch.
1315

1416
=== 3.4
1517

lib/rdoc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def self.const_missing const_name # :nodoc:
9595
##
9696
# RDoc version you are using
9797

98-
VERSION = '3.4.1'
98+
VERSION = '3.5'
9999

100100
##
101101
# Method visibilities

lib/rdoc/parser/ruby.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def initialize(top_level, file_name, content, options, stats)
185185
def collect_first_comment
186186
skip_tkspace
187187
comment = ''
188+
comment.force_encoding @encoding if @encoding
188189
first_line = true
189190

190191
tk = get_tk
@@ -1259,6 +1260,8 @@ def parse_require(context, comment)
12591260

12601261
def parse_statements(container, single = NORMAL, current_method = nil,
12611262
comment = '')
1263+
comment.force_encoding @encoding if @encoding
1264+
12621265
nest = 1
12631266
save_visibility = container.visibility
12641267

@@ -1282,6 +1285,7 @@ def parse_statements(container, single = NORMAL, current_method = nil,
12821285
comment.empty?
12831286

12841287
comment = ''
1288+
comment.force_encoding @encoding if @encoding
12851289
end
12861290

12871291
while TkCOMMENT === tk do
@@ -1421,7 +1425,10 @@ def parse_statements(container, single = NORMAL, current_method = nil,
14211425
keep_comment = false
14221426
end
14231427

1424-
comment = '' unless keep_comment
1428+
unless keep_comment then
1429+
comment = ''
1430+
comment.force_encoding @encoding if @encoding
1431+
end
14251432

14261433
begin
14271434
get_tkread

lib/rdoc/text.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def normalize_comment text
9797
text = strip_hashes text
9898
text = expand_tabs text
9999
text = flush_left text
100-
strip_newlines text
100+
text = strip_newlines text
101+
text
101102
end
102103

103104
##
@@ -139,7 +140,11 @@ def parse text
139140

140141
def strip_hashes text
141142
return text if text =~ /^(?>\s*)[^\#]/
142-
text.gsub(/^\s*(#+)/) { $1.tr '#',' ' }.gsub(/^\s+$/, '')
143+
144+
empty = ''
145+
empty.force_encoding text.encoding if Object.const_defined? :Encoding
146+
147+
text.gsub(/^\s*(#+)/) { $1.tr '#', ' ' }.gsub(/^\s+$/, empty)
143148
end
144149

145150
##

test/test_rdoc_parser_ruby.rb

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@ def teardown
3232
@tempfile2.close
3333
end
3434

35+
def test_collect_first_comment
36+
p = util_parser <<-CONTENT
37+
# first
38+
39+
# second
40+
class C; end
41+
CONTENT
42+
43+
comment = p.collect_first_comment
44+
45+
assert_equal "# first\n", comment
46+
end
47+
48+
def test_collect_first_comment_encoding
49+
skip "Encoding not implemented" unless Object.const_defined? :Encoding
50+
51+
@options.encoding = Encoding::CP852
52+
53+
p = util_parser <<-CONTENT
54+
# first
55+
56+
# second
57+
class C; end
58+
CONTENT
59+
60+
comment = p.collect_first_comment
61+
62+
assert_equal Encoding::CP852, comment.encoding
63+
end
64+
3565
def test_extract_call_seq
3666
m = RDoc::AnyMethod.new nil, 'm'
3767
p = util_parser ''
@@ -1439,6 +1469,28 @@ def test_parse_statements_class_nested
14391469
assert_equal 'my method', bar.comment
14401470
end
14411471

1472+
def test_parse_statements_encoding
1473+
skip "Encoding not implemented" unless Object.const_defined? :Encoding
1474+
@options.encoding = Encoding::CP852
1475+
1476+
content = <<-EOF
1477+
class Foo
1478+
##
1479+
# this is my method
1480+
add_my_method :foo
1481+
end
1482+
EOF
1483+
1484+
util_parser content
1485+
1486+
@parser.parse_statements @top_level
1487+
1488+
foo = @top_level.classes.first.method_list.first
1489+
assert_equal 'foo', foo.name
1490+
assert_equal 'this is my method', foo.comment
1491+
assert_equal Encoding::CP852, foo.comment.encoding
1492+
end
1493+
14421494
def test_parse_statements_identifier_meta_method
14431495
content = <<-EOF
14441496
class Foo
@@ -1450,7 +1502,7 @@ class Foo
14501502

14511503
util_parser content
14521504

1453-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1505+
@parser.parse_statements @top_level
14541506

14551507
foo = @top_level.classes.first.method_list.first
14561508
assert_equal 'foo', foo.name
@@ -1466,7 +1518,7 @@ def foo() end
14661518

14671519
util_parser content
14681520

1469-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1521+
@parser.parse_statements @top_level
14701522

14711523
foo = @top_level.classes.first.method_list[0]
14721524
assert_equal 'foo', foo.name
@@ -1499,7 +1551,7 @@ def foo()
14991551

15001552
util_parser content
15011553

1502-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1554+
@parser.parse_statements @top_level
15031555

15041556
foo = @top_level.classes.first.method_list[0]
15051557
assert_equal 'foo', foo.name
@@ -1565,7 +1617,7 @@ class Foo
15651617

15661618
util_parser content
15671619

1568-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1620+
@parser.parse_statements @top_level
15691621

15701622
constants = @top_level.classes.first.constants
15711623

@@ -1612,7 +1664,7 @@ def test_parse_statements_identifier_attr
16121664

16131665
util_parser content
16141666

1615-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1667+
@parser.parse_statements @top_level
16161668

16171669
foo = @top_level.classes.first.attributes.first
16181670
assert_equal 'foo', foo.name
@@ -1624,7 +1676,7 @@ def test_parse_statements_identifier_attr_accessor
16241676

16251677
util_parser content
16261678

1627-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1679+
@parser.parse_statements @top_level
16281680

16291681
foo = @top_level.classes.first.attributes.first
16301682
assert_equal 'foo', foo.name
@@ -1636,7 +1688,7 @@ def test_parse_statements_identifier_include
16361688

16371689
util_parser content
16381690

1639-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1691+
@parser.parse_statements @top_level
16401692

16411693
foo = @top_level.classes.first
16421694
assert_equal 'Foo', foo.name
@@ -1648,7 +1700,7 @@ def test_parse_statements_identifier_module_function
16481700

16491701
util_parser content
16501702

1651-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1703+
@parser.parse_statements @top_level
16521704

16531705
foo, s_foo = @top_level.modules.first.method_list
16541706
assert_equal 'foo', foo.name, 'instance method name'
@@ -1665,7 +1717,7 @@ def test_parse_statements_identifier_private
16651717

16661718
util_parser content
16671719

1668-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1720+
@parser.parse_statements @top_level
16691721

16701722
foo = @top_level.classes.first.method_list.first
16711723
assert_equal 'foo', foo.name
@@ -1677,7 +1729,7 @@ def test_parse_statements_identifier_require
16771729

16781730
util_parser content
16791731

1680-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1732+
@parser.parse_statements @top_level
16811733

16821734
assert_equal 1, @top_level.requires.length
16831735
end
@@ -1695,7 +1747,7 @@ def b
16951747
end
16961748
RUBY
16971749

1698-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1750+
@parser.parse_statements @top_level
16991751

17001752
c_a = @top_level.classes.first
17011753
assert_equal 'A', c_a.full_name
@@ -1887,7 +1939,7 @@ class Baz
18871939
end
18881940
EOS
18891941

1890-
@parser.parse_statements @top_level, RDoc::Parser::Ruby::NORMAL, nil, ''
1942+
@parser.parse_statements @top_level
18911943

18921944
foo = @top_level.modules.first.modules.first
18931945
assert_equal 'Foo', foo.name

test/test_rdoc_text.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,31 @@ def test_strip_hashes
134134
assert_equal expected, strip_hashes(text)
135135
end
136136

137+
def test_strip_hashes_encoding
138+
skip "Encoding not implemented" unless Object.const_defined? :Encoding
139+
140+
text = <<-TEXT
141+
##
142+
# we don't worry too much.
143+
#
144+
# The comments associated with
145+
TEXT
146+
147+
text.force_encoding Encoding::CP852
148+
149+
expected = <<-EXPECTED
150+
151+
we don't worry too much.
152+
153+
The comments associated with
154+
EXPECTED
155+
156+
stripped = strip_hashes text
157+
158+
assert_equal expected, stripped
159+
assert_equal Encoding::CP852, stripped.encoding
160+
end
161+
137162
def test_strip_newlines
138163
assert_equal ' ', strip_newlines("\n \n")
139164

@@ -178,6 +203,54 @@ def test_strip_stars
178203
assert_equal expected, strip_stars(text)
179204
end
180205

206+
def test_strip_stars_encoding
207+
skip "Encoding not implemented" unless Object.const_defined? :Encoding
208+
209+
text = <<-TEXT
210+
/*
211+
* * we don't worry too much.
212+
*
213+
* The comments associated with
214+
*/
215+
TEXT
216+
217+
text.force_encoding Encoding::CP852
218+
219+
expected = <<-EXPECTED
220+
221+
* we don't worry too much.
222+
223+
The comments associated with
224+
EXPECTED
225+
226+
assert_equal expected, strip_stars(text)
227+
assert_equal Encoding::CP852, text.encoding
228+
end
229+
230+
def test_strip_hashes_encoding
231+
232+
text = <<-TEXT
233+
##
234+
# we don't worry too much.
235+
#
236+
# The comments associated with
237+
TEXT
238+
239+
text.force_encoding Encoding::CP852
240+
241+
expected = <<-EXPECTED
242+
243+
we don't worry too much.
244+
245+
The comments associated with
246+
EXPECTED
247+
248+
stripped = strip_hashes text
249+
250+
assert_equal expected, stripped
251+
assert_equal Encoding::CP852, stripped.encoding
252+
end
253+
181254
def test_to_html_apostrophe
182255
assert_equal '‘a', to_html("'a")
183256
assert_equal 'a’', to_html("a'")

0 commit comments

Comments
 (0)