Skip to content

Commit

Permalink
Allow overriding source code path
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Mar 18, 2022
1 parent 65c8195 commit 3c53ad5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/handsdown/cli_parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CLINamespace():
include: Iterable[str],
exclude: Iterable[str],
source_code_url: str,
source_code_path: str,
source_code_path: Path,
branch: str,
project_name: str,
files: Iterable[Path],
Expand Down
12 changes: 7 additions & 5 deletions docs/handsdown/generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Generator():
loader: Optional[Loader] = None,
raise_errors: bool = False,
source_code_url: Optional[str] = None,
source_code_path: Optional[Path] = None,
toc_depth: int = 1,
encoding: str = ENCODING,
) -> None:
Expand All @@ -46,6 +47,7 @@ Main documentation generator.
- `raise_errors` - Raise `LoaderError` instead of silencing in.
- `source_code_url` - URL to source files to use instead of relative paths,
useful for [GitHub Pages](https://pages.github.com/).
- `source_code_path` - Path to local source code
- `toc_depth` - Maximum depth of child modules ToC
- `encoding` - File encoding

Expand All @@ -65,7 +67,7 @@ Main documentation generator.

### Generator().cleanup_old_docs

[[find in source code]](https://github.com/vemel/handsdown/blob/main/handsdown/generator.py#L149)
[[find in source code]](https://github.com/vemel/handsdown/blob/main/handsdown/generator.py#L152)

```python
def cleanup_old_docs() -> None:
Expand All @@ -75,7 +77,7 @@ Remove old docs generated for this module.

### Generator().generate_doc

[[find in source code]](https://github.com/vemel/handsdown/blob/main/handsdown/generator.py#L185)
[[find in source code]](https://github.com/vemel/handsdown/blob/main/handsdown/generator.py#L188)

```python
def generate_doc(source_path: Path) -> None:
Expand All @@ -93,7 +95,7 @@ Generate one module doc at once.

### Generator().generate_docs

[[find in source code]](https://github.com/vemel/handsdown/blob/main/handsdown/generator.py#L309)
[[find in source code]](https://github.com/vemel/handsdown/blob/main/handsdown/generator.py#L313)

```python
def generate_docs() -> None:
Expand All @@ -103,7 +105,7 @@ Generate all doc files at once.

### Generator().generate_index

[[find in source code]](https://github.com/vemel/handsdown/blob/main/handsdown/generator.py#L322)
[[find in source code]](https://github.com/vemel/handsdown/blob/main/handsdown/generator.py#L326)

```python
def generate_index() -> None:
Expand All @@ -115,7 +117,7 @@ Also `Modules` section that contains a Tree of all modules in the project.

### Generator().generate_modules

[[find in source code]](https://github.com/vemel/handsdown/blob/main/handsdown/generator.py#L348)
[[find in source code]](https://github.com/vemel/handsdown/blob/main/handsdown/generator.py#L352)

```python
def generate_modules() -> None:
Expand Down
10 changes: 5 additions & 5 deletions handsdown/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(
include: Iterable[str],
exclude: Iterable[str],
source_code_url: str,
source_code_path: str,
source_code_path: Path,
branch: str,
project_name: str,
files: Iterable[Path],
Expand Down Expand Up @@ -64,8 +64,8 @@ def get_source_code_url(self) -> str:
if self.branch:
result = f"{result}/blob/{self.branch}"

if self.source_code_path:
result = f"{result}/{self.source_code_path}".rstrip("/")
if self.source_code_path != Path():
result = f"{result}/{self.source_code_path.as_posix()}".rstrip("/")

result = urlunparse(urlparse(result.rstrip("/")))
return f"{result}/"
Expand Down Expand Up @@ -211,8 +211,8 @@ def parse_args(args: Iterable[str]) -> CLINamespace:
help="Path to source code in the project.",
dest="source_code_path",
metavar="REPO_PATH",
default="",
type=str,
default=Path(),
type=Path,
)
parser.add_argument(
"--branch",
Expand Down
6 changes: 5 additions & 1 deletion handsdown/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Generator:
raise_errors -- Raise `LoaderError` instead of silencing in.
source_code_url -- URL to source files to use instead of relative paths,
useful for [GitHub Pages](https://pages.github.com/).
source_code_path -- Path to local source code
toc_depth -- Maximum depth of child modules ToC
encoding -- File encoding
"""
Expand Down Expand Up @@ -71,6 +72,7 @@ def __init__(
loader: Optional[Loader] = None,
raise_errors: bool = False,
source_code_url: Optional[str] = None,
source_code_path: Optional[Path] = None,
toc_depth: int = 1,
encoding: str = ENCODING,
) -> None:
Expand All @@ -80,6 +82,7 @@ def __init__(
self._project_name = project_name or make_title(input_path.name)
self._root_path_finder = PathFinder(self._root_path)
self._source_code_url = source_code_url
self._source_code_path: Path = source_code_path or Path()
self._toc_depth = toc_depth
self._raise_errors = raise_errors
self._encoding = encoding
Expand Down Expand Up @@ -208,7 +211,8 @@ def generate_doc(self, source_path: Path) -> None:

def _get_source_code_url(self, module_record: ModuleRecord, md_document: MDDocument) -> str:
if not self._source_code_url:
return md_document.path_finder.relative(module_record.source_path).as_posix()
relative_path = md_document.path_finder.relative(module_record.source_path)
return (self._source_code_path / relative_path).as_posix()

relative_path_str = self._root_path_finder.relative(module_record.source_path).as_posix()
return f"{self._source_code_url}{relative_path_str}"
Expand Down
1 change: 1 addition & 0 deletions handsdown/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def main() -> None:
source_paths=path_finder.glob(SOURCES_GLOB),
raise_errors=args.panic,
source_code_url=args.get_source_code_url(),
source_code_path=args.source_code_path,
toc_depth=args.toc_depth,
encoding=args.encoding,
)
Expand Down
1 change: 1 addition & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_main(self, generator_mock, path_finder_mock, _get_logger_mock):
project_name="Handsdown",
raise_errors=False,
source_code_url="",
source_code_path=Path(),
source_paths=path_finder_mock().exclude().include().glob(),
toc_depth=1,
encoding="utf-8",
Expand Down

0 comments on commit 3c53ad5

Please sign in to comment.