diff --git a/README.md b/README.md index c8e35d5..90ff22b 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ You can add the following configurations to your `conf.py` file: * `markdown_docinfo`: Adds metadata to the top of each document containing author, copyright, and version. * `markdown_http_base`: If set, all references will link to this prefix address * `markdown_uri_doc_suffix`: If set, all references will link to documents with this suffix. +* `markdown_file_suffix`: Sets the file extension for generated markdown files (default: `.md`). * `markdown_bullet`: Sets the bullet marker. For example, if your `conf.py` file have the following configuration: diff --git a/sphinx_markdown_builder/__init__.py b/sphinx_markdown_builder/__init__.py index faa7df4..7fd15b8 100644 --- a/sphinx_markdown_builder/__init__.py +++ b/sphinx_markdown_builder/__init__.py @@ -12,6 +12,7 @@ def setup(app): app.add_builder(MarkdownBuilder) app.add_config_value("markdown_http_base", "", "html", str) app.add_config_value("markdown_uri_doc_suffix", ".md", "html", str) + app.add_config_value("markdown_file_suffix", ".md", "html", str) app.add_config_value("markdown_anchor_sections", False, "html", bool) app.add_config_value("markdown_anchor_signatures", False, "html", bool) app.add_config_value("markdown_docinfo", False, "html", bool) diff --git a/sphinx_markdown_builder/builder.py b/sphinx_markdown_builder/builder.py index 588a56c..1431f44 100644 --- a/sphinx_markdown_builder/builder.py +++ b/sphinx_markdown_builder/builder.py @@ -53,6 +53,7 @@ def __init__(self, app: Sphinx, env: BuildEnvironment = None): def init(self): self.sec_numbers = {} + self.out_suffix = self.config.markdown_file_suffix def _get_source_mtime(self, doc_name: str): source_name = self.env.doc2path(doc_name) diff --git a/tests/test_builder.py b/tests/test_builder.py index 3283bdc..790892e 100644 --- a/tests/test_builder.py +++ b/tests/test_builder.py @@ -31,6 +31,8 @@ "autodoc_typehints=signature", "-D", "markdown_bullet=-", + "-D", + "markdown_file_suffix=.html.md", ], ] BUILD_PATH_OPTIONS = [BUILD_PATH, os.path.join(BUILD_PATH, "overrides")] @@ -56,8 +58,8 @@ def _chmod_output(build_path: str, apply_func): for root, dirs, files in os.walk(build_path): for file_name in files: - _, ext = os.path.splitext(file_name) - if ext == ".md": + # Check if file ends with .md + if file_name.endswith(".md"): p = Path(root, file_name) p.chmod(apply_func(p.stat().st_mode)) @@ -94,3 +96,32 @@ def test_builder_access_issue(flags: Iterable[str], build_path: str): run_sphinx(build_path, *flags) finally: _chmod_output(build_path, lambda mode: mode | flag) + + +def test_custom_file_suffix(): + """Test that markdown_file_suffix configuration generates files with correct suffix""" + build_path = os.path.join(BUILD_PATH, "test_suffix") + suffix = ".html.md" + + # Clean and build + _rm_build_path(build_path) + run_sphinx(build_path, "-a", "-D", f"markdown_file_suffix={suffix}") + + # Verify files have the correct suffix + markdown_dir = os.path.join(build_path, "markdown") + assert os.path.exists(markdown_dir), f"Markdown output directory not found: {markdown_dir}" + + # Check that at least one file with the custom suffix exists + found_custom_suffix = False + for root, dirs, files in os.walk(markdown_dir): + for file in files: + if file.endswith(suffix): + found_custom_suffix = True + break + if found_custom_suffix: + break + + assert found_custom_suffix, f"No files with suffix '{suffix}' found in {markdown_dir}" + + # Clean up + _rm_build_path(build_path)