Skip to content

Commit

Permalink
Feature: add support for pre-commit (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpratz authored Mar 27, 2024
1 parent 6ce6d4f commit 62e9a9a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- id: panvimdoc-docker
name: pandoc to vimdoc (docker)
description: Convert markdown documentation to vimdoc (using docker)
entry: /panvimdoc.pre-commit.sh
language: docker
files: ^README\.md$
args:
- '--input-file'
- README.md
pass_filenames: false
- id: panvimdoc
name: pandoc to vimdoc
description: Convert markdown documentation to vimdoc (using local environment)
entry: panvimdoc.pre-commit.sh
language: script
files: ^README\.md$
args:
- '--input-file'
- README.md
pass_filenames: false
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ RUN apk update && apk upgrade && apk add bash vim neovim

# Copies your code file repository to the filesystem
COPY panvimdoc.sh /panvimdoc.sh
COPY panvimdoc.pre-commit.sh /panvimdoc.pre-commit.sh
COPY scripts/ /scripts/
COPY lib/Demojify.lua /usr/share/lua/common/lib/Demojify.lua

# change permission to execute the script and
RUN chmod +x /panvimdoc.sh
RUN chmod +x /panvimdoc.pre-commit.sh

# file to execute when the docker container starts up
ENTRYPOINT ["/panvimdoc.sh"]
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,44 @@ jobs:

</details>

### Using pre-commit
[pre-commit](https://pre-commit.com/) lets you easily install and manage pre-commit hooks locally.

Two hooks are available, differing only in the way dependencies are handled:

- `panvimdoc-docker`: Requires a running Docker engine on your host. All other dependencies will be loaded inside the container.
- `panvimdoc`: Runs in your local environment, so you have to make sure all dependencies of panvimdoc are installed
(i.e., `pandoc v3.0.0` or greater)

To use a hook, first install pre-commit. Then, add the following to your `.pre-commit-config.yaml` (here `panvimdoc-docker` is used):

```yaml
- repo: 'https://github.com/kdheepak/panvimdoc'
rev: v4.0.1
hooks:
- id: panvimdoc-docker
args:
- '--project-name'
- <your-project-name>
```

You can specify additional arguments to `panvimdoc.sh` using `args`. See the section below (or run `./panvimdoc.sh`) for the full list of arguments.

To change the input file, modify the `files` field of the hook and supply the corresponding `--input-file` to `args`. In the example below, the hook will be triggered if any `.md` file changes:

```yaml
- repo: 'https://github.com/kdhee'
rev: v4.0.1
hooks:
- id: panvimdoc-docker
files: ^.*\.md$
args:
- '--project-name'
- <your-project-name>
- '--input-file'
- <your-input-file.md>
```

### Using it manually

The `./panvimdoc.sh` script runs `pandoc` along with all the filters and custom output writer.
Expand Down
32 changes: 32 additions & 0 deletions panvimdoc.pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
args=$# # number of command line args
for (( i=1; i<=$args; i+=2 )) # loop from 1 to N (where N is number of args)
do
j=$((i + 1 ))
if [ "${!i}" == "--project-name" ] ; then
PROJECT_NAME="${!j}"
break;
fi
done

# extract project name to determine file location
if [ -z "$PROJECT_NAME" ]; then
echo "Error: argument --project-name is not set."
exit 1
fi
# store hash to check whether documentation has changed
if [ -f "doc/$PROJECT_NAME.txt" ]; then
DOC_HASH_PRE="$(md5sum doc/$PROJECT_NAME.txt)"
else
DOC_HASH_PRE=""
fi
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source "$SCRIPT_DIR/panvimdoc.sh"
DOC_HASH_POST="$(md5sum doc/$PROJECT_NAME.txt)"
# send FAIL when doc has changed
if [ "$DOC_HASH_PRE" != "$DOC_HASH_POST" ] ; then
echo ""
echo 'Updated documentation "doc/$PROJECT_NAME.txt"'
echo 'Use "git add doc/$PROJECT_NAME.txt" to stage the changes'
exit 1
fi

0 comments on commit 62e9a9a

Please sign in to comment.