Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Prefix all CSS classes with hxr- #205

Merged
merged 3 commits into from
Jul 6, 2022
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.hoverxref {
.{{ hoverxref_css_class_prefix }}hoverxref {
border-bottom: 1px dotted;
border-color: gray;
}
Expand Down
6 changes: 3 additions & 3 deletions hoverxref/_static/js/hoverxref.js_t
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ $(document).ready(function() {
// Remove ``title=`` attribute for intersphinx nodes that have hoverxref enabled.
// It doesn't make sense the browser shows the default tooltip (browser's built-in)
// and immediately after that our tooltip was shown.
$('.hoverxref.external').each(function () { $(this).removeAttr('title') });
$('.{{ hoverxref_css_class_prefix }}hoverxref.external').each(function () { $(this).removeAttr('title') });

$('.hoverxref.tooltip').tooltipster({
$('.{{ hoverxref_css_class_prefix }}hoverxref.{{ hoverxref_css_class_prefix }}tooltip').tooltipster({
theme: {{ hoverxref_tooltip_theme }},
interactive: {{ 'true' if hoverxref_tooltip_interactive else 'false' }},
maxWidth: {{ hoverxref_tooltip_maxwidth }},
Expand Down Expand Up @@ -235,7 +235,7 @@ $(document).ready(function() {
};

var delay = {{ hoverxref_modal_hover_delay }}, setTimeoutConst;
$('.hoverxref.modal').hover(function(event) {
$('.{{ hoverxref_css_class_prefix }}hoverxref.{{ hoverxref_css_class_prefix }}modal').hover(function(event) {
var element = $(this);
console.debug('Event: ' + event + ' Element: ' + element);
event.preventDefault();
Expand Down
11 changes: 7 additions & 4 deletions hoverxref/domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ class HoverXRefBaseDomain:
)

def _inject_hoverxref_data(self, env, refnode, typ):
classes = ['hoverxref']
from .extension import CSS_CLASSES, CSS_DEFAULT_CLASS

classes = [CSS_DEFAULT_CLASS]
type_class = None
if typ == 'hoverxreftooltip':
type_class = 'tooltip'
classes.append(type_class)
elif typ == 'hoverxrefmodal':
type_class = 'modal'
classes.append(type_class)

if not type_class:
type_class = env.config.hoverxref_role_types.get(typ)
if not type_class:
Expand All @@ -33,7 +34,9 @@ def _inject_hoverxref_data(self, env, refnode, typ):
default,
typ,
)
classes.append(type_class)

# Examples: hxr-tooltip, hxr-modal
classes.append(CSS_CLASSES[type_class])

refnode.replace_attr('classes', classes)
# TODO: log something else here, so we can unique identify this node
Expand Down
22 changes: 17 additions & 5 deletions hoverxref/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

logger = logging.getLogger(__name__)

CSS_CLASS_PREFIX = 'hxr-'
CSS_DEFAULT_CLASS = f'{CSS_CLASS_PREFIX}hoverxref'
CSS_CLASSES = {
'tooltip': f'{CSS_CLASS_PREFIX}tooltip',
'modal': f'{CSS_CLASS_PREFIX}modal',
}

HOVERXREF_ASSETS_FILES = [
'js/hoverxref.js_t', # ``_t`` tells Sphinx this is a template
Expand All @@ -27,7 +33,7 @@
TOOLTIP_ASSETS_FILES = [
# Tooltipster's Styles
'js/tooltipster.bundle.min.js',
'css/tooltipster.custom.css',
'css/tooltipster.custom.css_t',
'css/tooltipster.bundle.min.css',

# Tooltipster's Themes
Expand Down Expand Up @@ -65,6 +71,7 @@ def copy_asset_files(app, exception):
# Then, add the values that the user overrides
context[attr] = getattr(app.config, attr)

context['hoverxref_css_class_prefix'] = CSS_CLASS_PREFIX
context['http_hoverxref_version'] = __version__

# Finally, add some non-hoverxref extra configs
Expand All @@ -73,10 +80,13 @@ def copy_asset_files(app, exception):
context[attr] = getattr(app.config, attr)

for f in ASSETS_FILES:
# Example: "./_static/js/hoverxref.js_t"
path = os.path.join(os.path.dirname(__file__), '_static', f)
# Example: "<app.outdir>/_static/css" or "<app.outdir>/_static/js"
output = os.path.join(app.outdir, '_static', f.split('/')[0])
copy_asset(
path,
os.path.join(app.outdir, '_static', f.split('.')[-1].replace('js_t', 'js')),
output,
context=context,
)

Expand Down Expand Up @@ -264,7 +274,7 @@ def missing_reference(app, env, node, contnode):
hoverxref_type = hoverxref_type or app.config.hoverxref_default_type

classes = newnode.get('classes')
classes.extend(['hoverxref', hoverxref_type])
classes.extend([CSS_DEFAULT_CLASS, CSS_CLASSES[hoverxref_type]])
newnode.replace_attr('classes', classes)

return newnode
Expand Down Expand Up @@ -374,11 +384,13 @@ def setup(app):

app.connect('missing-reference', missing_reference)

# Include all assets previously copied/rendered by ``copy_asset_files`` as
# Javascript and CSS files into the Sphinx application
for f in ASSETS_FILES:
if f.endswith('.js') or f.endswith('.js_t'):
app.add_js_file(f.replace('.js_t', '.js'))
if f.endswith('.css'):
app.add_css_file(f)
if f.endswith('.css') or f.endswith('.css_t'):
app.add_css_file(f.replace('.css_t', '.css'))

return {
'version': __version__,
Expand Down
Loading