2016-03-19 13:45:40 +00:00
|
|
|
#!/bin/bash
|
2016-05-27 02:30:56 +00:00
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# Formatting/Migration tool for Spacemacs
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Authors: Eugene Yaremenko and Sylvain Benner
|
2016-05-27 02:18:44 +00:00
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# $1: action name can be `all`, `doc`, `config`
|
2016-05-27 02:30:56 +00:00
|
|
|
# `doc` formats documentation files
|
|
|
|
# `config` format configuration files
|
|
|
|
# `all` performs all actions
|
2016-05-27 02:18:44 +00:00
|
|
|
# $2: if equal to `test` then use test files as input
|
|
|
|
|
|
|
|
# TODO a function to add headers to config.el, packages.el etc...
|
2016-03-19 13:45:40 +00:00
|
|
|
|
|
|
|
if ! [ -d "./.git" ]
|
2016-04-26 03:49:48 +00:00
|
|
|
then
|
|
|
|
echo "Should be executed from the repo root."
|
|
|
|
exit 1
|
2016-03-19 13:45:40 +00:00
|
|
|
fi
|
|
|
|
|
2016-04-06 19:56:16 +00:00
|
|
|
#Use "sed" or "gsed" if avaliable.
|
|
|
|
seder="sed"
|
|
|
|
if hash gsed 2>/dev/null; then
|
2016-04-13 03:24:28 +00:00
|
|
|
seder="gsed"
|
2016-04-06 19:56:16 +00:00
|
|
|
fi
|
2016-03-19 13:45:40 +00:00
|
|
|
|
2016-05-27 02:18:44 +00:00
|
|
|
#Use md5 or md5sum
|
|
|
|
mdfive="md5sum"
|
|
|
|
if hash md5 2>/dev/null; then
|
|
|
|
mdfive="md5"
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
if [[ $2 = "test" ]]
|
2016-04-26 03:49:48 +00:00
|
|
|
then
|
2016-05-27 02:30:56 +00:00
|
|
|
places=("./core/tools/spacefmt/tests")
|
2016-04-26 03:49:48 +00:00
|
|
|
else
|
2016-04-06 19:56:16 +00:00
|
|
|
places=("./doc" "./layers")
|
|
|
|
fi
|
2016-03-19 13:45:40 +00:00
|
|
|
|
2016-04-06 19:56:16 +00:00
|
|
|
for place in "${places[@]}"
|
|
|
|
do :
|
2016-04-26 03:49:48 +00:00
|
|
|
before_md5="foo"
|
|
|
|
after_md5="bar"
|
|
|
|
while ! [ "$before_md5" = "$after_md5" ]
|
|
|
|
do
|
|
|
|
# Calculate md5 of the files in $place before formating
|
2016-05-27 02:18:44 +00:00
|
|
|
before_md5=$(find $place -type f -exec $mdfive {} \; | sort -k 2 | $mdfive)
|
|
|
|
|
|
|
|
if [ $1 = "all" ] || [ $1 == "doc" ]
|
|
|
|
then
|
|
|
|
# Remove trailing delimiters in headlines
|
|
|
|
find $place -name "*.org" -type f -exec $seder -i 's/^\(*\+\s\+.*\)[;,.]$/\1/g' {} \;
|
|
|
|
# Remove trailing spaces
|
|
|
|
find $place -name "*.org" -type f -exec $seder -i 's/[ \t]*$//' {} \;
|
|
|
|
# Remove #+HTML_HEAD_EXTRA: ... readtheorg.css" />
|
|
|
|
find $place -name "*.org" -type f -exec $seder -i '/#+HTML_HEAD_EXTRA.*readtheorg.css.*/d' {} \;
|
|
|
|
# Replace multiply empty lines with a single empty line
|
|
|
|
find $place -name "*.org" -type f -exec $seder -i '/^$/N;/^\n$/D' {} \;
|
|
|
|
# Replace :TOC_4_org: with :TOC_4_gh:
|
|
|
|
find $place -name "*.org" -type f -exec $seder -i 's/:TOC_4_org:/:TOC_4_gh:/' {} \;
|
|
|
|
# apply toc-org
|
2016-05-27 02:30:56 +00:00
|
|
|
find $place -name "*.org" -type f -exec emacs -batch -l ./core/tools/spacefmt/spacefmt.el '{}' -f apply-all \;
|
2016-05-27 02:18:44 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $1 = "all" ] || [ $1 == "config" ]
|
|
|
|
then
|
|
|
|
# migrate packages lists to config.el
|
2016-05-27 02:30:56 +00:00
|
|
|
find $place -name "packages.el" -type f -exec emacs -batch -l ./core/tools/spacefmt/spacefmt.el '{}' -f move-packages-to-config \;
|
2016-05-27 02:18:44 +00:00
|
|
|
fi
|
2016-04-26 03:49:48 +00:00
|
|
|
|
|
|
|
# Calculate md5 of the files in $place after formating
|
2016-05-27 02:18:44 +00:00
|
|
|
after_md5=$(find $place -type f -exec $mdfive {} \; | sort -k 2 | $mdfive)
|
2016-04-26 03:49:48 +00:00
|
|
|
done
|
2016-04-06 19:56:16 +00:00
|
|
|
done
|