|
| 1 | +import logging |
| 2 | +import os |
| 3 | + |
| 4 | +from utils import io |
| 5 | +from utils import rendering |
| 6 | + |
| 7 | +README_YAML = 'README.yaml' |
| 8 | +DOCS_DIR = 'docs' |
| 9 | + |
| 10 | + |
| 11 | +class TerraformDocsRenderingError(Exception): |
| 12 | + def __init__(self, message): |
| 13 | + self.message = message |
| 14 | + super().__init__(f"Failed to render README.md. {message}") |
| 15 | + |
| 16 | + |
| 17 | +class AbstractRenderer: |
| 18 | + def _pre_rendering_fixes(self, repo, module_download_dir): |
| 19 | + readme_yaml_file = os.path.join(module_download_dir, README_YAML) |
| 20 | + content = io.read_file_to_string(readme_yaml_file) |
| 21 | + content = rendering.remove_targets_md(content) |
| 22 | + content = rendering.rename_name(repo, content) |
| 23 | + io.save_string_to_file(readme_yaml_file, content) |
| 24 | + |
| 25 | + def _post_rendering_fixes(self, repo, readme_md_file): |
| 26 | + content = io.read_file_to_string(readme_md_file) |
| 27 | + content = rendering.fix_self_non_closing_br_tags(content) |
| 28 | + content = rendering.fix_custom_non_self_closing_tags_in_pre(content) |
| 29 | + content = rendering.fix_github_edit_url(content, repo) |
| 30 | + content = rendering.fix_sidebar_label(content, repo) |
| 31 | + io.save_string_to_file(readme_md_file, content) |
| 32 | + |
| 33 | + def _copy_extra_resources_for_docs(self, module_download_dir, module_docs_dir): |
| 34 | + extra_resources_dir = os.path.join(module_download_dir, DOCS_DIR) |
| 35 | + files = io.get_filenames_in_dir(extra_resources_dir, '*', True) |
| 36 | + |
| 37 | + for file in files: |
| 38 | + if os.path.basename(file).lower().endswith('.md') or os.path.isdir(file): |
| 39 | + continue |
| 40 | + |
| 41 | + dest_file = os.path.join(module_docs_dir, DOCS_DIR, os.path.relpath(file, extra_resources_dir)) |
| 42 | + io.copy_file(file, dest_file) |
| 43 | + |
| 44 | + logging.info(f"Copied extra file: {dest_file}") |
0 commit comments