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
9 changes: 6 additions & 3 deletions lib/bitclust/mdcompiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def signature_re
INDENTED_FENCE_RE = /\A([ \t]+)(`{3,})/
# dd 段落の継続行(インデント行。ただしインデントフェンスの手前で止まる)
DD_TEXT_RE = /\A[ \t](?![ \t]*`{3})/
DLIST_RE = /\A- \*\*(.+?)\*\*:(?:\s|$)/
# 用語の後ろに {#id} があればアンカー id として扱う(用語集の各用語への
# リンク用。rurema/doctree#2634)。id は $2 に入る。
DLIST_RE = /\A- \*\*(.+?)\*\*:(?:[ \t]*\{#([\w-]+)\})?(?:\s|$)/
INFO_RE = /\A- \*\*(?:param|arg|return|raise)\*\*/
SEE_RE = /\A- \*\*SEE\*\*/
# @undef など変換器が生のまま渡す未知メタデータ
Expand Down Expand Up @@ -271,12 +273,13 @@ def dlist
# GFM モード: `term` のコードスパンは <code> として描画し、
# 中身の参照はその中で解決する(<code><a>...</a></code>。spec/eval)
term = ($1 || raise)
id = $2 # {#id} があれば dt にアンカーを付ける(term の再マッチ前に退避)
if gfm? && term =~ /\A`(.+)`\z/
inner = ($1 || raise).strip
line dt("<code>#{rd_compile_text(MarkdownToRRD.restore_inline(inner))}</code>")
line dt("<code>#{rd_compile_text(MarkdownToRRD.restore_inline(inner))}</code>", id)
else
term = strip_code_span(term).strip
line dt(compile_text(term))
line dt(compile_text(term), id)
end
inline = l.sub(DLIST_RE, '').strip
@f.ungets(" #{inline}\n") unless inline.empty?
Expand Down
4 changes: 2 additions & 2 deletions lib/bitclust/rdcompiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ def dd_without_p
line '</dd>'
end

def dt(s)
"<dt>#{s}</dt>"
def dt(s, id = nil)
id ? %Q(<dt id="#{escape_html(id)}">#{s}</dt>) : "<dt>#{s}</dt>"
end

def stop_on_syntax_error?
Expand Down
2 changes: 1 addition & 1 deletion sig/bitclust/rdcompiler.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module BitClust
# empty lines do not separate paragraphs.
def dd_without_p: () -> void

def dt: (String s) -> String
def dt: (String s, ?String? id) -> String

METHOD_ATTRIBUTE_LINE_RE: ::Regexp

Expand Down
12 changes: 12 additions & 0 deletions test/test_mdcompiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ def test_dlist
RD
end

def test_dlist_term_anchor
# 用語の後ろの {#id} は dt のアンカー id になる(用語集の各用語リンク用, rurema/doctree#2634)
html = @md.compile("- **アリティー**: {#arity}\n- **`arity`**:\n 仮引数の数。\n")
assert_include(html, '<dt id="arity">アリティー</dt>')
# {#id} が無い項目には id は付かない
assert_not_include(html, '<dt id="arity">arity')
# 通常の dlist は従来どおり id 無し
plain = @md.compile("- **foo**:\n bar\n")
assert_include(plain, '<dt>foo</dt>')
assert_not_include(plain, '<dt id')
end

def test_entry_heading
assert_equivalent_method <<~RD
--- index(val) -> Integer
Expand Down
Loading