Skip to content

Commit

Permalink
fix chinese words search
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoXuan40404 committed Nov 21, 2024
1 parent 519fc27 commit e97a991
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
79 changes: 54 additions & 25 deletions 3.x/zh_CN/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#sys.path.insert(0, os.path.abspath('..'))
import sphinx_rtd_theme
from recommonmark.transform import AutoStructify
from pathlib import Path


DOC_SOURCES_DIR = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -64,6 +65,15 @@
'myst_parser'
]

myst_enable_extensions = [
"linkify", # 自动转换 URL 为链接
"replacements", # 支持文本替换
"smartquotes", # 美化引号
"strikethrough", # 支持删除线
"substitution", # 支持替换变量
"attrs_block", # 支持 Markdown 块级属性
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['../_templates']

Expand Down Expand Up @@ -233,6 +243,25 @@
# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr'
#html_search_language = 'en'
html_search_language = 'zh'
current_dir = Path(__file__).parent.resolve()
package_path = [p for p in sys.path if 'site-packages' in p][0] # 找到 jieba 的安装路径
dict_path = Path(package_path) / 'jieba/dict.txt' # jieba 默认词典路径

custom_dict_path = current_dir / "custom_words.txt" # 自定义词典路径
if custom_dict_path.exists():
custom_words = custom_dict_path.read_text(encoding="utf-8").splitlines()
custom_words = [f"{word} 3 n" for word in custom_words if word.strip()]
dict_texts = dict_path.read_text(encoding="utf-8").splitlines()
dict_texts.extend(custom_words)
dict_path.write_text("\n".join(dict_texts), encoding="utf-8")

html_search_options = {
'dict': str(dict_path),
'split_all': True, # 开启全分词模式
'minchars': 2, # 最小搜索字符数
'maxchars': 10, # 最大搜索字符数
}


# A dictionary with options for the search language support, empty by default.
# Now only 'ja' uses this config value
Expand Down Expand Up @@ -347,34 +376,34 @@
# Markdown support


github_doc_root = 'https://github.com/rtfd/recommonmark/tree/master/doc/'
def setup(app):
app.add_config_value('recommonmark_config', {
'url_resolver': lambda url: github_doc_root + url,
'auto_toc_tree_section': 'Contents',
'enable_eval_rst': True,
'enable_auto_doc_ref': True,
}, True)
# github_doc_root = 'https://github.com/rtfd/recommonmark/tree/master/doc/'
# def setup(app):
# app.add_config_value('recommonmark_config', {
# 'url_resolver': lambda url: github_doc_root + url,
# 'auto_toc_tree_section': 'Contents',
# 'enable_eval_rst': True,
# 'enable_auto_doc_ref': True,
# }, True)

app.add_transform(AutoStructify)
app.add_stylesheet('css/custom.css')
app.add_javascript('js/readthedocs-analytics.js')
app.connect('build-finished', replace_source)
# app.add_transform(AutoStructify)
# app.add_stylesheet('css/custom.css')
# app.add_javascript('js/readthedocs-analytics.js')
# app.connect('build-finished', replace_source)

def replace_source(app, exception):
if exception is None:
build_dir = os.environ.get('READTHEDOCS_OUTPUT', '_build')
# def replace_source(app, exception):
# if exception is None:
# build_dir = os.environ.get('READTHEDOCS_OUTPUT', '_build')

for root, dirs, files in os.walk(build_dir):
for file in files:
if file.endswith('.html'):
html_path = os.path.join(root, file)
# for root, dirs, files in os.walk(build_dir):
# for file in files:
# if file.endswith('.html'):
# html_path = os.path.join(root, file)

with open(html_path, 'r') as file:
lines = file.readlines()
# with open(html_path, 'r') as file:
# lines = file.readlines()

new_lines = [line.replace('https://unpkg.com', 'https://npm.onmicrosoft.cn') for line in lines]
new_lines = [line.replace('https://cdnjs.cloudflare.com', 'https://cdn.bootcdn.net') for line in new_lines]
# new_lines = [line.replace('https://unpkg.com', 'https://npm.onmicrosoft.cn') for line in lines]
# new_lines = [line.replace('https://cdnjs.cloudflare.com', 'https://cdn.bootcdn.net') for line in new_lines]

with open(html_path, 'w') as file:
file.writelines(new_lines)
# with open(html_path, 'w') as file:
# file.writelines(new_lines)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ git-lfs
Jinja2<3.1
myst-parser == 0.13.0
pyyaml

jieba

0 comments on commit e97a991

Please sign in to comment.