rswiki-book/category.sh
2015-06-13 00:04:22 -04:00

52 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
cd "$(dirname "$0")"
function prepareFile(){
path="$1"
tmp_path="$(basename "$path").tmp"
(
grep 'CODE AUTOMATICALLY GENERATED BY category.sh -->' "$path" &>/dev/null
if [ $? -eq 0 ]
then
sed -n '/CODE AUTOMATICALLY GENERATED BY category.sh -->/q;p' "$path"
else
cat "$path" && echo
fi
echo -e '<!-- DO NOT EDIT BELOW THIS LINE, OR CHANGE THIS COMMENT, CODE AUTOMATICALLY GENERATED BY category.sh -->'
) > "$tmp_path"
echo "$tmp_path"
}
function finishFile(){
path="$1"
tmp_path="$(basename "$path").tmp"
mv "$tmp_path" "$path"
}
files_lines_categories="$(grep '^\[\[Category [^]./\]*\]\]$' *)"
#echo "files_lines_categories: $files_lines_categories"
echo '<!-- DO NOT EDIT THIS FILE, CODE AUTOMATICALLY GENERATED BY category.sh -->' > Categories.mediawiki
echo 'The following categories contain pages or media.' >> Categories.mediawiki
echo "$files_lines_categories" | grep -o 'Category [^]]*' | sort | uniq | while read category
do
#echo "category: $category"
category_file="$(echo "$category" | sed 's/ /-/g').mediawiki"
echo "category_file: $category_file"
result="$(prepareFile "$category_file")"
echo "== '''Pages in category \"$(echo $category | sed 's/Category //')\"''' ==" >> "$result"
files="$(echo "$files_lines_categories" | grep ":\[\[${category}\]\]$" | sort)"
num_pages="$(echo "$files" | wc -l)"
echo -e "* [[$category]] ($num_pages members)" >> Categories.mediawiki
echo "The following $num_pages pages are in this category." >> "$result"
echo "$files" | while read file
do
echo "* [[$(echo $file | sed -e "s/\.mediawiki:\[\[${category}\]\]$//" -e 's/-/ /g')]]"
done >> "$result"
finishFile "$category_file"
done