-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add conditional rendering (#2547)
* feat: add conditional rendering * Update conditional_render.py * add value check
- Loading branch information
1 parent
d9bdfb5
commit 2a45226
Showing
5 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ mkdocs-exclude | |
mkdocs-redirects | ||
mkdocs-minify-plugin | ||
Markdown==3.3.7 | ||
pyyaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |