Markdown-Checker 0.0.5__tar.gz → 0.0.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. markdown-checker-0.0.8/MANIFEST.in +6 -0
  2. {markdown-checker-0.0.5/markdown_checker.egg-info → markdown-checker-0.0.8}/PKG-INFO +6 -4
  3. {markdown-checker-0.0.5 → markdown-checker-0.0.8}/README.md +5 -3
  4. markdown-checker-0.0.8/markdown_checker/check_markdown.py +52 -0
  5. markdown-checker-0.0.8/markdown_checker/generators/md_generator.py +51 -0
  6. markdown-checker-0.0.8/markdown_checker/inputs/__init__.py +0 -0
  7. markdown-checker-0.0.5/markdown_checker/get_input_args/get_input_args.py → markdown-checker-0.0.8/markdown_checker/inputs/input_arguments.py +4 -2
  8. markdown-checker-0.0.8/markdown_checker/links/__init__.py +0 -0
  9. markdown-checker-0.0.8/markdown_checker/links/link_operations.py +100 -0
  10. markdown-checker-0.0.8/markdown_checker/paths/__init__.py +0 -0
  11. markdown-checker-0.0.8/markdown_checker/paths/files_paths_reader.py +42 -0
  12. markdown-checker-0.0.8/markdown_checker/validate_markdown.py +48 -0
  13. {markdown-checker-0.0.5 → markdown-checker-0.0.8/markdown_checker.egg-info}/PKG-INFO +6 -4
  14. markdown-checker-0.0.8/markdown_checker.egg-info/SOURCES.txt +23 -0
  15. {markdown-checker-0.0.5 → markdown-checker-0.0.8}/setup.cfg +1 -1
  16. markdown-checker-0.0.8/tests/test_markdown_checker.py +46 -0
  17. markdown-checker-0.0.5/MANIFEST.in +0 -3
  18. markdown-checker-0.0.5/markdown_checker/markdown_checker.py +0 -190
  19. markdown-checker-0.0.5/markdown_checker/validate_markdown.py +0 -45
  20. markdown-checker-0.0.5/markdown_checker.egg-info/SOURCES.txt +0 -16
  21. {markdown-checker-0.0.5 → markdown-checker-0.0.8}/LICENSE +0 -0
  22. {markdown-checker-0.0.5 → markdown-checker-0.0.8}/markdown_checker/__init__.py +0 -0
  23. {markdown-checker-0.0.5/markdown_checker/get_input_args → markdown-checker-0.0.8/markdown_checker/generators}/__init__.py +0 -0
  24. {markdown-checker-0.0.5 → markdown-checker-0.0.8}/markdown_checker.egg-info/dependency_links.txt +0 -0
  25. {markdown-checker-0.0.5 → markdown-checker-0.0.8}/markdown_checker.egg-info/entry_points.txt +0 -0
  26. {markdown-checker-0.0.5 → markdown-checker-0.0.8}/markdown_checker.egg-info/top_level.txt +0 -0
  27. {markdown-checker-0.0.5 → markdown-checker-0.0.8}/pyproject.toml +0 -0
  28. {markdown-checker-0.0.5 → markdown-checker-0.0.8}/setup.py +0 -0
