Skip to content

Commit 8883d1c

Browse files
authored
Rework titleabbrev handling in direct_html (#1539)
This reworks `<titleabbrev>` handling in direct_html so it works for link targets as well. It turns out asciidoctor has a built in thing for this called `reftext`. So we just use it.
1 parent c2c6312 commit 8883d1c

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

resources/asciidoctor/lib/docbook_compat/convert_outline.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ def convert_outline(node, opts = {})
2020
def convert_outline_section(section, toclevels)
2121
return if section.roles.include? 'exclude'
2222

23-
title = section.attr 'titleabbrev'
24-
title ||= section.title
25-
link = %(<a href="##{section.id}">#{title}</a>)
23+
link = %(<a href="##{section.id}">#{section_link_text section}</a>)
2624
link = %(<span class="#{wrapper_class_for section}">#{link}</span>)
2725
[
2826
%(<li>#{link}),
@@ -31,6 +29,14 @@ def convert_outline_section(section, toclevels)
3129
].compact
3230
end
3331

32+
def section_link_text(section)
33+
text = section.xreftext nil
34+
# Normally we won't get an <em> wrapping the text *but* if it was set
35+
# with something like `reftext=_title_` to make it render properly in
36+
# in most places then it will have the <em> and we have to remove it.
37+
text.gsub %r{^<em>(.+)</em>$}, '\\1'
38+
end
39+
3440
def convert_outline_subsections(section, toclevels)
3541
return unless section.level < toclevels && section.sections?
3642

resources/asciidoctor/lib/docbook_compat/titleabbrev_handler.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module DocbookCompat
77
# Looks for pass blocks with `<titleabbrev>` and adds an attributes to their
88
# parent section. This attribute is an abbreviated title used when rendering
99
# the table of contents. This exists entirely for backwards compatibility with
10-
# docbook. It is simpler and recommended to set the `titleabbrev` attribute
10+
# docbook. It is simpler and recommended to set the `reftext` attribute
1111
# directly on the section when the document is built with `--direct_html`.
1212
class TitleabbrevHandler < TreeProcessorScaffold
1313
def process_block(block)
@@ -22,10 +22,17 @@ def process_pass(block)
2222

2323
text.slice! m.begin(0), m.end(0)
2424
block.lines = text.split "\n"
25+
process_titleabbrev block, m[1]
26+
end
27+
28+
private
2529

30+
def process_titleabbrev(block, reftext)
2631
section = block.parent
2732
section = section.parent until section.context == :section
28-
section.attributes['titleabbrev'] = m[1]
33+
# Docbook seems to bold links to sections less than 2 so we should too.
34+
reftext = "_#{reftext}_" if section.level < 2
35+
section.attributes['reftext'] = reftext
2936
end
3037
end
3138
end

resources/asciidoctor/spec/docbook_compat_spec.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@
240240
end
241241
end
242242
end
243-
context 'when a section has a titleabbrev' do
243+
context 'when a section has reftext' do
244244
let(:convert_attributes) do
245245
{
246246
# Shrink the output slightly so it is easier to read
@@ -253,11 +253,11 @@
253253
'toclevels' => 2,
254254
}
255255
end
256-
shared_examples 'titleabbrev' do
256+
shared_examples 'reftext' do
257257
context 'the table of contents' do
258258
it 'includes the abbreviated title' do
259259
expect(converted).to include <<~HTML
260-
<li><span class="chapter"><a href="#_section_1">S1</a></span>
260+
<li><span class="chapter"><a href="#s1">S1</a></span>
261261
HTML
262262
end
263263
it 'includes the correct title for a subsection' do
@@ -273,35 +273,45 @@
273273
it 'includes the unabbreviated title' do
274274
expect(converted).to include 'Section 1</h1>'
275275
end
276+
it 'includes a link to the abbreviated section' do
277+
expect(converted).to include <<~HTML.strip
278+
<a class="xref" href="#s1"title="Section 1"><em>S1</em></a>
279+
HTML
280+
end
276281
end
277282
end
278-
context 'using a pass block' do
283+
context 'using a pass block containing titleabbrev' do
279284
let(:input) do
280285
<<~ASCIIDOC
281286
= Title
282287
288+
[[s1]]
283289
== Section 1
284290
++++
285291
<titleabbrev>S1</titleabbrev>
286292
++++
287293
288294
=== Section 2
295+
296+
<<s1>>
289297
ASCIIDOC
290298
end
291-
include_examples 'titleabbrev'
299+
include_examples 'reftext'
292300
end
293301
context 'using an attribute' do
294302
let(:input) do
295303
<<~ASCIIDOC
296304
= Title
297305
298-
[titleabbrev=S1]
306+
[id=s1,reftext=_S1_]
299307
== Section 1
300308
301309
=== Section 2
310+
311+
<<s1>>
302312
ASCIIDOC
303313
end
304-
include_examples 'titleabbrev'
314+
include_examples 'reftext'
305315
end
306316
end
307317
end

0 commit comments

Comments
 (0)