Skip to content

Commit 9faa8a3

Browse files
committed
suppport version
1 parent fa11cb5 commit 9faa8a3

6 files changed

Lines changed: 289 additions & 28 deletions

File tree

docs/build.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
import sys
3+
import subprocess
4+
5+
6+
cfg_default_output_dir = 'build'
7+
8+
# def remove_dir(tgt_dir):
9+
# """Remove directory `tgt_dir`."""
10+
# if os.path.isdir(tgt_dir):
11+
# announce(__file__, f'Removing {tgt_dir}...')
12+
# shutil.rmtree(tgt_dir)
13+
# else:
14+
# announce(__file__, f'{tgt_dir} already removed...')
15+
16+
def fetch_all_branches():
17+
# 获取所有远程分支
18+
subprocess.run(["git", "fetch", "--all"])
19+
20+
# 为每个远程分支创建本地跟踪分支
21+
branches = subprocess.check_output(["git", "branch", "-r"]).decode().split('\n')
22+
for branch in branches:
23+
branch = branch.strip()
24+
if branch and not branch.endswith('->'):
25+
local_branch = branch.replace('origin/', '')
26+
subprocess.run(["git", "branch", "--track", local_branch, branch])
27+
28+
print("git fetch all branches done")
29+
30+
def run(args):
31+
32+
fetch_all_branches()
33+
34+
cpu = os.cpu_count()
35+
36+
base_dir = os.path.abspath(os.path.dirname(__file__))
37+
output_dir = os.path.join(base_dir, cfg_default_output_dir)
38+
39+
print("Start build English version")
40+
src = os.path.join(base_dir, 'en')
41+
dst = os.path.join(output_dir, 'en')
42+
cmd_line = f'sphinx-multiversion "{src}" "{dst}" -j {cpu}'
43+
subprocess.run(cmd_line, shell=True)
44+
45+
print("Start build Chinese version")
46+
src = os.path.join(base_dir, 'zh')
47+
dst = os.path.join(output_dir, 'zh')
48+
cmd_line = f'sphinx-multiversion "{src}" "{dst}" -j {cpu}'
49+
subprocess.run(cmd_line, shell=True)
50+
51+
52+
if __name__ == '__main__':
53+
run(sys.argv[1:])

