Skip to content

Commit ab15110

Browse files
authored
Replaced regex with backrefs package in search plugin (#8034)
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.
1 parent 37162a5 commit ab15110

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

material/plugins/search/plugin.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
import json
2222
import logging
2323
import os
24-
import regex as re
24+
import re
25+
from backrefs import bre
2526

2627
from html import escape
2728
from html.parser import HTMLParser
@@ -285,7 +286,7 @@ def _find_toc_by_id(self, toc, id):
285286

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

290291
# Replace callback
291292
def replace(match):

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ pymdown-extensions~=10.2
3030
babel~=2.10
3131
colorama~=0.4
3232
paginate~=0.5
33-
regex>=2022.4
33+
backrefs~=5.8
3434
requests~=2.26

src/plugins/search/plugin.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
import json
2222
import logging
2323
import os
24-
import regex as re
24+
import re
25+
import backrefs as bre
2526

2627
from html import escape
2728
from html.parser import HTMLParser
@@ -285,7 +286,7 @@ def _find_toc_by_id(self, toc, id):
285286

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

290291
# Replace callback
291292
def replace(match):

0 commit comments

Comments
 (0)