Skip to content

Commit

Permalink
Merge pull request #2 from sam9111/test-lint
Browse files Browse the repository at this point in the history
Test lint
  • Loading branch information
sam9111 authored May 19, 2023
2 parents 99a1d8d + 0b453b8 commit 06bf02f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 23 additions & 6 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,15 +20,37 @@ 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 ${{ github.workspace }}
shell: bash

- name: Install dependencies
if: steps.alt_text_checker.outputs.result == 'true'
run: |
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}}
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 [email protected]
git add .
git commit -m "Suggest alt text for images"
git push
shell: bash

- uses: DavidAnson/markdownlint-cli2-action@v10
continue-on-error: true
with:
command: fix
globs: "**/*.md"
36 changes: 8 additions & 28 deletions alt_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -107,31 +107,11 @@ 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'
repo_path = sys.argv[1]

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}")
for root, dirs, files in os.walk(repo_path):
for filename in files:
if filename.endswith('.md'):
file_path = os.path.join(root, filename)
update_markdown_file(
file_path, azure_subscription_key, azure_endpoint, language)
53 changes: 53 additions & 0 deletions alt_text_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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'<img.*?>', 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__':

repo_path = sys.argv[1]

md_files_without_alt = []

for root, dirs, files in os.walk(repo_path):
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 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'

with open(os.environ['GITHUB_OUTPUT'], 'w') as output_file:
output_file.write(f'result={result}\n')

0 comments on commit 06bf02f

Please sign in to comment.