@@ -0,0 +1,6 @@
1
+ include README.md
2
+ include markdown_checker/inputs/*
3
+ include markdown_checker/paths/*
4
+ include markdown_checker/links/*
5
+ include markdown_checker/generators/*
6
+ include setup.cfg
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: markdown-checker
3
- Version: 0.0.5
3
+ Version: 0.0.8
4
4
  Summary: A markdown validation reporting tool
5
5
  Home-page: https://github.com/john0isaac/markdown-checker
6
6
  Author: John Aziz
@@ -30,9 +30,11 @@ License-File: LICENSE
30
30
  markdown-checker is a markdown validation reporting tool.
31
31
 
32
32
  # How To
33
+
33
34
  1. Run `pip install markdown-checker`.
34
- 2. Run `markdown-checker -d {src} -f {func}`. Replace `{src}` with the directory you want to analyse and {func} with the available functions like check_broken_paths.
35
- 3. The output will be displayed in the terminal.
35
+ 2. Run `markdown-checker -d {src} -f {func} -gu {url}`. Replace `{src}` with the directory you want to analyze, {func} with the available functions like check_broken_paths, {gu} with your contribution guidance full URL.
36
+ 3. The output will be displayed in the terminal and in a `comments.md` file.
36
37
 
37
38
  # Using markdown-checker in GitHub Actions
38
- The tool has been designed to be run within a GitHub workflow using the [action-check-markdown](https://github.com/marketplace/actions/action-check-markdown) GitHub action. The action will automatically post the output of the tool to your GitHub pull request as a comment.
39
+
40
+ The tool has been designed to be run within a GitHub workflow using the [action-check-markdown](https://github.com/marketplace/actions/check-markdown) GitHub action. The action will automatically post the output of the tool to your GitHub pull request as a comment.
@@ -7,9 +7,11 @@
7
7
  markdown-checker is a markdown validation reporting tool.
8
8
 
9
9
  # How To
10
+
10
11
  1. Run `pip install markdown-checker`.
11
- 2. Run `markdown-checker -d {src} -f {func}`. Replace `{src}` with the directory you want to analyse and {func} with the available functions like check_broken_paths.
12
- 3. The output will be displayed in the terminal.
12
+ 2. Run `markdown-checker -d {src} -f {func} -gu {url}`. Replace `{src}` with the directory you want to analyze, {func} with the available functions like check_broken_paths, {gu} with your contribution guidance full URL.
13
+ 3. The output will be displayed in the terminal and in a `comments.md` file.
13
14
 
14
15
  # Using markdown-checker in GitHub Actions
15
- The tool has been designed to be run within a GitHub workflow using the [action-check-markdown](https://github.com/marketplace/actions/action-check-markdown) GitHub action. The action will automatically post the output of the tool to your GitHub pull request as a comment.
16
+
17
+ The tool has been designed to be run within a GitHub workflow using the [action-check-markdown](https://github.com/marketplace/actions/check-markdown) GitHub action. The action will automatically post the output of the tool to your GitHub pull request as a comment.
@@ -0,0 +1,52 @@
1
+ """
2
+ Module providing automatic checks functionality to markdown files
3
+ following some Guidelines
4
+ """
5
+ from markdown_checker.links.link_operations import (
6
+ get_links_from_file,
7
+ get_paths_from_links,
8
+ get_urls_from_links,
9
+ check_paths_exists,
10
+ check_url_locale,
11
+ check_url_tracking
12
+ )
13
+
14
+
15
+ def check_broken_links(file_path : str, link_type : str , check_type: str) -> str:
16
+ """function that checks if urls and hyperlinks are broken
17
+
18
+ Keyword arguments:
19
+ file_path -- a path to text file to check
20
+ link_type -- path or url
21
+ check_type -- broken or tracking or locale
22
+ Return: broken links and associated file path
23
+ """
24
+ all_links = get_links_from_file(file_path)
25
+
26
+ # check if file has links
27
+ if len(all_links) > 0:
28
+ formatted_output = f"|<code>{file_path}</code>|"
29
+ if link_type == "path":
30
+ paths = get_paths_from_links(all_links)
31
+ if check_type == "broken" and len(paths) > 0:
32
+ broken_path = check_paths_exists(file_path, paths)
33
+ if len (broken_path) > 0:
34
+ formatted_output += f'<code>{broken_path}</code>|\n'
35
+ return formatted_output
36
+ elif check_type == "tracking" and len(paths) > 0:
37
+ tracking_id_paths = check_url_tracking(paths)
38
+ if len(tracking_id_paths) > 0:
39
+ formatted_output += f'<code>{tracking_id_paths}</code>|\n'
40
+ return formatted_output
41
+ elif link_type == "url":
42
+ urls = get_urls_from_links(all_links)
43
+ if check_type == "tracking" and len(urls) > 0:
44
+ tracking_id_urls = check_url_tracking(urls)
45
+ if len(tracking_id_urls) > 0:
46
+ formatted_output += f'<code>{tracking_id_urls}</code>|\n'
47
+ return formatted_output
48
+ elif check_type == "locale" and len(urls) > 0:
49
+ country_locale_urls = check_url_locale(urls)
50
+ if len(country_locale_urls) > 0:
51
+ formatted_output += f'<code>{country_locale_urls}</code>|\n'
52
+ return formatted_output
@@ -0,0 +1,51 @@
1
+ CHECK_BROKEN_PATHS = """# Check Broken Paths
2
+
3
+ We have automatically detected the following broken relative paths in your lessons. Review and fix the paths to resolve this issue.
4
+
5
+ Check the file paths and associated broken paths inside them."""
6
+ CHECK_PATHS_TRACKING = """# Check Missing Tracking from Paths
7
+
8
+ We have automatically detected missing tracking id from the following relative paths in your lessons. Review and add tracking to paths to resolve this issue.
9
+
10
+ Check the file paths and associated paths inside them."""
11
+ CHECK_URLS_TRACKING = """# Check Missing Tracking from URLs
12
+
13
+ We have automatically detected missing tracking id from the following URLs in your lessons. Review and add tracking to URLs to resolve this issue.
14
+
15
+ Check the file paths and associated URLs inside them."""
16
+ CHECK_URLS_LOCALE = """# Check Country Locale in URLs
17
+
18
+ We have automatically detected added country locale to URLs in your lessons. Review and remove country specific locale from URLs to resolve this issue.
19
+
20
+ Check the file paths and associated URLs inside them."""
21
+
22
+ def write_md_file(generated_md: str) -> None:
23
+ """Write the formatted output to a markdown file"""
24
+ with open("comment.md", "w", encoding="utf-8") as file:
25
+ file.write(generated_md)
26
+
27
+ def generate_md(formatted_output: str, function_name: str, contributing_guide_url: str) -> None:
28
+ """Generate markdown file based on the formatted output, function name, and contributing guide URL.
29
+
30
+ Args:
31
+ formatted_output (str): The formatted output to be written to the markdown file.
32
+ function_name (str): The name of the function to determine the header for the markdown file.
33
+ contributing_guide_url (str): The URL of the contributing guide to be included in the markdown file.
34
+
35
+ Raises:
36
+ ValueError: If an invalid function name is provided.
37
+
38
+ """
39
+ contributing_guide_line = f" For more details, check our [Contributing Guide]({contributing_guide_url}).\n\n"
40
+ if function_name == "check_broken_paths":
41
+ formatted_output = CHECK_BROKEN_PATHS + contributing_guide_line + formatted_output
42
+ elif function_name == "check_paths_tracking":
43
+ formatted_output = CHECK_PATHS_TRACKING + contributing_guide_line + formatted_output
44
+ elif function_name == "check_urls_tracking":
45
+ formatted_output = CHECK_URLS_TRACKING + contributing_guide_line + formatted_output
46
+ elif function_name == "check_urls_locale":
47
+ formatted_output = CHECK_URLS_LOCALE + contributing_guide_line + formatted_output
48
+ else:
49
+ raise ValueError("Invalid function name")
50
+
51
+ write_md_file(formatted_output)
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env python3
2
1
  """
3
2
  Module providing automatic checks functionality to markdown files
4
3
  following some Guidelines
@@ -34,4 +33,7 @@ def get_input_args() -> None:
34
33
  'check_urls_tracking',
35
34
  'check_urls_locale'])
36
35
 
37
- return parser.parse_args()
36
+ parser.add_argument('-gu', '--guide-url', type = str, default = 'https://github.com/john0isaac/markdown-checker/blob/main/CONTRIBUTING.md',
37
+ help = 'full url of your contributing guide', required=True)
38
+
39
+ return parser.parse_args()
@@ -0,0 +1,100 @@
1
+ import re
2
+ import os
3
+
4
+ def get_links_from_file(file_path: str) -> list:
5
+ """function to get an array of markdown links from a file
6
+ flags markdown links captures the part inside () that comes right after []
7
+ """
8
+ all_links = []
9
+ with open(file_path, 'r', encoding="utf-8") as file:
10
+ data = file.read()
11
+ link_pattern = re.compile(r'\]\((.*?)\)| \)')
12
+ matches = re.finditer(link_pattern, data)
13
+ for matched_group in matches:
14
+ if matched_group.group(1):
15
+ all_links.append(matched_group.group(1))
16
+ return all_links
17
+
18
+ def get_urls_from_links(all_links: list) -> list:
19
+ """function to get an array of urls from a list"""
20
+ urls = []
21
+ url_pattern = re.compile(r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)')
22
+ allowed_list = ['github.com', 'microsoft.com', 'visualstudio.com', 'aka.ms', 'azure.com']
23
+ for link in all_links:
24
+ matches = re.findall(url_pattern, link)
25
+
26
+ if matches and any(allowed in link.lower() for allowed in allowed_list):
27
+ urls.append(link)
28
+ return urls
29
+
30
+ def get_paths_from_links(all_links: list) -> list:
31
+ """function to get relative paths from a list
32
+ flags paths that start with ./ or ../
33
+ """
34
+ paths = []
35
+ path_pattern = re.compile(r'(\.{1,2}\/)+([A-Za-z0-9-]+\/)*(.+\.[A-Za-z]+)')
36
+
37
+ for link in all_links:
38
+ link = link.split(" ")[0]
39
+ matches = re.findall(path_pattern, link)
40
+ if matches:
41
+ paths.append(link)
42
+ return paths
43
+
44
+ def check_paths_exists(file_path : str, paths : list) -> list:
45
+ """function checks if a path exist if not return non existent paths
46
+ flags any relative path that can't be accessed
47
+ """
48
+ broken_path = []
49
+ for path in paths:
50
+ path = re.sub(r'(\?|\&)(WT|wt)\.mc_id=.*', '', path)
51
+ if not os.path.exists(os.path.normpath(os.path.join(os.path.dirname(file_path), path))):
52
+ broken_path.append(path)
53
+ return broken_path
54
+
55
+ def check_url_locale(urls : list) -> list:
56
+ """function checks if a url has country locale
57
+ flags urls that have ==> /en-us/
58
+ """
59
+ country_locale = []
60
+ for url in urls:
61
+ locale_pattern = re.compile(r'\/[a-z]{2}-[a-z]{2}\/')
62
+ matches = re.findall(locale_pattern, url)
63
+ if matches:
64
+ country_locale.append(url)
65
+ return country_locale
66
+
67
+ def check_url_tracking(urls : list) -> list:
68
+ """function checks if a url has tracking id
69
+ flags urls missing ==> (? or &) plus WT.mc_id= or wt.mc_id=
70
+ """
71
+ tracking_id = []
72
+ for url in urls:
73
+ tracking_pattern = re.compile(r'(\?|\&)(WT|wt)\.mc_id=')
74
+ matches = re.findall(tracking_pattern, url)
75
+ if not matches:
76
+ tracking_id.append(url)
77
+ return tracking_id
78
+
79
+ # DEPRECATED
80
+ def get_urls_from_file(file_path: str) -> list:
81
+ """function to get an array of urls from a file"""
82
+ urls = []
83
+ with open(file_path, 'r', encoding="utf-8") as file:
84
+ data = file.read()
85
+ url_pattern = re.compile(r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)')
86
+ matches = re.finditer(url_pattern, data)
87
+ for matched_group in matches:
88
+ urls.append(matched_group.group())
89
+ return urls
90
+
91
+ def get_paths_from_file(file_path: str) -> list:
92
+ """function to get relative paths from a file"""
93
+ paths = []
94
+ with open(file_path, 'r', encoding="utf-8") as file:
95
+ data = file.read()
96
+ path_pattern = re.compile(r'(\.{1,2}\/)+([A-Za-z0-9-]+\/)*([A-Za-z0-9]+\.[A-Za-z]+)')
97
+ matches = re.finditer(path_pattern, data)
98
+ for matched_group in matches:
99
+ paths.append(matched_group.group())
100
+ return paths
@@ -0,0 +1,42 @@
1
+ """
2
+ This module contains a function to get a list of file paths from a root directory and
3
+ its subdirectories, filtered by file extension.
4
+
5
+ The main function in this module is `get_files_paths_list`.
6
+
7
+ Functions:
8
+ - get_files_paths_list(root_path: str, extension: list = None) -> list
9
+ """
10
+
11
+ import os
12
+
13
+ def get_files_paths_list(root_path: str, extensions: list = None) -> list:
14
+ """
15
+ function returns a list of files in a directory and its subdirectories,
16
+ filtered by file extensions.
17
+
18
+ Keyword arguments:
19
+ root_path (str): The root directory from which to start the search.
20
+ extensions (list, optional): A list of file extensions to filter by.
21
+ Defaults to [".md", ".ipynb"].
22
+
23
+ Returns:
24
+ list: A list of file paths that match the given file extensions.
25
+ """
26
+ if extensions is None:
27
+ extensions = [".md", ".ipynb"]
28
+
29
+ sub_folders, files_paths = [], []
30
+
31
+ for f in os.scandir(root_path):
32
+ if f.is_dir():
33
+ sub_folders.append(f.path)
34
+ if f.is_file():
35
+ if os.path.splitext(f.name)[1].lower() in extensions:
36
+ files_paths.append(f.path)
37
+
38
+ for directory in list(sub_folders):
39
+ sf, f = get_files_paths_list(directory, extensions)
40
+ sub_folders.extend(sf)
41
+ files_paths.extend(f)
42
+ return sub_folders, files_paths
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Module providing automatic checks functionality to markdown files
4
+ following some Guidelines
5
+ """
6
+ from markdown_checker.inputs.input_arguments import get_input_args
7
+ from markdown_checker.check_markdown import check_broken_links
8
+ from markdown_checker.paths.files_paths_reader import get_files_paths_list
9
+ from markdown_checker.generators.md_generator import generate_md
10
+
11
+ def main() -> None:
12
+ """Main program get inputs and run checks"""
13
+
14
+ # get input arguments directory, function to run
15
+ in_arg = get_input_args()
16
+
17
+ _, files_paths = get_files_paths_list(in_arg.dir)
18
+
19
+ pass_list = ['./CODE_OF_CONDUCT.md', './CONTRIBUTING.md', './SECURITY.md', '.\CODE_OF_CONDUCT.md', '.\CONTRIBUTING.md', '.\SECURITY.md']
20
+ files_paths = [file_path for file_path in files_paths if file_path not in pass_list]
21
+
22
+ formatted_output = ""
23
+
24
+ # iterate over the files to validate the content
25
+ for file_path in files_paths:
26
+ if "check_broken_paths" in in_arg.func:
27
+ broken_paths = check_broken_links(file_path, "path" , "broken")
28
+ if broken_paths:
29
+ formatted_output += broken_paths
30
+ if "check_paths_tracking" in in_arg.func:
31
+ paths_tracking = check_broken_links(file_path, "path" , "tracking")
32
+ if paths_tracking:
33
+ formatted_output += paths_tracking
34
+ if "check_urls_tracking" in in_arg.func:
35
+ urls_tracking = check_broken_links(file_path, "url" , "tracking")
36
+ if urls_tracking:
37
+ formatted_output += urls_tracking
38
+ if "check_urls_locale" in in_arg.func:
39
+ urls_locale = check_broken_links(file_path, "url" , "locale")
40
+ if urls_locale:
41
+ formatted_output += urls_locale
42
+ if formatted_output != "":
43
+ formatted_output = "| File Full Path | Issues |\n|--------|--------|\n" + formatted_output
44
+ print(formatted_output)
45
+ generate_md(formatted_output, in_arg.func, in_arg.guide_url)
46
+
47
+ if __name__ == "__main__":
48
+ main()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: markdown-checker
3
- Version: 0.0.5
3
+ Version: 0.0.8
4
4
  Summary: A markdown validation reporting tool
5
5
  Home-page: https://github.com/john0isaac/markdown-checker
6
6
  Author: John Aziz
@@ -30,9 +30,11 @@ License-File: LICENSE
30
30
  markdown-checker is a markdown validation reporting tool.
31
31
 
32
32
  # How To
33
+
33
34
  1. Run `pip install markdown-checker`.
34
- 2. Run `markdown-checker -d {src} -f {func}`. Replace `{src}` with the directory you want to analyse and {func} with the available functions like check_broken_paths.
35
- 3. The output will be displayed in the terminal.
35
+ 2. Run `markdown-checker -d {src} -f {func} -gu {url}`. Replace `{src}` with the directory you want to analyze, {func} with the available functions like check_broken_paths, {gu} with your contribution guidance full URL.
36
+ 3. The output will be displayed in the terminal and in a `comments.md` file.
36
37
 
37
38
  # Using markdown-checker in GitHub Actions
38
- The tool has been designed to be run within a GitHub workflow using the [action-check-markdown](https://github.com/marketplace/actions/action-check-markdown) GitHub action. The action will automatically post the output of the tool to your GitHub pull request as a comment.
39
+
40
+ The tool has been designed to be run within a GitHub workflow using the [action-check-markdown](https://github.com/marketplace/actions/check-markdown) GitHub action. The action will automatically post the output of the tool to your GitHub pull request as a comment.
@@ -0,0 +1,23 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ pyproject.toml
5
+ setup.cfg
6
+ setup.py
7
+ markdown_checker/__init__.py
8
+ markdown_checker/check_markdown.py
9
+ markdown_checker/validate_markdown.py
10
+ markdown_checker.egg-info/PKG-INFO
11
+ markdown_checker.egg-info/SOURCES.txt
12
+ markdown_checker.egg-info/dependency_links.txt
13
+ markdown_checker.egg-info/entry_points.txt
14
+ markdown_checker.egg-info/top_level.txt
15
+ markdown_checker/generators/__init__.py
16
+ markdown_checker/generators/md_generator.py
17
+ markdown_checker/inputs/__init__.py
18
+ markdown_checker/inputs/input_arguments.py
19
+ markdown_checker/links/__init__.py
20
+ markdown_checker/links/link_operations.py
21
+ markdown_checker/paths/__init__.py
22
+ markdown_checker/paths/files_paths_reader.py
23
+ tests/test_markdown_checker.py
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = markdown-checker
3
- version = 0.0.5
3
+ version = 0.0.8
4
4
  author = John Aziz
5
5
  author_email = johnaziz269@gmail.com
6
6
  maintainer = John Aziz
@@ -0,0 +1,46 @@
1
+ """
2
+ Module to check paths in markdown files.
3
+ """
4
+
5
+ import subprocess
6
+
7
+ def check_broken_paths():
8
+ """
9
+ Function to check for broken paths.
10
+ """
11
+ print("check_broken_paths was called")
12
+ subprocess.run(["markdown-checker", "-d", "./tests/resources/", "-f", "check_broken_paths", "-gu", "https://github.com/john0isaac/markdown-checker/blob/main/CONTRIBUTING.md"], check=False)
13
+
14
+ def check_paths_tracking():
15
+ """
16
+ Function to check paths tracking.
17
+ """
18
+ print("check_paths_tracking was called")
19
+ subprocess.run(["markdown-checker", "-d", "./tests/resources/", "-f", "check_paths_tracking", "-gu", "https://github.com/john0isaac/markdown-checker/blob/main/CONTRIBUTING.md"], check=False)
20
+
21
+ def check_urls_tracking():
22
+ """
23
+ Function to check URLs tracking.
24
+ """
25
+ print("check_urls_tracking was called")
26
+ subprocess.run(["markdown-checker", "-d", "./tests/resources/", "-f", "check_urls_tracking", "-gu", "https://github.com/john0isaac/markdown-checker/blob/main/CONTRIBUTING.md"], check=False)
27
+
28
+ def check_urls_locale():
29
+ """
30
+ Function to check URLs locale.
31
+ """
32
+ print("check_urls_locale was called")
33
+ subprocess.run(["markdown-checker", "-d", "./tests/resources/", "-f", "check_urls_locale", "-gu", "https://github.com/john0isaac/markdown-checker/blob/main/CONTRIBUTING.md"], check=False)
34
+
35
+ mydict = {1:check_broken_paths, 2:check_paths_tracking, 3:check_urls_tracking, 4:check_urls_locale}
36
+
37
+ try:
38
+ choices = list(map(int,input("Do you want to: \n(1) Run check_broken_paths \n(2) Run check_paths_tracking \n(3) Run check_urls_tracking \n(4) Run check_urls_locale \n").split()))
39
+ except ValueError:
40
+ print("Please input number")
41
+
42
+ for choice in choices:
43
+ if 0 < choice and choice < 5:
44
+ mydict[choice]()
45
+ else:
46
+ print("That is not between 1 and 4! Try again:")
@@ -1,3 +0,0 @@
1
- include README.md
2
- include markdown_checker/get_input_args/*
3
- include setup.cfg
@@ -1,190 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Module providing automatic checks functionality to markdown files
4
- following some Guidelines
5
- """
6
- import os
7
- import re
8
-
9
- # Helper Functions
10
-
11
- def get_lessons_paths(root_path: str) -> dict:
12
- """function to compile a dictionary of directories
13
-
14
- Keyword arguments:
15
- root_path -- directory to go through looking for md files
16
- Return: formatted dictionary with directories as key and an array of files as values
17
- """
18
- lessons = {}
19
- # add root path to the dictionary
20
- lessons[root_path] = []
21
- pass_list = ['CODE_OF_CONDUCT.md', 'CONTRIBUTING.md', 'SECURITY.md']
22
- # get lessons folders
23
- for item in os.listdir(root_path):
24
- if os.path.isdir(os.path.join(root_path, item)):
25
- lessons[item] = []
26
-
27
- # get lesson exercises (md, ipynb files)
28
- for lesson, _ in lessons.items():
29
- if lesson != root_path:
30
- for item in os.listdir(os.path.join(root_path, lesson)):
31
- # check for translations directories
32
- if os.path.isdir(os.path.join(root_path, lesson, item)):
33
- for sub_item in os.listdir(os.path.join(root_path, lesson, item)):
34
- if os.path.isdir(os.path.join(root_path, lesson, item, sub_item)):
35
- for sub_item2 in os.listdir(os.path.join(root_path, lesson, item, sub_item)):
36
- # check for .md and .ipynb files and store them
37
- if sub_item2.lower().endswith(('.md', '.ipynb')) and sub_item2 not in pass_list:
38
- lessons[lesson].append(os.path.join(item, sub_item, sub_item2))
39
- # check for .md and .ipynb files and store them
40
- elif sub_item.lower().endswith(('.md', '.ipynb')) and sub_item not in pass_list:
41
- lessons[lesson].append(os.path.join(item, sub_item))
42
- # check for .md and .ipynb files and store them
43
- elif item.lower().endswith(('.md', '.ipynb')) and item not in pass_list:
44
- lessons[lesson].append(item)
45
- # get .md and .ipynb in root directory
46
- for item in os.listdir(root_path):
47
- if item.lower().endswith(('.md', '.ipynb')) and item not in pass_list:
48
- lessons[root_path].append(item)
49
-
50
- # check to remove folders that don't have .md files in them
51
- lessons = {key: values for key, values in lessons.items() if len(values) > 0}
52
-
53
- return lessons
54
-
55
- def check_broken_links(file_path : str, link_type : str , check_type: str) -> str:
56
- """function that checks if urls and hyperlinks are broken
57
-
58
- Keyword arguments:
59
- file_path -- a path to text file to check
60
- link_type -- path or url
61
- check_type -- broken or tracking or locale
62
- Return: broken links and associated file path
63
- """
64
- all_links = get_links_from_file(file_path)
65
-
66
- # check if file has links
67
- if len(all_links) > 0:
68
- formatted_output = f" FILE '{file_path}'\n"
69
- if link_type == "path":
70
- paths = get_paths_from_links(all_links)
71
- if check_type == "broken" and len(paths) > 0:
72
- broken_path = check_paths_exists(file_path, paths)
73
- if len (broken_path) > 0:
74
- formatted_output += f' has the following broken relative paths {broken_path}\n'
75
- return formatted_output
76
- elif check_type == "tracking" and len(paths) > 0:
77
- tracking_id_paths = check_url_tracking(paths)
78
- if len(tracking_id_paths) > 0:
79
- formatted_output += f' has the following paths with no tracking id {tracking_id_paths}\n'
80
- return formatted_output
81
- elif link_type == "url":
82
- urls = get_urls_from_links(all_links)
83
- if check_type == "tracking" and len(urls) > 0:
84
- tracking_id_urls = check_url_tracking(urls)
85
- if len(tracking_id_urls) > 0:
86
- formatted_output += f' has the following links with no tracking id {tracking_id_urls}\n'
87
- return formatted_output
88
- elif check_type == "locale" and len(urls) > 0:
89
- country_locale_urls = check_url_locale(urls)
90
- if len(country_locale_urls) > 0:
91
- formatted_output += f' has the following links with country locale {country_locale_urls}\n'
92
- return formatted_output
93
-
94
- def get_links_from_file(file_path: str) -> list:
95
- """function to get an array of markdown links from a file
96
- flags markdown links captures the part inside () that comes right after []
97
- """
98
- all_links = []
99
- with open(file_path, 'r', encoding="utf-8") as file:
100
- data = file.read()
101
- link_pattern = re.compile(r'\]\((.*?)\)| \)')
102
- matches = re.finditer(link_pattern, data)
103
- for matched_group in matches:
104
- if matched_group.group(1):
105
- all_links.append(matched_group.group(1))
106
- return all_links
107
-
108
- def get_urls_from_links(all_links: list) -> list:
109
- """function to get an array of urls from a list"""
110
- urls = []
111
- url_pattern = re.compile(r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)')
112
- allowed_list = ['github.com', 'microsoft.com', 'visualstudio.com', 'aka.ms', 'azure.com']
113
- for link in all_links:
114
- matches = re.findall(url_pattern, link)
115
-
116
- if matches and any(allowed in link.lower() for allowed in allowed_list):
117
- urls.append(link)
118
- return urls
119
-
120
- def get_paths_from_links(all_links: list) -> list:
121
- """function to get relative paths from a list
122
- flags paths that start with ./ or ../
123
- """
124
- paths = []
125
- path_pattern = re.compile(r'(\.{1,2}\/)+([A-Za-z0-9-]+\/)*(.+\.[A-Za-z]+)')
126
-
127
- for link in all_links:
128
- link = link.split(" ")[0]
129
- matches = re.findall(path_pattern, link)
130
- if matches:
131
- paths.append(link)
132
- return paths
133
-
134
- def check_paths_exists(file_path : str, paths : list) -> list:
135
- """function checks if a path exist if not return non existent paths
136
- flags any relative path that can't be accessed
137
- """
138
- broken_path = []
139
- for path in paths:
140
- path = re.sub(r'(\?|\&)(WT|wt)\.mc_id=.*', '', path)
141
- if not os.path.exists(os.path.normpath(os.path.join(os.path.dirname(file_path), path))):
142
- broken_path.append(path)
143
- return broken_path
144
-
145
- def check_url_locale(urls : list) -> list:
146
- """function checks if a url has country locale
147
- flags urls that have ==> /en-us/
148
- """
149
- country_locale = []
150
- for url in urls:
151
- locale_pattern = re.compile(r'\/[a-z]{2}-[a-z]{2}\/')
152
- matches = re.findall(locale_pattern, url)
153
- if matches:
154
- country_locale.append(url)
155
- return country_locale
156
-
157
- def check_url_tracking(urls : list) -> list:
158
- """function checks if a url has tracking id
159
- flags urls missing ==> (? or &) plus WT.mc_id= or wt.mc_id=
160
- """
161
- tracking_id = []
162
- for url in urls:
163
- tracking_pattern = re.compile(r'(\?|\&)(WT|wt)\.mc_id=')
164
- matches = re.findall(tracking_pattern, url)
165
- if not matches:
166
- tracking_id.append(url)
167
- return tracking_id
168
-
169
- # DEPRECATED
170
- def get_urls_from_file(file_path: str) -> list:
171
- """function to get an array of urls from a file"""
172
- urls = []
173
- with open(file_path, 'r', encoding="utf-8") as file:
174
- data = file.read()
175
- url_pattern = re.compile(r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)')
176
- matches = re.finditer(url_pattern, data)
177
- for matched_group in matches:
178
- urls.append(matched_group.group())
179
- return urls
180
-
181
- def get_paths_from_file(file_path: str) -> list:
182
- """function to get relative paths from a file"""
183
- paths = []
184
- with open(file_path, 'r', encoding="utf-8") as file:
185
- data = file.read()
186
- path_pattern = re.compile(r'(\.{1,2}\/)+([A-Za-z0-9-]+\/)*([A-Za-z0-9]+\.[A-Za-z]+)')
187
- matches = re.finditer(path_pattern, data)
188
- for matched_group in matches:
189
- paths.append(matched_group.group())
190
- return paths
@@ -1,45 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Module providing automatic checks functionality to markdown files
4
- following some Guidelines
5
- """
6
- import os
7
- from markdown_checker.get_input_args.get_input_args import get_input_args
8
- from markdown_checker.markdown_checker import get_lessons_paths, check_broken_links
9
-
10
- def main() -> None:
11
- """Main program get inputs and run checks"""
12
-
13
- # get input arguments directory, function to run
14
- in_arg = get_input_args()
15
-
16
- lessons = get_lessons_paths(in_arg.dir)
17
-
18
- # iterate over the files to validate the content
19
- for lesson_folder_name, lessons_array in lessons.items():
20
- for lesson_file_name in lessons_array:
21
- file_path = os.path.join(
22
- in_arg.dir,
23
- lesson_folder_name,
24
- lesson_file_name)
25
- if in_arg.dir == lesson_folder_name:
26
- file_path = os.path.join(lesson_folder_name, lesson_file_name)
27
- if "check_broken_paths" in in_arg.func:
28
- formatted_output = check_broken_links(file_path, "path" , "broken")
29
- if formatted_output:
30
- print(formatted_output)
31
- if "check_paths_tracking" in in_arg.func:
32
- formatted_output = check_broken_links(file_path, "path" , "tracking")
33
- if formatted_output:
34
- print(formatted_output)
35
- if "check_urls_tracking" in in_arg.func:
36
- formatted_output = check_broken_links(file_path, "url" , "tracking")
37
- if formatted_output:
38
- print(formatted_output)
39
- if "check_urls_locale" in in_arg.func:
40
- formatted_output = check_broken_links(file_path, "url" , "locale")
41
- if formatted_output:
42
- print(formatted_output)
43
-
44
- if __name__ == "__main__":
45
- main()
@@ -1,16 +0,0 @@
1
- LICENSE
2
- MANIFEST.in
3
- README.md
4
- pyproject.toml
5
- setup.cfg
6
- setup.py
7
- markdown_checker/__init__.py
8
- markdown_checker/markdown_checker.py
9
- markdown_checker/validate_markdown.py
10
- markdown_checker.egg-info/PKG-INFO
11
- markdown_checker.egg-info/SOURCES.txt
12
- markdown_checker.egg-info/dependency_links.txt
13
- markdown_checker.egg-info/entry_points.txt
14
- markdown_checker.egg-info/top_level.txt
15
- markdown_checker/get_input_args/__init__.py
16
- markdown_checker/get_input_args/get_input_args.py