ml-tooling / lazydocs

📖 Generate markdown API documentation from Google-style Python docstring. The lazy alternative to Sphinx.
MIT License
205 stars 38 forks source link

Source Badge URL error #41

Closed agroden closed 1 year ago

agroden commented 2 years ago

Describe the bug:

When generating documentation the source badge creates non-compliant URLs. Specifically, when combining value provided by -src-base-url with the path, it seems to use the OS specific path separator. This results in URLs that look like:

https://src-base-url/path/to/repo\file.py

Expected behaviour:

I would expect this to function correctly on any operating system. Rather than the incorrect URL above, the source badge should use the correct path separator:

https://src-base-url/path/to/repo/file.py

Steps to reproduce the issue:

Run lazydocs on Windows with virtually any configuration. I run it specifying an output path and base url for the source.

Technical details:

Possible Fix:

In generation.py rather than os.path.join I would suggest using urljoin from urllib. Example:

from urllib.parse import urljoin

# ... generation.py contents...

        if append_base and self.src_base_url:
            relative_path = urljoin(self.src_base_url, relative_path)

# ... everything else...

Additional context: https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urljoin

agroden commented 2 years ago

Digging in a bit more I see the relative_path here will need to be sanitized as well since it could be several directories in. Rather than the aforementioned fix, you could get away with a simple sanitation one-liner for any output of the _get_src_path function. This following should work:

    def _get_src_path(self, obj: Any, append_base: bool = True, as_url=False) -> str:
        # ...everything else stays the same
        # change the last line to:
        return "/".join(s.strip("/") for s in relative_path.split(os.path.sep))
github-actions[bot] commented 1 year ago

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 14 days