docs/en/_templates/versions.html

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{# 适配sphinx-multiversion的版本选择器 #}
2+
{% if versions %}
3+
<div class="version-selector-wrapper">
4+
<select id="version-select" class="version-select">
5+
{# 优先显示master版本 #}
6+
{% for version in versions %}
7+
{% if version[0] == 'master' %}
8+
<option value="{{ pathto(version[1], 1) }}"
9+
{% if current_version == version[0] %}selected{% endif %}>
10+
{{ version[0] }}
11+
</option>
12+
{% endif %}
13+
{% endfor %}
14+
15+
{# 显示其他版本 #}
16+
{% for version in versions %}
17+
{% if version[0] != 'master' %}
18+
<option value="{{ pathto(version[1], 1) }}"
19+
{% if current_version == version[0] %}selected{% endif %}>
20+
{{ version[0] }}
21+
</option>
22+
{% endif %}
23+
{% endfor %}
24+
</select>
25+
</div>
26+
27+
<script>
28+
document.addEventListener('DOMContentLoaded', function() {
29+
const logo = document.querySelector('.wy-side-nav-search .logo');
30+
const searchBox = document.querySelector('.wy-side-nav-search .wy-form');
31+
32+
if (logo && searchBox) {
33+
const selector = document.querySelector('.version-selector-wrapper');
34+
searchBox.parentNode.insertBefore(selector, searchBox);
35+
}
36+
37+
// 设置选中的版本
38+
function setSelectedVersion() {
39+
const savedVersion = localStorage.getItem('selectedVersion');
40+
if (savedVersion) {
41+
const select = document.getElementById('version-select');
42+
for (let i = 0; i < select.options.length; i++) {
43+
if (select.options[i].text === savedVersion) {
44+
select.selectedIndex = i;
45+
break;
46+
}
47+
}
48+
}
49+
}
50+
51+
document.getElementById('version-select').addEventListener('change', function() {
52+
if (this.value) {
53+
// 保存当前选择的版本
54+
localStorage.setItem('selectedVersion', this.options[this.selectedIndex].text);
55+
// 执行原始跳转
56+
window.location.href = this.value;
57+
}
58+
});
59+
60+
// 页面加载时设置选中的版本
61+
setSelectedVersion();
62+
});
63+
</script>
64+
65+
<style>
66+
.version-selector-wrapper {
67+
padding: 10px 15px;
68+
background: #f8f8f8;
69+
border-bottom: 1px solid #e7e7e7;
70+
}
71+
72+
.version-select {
73+
width: auto;
74+
min-width: 120px;
75+
max-width: 180px;
76+
margin: 0 auto;
77+
padding: 6px 12px;
78+
border-radius: 4px;
79+
border: 1px solid #4d90fe;
80+
background: white;
81+
color: #333;
82+
font-size: 13px;
83+
display: block;
84+
}
85+
86+
@media screen and (max-width: 768px) {
87+
.version-selector-wrapper {
88+
padding: 8px 10px;
89+
}
90+
}
91+
</style>
92+
{% endif %}

docs/en/conf.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
#
33
# For the full list of built-in configuration values, see the documentation:
44
# https://www.sphinx-doc.org/en/master/usage/configuration.html
5+
import os
6+
import sys
7+
import datetime
58

69
# -- Project information -----------------------------------------------------
710
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
11+
current_year = datetime.datetime.now().year
812

913
project = 'TuyaOpen Development Guide'
10-
copyright = '2021-%Y, Tuya Inc'
14+
copyright = u'2021-{}, Tuya Inc'.format(current_year)
1115
author = 'Tuya'
12-
release = '1.2.0'
16+
release = '1.3.0'
1317

1418
# -- General configuration ---------------------------------------------------
1519
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
1620

17-
# 新增站点地图基础URL配置
18-
html_baseurl = 'https://github.com/tuya/TuyaOpen'
21+
html_baseurl = 'https://github.com/tuya/TuyaOpen'
1922

2023
extensions = [
2124
'sphinx.ext.autodoc',
@@ -27,15 +30,14 @@
2730
'sphinxcontrib.mermaid',
2831
'sphinx_design',
2932
'sphinx_sitemap',
30-
]
33+
'sphinx_multiversion',
34+
]
3135

3236
templates_path = ['_templates']
3337
exclude_patterns = []
3438
source_suffix = ['.rst', '.md']
3539

3640
language = 'en'
37-
38-
# 语法高亮设置
3941
pygments_style = 'friendly'
4042

4143
html_logo = '../images/TuyaOpen.png'
@@ -50,7 +52,7 @@ def setup(app):
5052
html_static_path = ['_static']
5153

5254
html_theme_options = {
53-
# 'display_version': True,
55+
'display_version': True,
5456
'prev_next_buttons_location': 'both',
5557
'style_external_links': False,
5658
'sticky_navigation': True,
@@ -59,26 +61,33 @@ def setup(app):
5961
'titles_only': False,
6062
'collapse_navigation': False,
6163
'logo_only': True,
62-
'body_max_width': None, # 取消页面宽度限制
63-
'sidebarwidth': '25%', # 调整侧边栏宽度
64+
'body_max_width': None,
65+
'sidebarwidth': '25%',
6466
}
6567

6668
html_js_files = [
67-
'js/custom.js', # 如果有自定义 JavaScript 文件
68-
'js/include_html.js' # 如果需要引入其他 JavaScript 文件
69+
'js/custom.js',
70+
'js/include_html.js'
6971
]
7072

71-
# -- GitHub 相关配置 ---------------------------------------------------
7273
html_context = {
73-
# GitHub 仓库设置(必需)
74-
"display_github": True, # 启用 GitHub 链接
75-
"github_user": "tuya", # 组织/用户名
76-
"github_repo": "TuyaOpen", # 仓库名
77-
"github_version": "master", # 默认分支(如 main/master)
78-
79-
# 页面路径配置(自动生成编辑链接)
80-
"conf_py_path": "/docs/en/", # 配置文件的仓库相对路径
74+
"display_github": True,
75+
"github_user": 'tuya',
76+
"github_repo": 'TuyaOpen',
77+
"github_version": 'master',
78+
"conf_py_path": '/docs/en/',
8179
}
8280

83-
highlight_language = 'c' # 默认高亮C语言代码
84-
primary_domain = 'c' # 主文档域为C语言
81+
highlight_language = 'c'
82+
primary_domain = 'c'
83+
84+
# sphinx-multiversion
85+
smv_branch_whitelist = r'^master$' # master only
86+
smv_remote_whitelist = None
87+
smv_tag_whitelist = r'.*'
88+
89+
html_sidebars = {
90+
'**': [
91+
'versions.html',
92+
],
93+
}

docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ sphinx-design
55
sphinx-reredirects
66
typing-extensions
77
sphinxcontrib-mermaid
8+
sphinx-multiversion
89
sphinx-markdown-tables>=0.0.17
910
sphinx-rtd-theme>=2.0.0
1011
recommonmark>=0.7.1

docs/zh/_templates/versions.html

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{# 适配sphinx-multiversion的版本选择器 #}
2+
{% if versions %}
3+
<div class="version-selector-wrapper">
4+
<select id="version-select" class="version-select">
5+
{# 优先显示master版本 #}
6+
{% for version in versions %}
7+
{% if version[0] == 'master' %}
8+
<option value="{{ pathto(version[1], 1) }}"
9+
{% if current_version == version[0] %}selected{% endif %}>
10+
{{ version[0] }}
11+
</option>
12+
{% endif %}
13+
{% endfor %}
14+
15+
{# 显示其他版本 #}
16+
{% for version in versions %}
17+
{% if version[0] != 'master' %}
18+
<option value="{{ pathto(version[1], 1) }}"
19+
{% if current_version == version[0] %}selected{% endif %}>
20+
{{ version[0] }}
21+
</option>
22+
{% endif %}
23+
{% endfor %}
24+
</select>
25+
</div>
26+
27+
<script>
28+
document.addEventListener('DOMContentLoaded', function() {
29+
const logo = document.querySelector('.wy-side-nav-search .logo');
30+
const searchBox = document.querySelector('.wy-side-nav-search .wy-form');
31+
32+
if (logo && searchBox) {
33+
const selector = document.querySelector('.version-selector-wrapper');
34+
searchBox.parentNode.insertBefore(selector, searchBox);
35+
}
36+
37+
// 设置选中的版本
38+
function setSelectedVersion() {
39+
const savedVersion = localStorage.getItem('selectedVersion');
40+
if (savedVersion) {
41+
const select = document.getElementById('version-select');
42+
for (let i = 0; i < select.options.length; i++) {
43+
if (select.options[i].text === savedVersion) {
44+
select.selectedIndex = i;
45+
break;
46+
}
47+
}
48+
}
49+
}
50+
51+
document.getElementById('version-select').addEventListener('change', function() {
52+
if (this.value) {
53+
// 保存当前选择的版本
54+
localStorage.setItem('selectedVersion', this.options[this.selectedIndex].text);
55+
// 执行原始跳转
56+
window.location.href = this.value;
57+
}
58+
});
59+
60+
// 页面加载时设置选中的版本
61+
setSelectedVersion();
62+
});
63+
</script>
64+
65+
<style>
66+
.version-selector-wrapper {
67+
padding: 10px 15px;
68+
background: #f8f8f8;
69+
border-bottom: 1px solid #e7e7e7;
70+
}
71+
72+
.version-select {
73+
width: auto;
74+
min-width: 120px;
75+
max-width: 180px;
76+
margin: 0 auto;
77+
padding: 6px 12px;
78+
border-radius: 4px;
79+
border: 1px solid #4d90fe;
80+
background: white;
81+
color: #333;
82+
font-size: 13px;
83+
display: block;
84+
}
85+
86+
@media screen and (max-width: 768px) {
87+
.version-selector-wrapper {
88+
padding: 8px 10px;
89+
}
90+
}
91+
</style>
92+
{% endif %}

0 commit comments

Comments
 (0)