Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2dot5
ClickHouse
提交
fd766a35
C
ClickHouse
项目概览
2dot5
/
ClickHouse
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
ClickHouse
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fd766a35
编写于
3月 14, 2021
作者:
A
Alexey Milovidov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Better than nothing
上级
f6179765
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
50 addition
and
39 deletion
+50
-39
docs/tools/single_page.py
docs/tools/single_page.py
+50
-30
docs/tools/util.py
docs/tools/util.py
+0
-9
未找到文件。
docs/tools/single_page.py
浏览文件 @
fd766a35
...
...
@@ -24,55 +24,71 @@ def recursive_values(item):
yield
item
anchor_not_allowed_chars
=
re
.
compile
(
r
'[^\w\-]'
)
def
generate_anchor_from_path
(
path
):
return
re
.
sub
(
anchor_not_allowed_chars
,
'-'
,
path
)
def
replace_link
(
match
,
path
):
link
=
match
.
group
(
1
)
if
link
.
endswith
(
'/'
):
link
=
link
[
0
:
-
1
]
+
'.md'
return
'(#{})'
.
format
(
generate_anchor_from_path
(
os
.
path
.
normpath
(
os
.
path
.
join
(
os
.
path
.
dirname
(
path
),
link
))))
# Concatenates Markdown files to a single file.
def
concatenate
(
lang
,
docs_path
,
single_page_file
,
nav
):
lang_path
=
os
.
path
.
join
(
docs_path
,
lang
)
az_re
=
re
.
compile
(
r
'[a-z]'
)
proj_config
=
f
'
{
docs_path
}
/toc_
{
lang
}
.yml'
if
os
.
path
.
exists
(
proj_config
):
with
open
(
proj_config
)
as
cfg_file
:
nav
=
yaml
.
full_load
(
cfg_file
.
read
())[
'nav'
]
files_to_concatenate
=
list
(
recursive_values
(
nav
))
files_count
=
len
(
files_to_concatenate
)
logging
.
info
(
f
'
{
files_count
}
files will be concatenated into single md-file for
{
lang
}
.'
)
logging
.
debug
(
'Concatenating: '
+
', '
.
join
(
files_to_concatenate
))
assert
files_count
>
0
,
f
'Empty single-page for
{
lang
}
'
# (../anything) or (../anything#anchor) or (xyz-abc.md) or (xyz-abc.md#anchor)
relative_link_regexp
=
re
.
compile
(
r
'\((\.\./[^)#]+|[\w\-]+\.md)(?:#[^\)]*)?\)'
)
for
path
in
files_to_concatenate
:
if
path
.
endswith
(
'introduction/info.md'
):
continue
try
:
with
open
(
os
.
path
.
join
(
lang_path
,
path
))
as
f
:
anchors
=
set
()
tmp_path
=
path
.
replace
(
'/index.md'
,
'/'
).
replace
(
'.md'
,
'/'
)
prefixes
=
[
''
,
'../'
,
'../../'
,
'../../../'
]
parts
=
tmp_path
.
split
(
'/'
)
anchors
.
add
(
parts
[
-
2
]
+
'/'
)
anchors
.
add
(
'/'
.
join
(
parts
[
1
:]))
for
part
in
parts
[
0
:
-
2
]
if
len
(
parts
)
>
2
else
parts
:
for
prefix
in
prefixes
:
anchor
=
prefix
+
tmp_path
if
anchor
:
anchors
.
add
(
anchor
)
anchors
.
add
(
'../'
+
anchor
)
anchors
.
add
(
'../../'
+
anchor
)
tmp_path
=
tmp_path
.
replace
(
part
,
'..'
)
for
anchor
in
anchors
:
if
re
.
search
(
az_re
,
anchor
):
single_page_file
.
write
(
'<a name="%s"></a>'
%
anchor
)
single_page_file
.
write
(
'
\n
'
)
# Insert a horizontal ruler. Then insert an anchor that we will link to. Its name will be a path to the .md file.
single_page_file
.
write
(
'
\n
______
\n
<a name="%s"></a>
\n
'
%
generate_anchor_from_path
(
path
))
in_metadata
=
False
for
l
in
f
:
if
l
.
startswith
(
'---'
):
for
line
in
f
:
# Skip YAML metadata.
if
line
==
'---
\n
'
:
in_metadata
=
not
in_metadata
if
l
.
startswith
(
'#'
):
l
=
'#'
+
l
continue
if
not
in_metadata
:
single_page_file
.
write
(
l
)
# Increase the level of headers.
if
line
.
startswith
(
'#'
):
line
=
'#'
+
line
# Replace links within the docs.
if
re
.
search
(
relative_link_regexp
,
line
):
line
=
re
.
sub
(
relative_link_regexp
,
lambda
match
:
replace_link
(
match
,
path
),
line
)
# If failed to replace the relative link, print to log
if
'../'
in
line
:
logging
.
info
(
'Failed to resolve relative link:'
)
logging
.
info
(
path
)
logging
.
info
(
line
)
single_page_file
.
write
(
line
)
except
IOError
as
e
:
logging
.
warning
(
str
(
e
))
...
...
@@ -86,7 +102,7 @@ def build_single_page_version(lang, args, nav, cfg):
extra
[
'single_page'
]
=
True
extra
[
'is_amp'
]
=
False
with
util
.
autoremoved_file
(
os
.
path
.
join
(
args
.
docs_dir
,
lang
,
'single.md'
)
)
as
single_md
:
with
open
(
os
.
path
.
join
(
args
.
docs_dir
,
lang
,
'single.md'
),
'w'
)
as
single_md
:
concatenate
(
lang
,
args
.
docs_dir
,
single_md
,
nav
)
with
util
.
temp_dir
()
as
site_temp
:
...
...
@@ -123,11 +139,14 @@ def build_single_page_version(lang, args, nav, cfg):
single_page_index_html
=
os
.
path
.
join
(
single_page_output_path
,
'index.html'
)
single_page_content_js
=
os
.
path
.
join
(
single_page_output_path
,
'content.js'
)
with
open
(
single_page_index_html
,
'r'
)
as
f
:
sp_prefix
,
sp_js
,
sp_suffix
=
f
.
read
().
split
(
'<!-- BREAK -->'
)
with
open
(
single_page_index_html
,
'w'
)
as
f
:
f
.
write
(
sp_prefix
)
f
.
write
(
sp_suffix
)
with
open
(
single_page_content_js
,
'w'
)
as
f
:
if
args
.
minify
:
import
jsmin
...
...
@@ -151,6 +170,7 @@ def build_single_page_version(lang, args, nav, cfg):
js_in
=
' '
.
join
(
website
.
get_js_in
(
args
))
subprocess
.
check_call
(
f
'cat
{
css_in
}
>
{
test_dir
}
/css/base.css'
,
shell
=
True
)
subprocess
.
check_call
(
f
'cat
{
js_in
}
>
{
test_dir
}
/js/base.js'
,
shell
=
True
)
if
args
.
save_raw_single_page
:
shutil
.
copytree
(
test_dir
,
args
.
save_raw_single_page
)
...
...
docs/tools/util.py
浏览文件 @
fd766a35
...
...
@@ -22,15 +22,6 @@ def temp_dir():
shutil
.
rmtree
(
path
)
@
contextlib
.
contextmanager
def
autoremoved_file
(
path
):
try
:
with
open
(
path
,
'w'
)
as
handle
:
yield
handle
finally
:
os
.
unlink
(
path
)
@
contextlib
.
contextmanager
def
cd
(
new_cwd
):
old_cwd
=
os
.
getcwd
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录