Skip to content

Commit f328a47

Browse files
authored
Merge pull request #219 from znz/meta-description-plain-text
meta description の改行由来の空白とブラケットリンクを整形する
2 parents c90f90a + bf1e2fe commit f328a47

8 files changed

Lines changed: 100 additions & 5 deletions

File tree

lib/bitclust/classentry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def inherited_method_specs
425425
end
426426

427427
def description
428-
display_text(source.split(/\n\n+/, 2)[0].strip)
428+
description_text(source.split(/\n\n+/, 2)[0].strip)
429429
end
430430

431431
def clear_cache

lib/bitclust/docentry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def libraries
8484
end
8585

8686
def description
87-
display_text(source.split(/\n\n/, 2)[0].strip)
87+
description_text(source.split(/\n\n/, 2)[0].strip)
8888
end
8989
end
9090
end

lib/bitclust/entry.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,25 @@ def display_text(text)
165165
end
166166
private :display_text
167167

168+
# BitClust::RDCompiler::BracketLink と同等の正規表現(/n なし)
169+
BracketLink = /\[\[[\w-]+?:[!-~]+?(?:\[\] )?\]\]/
170+
171+
# meta description など、コンパイラを通さずマークアップも解釈されない
172+
# 場所に使うテキスト。非 ASCII 文字間の改行は削除し(ブラウザでは
173+
# 空白扱いになり日本語文中に不自然な空白が見えるため)、残りの改行は
174+
# 空白に変換、ブラケットリンクはリンク先名のみにする
175+
def description_text(text)
176+
text = display_text(text)
177+
return text unless text
178+
text = text.split("\n").map(&:strip).join("\n")
179+
.gsub(/(\P{ascii})\n(?=\P{ascii})/) { $1 || raise }
180+
.tr("\n", ' ')
181+
text.gsub(BracketLink) {|link|
182+
((link[2..-3] || raise).split(':', 2).last || raise).rstrip
183+
}
184+
end
185+
private :description_text
186+
168187
def type_id
169188
self.class.type_id
170189
end

lib/bitclust/functionentry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def header
7979
end
8080

8181
def description
82-
display_text(source.split(/\n\n+/, 2)[0].strip)
82+
description_text(source.split(/\n\n+/, 2)[0].strip)
8383
end
8484
end
8585
end

lib/bitclust/libraryentry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def inspect
8080
end
8181

8282
def description
83-
display_text(source.split(/\n\n+/, 2)[0])
83+
description_text(source.split(/\n\n+/, 2)[0])
8484
end
8585

8686
def check_link(path = [])

lib/bitclust/methodentry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def undefined?
216216
end
217217

218218
def description
219-
display_text(source.split(/\n\n+/, 3)[1])
219+
description_text(source.split(/\n\n+/, 3)[1])
220220
end
221221
end
222222
end

sig/bitclust/entry.rbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ module BitClust
4242
# サブクラスで定義されている
4343
def self.type_id: () -> Symbol
4444

45+
BracketLink: ::Regexp
46+
47+
def display_text: (String? text) -> String?
48+
49+
def description_text: (String? text) -> String?
50+
4551
def type_id: () -> Symbol
4652

4753
# for entry_class.type_id

test/test_entry.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,73 @@ def test_instance_method?
7272
assert(hoge.instance_method?('fugafuga', false))
7373
end
7474
end
75+
76+
class TestEntryDescription < Test::Unit::TestCase
77+
def parse_class(class_source)
78+
lib, = BitClust::RRDParser.parse(class_source, 'hoge')
79+
lib.fetch_class('Hoge')
80+
end
81+
82+
def test_description_truncates_newline_between_non_ascii
83+
klass = parse_class(<<HERE)
84+
= class Hoge
85+
これは1行目で
86+
あとに続きます。
87+
88+
以降の段落は含まれません。
89+
HERE
90+
assert_equal('これは1行目であとに続きます。', klass.description)
91+
end
92+
93+
def test_description_truncates_consecutive_newlines_between_non_ascii
94+
klass = parse_class(<<HERE)
95+
= class Hoge
96+
あい
97+
うえ
98+
おか。
99+
HERE
100+
assert_equal('あいうえおか。', klass.description)
101+
end
102+
103+
def test_description_converts_newline_to_space_around_ascii
104+
klass = parse_class(<<HERE)
105+
= class Hoge
106+
first line
107+
second line
108+
HERE
109+
assert_equal('first line second line', klass.description)
110+
end
111+
112+
def test_description_replaces_bracket_link_with_plain_text
113+
klass = parse_class(<<HERE)
114+
= class Hoge
115+
自身の hostname を文字列で返します。また、[[m:URI::Generic#host]] が設
116+
定されていない場合は [[c:Array]] を返します。
117+
HERE
118+
assert_equal('自身の hostname を文字列で返します。また、URI::Generic#host が設定されていない場合は Array を返します。',
119+
klass.description)
120+
end
121+
122+
def test_description_replaces_indexer_bracket_link
123+
klass = parse_class(<<HERE)
124+
= class Hoge
125+
[[m:String#[] ]] を参照。
126+
HERE
127+
assert_equal('String#[] を参照。', klass.description)
128+
end
129+
130+
def test_method_description_is_plain_text
131+
klass = parse_class(<<HERE)
132+
= class Hoge
133+
== Instance Methods
134+
--- fuga
135+
136+
[[c:Array]] を日本語で
137+
返します。
138+
139+
次の段落。
140+
HERE
141+
method = klass.entries.detect {|m| m.name == 'fuga' }
142+
assert_equal('Array を日本語で返します。', method.description)
143+
end
144+
end

0 commit comments

Comments
 (0)