Skip to content

Commit

Permalink
Replaced regex with backrefs package in search plugin (squidfunk#8034)
Browse files Browse the repository at this point in the history
Use Re in all places in the search plugin except where Unicode
properties are desired (checking for Chinese chars). Use Backrefs'
`bre` to check for Unicode properties. To minimize any differences,
explicitly use `script` as Backrefs uses `script_extensions` (or `scx`)
by default with `Is*` properties.
  • Loading branch information
facelessuser authored Feb 26, 2025
1 parent 37162a5 commit ab15110
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
5 changes: 3 additions & 2 deletions material/plugins/search/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import json
import logging
import os
import regex as re
import re
from backrefs import bre

from html import escape
from html.parser import HTMLParser
Expand Down Expand Up @@ -285,7 +286,7 @@ def _find_toc_by_id(self, toc, id):

# Find and segment Chinese characters in string
def _segment_chinese(self, data):
expr = re.compile(r"(\p{IsHan}+)", re.UNICODE)
expr = bre.compile(r"(\p{script: Han}+)", bre.UNICODE)

# Replace callback
def replace(match):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ pymdown-extensions~=10.2
babel~=2.10
colorama~=0.4
paginate~=0.5
regex>=2022.4
backrefs~=5.8
requests~=2.26
5 changes: 3 additions & 2 deletions src/plugins/search/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import json
import logging
import os
import regex as re
import re
import backrefs as bre

from html import escape
from html.parser import HTMLParser
Expand Down Expand Up @@ -285,7 +286,7 @@ def _find_toc_by_id(self, toc, id):

# Find and segment Chinese characters in string
def _segment_chinese(self, data):
expr = re.compile(r"(\p{IsHan}+)", re.UNICODE)
expr = bre.compile(r"(\p{script: Han}+)", bre.UNICODE)

# Replace callback
def replace(match):
Expand Down

0 comments on commit ab15110

Please sign in to comment.