Skip to content

Commit fd69a1d

Browse files
committed
Inject link CSS (#3)
1 parent 24829db commit fd69a1d

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

docs/src/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Automatic links from Python code examples to reference documentation
66
at the flick of a switch!
77
sphinx-codeautolink analyses the code in your documentation
88
and inserts links to definitions that you use.
9+
Click any names in the code example below.
910

1011
.. code:: python
1112

src/sphinx_codeautolink/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
def setup(app: Sphinx):
1717
"""Set up extension, directives and events."""
18+
app.add_css_file('sphinx-codeautolink.css')
1819
app.add_config_value('codeautolink_concat_blocks', 'none', 'html', types=[str])
1920
app.add_config_value('codeautolink_autodoc_inject', True, 'html', types=[bool])
2021

@@ -23,7 +24,7 @@ def setup(app: Sphinx):
2324
app.add_directive('implicit-import', ImplicitImport)
2425
app.add_directive('autolink-skip', AutoLinkSkip)
2526

26-
app.connect('builder-inited', state.read_references)
27+
app.connect('builder-inited', state.build_inited)
2728
app.connect('doctree-read', state.parse_blocks)
2829
app.connect('doctree-resolved', state.generate_backref_tables)
2930
app.connect('build-finished', state.apply_links)

src/sphinx_codeautolink/extension/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@ def flat_refs(self):
3333

3434
return self._flat_refs
3535

36-
def read_references(self, app):
37-
"""Nullify extension if not on HTML builder, read ref file."""
36+
def build_inited(self, app):
37+
"""Handle initial setup."""
3838
if app.builder.name != 'html':
3939
self.do_nothing = True
4040
return
4141

42+
# Append static resources path so references in setup() are valid
43+
app.config.html_static_path.append(
44+
str(Path(__file__).parent.with_name('static').absolute())
45+
)
46+
47+
# Read serialised references from last build
4248
refs_file = Path(app.srcdir) / self.code_refs_file
4349
if not refs_file.exists():
4450
return

src/sphinx_codeautolink/extension/block.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ def link_html(
171171
inners = [block.select('div > pre')[0] for block in blocks]
172172

173173
up_lvls = len(document.relative_to(out_dir).parents) - 1
174-
link_pattern = '<a href="' + '../' * up_lvls + '{link}" title="{title}">{text}</a>'
174+
link_pattern = (
175+
'<a href="' + '../' * up_lvls
176+
+ '{link}" title="{title}" class="sphinx-codeautolink-a">{text}</a>'
177+
)
175178
name_pattern = '<span class="n">{name}</span>'
176179
period = '<span class="o">.</span>'
177180

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.sphinx-codeautolink-a:link{color: inherit}
2+
.sphinx-codeautolink-a:active{color: inherit}
3+
.sphinx-codeautolink-a:visited{color: inherit}
4+
.sphinx-codeautolink-a:hover{color: rgb(0, 139, 139)}

0 commit comments

Comments
 (0)