From 7e0ab9cf0cb4ea77234d45478e3ea43b889cd243 Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 17:14:56 +0530 Subject: [PATCH 01/16] add markdownlint action --- .github/workflows/main.yaml | 2 +- action.yaml | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index a0d4b99..3b7bf7c 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -10,4 +10,4 @@ jobs: runs-on: ubuntu-latest steps: - name: Markdown Accessibility - uses: sam9111/markdown-accessibility-helper@main + uses: sam9111/markdown-accessibility-helper@test-lint diff --git a/action.yaml b/action.yaml index c916468..ab2674c 100644 --- a/action.yaml +++ b/action.yaml @@ -37,3 +37,9 @@ runs: run: | python ${{ github.action_path }}/alt_text.py ${{inputs.token}} ${{inputs.azure_key}} ${{inputs.azure_endpoint}} ${{inputs.language}} shell: bash + + - uses: DavidAnson/markdownlint-cli2-action@v10 + continue-on-error: true + with: + command: fix + globs: "**/*.md" From 8dad15d877d13927437997d51c9caf1649f32db2 Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 17:39:29 +0530 Subject: [PATCH 02/16] added alt text checker --- action.yaml | 6 +++++ alt_text_checker.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 alt_text_checker.py diff --git a/action.yaml b/action.yaml index ab2674c..b326c6d 100644 --- a/action.yaml +++ b/action.yaml @@ -28,6 +28,12 @@ runs: - name: Install Python uses: actions/setup-python@v2 + - name: Check Alt Text + id: alt_text_checker + run: | + python alt_text_checker.py + shell: bash + - name: Install dependencies run: | pip install -r ${{ github.action_path }}/requirements.txt diff --git a/alt_text_checker.py b/alt_text_checker.py new file mode 100644 index 0000000..b5c923f --- /dev/null +++ b/alt_text_checker.py @@ -0,0 +1,54 @@ +import os +import re +import sys + + +def has_image_without_alt(file_path): + with open(file_path, 'r') as f: + content = f.read() + + # Match all markdown images with empty alt text + matches = re.findall(r'\!\[(.*?)\]\((.*?)\)(?!\(|\w)', content) + + for match in matches: + alt_text = match[0] + if not alt_text: + return True + + # Match all img tags with empty alt attribute + pattern = re.compile(r'', re.S) + result = pattern.findall(content) + + for i in result: + alt = re.findall(r'alt="(.*?)"', i) + if not alt: + return True + + return False + + +if __name__ == '__main__': + if len(sys.argv) > 1: + directory = sys.argv[1] + else: + directory = '.' + + md_files_without_alt = [] + + for filename in os.listdir(directory): + if filename.endswith('.md'): + file_path = os.path.join(directory, filename) + if has_image_without_alt(file_path): + md_files_without_alt.append(filename) + + if md_files_without_alt: + print("The following Markdown files contain images without alt text:") + for filename in md_files_without_alt: + print(filename) + + result = 'true' + else: + print("All Markdown files have alt text for their images.") + result = 'false' + + print(f"::set-output name=result::{result}") From 6d362d204e3a5e869fc79cf09186467c2f9e803e Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 17:43:53 +0530 Subject: [PATCH 03/16] change action path --- action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yaml b/action.yaml index b326c6d..abb5299 100644 --- a/action.yaml +++ b/action.yaml @@ -31,7 +31,7 @@ runs: - name: Check Alt Text id: alt_text_checker run: | - python alt_text_checker.py + python ${{ github.action_path }}/alt_text_checker.py shell: bash - name: Install dependencies From 37f57af6f9f329d316ee8db8093c68ce75176790 Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 18:20:34 +0530 Subject: [PATCH 04/16] use github output instead --- alt_text_checker.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/alt_text_checker.py b/alt_text_checker.py index b5c923f..1960596 100644 --- a/alt_text_checker.py +++ b/alt_text_checker.py @@ -35,20 +35,22 @@ def has_image_without_alt(file_path): md_files_without_alt = [] - for filename in os.listdir(directory): - if filename.endswith('.md'): - file_path = os.path.join(directory, filename) - if has_image_without_alt(file_path): - md_files_without_alt.append(filename) + for root, dirs, files in os.walk(directory): + for filename in files: + if filename.endswith('.md'): + file_path = os.path.join(root, filename) + if has_image_without_alt(file_path): + md_files_without_alt.append(file_path) if md_files_without_alt: print("The following Markdown files contain images without alt text:") - for filename in md_files_without_alt: - print(filename) + for file_path in md_files_without_alt: + print(file_path) result = 'true' else: print("All Markdown files have alt text for their images.") result = 'false' - print(f"::set-output name=result::{result}") + with open(os.environ['GITHUB_OUTPUT'], 'w') as output_file: + output_file.write(f'result={result}\n') From 7a2e0430dca719e5f2130a0ab86ad200559a1f32 Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 18:35:48 +0530 Subject: [PATCH 05/16] add condition --- action.yaml | 2 ++ test.md | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/action.yaml b/action.yaml index abb5299..4d3d6a7 100644 --- a/action.yaml +++ b/action.yaml @@ -35,7 +35,9 @@ runs: shell: bash - name: Install dependencies + if: steps.alt_text_checker.outputs.result == 'true' run: | + echo "Step 2 is running because Step 1 output is 'true'" pip install -r ${{ github.action_path }}/requirements.txt shell: bash diff --git a/test.md b/test.md index 68a3081..a9d2089 100644 --- a/test.md +++ b/test.md @@ -6,7 +6,7 @@ Image from the web: Image from the repo: -![a puppy eating food from a bowl on the floor.](https://github.com/sam9111/markdown-accessibility-helper/blob/main/puppy.jpeg?raw=true) +![](https://github.com/sam9111/markdown-accessibility-helper/blob/main/puppy.jpeg?raw=true) Image declared in HTML: From 0125c4288cce7563cf34e402736a1e771350cc80 Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 18:38:17 +0530 Subject: [PATCH 06/16] add if condition --- action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yaml b/action.yaml index 4d3d6a7..b2f5272 100644 --- a/action.yaml +++ b/action.yaml @@ -37,11 +37,11 @@ runs: - name: Install dependencies if: steps.alt_text_checker.outputs.result == 'true' run: | - echo "Step 2 is running because Step 1 output is 'true'" pip install -r ${{ github.action_path }}/requirements.txt shell: bash - name: Suggest Alt Text + if: steps.alt_text_checker.outputs.result == 'true' run: | python ${{ github.action_path }}/alt_text.py ${{inputs.token}} ${{inputs.azure_key}} ${{inputs.azure_endpoint}} ${{inputs.language}} shell: bash From c4b084de93add81693c86a697a393a3508688a18 Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 18:42:59 +0530 Subject: [PATCH 07/16] debug --- alt_text_checker.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/alt_text_checker.py b/alt_text_checker.py index 1960596..3fbfff4 100644 --- a/alt_text_checker.py +++ b/alt_text_checker.py @@ -9,7 +9,7 @@ def has_image_without_alt(file_path): # Match all markdown images with empty alt text matches = re.findall(r'\!\[(.*?)\]\((.*?)\)(?!\(|\w)', content) - + print(matches) for match in matches: alt_text = match[0] if not alt_text: @@ -39,6 +39,7 @@ def has_image_without_alt(file_path): for filename in files: if filename.endswith('.md'): file_path = os.path.join(root, filename) + print(f'Checking {file_path}') if has_image_without_alt(file_path): md_files_without_alt.append(file_path) From fc347699b1a36801bbb2fdfce3a5306349c93d18 Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 18:51:53 +0530 Subject: [PATCH 08/16] add checks --- alt_text_checker.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/alt_text_checker.py b/alt_text_checker.py index 3fbfff4..d5d9230 100644 --- a/alt_text_checker.py +++ b/alt_text_checker.py @@ -9,7 +9,6 @@ def has_image_without_alt(file_path): # Match all markdown images with empty alt text matches = re.findall(r'\!\[(.*?)\]\((.*?)\)(?!\(|\w)', content) - print(matches) for match in matches: alt_text = match[0] if not alt_text: @@ -28,21 +27,20 @@ def has_image_without_alt(file_path): if __name__ == '__main__': - if len(sys.argv) > 1: - directory = sys.argv[1] - else: - directory = '.' md_files_without_alt = [] - for root, dirs, files in os.walk(directory): + for root, dirs, files in os.walk('.'): + print(f'Checking {root}') for filename in files: + print(f'Checking {filename}') if filename.endswith('.md'): file_path = os.path.join(root, filename) print(f'Checking {file_path}') if has_image_without_alt(file_path): md_files_without_alt.append(file_path) + print(md_files_without_alt) if md_files_without_alt: print("The following Markdown files contain images without alt text:") for file_path in md_files_without_alt: From 5e43d99029f847ac5876cdae40d8d27e578e1e3c Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 18:57:04 +0530 Subject: [PATCH 09/16] check current dir --- alt_text_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alt_text_checker.py b/alt_text_checker.py index d5d9230..0238d4d 100644 --- a/alt_text_checker.py +++ b/alt_text_checker.py @@ -30,7 +30,7 @@ def has_image_without_alt(file_path): md_files_without_alt = [] - for root, dirs, files in os.walk('.'): + for root, dirs, files in os.walk('./'): print(f'Checking {root}') for filename in files: print(f'Checking {filename}') From 8b9efe19e56bba5f694e0f38b70c662a2a5fc800 Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 18:58:44 +0530 Subject: [PATCH 10/16] walk current dir --- alt_text_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alt_text_checker.py b/alt_text_checker.py index 0238d4d..b19ee26 100644 --- a/alt_text_checker.py +++ b/alt_text_checker.py @@ -30,7 +30,7 @@ def has_image_without_alt(file_path): md_files_without_alt = [] - for root, dirs, files in os.walk('./'): + for root, dirs, files in os.walk(os.curdir): print(f'Checking {root}') for filename in files: print(f'Checking {filename}') From ba2b050ed686dda15cb89d53633043bd4e2800db Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 19:07:42 +0530 Subject: [PATCH 11/16] checkout code --- action.yaml | 5 ++++- alt_text_checker.py | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/action.yaml b/action.yaml index b2f5272..35ea4e4 100644 --- a/action.yaml +++ b/action.yaml @@ -25,13 +25,16 @@ inputs: runs: using: "composite" steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Install Python uses: actions/setup-python@v2 - name: Check Alt Text id: alt_text_checker run: | - python ${{ github.action_path }}/alt_text_checker.py + python ${{ github.action_path }}/alt_text_checker.py ${{ github.workspace }} shell: bash - name: Install dependencies diff --git a/alt_text_checker.py b/alt_text_checker.py index b19ee26..9a04849 100644 --- a/alt_text_checker.py +++ b/alt_text_checker.py @@ -28,9 +28,11 @@ def has_image_without_alt(file_path): if __name__ == '__main__': + repo_path = sys.argv[1] + md_files_without_alt = [] - for root, dirs, files in os.walk(os.curdir): + for root, dirs, files in os.walk(repo_path): print(f'Checking {root}') for filename in files: print(f'Checking {filename}') From 5f3d06a1739fc60e994ac31c83c5e65fc99e550e Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 23:26:30 +0530 Subject: [PATCH 12/16] use checkout action --- action.yaml | 7 +------ alt_text.py | 39 +++++++++++---------------------------- 2 files changed, 12 insertions(+), 34 deletions(-) diff --git a/action.yaml b/action.yaml index 35ea4e4..aebee98 100644 --- a/action.yaml +++ b/action.yaml @@ -5,11 +5,6 @@ branding: color: purple inputs: - token: - description: "GitHub token" - required: true - default: ${{ github.token }} - azure_key: description: "Azure Computer Vision API key" required: false @@ -46,7 +41,7 @@ runs: - name: Suggest Alt Text if: steps.alt_text_checker.outputs.result == 'true' run: | - python ${{ github.action_path }}/alt_text.py ${{inputs.token}} ${{inputs.azure_key}} ${{inputs.azure_endpoint}} ${{inputs.language}} + python ${{ github.action_path }}/alt_text.py ${{ github.workspace }} ${{inputs.azure_key}} ${{inputs.azure_endpoint}} ${{inputs.language}} shell: bash - uses: DavidAnson/markdownlint-cli2-action@v10 diff --git a/alt_text.py b/alt_text.py index 7ea47f8..4703a8d 100644 --- a/alt_text.py +++ b/alt_text.py @@ -107,31 +107,14 @@ def update_markdown_file(file_path, azure_subscription_key, azure_endpoint): azure_subscription_key = None azure_endpoint = None - repo = os.environ['GITHUB_REPOSITORY'] - repo_name = repo.split('/')[1] - clone_url = f'https://github.com/{repo}.git' - - branch = "main" - - if os.environ['GITHUB_HEAD_REF']: - branch = os.environ['GITHUB_HEAD_REF'] - - os.system(f"git clone --depth=1 --branch={branch} {clone_url} repo") - os.chdir('repo') - - for filename in os.listdir('.'): - if filename.endswith('.md'): - update_markdown_file( - filename, azure_subscription_key, azure_endpoint) - os.system(f"git add {filename}") - - # Commit and push - - github_username = os.environ['GITHUB_ACTOR'] - os.system( - f'git config --global user.email "{github_username}@users.noreply.github.com"') - os.system(f'git config --global user.name "{github_username}"') - os.system('git commit -m "Suggest alt text for inline images"') - token = sys.argv[1] - os.system( - f"git push {clone_url.replace('https://',f'https://{github_username}:{token}@')} {branch}") + repo_path = sys.argv[1] + + for root, dirs, files in os.walk(repo_path): + print(f'Checking {root}') + for filename in files: + print(f'Checking {filename}') + if filename.endswith('.md'): + file_path = os.path.join(root, filename) + print(f'Checking {file_path}') + update_markdown_file( + filename, azure_subscription_key, azure_endpoint) From 5e926e802278cbc031a06d4f6a20a81d08262e6a Mon Sep 17 00:00:00 2001 From: sam9111 Date: Fri, 19 May 2023 23:41:46 +0530 Subject: [PATCH 13/16] remove prints --- alt_text.py | 3 --- alt_text_checker.py | 4 ---- 2 files changed, 7 deletions(-) diff --git a/alt_text.py b/alt_text.py index 4703a8d..d5c928c 100644 --- a/alt_text.py +++ b/alt_text.py @@ -110,11 +110,8 @@ def update_markdown_file(file_path, azure_subscription_key, azure_endpoint): repo_path = sys.argv[1] for root, dirs, files in os.walk(repo_path): - print(f'Checking {root}') for filename in files: - print(f'Checking {filename}') if filename.endswith('.md'): file_path = os.path.join(root, filename) - print(f'Checking {file_path}') update_markdown_file( filename, azure_subscription_key, azure_endpoint) diff --git a/alt_text_checker.py b/alt_text_checker.py index 9a04849..6b3f8d4 100644 --- a/alt_text_checker.py +++ b/alt_text_checker.py @@ -33,16 +33,12 @@ def has_image_without_alt(file_path): md_files_without_alt = [] for root, dirs, files in os.walk(repo_path): - print(f'Checking {root}') for filename in files: - print(f'Checking {filename}') if filename.endswith('.md'): file_path = os.path.join(root, filename) - print(f'Checking {file_path}') if has_image_without_alt(file_path): md_files_without_alt.append(file_path) - print(md_files_without_alt) if md_files_without_alt: print("The following Markdown files contain images without alt text:") for file_path in md_files_without_alt: From f636045c972f63efac452c411d919f948bbcee48 Mon Sep 17 00:00:00 2001 From: sam9111 Date: Sat, 20 May 2023 00:12:33 +0530 Subject: [PATCH 14/16] push --- action.yaml | 5 +++++ alt_text.py | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/action.yaml b/action.yaml index aebee98..59e234f 100644 --- a/action.yaml +++ b/action.yaml @@ -42,6 +42,11 @@ runs: if: steps.alt_text_checker.outputs.result == 'true' run: | python ${{ github.action_path }}/alt_text.py ${{ github.workspace }} ${{inputs.azure_key}} ${{inputs.azure_endpoint}} ${{inputs.language}} + git config user.name github-actions + git config user.email github-actions@github.com + git add . + git commit -m "Suggest alt text for images" + git push shell: bash - uses: DavidAnson/markdownlint-cli2-action@v10 diff --git a/alt_text.py b/alt_text.py index d5c928c..a276c5b 100644 --- a/alt_text.py +++ b/alt_text.py @@ -44,7 +44,7 @@ def suggest_alt_text(image_url, azure_subscription_key, azure_endpoint, language # Update all markdown files with the suggested alt text if no alt text is provided -def update_markdown_file(file_path, azure_subscription_key, azure_endpoint): +def update_markdown_file(file_path, azure_subscription_key, azure_endpoint, language): with open(file_path, 'r') as f: content = f.read() @@ -114,4 +114,4 @@ def update_markdown_file(file_path, azure_subscription_key, azure_endpoint): if filename.endswith('.md'): file_path = os.path.join(root, filename) update_markdown_file( - filename, azure_subscription_key, azure_endpoint) + filename, azure_subscription_key, azure_endpoint, language) From 1607706d117dda986346bb8c215600f51183dc83 Mon Sep 17 00:00:00 2001 From: github-actions Date: Fri, 19 May 2023 18:48:42 +0000 Subject: [PATCH 15/16] Suggest alt text for images --- test.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.md b/test.md index a9d2089..68a3081 100644 --- a/test.md +++ b/test.md @@ -6,7 +6,7 @@ Image from the web: Image from the repo: -![](https://github.com/sam9111/markdown-accessibility-helper/blob/main/puppy.jpeg?raw=true) +![a puppy eating food from a bowl on the floor.](https://github.com/sam9111/markdown-accessibility-helper/blob/main/puppy.jpeg?raw=true) Image declared in HTML: From c7d92f90b0009ecb800863bec8b9aa0fcd2439be Mon Sep 17 00:00:00 2001 From: sam9111 Date: Sat, 20 May 2023 00:49:12 +0530 Subject: [PATCH 16/16] update path --- alt_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alt_text.py b/alt_text.py index a276c5b..f3adebf 100644 --- a/alt_text.py +++ b/alt_text.py @@ -114,4 +114,4 @@ def update_markdown_file(file_path, azure_subscription_key, azure_endpoint, lang if filename.endswith('.md'): file_path = os.path.join(root, filename) update_markdown_file( - filename, azure_subscription_key, azure_endpoint, language) + file_path, azure_subscription_key, azure_endpoint, language)