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.
2014-09-15 22:05:36 -05:00
# Directory used to store converted html files.
2014-09-17 00:28:29 -05:00
PWD = "."
2014-09-15 22:05:36 -05:00
SOURCE_DIR = " ${ PWD } /src "
2014-09-30 10:13:21 -05:00
OUTPUT_DIR = " ${ PWD } /html "
2014-09-15 22:05:36 -05:00
INDEX_MD = " ${ OUTPUT_DIR } /index.md "
2014-09-17 09:16:39 -05:00
README_MD = " ${ PWD } /README.md "
2014-09-15 22:05:36 -05:00
2014-10-10 01:17:58 -05:00
# Pack all converted HTML files for downloading
PACK_NAME = "iredmail-docs"
PACK_TAR_NAME = "iredmail-docs.tar.bz2"
2014-09-15 22:05:36 -05:00
[ -d ${ OUTPUT_DIR } ] || mkdir -p ${ OUTPUT_DIR }
2014-10-09 21:55:13 -05:00
CONVERTER = " ${ PWD } /tools/markdown2html.py "
CMD_CONVERT = " python ${ CONVERTER } "
2014-09-18 01:10:35 -05:00
#CMD_CONVERT=":"
2014-10-09 21:55:13 -05:00
CHANGED_FILES = " $( hg st) "
2014-10-10 01:17:58 -05:00
TODAY = " $( date +%Y-%m-%d) "
2014-09-15 23:34:01 -05:00
2014-09-15 22:05:36 -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
2014-09-30 09:59:20 -05:00
all_chapter_dirs = " installation \
2014-12-03 08:11:32 -06:00
upgrade \
2014-10-21 21:31:09 -05:00
mua \
2014-09-30 10:04:28 -05:00
howto \
integrations \
cluster \
2014-10-10 10:30:43 -05:00
migrations \
2014-09-30 10:04:28 -05:00
troubleshooting \
faq"
2014-09-30 09:59:20 -05:00
2014-09-15 22:05:36 -05:00
# Get chapter info
# - title: _title.md
# - summary: _summary.md
2014-10-30 07:22:36 -06:00
echo "We're migrating [old wiki documents](http://www.iredmail.org/wiki) to Markdown format for easier maintenance, all documents are available [here](https://bitbucket.org/zhb/docs.iredmail.org/src)." > ${ INDEX_MD }
echo "We're migrating [old wiki documents](http://www.iredmail.org/wiki) to Markdown format for easier maintenance." > ${ README_MD }
2014-09-22 21:47:49 -05:00
2014-09-15 22:05:36 -05:00
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' ) "
2014-12-03 08:11:32 -06:00
# Don't show chapter title and articles in index page.
hide_in_index = 'NO'
[ -f ${ chapter_dir } /_hide_in_index ] && hide_in_index = 'YES'
2014-09-15 22:05:36 -05:00
# Output directory.
# Remove prefix '[number]-' in chapter directory name.
2014-09-22 22:13:40 -05:00
#chapter_dir_in_article="$(strip_name_prefix ${chapter_dir})"
#_output_chapter_dir="${OUTPUT_DIR}/${chapter_dir_in_article}"
2014-09-15 22:05:36 -05:00
2014-10-06 09:47:07 -05:00
# Get chapter title.
2014-09-15 22:05:36 -05:00
_title_md = " ${ chapter_dir } /_title.md "
_summary_md = " ${ chapter_dir } /_summary.md "
2014-12-03 08:11:32 -06:00
if [ X" ${ hide_in_index } " != X'YES' ] ; then
if [ -f ${ _title_md } ] ; then
# generate index info of chapter
_chapter_title = " $( cat ${ _title_md } ) "
echo -e " ### ${ _chapter_title } " >> ${ INDEX_MD }
echo -e " # ${ _chapter_title } " >> ${ README_MD }
if [ -f ${ _summary_md } ] ; then
_chapter_summary = " $( cat ${ _summary_md } ) "
echo -e " ${ _chapter_summary } " >> ${ INDEX_MD }
echo -e " ${ _chapter_summary } " >> ${ README_MD }
fi
2014-09-17 00:37:35 -05:00
fi
2014-09-15 22:05:36 -05:00
fi
# Article info:
# - title: first line (without '#') of markdown file
for article_file in ${ all_chapter_articles } ; do
article_file_basename = " $( basename ${ article_file } ) "
2014-09-20 23:59:56 -05:00
article_file_without_prefix_path = " $( echo ${ article_file /# \. \/ / } ) "
article_file_without_prefix = " $( strip_name_prefix ${ article_file } ) "
2014-09-15 22:05:36 -05:00
2014-09-26 10:56:15 -05:00
article_html_file = " $( strip_name_prefix ${ article_file_basename } ) "
2014-09-15 22:05:36 -05:00
# Replace '.md' suffix by '.html'
article_html_file = " $( echo ${ article_html_file /%.md/.html } ) "
# Get title.
2014-09-20 23:35:18 -05:00
_article_title = " $( head -1 ${ article_file } | awk -F'# ' '{print $2}' ) "
#_article_title="$(head -1 ${article_file} | awk -F'Title: ' '{print $2}')"
2014-09-15 22:05:36 -05:00
#echo "article title: ${_article_title}"
2014-09-22 22:13:40 -05:00
#echo "* [${_article_title}](${chapter_dir_in_article}/${article_html_file})" >> ${INDEX_MD}
2014-12-03 08:11:32 -06:00
if [ X" ${ hide_in_index } " != X'YES' ] ; then
echo " * [ ${ _article_title } ]( ${ article_html_file } ) " >> ${ INDEX_MD }
2014-09-18 21:00:39 -05:00
2014-12-03 08:11:32 -06:00
# 'src/default/' is path to view source file on bitbucket.org
echo " * [ ${ _article_title } ](https://bitbucket.org/zhb/docs.iredmail.org/src/default/ ${ article_file_without_prefix_path } ) " >> ${ README_MD }
fi
2014-09-15 22:05:36 -05:00
2014-10-09 21:55:13 -05:00
# Convert file if it was modified
echo ${ CHANGED_FILES } | grep ${ article_file } > /dev/null
md_changed = " $? "
echo ${ CHANGED_FILES } | grep $( basename ${ CONVERTER } ) > /dev/null
converter_changed = " $? "
2014-10-09 22:09:43 -05:00
if [ X" ${ md_changed } " = = X'0' -o X" ${ converter_changed } " = = X'0' ] ; then
2014-10-09 21:55:13 -05:00
echo " * Converting: ${ article_file } "
${ CMD_CONVERT } ${ article_file } ${ OUTPUT_DIR } \
output_filename = " ${ article_html_file } " \
title = " ${ _article_title } " \
2014-10-09 22:30:58 -05:00
add_index_link = 'yes' \
add_page_footer = 'yes'
2014-10-09 21:55:13 -05:00
fi
2014-09-15 22:05:36 -05:00
done
2014-10-06 09:47:07 -05:00
# Append addition links at the chapter bottom on index page.
2014-12-03 08:11:32 -06:00
if [ X" ${ hide_in_index } " != X'YES' ] ; then
_links_md = " ${ chapter_dir } /_links.md "
2014-10-06 09:47:07 -05:00
2014-12-03 08:11:32 -06:00
if [ -f ${ _links_md } ] ; then
cat ${ _links_md } >> ${ INDEX_MD }
cat ${ _links_md } >> ${ README_MD }
fi
2014-10-06 09:47:07 -05:00
fi
2014-09-15 22:05:36 -05:00
done
2014-09-17 00:28:29 -05:00
#cd ${OUTPUT_DIR}
2014-09-15 22:05:36 -05:00
2014-10-10 01:17:58 -05:00
echo " * Converting ${ INDEX_MD } for index page. "
2014-09-22 22:17:52 -05:00
${ CMD_CONVERT } ${ INDEX_MD } ${ OUTPUT_DIR } title = "iRedMail Documentations"
2014-09-15 22:05:36 -05:00
# Cleanup
2014-09-17 09:14:00 -05:00
rm -f ${ INDEX_MD }
2014-09-30 10:13:21 -05:00
2014-10-10 01:17:58 -05:00
if echo " $@ " | grep -q -- '--pack' ; then
echo " * Pack all HTML files: ${ PACK_TAR_NAME } "
cd ${ PWD }
pack_dir = " ${ PACK_NAME } - ${ TODAY } "
mkdir -p ${ pack_dir }
cp -rf html/* ${ pack_dir }
tar cjf ${ PACK_TAR_NAME } ${ pack_dir }
rm -rf ${ pack_dir }
fi
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/
rm -rf ../web/docs/*
cp -rf html/* ../web/docs/
2014-10-10 01:17:58 -05:00
if [ -f ${ PACK_TAR_NAME } ] ; then
cp ${ PACK_TAR_NAME } ../web/docs/
fi
2014-10-07 05:19:13 -05:00
# Copy to iredmail.com/docs/
rm -rf /Volumes/STORAGE/Dropbox/Backup/iredmail.com/docs/*
cp -rf html/* /Volumes/STORAGE/Dropbox/Backup/iredmail.com/docs/
2014-10-10 12:53:03 -05:00
if [ -f ${ PACK_TAR_NAME } ] ; then
cp -f ${ PACK_TAR_NAME } /Volumes/STORAGE/Dropbox/Backup/iredmail.com/docs/
fi
2014-10-10 01:17:58 -05:00
rm -f ${ PACK_TAR_NAME }
2014-10-07 05:19:13 -05:00
fi