Skip to content

Commit 6d27382

Browse files
committed
skip link visibility on cli with status
1 parent 465eaf0 commit 6d27382

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This is simple link status checker for text/markdown files.
2424
- source
2525

2626
```bash
27-
python install . --user
27+
pip install . --user
2828
```
2929

3030
### Usage:
@@ -37,10 +37,13 @@ Usage: linkstatus [OPTIONS] [SOURCE]...
3737
Options:
3838
-r, --recursive Include all files from directories recursively
3939
-t, --timeout INTEGER Request timeout (default 4 second)
40+
-rt, --retry INTEGER Retry link status (default 2 time)
4041
--help Show this message and exit.
4142
```
4243

43-
![linkstatus](https://user-images.githubusercontent.com/11618054/67676030-aafb6400-f9a6-11e9-9d56-c27c21a9e0a8.png)
44+
![linkstatus](https://user-images.githubusercontent.com/11618054/67754970-5a930d80-fa5d-11e9-851c-afd38147cf28.png)
4445

4546

46-
**Note: Skip `link` check for any line by adding `noqa` (no quality assurance) as inline comment.**
47+
48+
**Note: Skip `link` check for any line by adding `noqa` (no quality assurance) as inline comment
49+
.** like `<-- noqa -->` for `html` and `markdown`, `#noqa` for `python` etc...

linkstatus/linkstatus.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,47 @@ def main(source, recursive, timeout, retry):
5858
links = parse_file(f)
5959

6060
if links:
61-
click.echo(click.style("File: {}".format(f), bg="blue", fg="white"))
61+
click.echo(click.style("Links in File: '{}'".format(f), bg="blue", fg="white"))
6262

6363
for link in links:
6464
for url in link.urls:
65-
# try two time at least
66-
for _ in range(int(retry)):
67-
status, code = link_status(url, int(timeout))
68-
if status is True:
69-
break
70-
71-
if status:
72-
fg = "green"
73-
icon = "✓"
65+
if link.skip:
66+
click.echo(
67+
"{icon} L{ln} : {url} (skip)".format(
68+
icon=click.style("…", fg="blue", bold=True),
69+
ln=link.line,
70+
url=click.style(url, fg="blue"),
71+
)
72+
)
7473
else:
75-
fg = "red"
76-
icon = "✗"
77-
exit_code = 1
78-
79-
click.echo(
80-
"{icon} L{ln} : {url} {code}".format(
81-
icon=click.style(icon, fg=fg, bold=True),
82-
ln=link.line,
83-
url=click.style(url, fg=fg),
84-
code="" if code == 200 else "({})".format(code),
74+
# retry to take status default 2 time
75+
for _ in range(int(retry)):
76+
status, code = link_status(url, int(timeout))
77+
if status is True:
78+
break
79+
80+
if status:
81+
fg = "green"
82+
icon = "✓"
83+
else:
84+
fg = "red"
85+
icon = "✗"
86+
exit_code = 1
87+
88+
click.echo(
89+
"{icon} L{ln} : {url} {code}".format(
90+
icon=click.style(icon, fg=fg, bold=True),
91+
ln=link.line,
92+
url=click.style(url, fg=fg),
93+
code="" if code == 200 else "({})".format(code),
94+
)
8595
)
86-
)
96+
8797
if exit_code == 1:
8898
click.echo(
8999
click.style(
90-
"Note: Use `noqa` inline comment to skip link check. "
91-
"eg. response code 403 due to header restrictions etc...",
100+
"Warning: Use `noqa` inline comment to skip link check. "
101+
"like, response code 403 due to header restrictions etc...",
92102
fg="red",
93103
bold=True,
94104
)

linkstatus/parser.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
REGULAR_EXP = r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
88

9-
LINKS = namedtuple("LINKS", ["line", "urls"])
9+
LINKS = namedtuple("LINKS", ["line", "urls", "skip"])
1010

1111

1212
def parse_line(line):
@@ -18,17 +18,12 @@ def parse_line(line):
1818
list of links
1919
"""
2020
string = line.strip()
21+
html_format = markdown.markdown(string, output_format="html")
22+
links = re.findall(REGULAR_EXP, html_format)
2123

22-
# if `noqa` (no quality assurance) marked in line then just skip that string
23-
if "noqa" not in string:
24-
html_format = markdown.markdown(string, output_format="html")
25-
links = re.findall(REGULAR_EXP, html_format)
26-
27-
# TODO: Improve regex to remove this workaround for trailing </p> or </li>
28-
links = [l.replace("</p>", "").replace("</li>", "").replace("</a>", "") for l in links]
29-
return links
30-
else:
31-
return []
24+
# TODO: Improve regex to remove this workaround for trailing </p> or </li>
25+
links = [l.replace("</p>", "").replace("</li>", "").replace("</a>", "") for l in links]
26+
return links
3227

3328

3429
def parse_file(file_path):
@@ -43,5 +38,6 @@ def parse_file(file_path):
4338
for line_number, line in enumerate(f):
4439
line_links = parse_line(line)
4540
if line_links:
46-
links.append(LINKS(line=line_number + 1, urls=line_links))
41+
skip = True if "noqa" in line else False
42+
links.append(LINKS(line=line_number + 1, urls=line_links, skip=skip))
4743
return links

0 commit comments

Comments
 (0)