Skip to content

Commit

Permalink
feat: add conditional rendering (#2547)
Browse files Browse the repository at this point in the history
* feat: add conditional rendering

* Update conditional_render.py

* add value check
  • Loading branch information
randomJoe211 authored Feb 3, 2023
1 parent d9bdfb5 commit 2a45226
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
8 changes: 7 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ plugins:
- spark-connector/*
- 4.deployment-and-installation/5.zone.md
- 4.deployment-and-installation/3.upgrade-nebula-graph/upgrade-nebula-from-200-to-latest.md

# 仅发布包含企业版功能的文档版本时,注释以下页面
# begin
# - 3.ngql-guide/6.functions-and-expressions/17.ES-function.md
Expand All @@ -95,7 +96,8 @@ plugins:
# - nebula-dashboard-ent/4.cluster-operator/operator/scale.md
# - 6.monitor-and-metrics/3.bbox
# end

# comm.begin
# comm.end

# Exclude the file with the following file name.
# - abc.md
Expand Down Expand Up @@ -125,6 +127,10 @@ extra_javascript:

# modify when release:
extra:
# For conditional rendering
# Valid options: enterprise, community, both
# enterprise means this version is for the Enterprise only. And so on in a similar fashion
database_edition: community
# Language selector.
alternate:
- name: English
Expand Down
6 changes: 5 additions & 1 deletion prepare.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
sudo apt update -y
sudo apt install -y python3-pip python3-cffi python3-brotli libpango-1.0-0 libharfbuzz0b libpangoft2-1.0-0 pango1.0-tools


# Install dependencies
pip install --upgrade pip
pip install -r ./requirements.txt

# Render content according to the database_edition in mkdocs.yml
python ./scripts/conditional_render.py
python ./scripts/conditional_yml.py

# zh language
sudo apt install font-manager fonts-noto-cjk language-pack-zh-hans fonts-arphic-ukai fonts-arphic-uming fonts-ipafont-mincho fonts-ipafont-gothic fonts-unfonts-core

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ mkdocs-exclude
mkdocs-redirects
mkdocs-minify-plugin
Markdown==3.3.7
pyyaml
42 changes: 42 additions & 0 deletions scripts/conditional_render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Rules for processing the markdown files in the docs-2.0 directory:
# - If database_edition is enterprise, the content between {{ ent.ent_begin }} and {{ ent.ent_end }} is kept and the content between {{ comm.comm_begin }} and {{ comm.comm_end }} is removed.
# - If database_edition is community, the content between {{ comm.comm_begin }} and {{ comm.comm_end }} is kept and the content between {{ ent.ent_begin }} and {{ ent.ent_end }} is removed.
# - If database_edition is both, both types of content are kept.

import os
import re
import yaml

def process_files(file_path, database_edition):
for root, dirs, files in os.walk(file_path):
for file in files:
if file.endswith('.md'):
file_full_path = os.path.join(root, file)
with open(file_full_path, 'r', encoding='utf-8') as f:
content = f.read()
if database_edition == 'enterprise':
content = re.sub(
r'{{\s*ent\.ent_begin\s*}}(.*?){{\s*ent\.ent_end\s*}}',
'\\1', content, flags=re.DOTALL)
content = re.sub(
r'{{\s*comm\.comm_begin\s*}}(.*?){{\s*comm\.comm_end\s*}}',
'', content, flags=re.DOTALL)
elif database_edition == 'community':
content = re.sub(
r'{{\s*ent\.ent_begin\s*}}(.*?){{\s*ent\.ent_end\s*}}',
'', content, flags=re.DOTALL)
content = re.sub(
r'{{\s*comm\.comm_begin\s*}}(.*?){{\s*comm\.comm_end\s*}}',
'\\1', content, flags=re.DOTALL)
with open(file_full_path, 'w', encoding='utf-8') as f:
f.write(content)

if __name__ == '__main__':
mkdocs_yml_path = 'mkdocs.yml'
with open(mkdocs_yml_path, 'r', encoding='utf-8') as f:
config = yaml.safe_load(f)
database_edition = config.get("extra", {}).get("database_edition", "both")
if database_edition not in ['community', 'enterprise', 'both']:
raise ValueError("Invalid value for database_edition: {}".format(database_edition))
file_path = 'docs-2.0/'
process_files(file_path, database_edition)
47 changes: 47 additions & 0 deletions scripts/conditional_yml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Rules for processing the mkdocs.yml file:
# - When `database_edition` is `community`, keep all content between tags `# ent.begin` and `# ent.end`, and delete all content between tags `# comm.begin` and `# comm.end`.
# - When `database_edition` is `enterprise`, keep all content between `# comm.begin` and `# comm.end`, and delete all content between `# ent.begin` and `# ent.end`
# - When `database_edition` is `both`, delete all content between `# ent.begin` and `# ent.end`, and between `# comm.begin` and `# comm.end`.
# - Always keep the tags.

import os
import re
import yaml

mkdocs_yml_path = 'mkdocs.yml'

def process_mkdocs_yml(mkdocs_yml_path, database_edition):
with open(mkdocs_yml_path, 'r', encoding='utf-8') as f:
content = f.read()
if database_edition == 'enterprise':
content = re.sub(
r'#\s*ent\.begin(.*?)#\s*ent\.end',
'', content, flags=re.DOTALL)
content = re.sub(
r'#\s*comm\.begin(.*?)#\s*comm\.end',
'\\1', content, flags=re.DOTALL)
elif database_edition == 'community':
content = re.sub(
r'#\s*ent\.begin(.*?)#\s*ent\.end',
'\\1', content, flags=re.DOTALL)
content = re.sub(
r'#\s*comm\.begin(.*?)#\s*comm\.end',
'', content, flags=re.DOTALL)
elif database_edition == 'both':
content = re.sub(
r'#\s*ent\.begin(.*?)#\s*ent\.end',
'', content, flags=re.DOTALL)
content = re.sub(
r'#\s*comm\.begin(.*?)#\s*comm\.end',
'', content, flags=re.DOTALL)
with open(mkdocs_yml_path, 'w', encoding='utf-8') as f:
f.write(content)

if __name__ == '__main__':
mkdocs_yml_path = 'mkdocs.yml'
with open(mkdocs_yml_path, 'r', encoding='utf-8') as f:
config = yaml.safe_load(f)
database_edition = config.get("extra", {}).get("database_edition", "both")
if database_edition not in ['community', 'enterprise', 'both']:
raise ValueError("Invalid value for database_edition: {}".format(database_edition))
process_mkdocs_yml(mkdocs_yml_path, database_edition)

0 comments on commit 2a45226

Please sign in to comment.