Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/bitclust/rdcompiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,21 +451,31 @@ def method_signature(sig_line, first)
end

BracketLink = /\[\[[\w-]+?:[!-~]+?(?:\[\] )?\]\]/n
# BitClust には ISO/JIS へのリンク機能がないため、[[ISO:8601]]/[[JIS:X 0301]] は
# リンクにせず平文で表示する (rurema/bitclust#236)。引数に空白を含む JIS も拾う。
StandardRef = /\[\[(?:ISO|JIS):[!-~][!-~ ]*?\]\]/n
NeedESC = /[&"<>]/

def compile_text(str)
escape_table = HTMLUtils::ESC
str.gsub(/(#{NeedESC})|(#{BracketLink})/o) {
str.gsub(/(#{NeedESC})|(#{StandardRef})|(#{BracketLink})/o) {
# @type var char: '&' | '"' | '<' | '>'
if char = _ = $1 then escape_table[char]
elsif tok = $2 then bracket_link(tok[2..-3] || raise)
elsif tok = $3 then seems_code(tok)
elsif tok = $2 then standard_ref(tok[2..-3] || raise)
elsif tok = $3 then bracket_link(tok[2..-3] || raise)
else
raise 'must not happen'
end
}
end

def standard_ref(link)
# 例: "ISO:8601" -> "ISO 8601", "JIS:X 0301" -> "JIS X 0301"
type, _arg = link.split(':', 2)
arg = _arg&.strip or raise
escape_html("#{type} #{arg}")
end

def bracket_link(link, label = nil, frag = nil)
type, _arg = link.split(':', 2)
arg = _arg&.rstrip or raise
Expand Down
4 changes: 4 additions & 0 deletions sig/bitclust/rdcompiler.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ module BitClust

BracketLink: ::Regexp

StandardRef: ::Regexp

NeedESC: ::Regexp

def compile_text: (String str) -> String

def standard_ref: (String link) -> String

def bracket_link: (String link, ?String? label, ?String? frag) -> String

def protect: (String src) { () -> String } -> String
Expand Down
2 changes: 2 additions & 0 deletions test/test_rdcompiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ def test_invalid_case
"man header" => ['[[man:sys/socket.h(header)]]', '<a class="external" href="http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/socket.h.html">sys/socket.h(header)</a>'],
"man system call" => ['[[man:fopen(3linux)]]', '<a class="external" href="http://man7.org/linux/man-pages/man3/fopen.3.html">fopen(3linux)</a>'],
"RFC" => ['[[RFC:2822]]', '<a class="external" href="https://tools.ietf.org/html/rfc2822">[RFC2822]</a>'],
"ISO standard" => ['[[ISO:8601]]', 'ISO 8601'],
"JIS standard" => ['[[JIS:X 0301]]', 'JIS X 0301'],
"special var $~" => ['[[m:$~]]', '<a href="dummy/method/Kernel/v/=7e">$~</a>'],
"special var $," => ['[[m:$,]]', '<a href="dummy/method/Kernel/v/=2c">$,</a>'],
"extra close bracket" => ['[[c:String]]]', '<a href="dummy/class/String">String</a>]'],
Expand Down
Loading