Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Urhengulas committed Mar 12, 2024
1 parent 858fa91 commit 55ec1aa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

__pycache__
*.py[co]
.env
9 changes: 4 additions & 5 deletions exts/ferrocene_autoglossary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ def apply(self):

def apply_to_node(self, state, node):
lexed = list(lexer.lexer(node.astext(), state.terms))
if len(lexed) == 1 and type(lexed[0]) == str:
if len(lexed) == 1 and type(lexed[0]) is str:
# Do nothing if the lexed version returned the same string.
pass
else:
container = nodes.inline()
for part in lexed:
if type(part) == str:
if type(part) is str:
container.append(nodes.Text(part))
elif type(part) == MatchedTerm:
elif type(part) is MatchedTerm:
container.append(self.make_link(part))
else:
raise RuntimeError("unexpected result of lexer")
Expand All @@ -88,7 +88,6 @@ class PruneGlossaryTransform(SphinxTransform):
default_priority = 500

def apply(self):
state = State.get(self.env)
glossaries = list(self.document.findall(addnodes.glossary))
if glossaries:
used_terms = self.discover_used_terms()
Expand Down Expand Up @@ -116,7 +115,7 @@ def discover_used_terms(self):
doctree = self.env.get_doctree(docname)
for node in lexer.find_lexable_nodes(doctree):
for part in lexer.lexer(node.node.astext(), state.terms):
if type(part) != MatchedTerm:
if type(part) is not MatchedTerm:
continue
name = part.term.name
# Join the list of dependencies, setting None when either
Expand Down
4 changes: 2 additions & 2 deletions exts/ferrocene_autoglossary/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def render_lexed_node(terms, node):
has_matches = False

for token in lexer.lexer(node.astext(), terms):
if type(token) == MatchedTerm:
if type(token) is MatchedTerm:
result += f"[{token.text}]"
has_matches = True
elif type(token) == str:
elif type(token) is str:
result += token
else:
raise RuntimeError("invalid token type")
Expand Down
12 changes: 6 additions & 6 deletions exts/ferrocene_autoglossary/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@


def find_lexable_nodes(node, *, inside_glossary=False, inside_definition_of=None):
if type(node) == nodes.Text:
if type(node) is nodes.Text:
yield LexableNode(node=node, inside_definition_of=inside_definition_of)
elif type(node) == addnodes.glossary:
elif type(node) is addnodes.glossary:
inside_glossary = True
elif inside_glossary and type(node) == nodes.definition_list_item:
elif inside_glossary and type(node) is nodes.definition_list_item:
inside_definition_of = {term.astext() for term in node.findall(nodes.term)}
elif type(node) in (
nodes.reference,
Expand All @@ -30,7 +30,7 @@ def find_lexable_nodes(node, *, inside_glossary=False, inside_definition_of=None
return

for child in node.children:
if inside_glossary and type(child) == nodes.term:
if inside_glossary and type(child) is nodes.term:
continue
for result in find_lexable_nodes(
child,
Expand All @@ -56,12 +56,12 @@ def _filter_matches(matches):
for token in matches:
# Convert a match into a token if the previous token doesn't allow the
# following token to be a match.
if type(token) == MatchedTerm and not previous_token_allows_match:
if type(token) is MatchedTerm and not previous_token_allows_match:
token = token.text

# Only allow the next token to be a match if this is a text token
# that doesn't end with forbidden chars.
previous_token_allows_match = type(token) == str and (
previous_token_allows_match = type(token) is str and (
not token or token[-1] not in FORBID_MATCH_WHEN_PREVIOUS_ENDS_WITH
)

Expand Down

0 comments on commit 55ec1aa

Please sign in to comment.