Skip to content

Commit

Permalink
chore: Cleanup and format
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ committed Dec 4, 2024
1 parent f6aa8e6 commit e0cd8f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 61 deletions.
37 changes: 0 additions & 37 deletions .sphinxnotes/template/update.py

This file was deleted.

62 changes: 38 additions & 24 deletions src/sphinxnotes/isso/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""
sphinxnotes.isso
~~~~~~~~~~~~~~~~
sphinxnotes.isso
~~~~~~~~~~~~~~~~
This extension is modified from sphinxcontrib-disqus.
This extension is modified from sphinxcontrib-disqus.
:copyright: Copyright 2021 Shengyu Zhang
:copyright: Copyright 2016 Robpol86
:license: BSD, see LICENSE for details.
:copyright: Copyright 2021 Shengyu Zhang
:copyright: Copyright 2016 Robpol86
:license: BSD, see LICENSE for details.
"""

from __future__ import annotations
Expand All @@ -22,7 +22,7 @@
from sphinx.application import Sphinx
from sphinx.util import logging

__title__= 'sphinxnotes-isso'
__title__ = 'sphinxnotes-isso'
__license__ = 'BSD'
__version__ = '1.0'
__author__ = 'Shengyu Zhang'
Expand All @@ -32,16 +32,27 @@

# Isso client configuration items
# https://posativ.org/isso/docs/configuration/client/
CONFIG_ITEMS = ['isso_css', 'isso_lang', 'isso_reply_to_self',
'isso_require_author', 'isso_require_email',
'isso_max_comments_top', 'isso_max_comments_nested',
'isso_reveal_on_click', 'isso_avatar', 'isso_avatar_bg',
'isso_avatar_fg', 'isso_vote', 'isso_vote_levels',
'isso_feed']
CONFIG_ITEMS = [
'isso_css',
'isso_lang',
'isso_reply_to_self',
'isso_require_author',
'isso_require_email',
'isso_max_comments_top',
'isso_max_comments_nested',
'isso_reveal_on_click',
'isso_avatar',
'isso_avatar_bg',
'isso_avatar_fg',
'isso_vote',
'isso_vote_levels',
'isso_feed',
]

logger = logging.getLogger(__name__)

def ext_config_to_isso_config(key:str, value:Any) -> Tuple[str,str]:

def ext_config_to_isso_config(key: str, value: Any) -> Tuple[str, str]:
assert key in CONFIG_ITEMS
key = 'data-' + key.replace('_', '-')
if isinstance(value, str):
Expand All @@ -53,7 +64,9 @@ def ext_config_to_isso_config(key:str, value:Any) -> Tuple[str,str]:
return (key, value)


class IssoNode(nodes.General, nodes.Element): pass
class IssoNode(nodes.General, nodes.Element):
pass


def html_visit_isso_node(self: HTML5Translator, node):
docname = node['docname']
Expand All @@ -66,11 +79,11 @@ def html_visit_isso_node(self: HTML5Translator, node):
if 'nocomments' in metadata:
raise nodes.SkipNode

thread_id = node.get('thread-id') or \
metadata.get('isso-id') or \
'/' + docname
thread_id = node.get('thread-id') or metadata.get('isso-id') or '/' + docname
if not thread_id.startswith('/'):
logger.warning(f'isso thread-id {thread_id} doesn\'t start with slash', location=node)
logger.warning(
f"isso thread-id {thread_id} doesn't start with slash", location=node
)

kwargs = {
'data-isso-id': thread_id,
Expand Down Expand Up @@ -100,7 +113,7 @@ def run(self):

node = IssoNode()
node['ids'] = ['isso-thread']
# Save docname for later looking up :attr:`self.env.metadata`,
# Save docname for later looking up :attr:`self.env.metadata`,
# which is not yet available now.
node['docname'] = self.env.docname

Expand All @@ -118,8 +131,9 @@ def run(self):
return [node]


def on_html_page_context(app:Sphinx, pagename:str, templatename:str, context,
doctree:nodes.document) -> None:
def on_html_page_context(
app: Sphinx, pagename: str, templatename: str, context, doctree: nodes.document
) -> None:
"""Called when the HTML builder has created a context dictionary to render a template with.
Conditionally adding isso client script to <head /> if the directive is used in a page.
Expand All @@ -140,14 +154,14 @@ def on_html_page_context(app:Sphinx, pagename:str, templatename:str, context,
}
for cfg in CONFIG_ITEMS:
val = getattr(app.config, cfg)
if val is not None: # Maybe 0, False, '' or anything
if val is not None: # Maybe 0, False, '' or anything
issocfg, issoval = ext_config_to_isso_config(cfg, val)
kwargs[issocfg] = issoval
js_path = posixpath.join(app.config.isso_url, 'js/embed.min.js')
app.add_js_file(js_path, **kwargs)


def setup(app:Sphinx):
def setup(app: Sphinx):
app.add_config_value('isso_url', None, '')
for cfg in CONFIG_ITEMS:
app.add_config_value(cfg, None, '')
Expand Down

0 comments on commit e0cd8f2

Please sign in to comment.