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..59e234f 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 @@ -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 github-actions@github.com + 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" diff --git a/alt_text.py b/alt_text.py index 7ea47f8..f3adebf 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() @@ -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) diff --git a/alt_text_checker.py b/alt_text_checker.py new file mode 100644 index 0000000..6b3f8d4 --- /dev/null +++ b/alt_text_checker.py @@ -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'', 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')