利用 pandoc 把 markdown 转换成 html 并借助 GitHub pages 发布文档
Resources:
git clone git@github.com:happycasts/episode-21-demo.git./1_split_file.sh ;./2_md_to_html.sh ;./3_get_toc.sh ;./4_jekyll_header.sh
#!/bin/bash################################### Step1: Split file##################################cd book-content/for file in `ls ch*.md`do i=0 IFS= # Set IFS, otherwise the leading spaces for code block is ignored shortname=${file%.*} while read line do if echo $line | grep "^#"|grep -v "^###"; then # this will match "##" and "#" currentname=$shortname-$i.md #can be section or chapter synopsis echo '$currentname:'"$currentname" i=$((i+1)) fi echo $line >> $currentname done < $file unset IFS #IFS will casue trouble in some cases, so must unsetdone
cd ..rm -rf md_tmp &>/dev/nullmkdir md_tmpmv book-content/ch*-*.md md_tmpcp -rf book-content/figures/ md_tmp &>/dev/null
#!/bin/bash################################### Step2: md -> html##################################
cd md_tmpfor file in `ls *.md`do shortname=${file%.*} pandoc $file >$shortname.htmldone
cd ..rm -rf html_tmpmkdir html_tmpmv md_tmp/*.html html_tmpmv md_tmp/figures html_tmp &>/dev/nullrm -rf md_tmp
#!/bin/bash################################### Step3: get toc##################################
echo "<ul id="toc">">index.htmlchapter_no=1for file in `ls book-content/ch*.md`do i=0 while read line do basefilename=`basename $file` shortname=${basefilename%.*} if echo $line | grep "^#"|grep -v "^###"; then if echo $line | grep "##"; then i=$((i+1)) stringA=$line section=${stringA#'##'} echo "<li><h2><a href=\"${shortname}-$i.html\">$chapter_no.$i $section</a></h2></li>" >>index.html else stringB=$line chapter=${stringB#'#'} echo "<li><h1><a href=\"${shortname}-$i.html\">$chapter_no. $chapter</a></h1></li>" >>index.html fi fi done< $file chapter_no=$((chapter_no+1))doneecho "</ul>">>index.html
mv index.html html_tmp
#!/bin/bash################################### Step4: add jekyll header##################################
for file in `ls html_tmp/*.html`do cp $file $file.tmp echo "---" >$file echo "layout: master" >>$file echo "---" >> $file cat $file.tmp >> $file rm $file.tmpdone