Skip to content

Commit 89e63e5

Browse files
committed
support autobuild
1 parent fa11cb5 commit 89e63e5

7 files changed

Lines changed: 598 additions & 29 deletions

File tree

docs/build.py

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

docs/en/_templates/versions.html

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{# 版本选择器模板 #}
2+
<div class="version-selector-wrapper">
3+
{% if versions %}
4+
<select class="version-select">
5+
{# 遍历所有可用版本 #}
6+
{% for item in versions.branches + versions.tags %}
7+
{# 检查当前页面在其他版本中是否存在 #}
8+
{% if item.name == current_version.name %}
9+
<option value="{{ item.url }}" selected>{{ item.name }}</option>
10+
{% else %}
11+
<option value="{{ item.url }}">{{ item.name }}</option>
12+
{% endif %}
13+
{% endfor %}
14+
</select>
15+
{% endif %}
16+
</div>
17+
18+
<script>
19+
document.addEventListener('DOMContentLoaded', function() {
20+
// 获取logo和搜索框元素
21+
const logo = document.querySelector('.wy-side-nav-search .logo');
22+
const searchBox = document.querySelector('.wy-side-nav-search .wy-form');
23+
24+
if (logo && searchBox) {
25+
const selector = document.querySelector('.version-selector-wrapper');
26+
// 在logo之后、搜索框之前插入
27+
searchBox.parentNode.insertBefore(selector, searchBox);
28+
}
29+
30+
// 添加版本切换事件监听
31+
const select = document.querySelector('.version-select');
32+
if (select) {
33+
select.addEventListener('change', function() {
34+
if (this.value) {
35+
window.location.href = this.value;
36+
}
37+
});
38+
}
39+
});
40+
</script>
41+
42+
<style>
43+
.version-selector-wrapper {
44+
padding: 10px 15px;
45+
background: #f8f8f8;
46+
border-bottom: 1px solid #e7e7e7;
47+
}
48+
49+
.version-select {
50+
width: auto;
51+
min-width: 120px;
52+
max-width: 180px;
53+
margin: 0 auto;
54+
padding: 6px 12px;
55+
border-radius: 4px;
56+
border: 1px solid #d1d1d1;
57+
background: white;
58+
color: #333;
59+
font-size: 13px;
60+
display: block;
61+
}
62+
63+
/* 响应式调整 */
64+
@media screen and (max-width: 768px) {
65+
.version-selector-wrapper {
66+
padding: 8px 10px;
67+
}
68+
}
69+
</style>

docs/en/conf.py

Lines changed: 33 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,34 @@ 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|release/.*)$'
86+
smv_remote_whitelist = r'origin'
87+
smv_tag_whitelist = None
88+
smv_outputdir_format = '{ref.name}'
89+
90+
html_sidebars = {
91+
'**': [
92+
'versions.html',
93+
],
94+
}

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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{# 版本选择器模板 #}
2+
<div class="version-selector-wrapper">
3+
{% if versions %}
4+
<select class="version-select">
5+
{# 遍历所有可用版本 #}
6+
{% for item in versions.branches + versions.tags %}
7+
{# 检查当前页面在其他版本中是否存在 #}
8+
{% if item.name == current_version.name %}
9+
<option value="{{ item.url }}" selected>{{ item.name }}</option>
10+
{% else %}
11+
<option value="{{ item.url }}">{{ item.name }}</option>
12+
{% endif %}
13+
{% endfor %}
14+
</select>
15+
{% endif %}
16+
</div>
17+
18+
<script>
19+
document.addEventListener('DOMContentLoaded', function() {
20+
// 获取logo和搜索框元素
21+
const logo = document.querySelector('.wy-side-nav-search .logo');
22+
const searchBox = document.querySelector('.wy-side-nav-search .wy-form');
23+
24+
if (logo && searchBox) {
25+
const selector = document.querySelector('.version-selector-wrapper');
26+
// 在logo之后、搜索框之前插入
27+
searchBox.parentNode.insertBefore(selector, searchBox);
28+
}
29+
30+
// 添加版本切换事件监听
31+
const select = document.querySelector('.version-select');
32+
if (select) {
33+
select.addEventListener('change', function() {
34+
if (this.value) {
35+
window.location.href = this.value;
36+
}
37+
});
38+
}
39+
});
40+
</script>
41+
42+
<style>
43+
.version-selector-wrapper {
44+
padding: 10px 15px;
45+
background: #f8f8f8;
46+
border-bottom: 1px solid #e7e7e7;
47+
}
48+
49+
.version-select {
50+
width: auto;
51+
min-width: 120px;
52+
max-width: 180px;
53+
margin: 0 auto;
54+
padding: 6px 12px;
55+
border-radius: 4px;
56+
border: 1px solid #d1d1d1;
57+
background: white;
58+
color: #333;
59+
font-size: 13px;
60+
display: block;
61+
}
62+
63+
/* 响应式调整 */
64+
@media screen and (max-width: 768px) {
65+
.version-selector-wrapper {
66+
padding: 8px 10px;
67+
}
68+
}
69+
</style>

0 commit comments

Comments
 (0)