iredmail-doc/convert.sh

150 lines
4.6 KiB
Bash
Raw Normal View History

2014-09-15 23:24:10 -05:00
#!/usr/bin/env bash
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Convert markdown articles to HTML files.
PWD="."
2014-12-06 07:11:37 -06:00
# Directory used to store converted html files.
OUTPUT_DIR="${PWD}/html"
2014-12-06 07:11:37 -06:00
# Markdown file used to store index of chapters/articles.
INDEX_MD="${OUTPUT_DIR}/index.md"
2014-12-06 07:11:37 -06:00
[ -d ${OUTPUT_DIR} ] || mkdir -p ${OUTPUT_DIR}
CONVERTER="${PWD}/tools/markdown2html.py"
CMD_CONVERT="python ${CONVERTER}"
2014-09-18 01:10:35 -05:00
#CMD_CONVERT=":"
2015-01-07 09:12:18 -06:00
CHANGED_FILES="$(hg st | grep '\.md$')"
TODAY="$(date +%Y-%m-%d)"
2014-09-15 23:34:01 -05:00
strip_name_prefix()
{
name="${1}"
name="$(echo ${name/#\.\//})" # ./filename
name="$(echo ${name/#[0-9][0-9][0-9]-/})" # nnn-
name="$(echo ${name/#[0-9][0-9]-/})" # nn-
name="$(echo ${name/#[0-9]-/})" # n-
echo "${name}"
}
2014-10-07 04:07:05 -05:00
# Chapter directories in specified order
all_chapter_dirs="overview \
installation \
2015-03-02 23:11:26 -06:00
mua \
upgrade \
2014-12-06 22:54:00 -06:00
migrations \
2014-09-30 10:04:28 -05:00
howto \
integrations \
cluster \
troubleshooting \
faq"
2014-09-30 09:59:20 -05:00
2015-06-03 09:48:41 -05:00
# Initial index file.
echo "All articles are written in Markdown format, you can download them for offline reading, contribute new articles or update existing ones in [BitBucket repository](https://bitbucket.org/zhb/docs.iredmail.org/)." > ${INDEX_MD}
2014-09-22 21:47:49 -05:00
# Compile all Markdown files.
if echo "$@" | grep -q -- '--all' &>/dev/null; then
compile_all='YES'
fi
article_counter=0
echo -n "* Processing Markdown files: "
2015-06-03 09:48:41 -05:00
# Get chapter info
# - chapter summary: _summary.md
# - article title: _title.md
for chapter_dir in ${all_chapter_dirs}; do
# Get articles
all_chapter_articles="$(find ${chapter_dir} -depth 1 -type f -iname '[0-9a-z]*.md')"
# Output directory.
# Remove prefix '[number]-' in chapter directory name.
#chapter_dir_in_article="$(strip_name_prefix ${chapter_dir})"
#_output_chapter_dir="${OUTPUT_DIR}/${chapter_dir_in_article}"
# Get chapter title.
_title_md="${chapter_dir}/_title.md"
_summary_md="${chapter_dir}/_summary.md"
2014-12-03 21:11:39 -06:00
if [ -f ${_title_md} ]; then
# generate index info of chapter
_chapter_title="$(cat ${_title_md})"
echo -e "### ${_chapter_title}" >> ${INDEX_MD}
if [ -f ${_summary_md} ]; then
2014-12-18 20:01:25 -06:00
cat ${_summary_md} >> ${INDEX_MD}
# Insert an empty line to not mess up other formats like list.
echo '' >> ${INDEX_MD}
2014-09-17 00:37:35 -05:00
fi
fi
# Article info:
# - title: first line (without '#') of markdown file
for article_file in ${all_chapter_articles}; do
article_counter="$((article_counter+1))"
article_file_basename="$(basename ${article_file})"
article_html_file="$(strip_name_prefix ${article_file_basename})"
# Replace '.md' suffix by '.html'
article_html_file="$(echo ${article_html_file/%.md/.html})"
2014-12-03 21:11:39 -06:00
hide_article_in_index='NO'
if echo "${article_file_basename}" | grep '^0-' &>/dev/null; then
hide_article_in_index='YES'
fi
2015-05-10 04:34:55 -05:00
# Get title in markdown file: '# title'
_article_title="$(head -1 ${article_file} | awk -F'# ' '{print $2}')"
2014-12-03 21:11:39 -06:00
if [ X"${hide_article_in_index}" == X'NO' ]; then
echo "* [${_article_title}](${article_html_file})" >> ${INDEX_MD}
fi
2015-01-07 09:12:18 -06:00
# Convert modified file
echo ${CHANGED_FILES} | grep ${article_file} &> /dev/null
compile_this_file="$?"
if [ X"${compile_this_file}" == X'0' -o X"${compile_all}" == X'YES' ]; then
2015-05-10 04:34:55 -05:00
echo -e "\n* Converting (#${article_counter}): ${article_file}"
${CMD_CONVERT} ${article_file} ${OUTPUT_DIR} \
output_filename="${article_html_file}" \
title="${_article_title}" \
2014-12-04 07:30:08 -06:00
add_index_link='yes'
2015-02-01 02:15:03 -06:00
else
echo -n '.'
fi
done
# Append addition links at the chapter bottom on index page.
2014-12-03 21:11:39 -06:00
_links_md="${chapter_dir}/_links.md"
2014-12-03 21:11:39 -06:00
if [ -f ${_links_md} ]; then
cat ${_links_md} >> ${INDEX_MD}
fi
done
echo ''
echo "* ${article_counter} files total."
echo "* Converting ${INDEX_MD} for index page."
2014-09-22 22:17:52 -05:00
${CMD_CONVERT} ${INDEX_MD} ${OUTPUT_DIR} title="iRedMail Documentations"
# Cleanup
2014-09-17 09:14:00 -05:00
rm -f ${INDEX_MD}
2014-10-07 05:19:13 -05:00
# Sync newly generated HTML files to local diretories.
if echo "$@" | grep -q -- '--sync-local'; then
# Copy to local hg repo of http://www.iredmail.org/docs/
2014-12-03 21:11:39 -06:00
echo "* Syncing converted HTML files."
2014-10-07 05:19:13 -05:00
rm -rf ../web/docs/*
cp -rf html/* ../web/docs/
# Copy to iredmail.com/docs/
rm -rf /Volumes/STORAGE/Dropbox/Backup/iredmail.com/docs/*
cp -rf html/* /Volumes/STORAGE/Dropbox/Backup/iredmail.com/docs/
fi