Skip to content

Commit

Permalink
Merge pull request #6 from annProg/recon
Browse files Browse the repository at this point in the history
重构
  • Loading branch information
annProg committed Apr 16, 2019
2 parents 8d456cc + 0548534 commit a370478
Show file tree
Hide file tree
Showing 92 changed files with 987 additions and 554 deletions.
90 changes: 45 additions & 45 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
SERVER ?= api.annhe.net
USER ?= root
PORT ?= 22
DIR ?= /PanBook

OWNER ?= annProg
REPO ?= PanBook
TAG ?= $(shell git rev-parse --short HEAD)

all: ctex ctex6in ctexart elegantbook epub online
pdf: ctex ctex6in elegantbook

ctex:
COVER=r panbook pdf
ctex6in:
DEVICE=mobile COVER=r panbook pdf
article:
DOCUMENT=ctexart TPL=latex panbook pdf
elegantbook:
TPL=elegantbook panbook pdf
epub:
rm -f build/*.epub
panbook epub d
mv build/$(REPO)-*.epub build/$(REPO).epub

up: release upload

release:
git tag -a $(TAG) -m "$(TAG)"
git push origin $(TAG)
curl -H "Content-Type:application/json" -H "Authorization: token $$GITHUB_TOKEN" -XPOST "https://api.github.com/repos/$(OWNER)/$(REPO)/releases" -d '{"tag_name":"$(TAG)"}'
upload:
ID=`curl -s -H "Authorization: token $$GITHUB_TOKEN" "https://api.github.com/repos/$(OWNER)/$(REPO)/releases/tags/$(TAG)" |grep '"id"' |head -n 1 |awk '{print $$2}' |tr -d ','`; echo "ID: $$ID";\
cd build/; \
for FILE in `ls *.pdf` $(REPO).epub;do \
echo $$FILE;\
curl -H "Authorization: token $$GITHUB_TOKEN" -H "Content-Type: $$(file -b --mime-type $$FILE)" "https://uploads.github.com/repos/$(OWNER)/$(REPO)/releases/$$ID/assets?name=$$(basename $$FILE)" --data-binary @$$FILE; echo; \
done

online:
scp -P $(PORT) build/$(REPO)-ctex-pc.pdf $(USER)@$(SERVER):$(DIR)
scp -P $(PORT) build/$(REPO)-latex-pc.pdf $(USER)@$(SERVER):$(DIR)
scp -P $(PORT) build/$(REPO)-ctex-mobile.pdf $(USER)@$(SERVER):$(DIR)
scp -P $(PORT) build/$(REPO)-elegantbook-pc.pdf $(USER)@$(SERVER):$(DIR)
scp -P $(PORT) build/$(REPO).epub $(USER)@$(SERVER):$(DIR)
SERVER ?= api.annhe.net
USER ?= root
PORT ?= 22
DIR ?= /PanBook

OWNER ?= annProg
REPO ?= PanBook
TAG ?= $(shell git rev-parse --short HEAD)

all: ctex ctex6in ctexart elegantbook epub online
pdf: ctex ctex6in elegantbook

ctex:
panbook pdf -E cover=R
ctex6in:
panbook pdf -E cover=R -E device=mobile
ctexart:
panbook pdf --class=ctexart
elegantbook:
panbook pdf --class=elegantbook
epub:
rm -f build/*.epub
panbook epub -d
mv build/$(REPO)-*.epub build/$(REPO).epub

up: release upload

release:
git tag -a $(TAG) -m "$(TAG)"
git push origin $(TAG)
curl -H "Content-Type:application/json" -H "Authorization: token $$GITHUB_TOKEN" -XPOST "https://api.github.com/repos/$(OWNER)/$(REPO)/releases" -d '{"tag_name":"$(TAG)"}'
upload:
ID=`curl -s -H "Authorization: token $$GITHUB_TOKEN" "https://api.github.com/repos/$(OWNER)/$(REPO)/releases/tags/$(TAG)" |grep '"id"' |head -n 1 |awk '{print $$2}' |tr -d ','`; echo "ID: $$ID";\
cd build/; \
for FILE in `ls *.pdf` $(REPO).epub;do \
echo $$FILE;\
curl -H "Authorization: token $$GITHUB_TOKEN" -H "Content-Type: $$(file -b --mime-type $$FILE)" "https://uploads.github.com/repos/$(OWNER)/$(REPO)/releases/$$ID/assets?name=$$(basename $$FILE)" --data-binary @$$FILE; echo; \
done

online:
scp -P $(PORT) build/$(REPO)-latex-ctexbook-pc.pdf $(USER)@$(SERVER):$(DIR)
scp -P $(PORT) build/$(REPO)-latex-ctexbook-mobile.pdf $(USER)@$(SERVER):$(DIR)
scp -P $(PORT) build/$(REPO)-latex-ctexart-pc.pdf $(USER)@$(SERVER):$(DIR)
scp -P $(PORT) build/$(REPO)-latex-elegantbook-pc.pdf $(USER)@$(SERVER):$(DIR)
scp -P $(PORT) build/$(REPO).epub $(USER)@$(SERVER):$(DIR)

7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

查看电子书效果:

- [ElegantBook模板-pc](https://api.annhe.net/PanBook/PanBook-elegantbook-pc.pdf)
- [Ctex模板-pc](https://api.annhe.net/PanBook/PanBook-ctex-pc.pdf)
- [Ctex模板-mobile](https://api.annhe.net/PanBook/PanBook-ctex-mobile.pdf)
- [ElegantBook模板-pc](https://api.annhe.net/PanBook/PanBook-latex-elegantbook-pc.pdf)
- [Ctex模板-pc](https://api.annhe.net/PanBook/PanBook-latex-ctexbook-pc.pdf)
- [Ctexart模板-pc](https://api.annhe.net/PanBook/PanBook-latex-ctexart-pc.pdf)
- [Ctex模板-mobile](https://api.annhe.net/PanBook/PanBook-latex-ctexbook-mobile.pdf)
- [Epub模板](https://api.annhe.net/PanBook/PanBook.epub)

## 快速开始
Expand Down
4 changes: 2 additions & 2 deletions config.default
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PDF_OPTIONS="-t latex+smart --toc --number-sections --top-level-division=$DIVISION -V links-as-notes --listings --pdf-engine=xelatex"
HTML_OPTIONS="-t html+smart --toc --highlight-style=$theme"
PDF_OPTIONS="-t latex+smart --toc --number-sections --pdf-engine=xelatex --listings"
HTML_OPTIONS="-t html+smart --toc --highlight-style=$theme"
EPUB_OPTIONS="-t epub3+smart --number-sections --highlight-style=$theme"
28 changes: 14 additions & 14 deletions demo/beamer/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
THEME ?= R
CONVERT ?= /mingw64/bin/convert

run:
THEME=$(THEME) panbook beamer

png:
cd build && mkdir all-beamer && mkdir all-beamer-title && \
for item in `ls *.pdf`;do \
theme=`echo $$item | sed 's/.pdf//g'`;\
pdftocairo -png $$item $$theme;\
$(CONVERT) -append $$theme-*.png all-beamer/$$theme.png;\
mv $$theme-1.png all-beamer-title/$${theme}0.png;\
rm -f $$theme-*.png;\
THEME ?= R
CONVERT ?= /mingw64/bin/convert

run:
panbook beamer --theme=$(THEME)

png:
cd build && mkdir all-beamer && mkdir all-beamer-title && \
for item in `ls *.pdf`;do \
theme=`echo $$item | sed 's/.pdf//g'`;\
pdftocairo -png $$item $$theme;\
$(CONVERT) -append $$theme-*.png all-beamer/$$theme.png;\
mv $$theme-1.png all-beamer-title/$${theme}0.png;\
rm -f $$theme-*.png;\
done
220 changes: 220 additions & 0 deletions libs/core.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
#!/bin/bash

function meta() {
cat $SCRIPTDIR/src/metadata.yaml
}

function parseMeta() {
# 仅支持key全为字母且值为string
source <(grep -E "^[a-zA-Z]+: " $METADATA | sed -e 's/\s*#.*$//g;s/:[^:\/\/]/="/g;s/$/"/g;s/ *=/=/g' |grep "=")
}

function setPandocVar() {
echo "$PANDOCVARS" |grep -w "$1" &>/dev/null || PANDOCVARS="$PANDOCVARS -V $1=$2"
}

function clean() {
cd $BUILD
rand=`echo $RANDOM$RANDOM$RANDOM$RANDOM`
release="/tmp/release-$rand"
mkdir $release
mv *.pdf *.tex *.epub *.html $release 2>/dev/null
rm -fr *
mv $release/* .
rm -fr $release
}

function cleanall()
{
cd $BUILD
rm -fr *
}

# 保存网络图片至本地
function saveimg()
{
cd $IMGDIR
for item in `ls $WORKDIR/*.md`;do
for url in `grep -E "^\!\[.*?\]\(http(s)?://.*\)" $item |awk -F"(" '{print $2}' |tr -d ')'`;do
wget -m -np $url
localpath=`echo $url |sed -r 's#http(s)?://##g'`
echo $localpath |grep -E "\.gif$" && r=0 || r=1
if [ $r -eq 0 ];then
new=`echo $localpath |sed -r "s/\.gif$//g"`
$CMD_CONVERT $localpath $new.eps
mv $new-0.eps $new.eps 2>/dev/null
rm -f $new-*.eps
fi
done
done
cd $cwd
}

function toeps()
{
cd $IMGDIR
for item in `ls *.$1`;do
new=`echo $item |sed -r "s/.$1$//g"`
$CMD_CONVERT $item $new.eps
mv $new-0.eps $new.eps 2>/dev/null
rm -f $new-*.eps
done
cd $cwd
}

# 转换为eps格式
function eps()
{
toeps gif
}

function pdf2jpg()
{
cd $BUILD
for id in $BODY $FRONTMATTER $BACKMATTER;do
sed -i -r 's/(!\[.*?\]\(.*?)(\.pdf\))/\1.jpg)/g' $id
done
}

function templateError() {
# TPL变量为空时不做判断
[ "$TPL"x == ""x ] && return
# 指定template不存在时用默认模板编译
TEMPLATE="--template=$cwd/build/$TPL.tpl"
[ ! -f $cwd/build/$TPL.tpl ] && error "Template $TPL not found." && exit 1 #TEMPLATE="-V CJKmainfont=$CJK -V documentclass=ctexbook"
}

function compileStatus() {
status=$?
info "$1 Compile status: $status"
if [ $status -ne 0 ];then
error "$1 Compile status is not 0. Please Check. you may add -d to see more output"
else
note "$1 Compile SUCCESSFUL"
fi
}

# 兼容不规范源码
function compatible()
{
# IMGDIR 相对路径
IMGDIRFULL=`cd $IMGDIR && pwd`
echo $IMGDIRFULL |grep -w $WORKDIR &>/dev/null && r=0 || r=1
info "IMGDIR=$IMGDIRFULL"
if [ $r -eq 0 ];then
IMGDIRRELATIVE=`echo $IMGDIRFULL|sed "s#$WORKDIR#.#g"`
else
IMGDIRRELATIVE=`echo $IMGDIRFULL|sed "s#$cwd#..#g"`
fi

cd $BUILD
COMPATIBLE="compatible.conf"
PREFIX="PanBook-compatible-"
if [ -f $COMPATIBLE ];then
for stype in frontmatter backmatter body exclude;do
newName=$PREFIX$stype.md
echo > $newName
for item in `grep -E "$stype$" $COMPATIBLE |awk '{print $1}'`;do
if [ "$stype" == "exclude" ];then
rm -f $item
else
cat $item >> $newName
echo -e "\n\n" >> $newName
fi

# 被删除及写入frontmatter和backmatter的源码,需要从BODY中排除
BODY=`echo $BODY |sed -r "s/($item | $item)//g"`
[ $stype == "frontmatter" ] && FRONTMATTER=$newName
[ $stype == "backmatter" ] && BACKMATTER=$newName
[ $stype == "body" ] && BODY=$newName
done
done
fi
}

function init()
{
[ ! -d $BUILD ] && mkdir $BUILD
[ ! -d $WORKDIR ] && mkdir $WORKDIR
[ ! -d $IMGDIR ] && mkdir $IMGDIR
[ ! -d $cwd/templates ] && mkdir $cwd/templates
[ ! -d $cwd/fonts ] && mkdir $cwd/fonts

cd $WORKDIR

# 模板, 支持用户自定义模板
[ -d $SCRIPTDIR/templates/$TPL ] && cp -rfu $SCRIPTDIR/templates/$TPL/* $BUILD
[ -d $cwd/templates/$TPL ] && cp -rfu $cwd/templates/$TPL/* $BUILD 2>/dev/null

# 字体
cp -rfu $SCRIPTDIR/fonts $BUILD
[ -d $cwd/fonts ] && cp -rfu $cwd/fonts/* $BUILD/fonts/ 2>/dev/null

# 此时还未复制书籍源码,build目录中的 template或者font中包含的 .md 文件应该被删除
rm -f $BUILD/*.md

# 文件名规范
FRONTMATTER="frontmatter.md"
BACKMATTER="backmatter.md"
chapters=`ls *.md |grep -vE "$FRONTMATTER|$BACKMATTER" 2>/dev/null`
BODY="$chapters"

# 前言和后记部分
[ ! -f $FRONTMATTER ] && touch $FRONTMATTER
[ ! -f $BACKMATTER ] && touch $BACKMATTER
[ ! -f metadata.yaml ] && meta > metadata.yaml
[ "$DEBUG"x = "true"x ] && highlightStyle=(tango)

# 复制$SRC目录下资源文件到build目录
cp -rf $WORKDIR/* $BUILD
cd $BUILD

info "Template is: $TPL"
templateError

# 兼容性处理
compatible
}

function setBase() {
Type=$1
Select=$2
List=$3

SELECTED=($Select)

if [ "$Select"x == "R"x ];then
len=`echo ${#List[@]}`
index=$(($RANDOM%$len))
SELECTED=(${List[$index]})
fi

if [ "$Select"x == "A"x ];then
SELECTED=`echo ${themeList[@]}`
fi

info "SELECTED $Type: $SELECTED"
if [ "$TPL"x != ""x ];then
origAddOptions="--template=$TPL.tpl"
else
origAddOptions=""
fi

origHighLight="--listings -H $SCRIPTDIR/templates/latex/listings-set.tex"
}
function setTheme() {
setBase BeamerTheme $THEME $1
}

function setClass() {
setBase PdfClass $DOCUMENTCLASS $1
}

function copyrightPage() {
getVar copyright "true"
getVar licence "ccncnd"

if [ "$copyright"x == "true"x ];then
echo "-H $SCRIPTDIR/templates/latex/add-copyright-page.tex -V copyright=true -V licence=$licence"
fi
}
8 changes: 8 additions & 0 deletions libs/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

highlightStyle=(pygments kate monochrome espresso haddock tango zenburn breezedark)
DATE=`date +%Y/%m/%d`
DATETIME=`date +%Y/%m/%d\ %H:%M:%S`
DEBUG="false"
CMD_CONVERT="convert"
[ "`uname -o`"x == "Msys"x ] && CMD_CONVERT="/mingw64/bin/convert"
Loading

0 comments on commit a370478

Please sign in to comment.