Commit f9f1c238 authored by Felix Hilsky's avatar Felix Hilsky
Browse files

build scripts to have proper math in markdown

a pre-commit hook that checks if $ $ without ` exists and a script that introduces the ` where necessary
parent 8d2cd338
#!/bin/bash
set -e
# If we have a STDIN, use it, otherwise get one
if tty >/dev/null 2>&1; then
TTY=$(tty)
else
TTY=/dev/tty
fi
IFS=$'\n'
# http://djm.me/ask
ask() {
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "$1 [$prompt] "
# Read the answer
read REPLY < "$TTY"
# Default?
if [ -z "$REPLY" ]; then
REPLY=$default
fi
# Check if the reply is valid
case "$REPLY" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
check_file() {
local file="$1"
local match_pattern="$2"
local file_changes_with_context=$(git diff -U999999999 -p --cached --color=always -- "${file}")
# From the diff, get the green lines starting with '+' and including '$match_pattern'
local matched_additions=$(echo "$file_changes_with_context" | grep -C1 $'^\e\\[32m\+.*'"$match_pattern")
if [ -n "$matched_additions" ]; then
echo -e "\n$file additions match '$match_pattern':\n"
for matched_line in $matched_additions
do
echo "$matched_line"
done
if ask "Include this in your commit?"; then
echo 'Including'
else
echo "Not committing, because $file matches $match_pattern"
exit 1
fi
fi
}
# Actual hook logic:
for file in $(git diff --cached -p --name-status | cut -c3-); do
case "${file}" in
*.md) echo "check file ${file}"
check_file "${file}" '[^`]\$[^$`]*\$' ;;
esac
done
exit
#! /usr/bin/env sh
# gitlab marks math with $` `$. I often forget the backticks
# so this script should add them
for i in $(find .maindir/notes -type f -name "*.md")
# -type f means regular file. Without it, the symbolic links are changed to regular files by sed
do
sed -i 's#\([^`]\|^\)\$\([^$`]\+\)\$#\1\$`\2`\$#g' "${i}"
# \( # the symbol in front of the math is captured (-> \1)
# [^`] # if it is `, then we are looking at text behind a math env, so it should not be matched
# |^ # but it is OK if there is nothing but the beginning of the line
# \)
# \$ # without \, the $ is a special symbol for the beginning of a line
# \( # the actual math content needs to be captured
# [^$`]\+ # sed regexp is greedy, taking the end of the math until the end of the last math in the line if we didn't forbid $ and ` (if the content has `, then it's already fine)
# \)
# \$ # the closing $
done
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment