From 310366b7893b14d8a5f3e90f6ada80d302192f04 Mon Sep 17 00:00:00 2001 From: pi Date: Tue, 13 Nov 2018 20:05:15 -0800 Subject: [PATCH] update postToBlog to post2blog --- configure.sh | 2 +- post2blog.sh | 285 ++++++++++++++++++++++++++++++++++++++++++++++++++ postToBlog.sh | 141 ------------------------- 3 files changed, 286 insertions(+), 142 deletions(-) create mode 100755 post2blog.sh delete mode 100755 postToBlog.sh diff --git a/configure.sh b/configure.sh index 14382b3..a362fc0 100755 --- a/configure.sh +++ b/configure.sh @@ -48,7 +48,7 @@ for element in ${scriptName[@]}; do objectName=${element%.*} #若链接不存在 则建立 if [[ ! -L ${objectAddress}/${objectName} ]]; then - printf "creating links for " $objectName "... " + printf "creating links for " ${objectName} "... " ln -s ${folderAddress}/${element} ${objectAddress}/${objectName} printf "done\n" fi diff --git a/post2blog.sh b/post2blog.sh new file mode 100755 index 0000000..9b17f04 --- /dev/null +++ b/post2blog.sh @@ -0,0 +1,285 @@ +#!/bin/bash +#define functions for display help information +function dispTitle(){ + winWidth=`tput cols` + message=${2} + + printf "%s\n%-9s" "${1}" + + head_length=19 + print_length=${head_length} + for segment in ${message[@]}; do + print_length=`expr ${print_length} + ${#segment} + 1` + if [[ ${print_length} -le ${winWidth} ]]; then + printf " %s" "${segment}" + else + print_length=`expr ${head_length} + ${#segment} + 1` + printf "\n%-10s%s" " " "${segment}" + fi + done + printf "\n\n" +} + +function dispAuthorInfo(){ + winWidth=`tput cols` + message=${1} + + printf "%-9s" "Author:" + + head_length=19 + print_length=${head_length} + for segment in ${message[@]}; do + print_length=`expr ${print_length} + ${#segment} + 1` + if [[ ${print_length} -le ${winWidth} ]]; then + printf " %s" "${segment}" + else + print_length=`expr ${head_length} + ${#segment} + 1` + printf "\n%-10s%s" " " "${segment}" + fi + done + printf "\n\n" +} + +function dispUsage(){ + winWidth=`tput cols` + message=${1} + + printf "%-9s" "Usage:" + + head_length=19 + print_length=${head_length} + for segment in ${message[@]}; do + print_length=`expr ${print_length} + ${#segment} + 1` + if [[ ${print_length} -le ${winWidth} ]]; then + printf " %s" "${segment}" + else + print_length=`expr ${head_length} + ${#segment} + 1` + printf "\n%-10s%s" " " "${segment}" + fi + done + printf "\n\n" +} + +function dispOptionShort(){ + winWidth=`tput cols` + message=${2} + + printf "%-4s%-5s" " " "${1}" + + head_length=19 + print_length=${head_length} + for segment in ${message[@]}; do + print_length=`expr ${print_length} + ${#segment} + 1` + if [[ ${print_length} -le ${winWidth} ]]; then + printf " %s" "${segment}" + else + print_length=`expr ${head_length} + ${#segment} + 1` + printf "\n%-10s%s" " " "${segment}" + fi + done + printf "\n" +} + +#change initial parameters depending on your own file system +_repository_address="/Users/zhangyi/Code/GitHub/YiZhangCUG.github.io" +_posts_address="_posts/" #directory of your posts +_assets_address="assets/" #directory of your assets +_posts_sub_address="2018/" #sub-directory of your posts +_mfile='README.md' #default input filename +_layout='post' #default post layout +_categories='Null' #default category +_tags='Null' #default tag +# +#end of self configuration. revise the left script with caution +# +_nowtime=`date +"%Y-%m-%d %H:%M:%S %z"` #get your current date, time and timezone +_outfile=`date +"%Y-%m-%d-"` #initial output filename +_title='Null' +_outfile_end='Null' +_savefile=0 +_link_string="" +#get arguments from terminal +while getopts "hSi:l:T:c:t:s:" arg +do + case $arg in + h) + dispTitle "${0##*/}" "Post a markdown file to a personal blog hosted on the github.pages. The site is powered by the Jekyll (https://jekyllrb.com)." + dispAuthorInfo "Yi Zhang (zhangyi.cugwuhan@gmail.com)" + dispUsage "${0##*/} [-i] [-l] [-T] [-c<categories>] [-t<tags>] [-s<sub-directory>] [-S] [-h]" + dispOptionShort "-i" "Input markdown file. The default is '${_mfile}'." + dispOptionShort "-l" "Page layout defined in the Jekyll's styles. The default is '${_layout}'." + dispOptionShort "-T" "Blog title. The default will use the h1 level title in the input markdown file. The use of this option will overwrite the default. The output filename will use the current date plus the blog title. Note that following symbols in the title will be replaced by the '-' symbol: '.',!','@','#','$','%','^','&','*','_',':'." + dispOptionShort "-c" "Blog categories that are separated by commas. The default is/are '${_categories}'." + dispOptionShort "-t" "Blog tags that are separated by commas. The default is/are '${_tags}'." + dispOptionShort "-s" "Sub-directory of the posts. The main directory of the posts is placed under '${_posts_address}' folder of your github.io directory. The default is '${_posts_sub_address}'." + dispOptionShort "-S" "Save output markdown file. The default will remove the output file after committing the blog to Github." + dispOptionShort "-h" "Show help information." + exit 0;; + i) + _mfile=$OPTARG;; + l) + _layout=$OPTARG;; + T) + _title=$OPTARG;; + c) + _categories=$OPTARG;; + t) + _tags=$OPTARG;; + s) + _posts_sub_address=$OPTARG;; + S) + _savefile=1;; + ?) + printf "\e[1m\e[31mError ==>\e[0m Unknow argument.\n" + printf "Use -h option to see help information.\n" + exit 1;; + esac +done + +#check if _title is set or not. If so, skip getting _title and _outfile_end from the input file +if [[ ${_title} != 'Null' ]]; then + #read input file and put it into a temporary file + if [[ -f ${_mfile} ]]; then + _tmpfile=`mktemp tmp.XXXX` + while read _oneline + do + #first get all hyper links, including websites, images and files. save them to a single string separated by whitespace + _link_string=${_link_string}`echo "${_oneline}" | grep -Eo "!?\[[a-zA-Z0-9 \-\_]*\]\([a-zA-Z0-9\\\./: \-\_]*\)"`" " + echo "${_oneline}" >> ${_tmpfile} + done < ${_mfile} + else + printf "\e[1m\e[31mError ==>\e[0m File not found: ${_mfile}\n" + exit 1 + fi + # remove special symbols from the title + _outfile_end=${_title//[.:_!@#\$\%\^\&\*]/''} + _outfile_end=${_outfile_end// /-} +else + #read input file and put it into a temporary file + if [[ -f ${_mfile} ]]; then + _tmpfile=`mktemp tmp.XXXX` + while read _oneline + do + if [[ `echo ${_oneline} | grep "^# "` ]]; then + _outfile_end=${_oneline:2} + else + #first get all hyper links, including websites, images and files. save them to a single string separated by whitespace + _link_string=${_link_string}`echo "${_oneline}" | grep -Eo "!?\[[a-zA-Z0-9 \-\_]*\]\([a-zA-Z0-9\\\./: \-\_]*\)"`" " + echo "${_oneline}" >> ${_tmpfile} + fi + done < ${_mfile} + else + printf "\e[1m\e[31mError ==>\e[0m File not found: ${_mfile}\n" + exit 1 + fi + + #check if _outfile_end is found or not + if [[ ${_outfile_end} != 'Null' ]]; then + _title=${_outfile_end} + # remove special symbols from the h1 title + _outfile_end=${_outfile_end//[.:_!@#\$\%\^\&\*]/''} + _outfile_end=${_outfile_end// /-} + else + printf "\e[1m\e[31mError ==>\e[0m No h1 level title found in: ${_mfile}.\n" + printf "Please set the blog title and output file name manually.\n" + printf "\e[1m\e[32m==>\e[0m " + read _outfile_end + _title=${_outfile_end} + # remove special symbols from the input title + _outfile_end=${_outfile_end//[.:_!@#\$\%\^\&\*]/''} + _outfile_end=${_outfile_end// /-} + fi +fi + +#combine output filename +_outfile=${_outfile}${_outfile_end}.md + +#get link names from _link_string and save a string +_link_names_string=`echo ${_link_string} | grep -Eo "\[[a-zA-Z0-9 \-\_]*\]"` +#change whitespace in every link name into - +_link_names_string=${_link_names_string//" "/"-"} +#delete symbols \[ and \] in link names +_link_names_string=${_link_names_string//[\[|\]]/''} +#convert _link_names_string to an array +_link_names=(${_link_names_string}) + +#get link addresses from _link_string and save a string +_link_addresses_string=`echo ${_link_string} | grep -Eo "\([a-zA-Z0-9\\\./: \-\_]*\)"` +#change whitespace in every link address into - +_link_addresses_string=${_link_addresses_string//" "/"-"} +#delete symbols \( and \) in link names +_link_addresses_string=${_link_addresses_string//[\(|\)]/''} +#convert _link_addresses_string to an array +_link_addresses=(${_link_addresses_string}) + +#exclude http[s] links to get _copy_names and _copy_addresses +#we use year-month to sort files by month +unset _copy_names +unset _copy_addresses +for (( i = 0; i < ${#_link_names[@]}; i++ )); do + if [[ ! `echo ${_link_addresses[i]} | grep -E "https?://"` ]]; then + _copy_names+=(`date +"%Y-%m/"`"${_link_names[i]}.${_link_addresses[i]##*.}") + _copy_addresses+=("${_link_addresses[i]}") + fi +done + +#read temporary file and change file link address, then input it into another temporary file +_tmpfile2=`mktemp tmp.XXXX` +while read _oneline +do + #check every line for saved file links and replace theme with _copy_names + for (( i = 0; i < ${#_copy_addresses[@]}; i++ )); do + _oneline=${_oneline//${_copy_addresses[i]}/"/${_assets_address}${_copy_names[i]}"} + done + echo "${_oneline}" >> ${_tmpfile2} +done < ${_tmpfile} +rm ${_tmpfile} + +#combine head information and temporary file into the output, then remove the temporary one +echo "---" > ${_outfile} +echo "layout: ${_layout}" >> ${_outfile} +echo "title: \"${_title}\"" >> ${_outfile} +echo "date: ${_nowtime}" >> ${_outfile} +echo "categories: [${_categories}]" >> ${_outfile} +echo "tags: [${_tags}]" >> ${_outfile} +echo "---" >> ${_outfile} +echo "" >> ${_outfile} +echo "* content" >> ${_outfile} +echo "{:toc}" >> ${_outfile} +echo "" >> ${_outfile} +echo "" >> ${_outfile} +cat ${_tmpfile2} >> ${_outfile} +rm ${_tmpfile2} + +printf "Output file generated. Please check it. Is every thing ok? (y/n) " +read _check_result +if [[ ${_check_result} == 'y' ]]; then + #copy files to github.io directory. Check directory exists or not + #"${_repository_address}/${_assets_address}`date +"%Y-%m"`" + if [[ ! -d "${_repository_address}/${_assets_address}`date +"%Y-%m"`" ]]; then + mkdir ${_repository_address}/${_assets_address}`date +"%Y-%m"` + fi + + for (( i = 0; i < ${#_copy_addresses[@]}; i++ )); do + cp ${_copy_addresses[i]} ${_repository_address}/${_assets_address}${_copy_names[i]} + done + + #pull your git repository + git --git-dir=${_repository_address}/.git --work-tree=${_repository_address} pull + #copy output markdown file to posts directory + cp ${_outfile} ${_repository_address}/${_posts_address}${_posts_sub_address} + #add file to git + git --git-dir=${_repository_address}/.git --work-tree=${_repository_address} add ${_posts_address}${_posts_sub_address}${_outfile} + for (( i = 0; i < ${#_copy_names[@]}; i++ )); do + git --git-dir=${_repository_address}/.git --work-tree=${_repository_address} add ${_assets_address}${_copy_names[i]} + done + #commit changes + git --git-dir=${_repository_address}/.git --work-tree=${_repository_address} commit -m "post blog ${_outfile}" + #push to remote + git --git-dir=${_repository_address}/.git --work-tree=${_repository_address} push +fi + +#check if the output file should be saved +if [[ ${_savefile} == 0 ]]; then + rm ${_outfile} +fi \ No newline at end of file diff --git a/postToBlog.sh b/postToBlog.sh deleted file mode 100755 index 3935342..0000000 --- a/postToBlog.sh +++ /dev/null @@ -1,141 +0,0 @@ -#!/bin/bash -#包含dispOption脚本 -. dispOptions -#初始化参数 -_repository_address="/Users/zhangyi/Code/GitHub/YiZhangCUG.github.io" -_posts_address="_posts/" -_posts_sub_address="2018/" -_mfile='README.md' -_layout='post' -_title='Null' -_categories='Null' -_tags='Null' -#获取当前日期加时间加时区 -_nowtime=`date +"%Y-%m-%d %H:%M:%S %z"` -#初始化输出文件名 -_outfile=`date +"%Y-%m-%d-"` -_outfile_end='Null' -#是否保留输出文件 -_savefile=0 -#从命令行获取参数 -while getopts "hSi:l:T:c:t:s:" arg -do - case $arg in - h) - dispTitle "${0##*/}" "Post a markdown file to a personal blog hosted on the Github.io. The site is powered by Jekyll (https://jekyllrb.com)." - dispAuthorInfo "Yi Zhang (zhangyi.cugwuhan@gmail.com)" - dispUsage "${0##*/} -i<markdown-file> [-l<layout>] [-T<title>] [-c<categories>] [-t<tags>] [-s<sub-directory>] [-S] [-h]" - dispOptionShort "-i" "Input markdown file. The default is '${_mfile}'." - dispOptionShort "-l" "Page layout defined in Jekyll's styles. The default is '${_layout}'." - dispOptionShort "-T" "Blog title. The default will use the h1 level title in the input markdown file. The use of this option will overwrite the default. The output filename will use the current date plus the blog title. Note that following symbols in the title will be replaced by the '-' symbol: '.',!','@','#','$','%','^','&','*','_',':'." - dispOptionShort "-c" "Blog categories that are separated by commas. The default is/are '${_categories}'." - dispOptionShort "-t" "Blog tags that are separated by commas. The default is/are '${_tags}'." - dispOptionShort "-s" "Sub-directory of the posts. The main directory of the posts is placed under '${_posts_address}' folder of your Github.io directory. The default is '${_posts_sub_address}'." - dispOptionShort "-S" "Save output markdown file. The default will remove the output file after committing the blog to Github." - dispOptionShort "-h" "Show help information." - exit 0;; - i) - _mfile=$OPTARG;; - l) - _layout=$OPTARG;; - T) - _title=$OPTARG;; - c) - _categories=$OPTARG;; - t) - _tags=$OPTARG;; - s) - _posts_sub_address=$OPTARG;; - S) - _savefile=1;; - ?) - printf "\e[1m\e[31mError ==>\e[0m Unknow argument.\n" - printf "Use -h option to see help information.\n" - exit 1;; - esac -done - -#检测_title是否设置 如果设置则跳过拾取部分 -if [[ ${_title} != 'Null' ]]; then - #读入输入文件输出到临时文件 - if [[ -f ${_mfile} ]]; then - _tmpfile=`mktemp tmp.XXXX` - while read oneline - do - echo ${oneline} >> ${_tmpfile} - done < ${_mfile} - else - printf "\e[1m\e[31mError ==>\e[0m File not found: ${_mfile}\n" - exit 1 - fi - # remove special symbols from the title - _outfile_end=${_title//[.:_!@#\$\%\^\&\*]/''} - _outfile_end=${_outfile_end// /-} -else - #读入输入文件输出到临时文件 - if [[ -f ${_mfile} ]]; then - _tmpfile=`mktemp tmp.XXXX` - while read oneline - do - if [[ `echo ${oneline} | grep "^# "` ]]; then - _outfile_end=${oneline:2} - else - echo ${oneline} >> ${_tmpfile} - fi - done < ${_mfile} - else - printf "\e[1m\e[31mError ==>\e[0m File not found: ${_mfile}\n" - exit 1 - fi - - #检测_outfile_end是否找到 - if [[ ${_outfile_end} != 'Null' ]]; then - _title=${_outfile_end} - # remove special symbols from the h1 title - _outfile_end=${_outfile_end//[.:_!@#\$\%\^\&\*]/''} - _outfile_end=${_outfile_end// /-} - else - printf "\e[1m\e[31mError ==>\e[0m No h1 level title found in: ${_mfile}.\n" - printf "Please set the blog title and output file name manually.\n" - printf "\e[1m\e[32m==>\e[0m " - read _outfile_end - _title=${_outfile_end} - # remove special symbols from the input title - _outfile_end=${_outfile_end//[.:_!@#\$\%\^\&\*]/''} - _outfile_end=${_outfile_end// /-} - fi -fi - -#合成输出文件名称 -_outfile=${_outfile}${_outfile_end}.md -#将头信息和临时文件内容拷贝到_outfile 然后删除临时文件 -echo "---" > ${_outfile} -echo "layout: ${_layout}" >> ${_outfile} -echo "title: \"${_title}\"" >> ${_outfile} -echo "date: ${_nowtime}" >> ${_outfile} -echo "categories: [${_categories}]" >> ${_outfile} -echo "tags: [${_tags}]" >> ${_outfile} -echo "---" >> ${_outfile} -echo "" >> ${_outfile} -echo "* content" >> ${_outfile} -echo "{:toc}" >> ${_outfile} -echo "" >> ${_outfile} -echo "" >> ${_outfile} -cat ${_tmpfile} >> ${_outfile} -rm ${_tmpfile} - -#pull your git repository -git --git-dir=${_repository_address}/.git --work-tree=${_repository_address} pull -# copy output markdown file to posts directory -cp ${_outfile} ${_repository_address}/${_posts_address}${_posts_sub_address} -#add file to git -git --git-dir=${_repository_address}/.git --work-tree=${_repository_address} add ${_posts_address}${_posts_sub_address}${_outfile} -# commit changes -git --git-dir=${_repository_address}/.git --work-tree=${_repository_address} commit -m "added file ${_outfile}" -# push to remote -git --git-dir=${_repository_address}/.git --work-tree=${_repository_address} push - -#是否保存文件 -if [[ ${_savefile} == 0 ]]; then - rm ${_outfile} -fi \ No newline at end of file