Skip to content

Commit

Permalink
リンクチェックCI : URLをコマンドライン引数で指定できるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
faithandbrave committed Oct 16, 2024
1 parent ab36172 commit f1d9bfb
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions .github/workflows/script/link_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ def add_link(origin_link: str):
dest='check_outer_link',
action='store_true',
default=False)
argparser.add_argument("--url",
dest='url',
type=str,
default='')
args = argparser.parse_args()

if not args.check_inner_link and not args.check_outer_link:
Expand All @@ -131,36 +135,40 @@ def add_link(origin_link: str):
found_error = False
current_dir = os.getcwd()
outer_link_dict = dict()
for p in glob.glob("**/*.md", recursive=True):
dirname = os.path.dirname(p)
with open(p) as f:
text = f.read()

inner_links, outer_links = find_all_links(text)
for link in outer_links:
if link in outer_link_dict:
outer_link_dict[link].append(p)
else:
outer_link_dict[link] = [p]

if args.check_inner_link:
for link in inner_links:
rel_link = ""
if link.startswith("/"):
rel_link = os.path.join(current_dir, link.lstrip("/"))
if len(args.url) <= 0:
for p in glob.glob("**/*.md", recursive=True):
dirname = os.path.dirname(p)
with open(p) as f:
text = f.read()

inner_links, outer_links = find_all_links(text)
for link in outer_links:
if link in outer_link_dict:
outer_link_dict[link].append(p)
else:
rel_link = os.path.join(dirname, link)

if link.endswith(".nolink"):
if os.path.exists(rel_link.rstrip(".nolink")):
print("nolinked {} href {} found.".format(p, link.rstrip(".nolink")), file=sys.stderr)
found_error = True
else:
if not os.path.exists(rel_link):
print("{} href {} not found.".format(p, link), file=sys.stderr)
found_error = True
outer_link_dict[link] = [p]

if args.check_inner_link:
for link in inner_links:
rel_link = ""
if link.startswith("/"):
rel_link = os.path.join(current_dir, link.lstrip("/"))
else:
rel_link = os.path.join(dirname, link)

if link.endswith(".nolink"):
if os.path.exists(rel_link.rstrip(".nolink")):
print("nolinked {} href {} found.".format(p, link.rstrip(".nolink")), file=sys.stderr)
found_error = True
else:
if not os.path.exists(rel_link):
print("{} href {} not found.".format(p, link), file=sys.stderr)
found_error = True

if args.check_outer_link:
if len(args.url) > 0:
outer_link_dict[args.url] = ""

for link, from_list in outer_link_dict.items():
exists, reason = check_url(link)
if not exists:
Expand Down

0 comments on commit f1d9bfb

Please sign in to comment.