|
| 1 | +##################### |
| 2 | +pre-commit-docx-plain |
| 3 | +##################### |
| 4 | + |
| 5 | +**Pre-commit hook for mirroring Word (docx) files into plain text files (using Pandoc).** |
| 6 | + |
| 7 | +This pre-commit hook provides a solution for organizations that manage Word (.docx) documents with Git and GitHub. |
| 8 | +With this hook, whenever a Word document is committed or updated in a Git repository, a plain text version is also created. |
| 9 | +You can use this plain-text mirror to facilitate GitHub Pull Request reviews. |
| 10 | + |
| 11 | +Set up |
| 12 | +====== |
| 13 | + |
| 14 | +At the root of your document's Git repository, add a file named ``.pre-commit-config.yaml`` with the following contents:: |
| 15 | + |
| 16 | + repos: |
| 17 | + - repo: https://github.com/jsickcodes/pre-commit-docx-plain |
| 18 | + rev: 0.1.0 |
| 19 | + hooks: |
| 20 | + - id: docxplain |
| 21 | + |
| 22 | +Next, you'll need to install pre-commit (if you haven't already):: |
| 23 | + |
| 24 | + pip install -U pre-commit |
| 25 | + |
| 26 | +Initialize the pre-commit hooks in the repository itself:: |
| 27 | + |
| 28 | + pre-commit install |
| 29 | + |
| 30 | +If the repository has an existing Word document, it is a good idea to create the mirrored plain text file now:: |
| 31 | + |
| 32 | + pre-commit run --all-files |
| 33 | + |
| 34 | +Commit the plain text (``.txt``) file that is generated. |
| 35 | + |
| 36 | +Local usage |
| 37 | +=========== |
| 38 | + |
| 39 | +If you are contributing to a repository using ``pre-commit-docx-plain``, you will also need to install pre-commit itself and install the pre-commit hooks in your local clone of the repository:: |
| 40 | + |
| 41 | + pre-commit install -U pre-commit |
| 42 | + pre-commit install |
| 43 | + |
| 44 | +Now, when you update and commit changes to the Word file in your repository, pre-commit will run the ``pre-commit-docx-plain`` hook and generate a new or updated mirror of the file in plain text. |
| 45 | +Use ``git add`` to stage the plain text file and try your ``git commit`` again. |
| 46 | +On this second try, the plain text mirror file should be in sync with the Word file, and the commit can go ahead. |
| 47 | + |
| 48 | +Usage with GitHub Actions |
| 49 | +========================= |
| 50 | + |
| 51 | +You can run ``pre-commit-docx-plain`` in GitHub Actions to ensure that the plain-text mirror file is always up-to-date. |
| 52 | +If the repository does not already have a GitHub Actions workflow, create a file with the path ``.github/workflows/ci.yaml`` with the following contents:: |
| 53 | + |
| 54 | + name: CI |
| 55 | + |
| 56 | + "on": [push, pull_request] |
| 57 | + |
| 58 | + jobs: |
| 59 | + pre-commit: |
| 60 | + runs-on: ubuntu-latest |
| 61 | + |
| 62 | + steps: |
| 63 | + - uses: actions/checkout@v2 |
| 64 | + |
| 65 | + - name: Set up Python |
| 66 | + uses: actions/setup-python@v2 |
| 67 | + |
| 68 | + - name: Run pre-commit hooks |
| 69 | + uses: pre-commit/action@v2.0.0 |
| 70 | + |
| 71 | +This workflow will generate a build "failure" if the plain-text mirror file is out of date with the Word file in the repository — as might happen if a contributor did not install pre-commit locally. |
| 72 | + |
| 73 | +Automatically generate the plain text mirror from GitHub Actions |
| 74 | +================================================================ |
| 75 | + |
| 76 | +To avoid complexities related to installing pre-commit, the GitHub Actions workflow can be configured to automatically generate, commit, and push updates to the plain text mirror. |
| 77 | +The ``.github/workflows/ci.yaml`` file:: |
| 78 | + |
| 79 | + name: CI |
| 80 | + |
| 81 | + "on": [push, pull_request] |
| 82 | + |
| 83 | + jobs: |
| 84 | + pre-commit: |
| 85 | + runs-on: ubuntu-latest |
| 86 | + |
| 87 | + steps: |
| 88 | + - uses: actions/checkout@v2 |
| 89 | + with: |
| 90 | + fetch-depth: 0 |
| 91 | + |
| 92 | + - name: Set up Python |
| 93 | + uses: actions/setup-python@v2 |
| 94 | + |
| 95 | + - name: Run pre-commit hooks |
| 96 | + uses: pre-commit/action@v2.0.0 |
| 97 | + with: |
| 98 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 99 | + |
| 100 | +Note that this workflow can only run with private repositories. |
| 101 | +The ``GITHUB_TOKEN`` secret is not available to public forks. |
| 102 | + |
| 103 | +When using this workflow, contributors need to either pull down the plain text file update to their local branch, or be prepared to use a forced push (``git push --force``) because their branch is "behind" the GitHub origin. |
0 commit comments