Skip to content

Commit

Permalink
Linkify repo URLs (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Jan 30, 2024
2 parents 6eb315c + 23bacec commit 4816978
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,42 @@ run("linky --help")

```console
$ linky --help
usage: linky [-h] [-V] [--no-copy] [--md | --rst] input
usage: linky [-h] [-V] [--no-copy] [-m | -r] input

linkotron: CLI to format GitHub links in a shorter format.

positional arguments:
input text containing GitHub links to shorten
input text containing GitHub links to shorten

options:
-h, --help show this help message and exit
-V, --version show program's version number and exit
--no-copy do not copy output to clipboard
-h, --help show this help message and exit
-V, --version show program's version number and exit
--no-copy do not copy output to clipboard

formatters:
--md output in Markdown
--rst output in reStructuredText
-m, --md, --markdown output in Markdown
-r, --rst, --restructuredtext
output in reStructuredText
```

<!-- [[[end]]] -->

### Linkify a repo

<!-- [[[cog
run("linky https://github.com/python/peps")
]]] -->

```console
$ linky https://github.com/python/peps
Copied! python/peps
```

<!-- [[[end]]] -->

### Linkify an issue

<!-- [[[cog
from linkotron.scripts.run_command import run
run("linky https://github.com/python/peps/issues/1012")
]]] -->

Expand All @@ -81,7 +94,6 @@ Copied! python/peps#1012
### Linkify a pull request

<!-- [[[cog
from linkotron.scripts.run_command import run
run("linky https://github.com/python/peps/pull/2399")
]]] -->

Expand All @@ -95,7 +107,6 @@ Copied! python/peps#2399
### Linkify a commit

<!-- [[[cog
from linkotron.scripts.run_command import run
run("linky https://github.com/hugovk/cpython/commit/28b23555030d58fdb52b74a547cc621c49690de0")
]]] -->

Expand All @@ -109,7 +120,6 @@ Copied! hugovk/cpython#28b2355
### Linkify a comment

<!-- [[[cog
from linkotron.scripts.run_command import run
run("linky https://github.com/python/peps/pull/2399#issuecomment-1063409480")
]]] -->

Expand All @@ -126,7 +136,6 @@ Copied! python/peps#2399 (comment)

<!-- [[[cog
run("linky --md https://github.com/python/peps/pull/2399")
from linkotron.scripts.run_command import run
]]] -->

```console
Expand All @@ -139,7 +148,6 @@ Copied! [python/peps#2399](https://github.com/python/peps/pull/2399)
#### reStructuredText

<!-- [[[cog
from linkotron.scripts.run_command import run
run("linky --rst https://github.com/python/peps/pull/2399")
]]] -->

Expand Down
3 changes: 3 additions & 0 deletions src/linkotron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Patterns:
USERNAME = "[a-zA-Z0-9]+([-_][a-zA-Z0-9]+)*"
REPO = r"[a-zA-Z0-9]+([-_\.][a-zA-Z0-9]+)*[-_\.]?[a-zA-Z0-9]+"

REPO_URL = re.compile(rf"^https://github.com/({USERNAME})/({REPO})/?$")
PR_OR_ISSUE = re.compile(
rf"^https://github.com/({USERNAME})/({REPO})/(pull|issues)/(\d+)/?$"
)
Expand All @@ -44,6 +45,8 @@ class Patterns:
def shorten(line: str, *, formatter: str | None = None) -> str:
"""Shorten GitHub links"""
match m := RegexMatcher(line):
case Patterns.REPO_URL:
short = f"{m[1]}/{m[3]}"
case Patterns.PR_OR_ISSUE:
short = f"{m[1]}/{m[3]}#{m[6]}"
case Patterns.COMMIT:
Expand Down
2 changes: 2 additions & 0 deletions src/linkotron/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def main() -> None:
("rst", "reStructuredText"),
):
format_group.add_argument(
f"-{name[0]}",
f"--{name}",
f"--{help_text.lower()}",
action="store_const",
const=name,
dest="formatter",
Expand Down
1 change: 1 addition & 0 deletions tests/test_linkotron.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@pytest.mark.parametrize(
"link, expected",
[
("https://github.com/python/peps", "python/peps"),
("https://github.com/python/peps/issues/2", "python/peps#2"),
("https://github.com/python/peps/pull/2399", "python/peps#2399"),
(
Expand Down

0 comments on commit 4816978

Please sign in to comment.