Add config as language file
This patch will change several behaviour while generating the ebook:
- `build.go` now accepts two environment variables: TMP, WORKDIR
* TMP:
Temporary directory path.
The directory stores intermediate files like '*.html' or metadata.txt.
* WORKDIR:
Script base directory.
- `config` as language file
config file will be `source`d to change its ebook metadata.
- `genepub.sh` is integrated to `build.sh` and `build.go`
See `FixHeader` and `RemoveFooterLink` functions.
This commit is contained in:
120
ebook/build.go
120
ebook/build.go
@@ -11,48 +11,98 @@ import (
|
||||
)
|
||||
|
||||
// 定义一个访问者结构体
|
||||
type Visitor struct {}
|
||||
type Visitor struct{}
|
||||
|
||||
func (self *Visitor) visit(path string, f os.FileInfo, err error) error {
|
||||
if f == nil {
|
||||
return err
|
||||
}
|
||||
if f.IsDir() {
|
||||
return nil
|
||||
} else if (f.Mode() & os.ModeSymlink) > 0 {
|
||||
return nil
|
||||
} else {
|
||||
if strings.HasSuffix(f.Name(), ".md") {
|
||||
fmt.Println(f)
|
||||
file, err := os.Open(f.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
input, _ := ioutil.ReadAll(file)
|
||||
input = regexp.MustCompile("\\[(.*?)\\]\\(<?(.*?)\\.md>?\\)").ReplaceAll(input, []byte("[$1](<$2.html>)"))
|
||||
var out *os.File
|
||||
if out, err = os.Create(strings.Replace(f.Name(), ".md", ".html", -1)); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
defer out.Close()
|
||||
md := md2min.New("none")
|
||||
err = md.Parse(input, out)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Parsing Error", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
func (self *Visitor) md2html(arg map[string]string) error {
|
||||
from := arg["from"]
|
||||
to := arg["to"]
|
||||
err := filepath.Walk(from+"/", func(path string, f os.FileInfo, err error) error {
|
||||
if f == nil {
|
||||
return err
|
||||
}
|
||||
if f.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if (f.Mode() & os.ModeSymlink) > 0 {
|
||||
return nil
|
||||
}
|
||||
if !strings.HasSuffix(f.Name(), ".md") {
|
||||
return nil
|
||||
}
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input_byte, _ := ioutil.ReadAll(file)
|
||||
input := string(input_byte)
|
||||
input = regexp.MustCompile(`\[(.*?)\]\(<?(.*?)\.md>?\)`).ReplaceAllString(input, "[$1](<$2.html>)")
|
||||
|
||||
if f.Name() == "README.md" {
|
||||
input = regexp.MustCompile(`https:\/\/github\.com\/astaxie\/build-web-application-with-golang\/blob\/master\/`).ReplaceAllString(input, "")
|
||||
}
|
||||
|
||||
// 以#开头的行,在#后增加空格
|
||||
// 以#开头的行, 删除多余的空格
|
||||
input = FixHeader(input)
|
||||
|
||||
// 删除页面链接
|
||||
input = RemoveFooterLink(input)
|
||||
|
||||
var out *os.File
|
||||
filename := strings.Replace(f.Name(), ".md", ".html", -1)
|
||||
fmt.Println(to + "/" + filename)
|
||||
if out, err = os.Create(to + "/" + filename); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
defer out.Close()
|
||||
md := md2min.New("none")
|
||||
err = md.Parse([]byte(input), out)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Parsing Error", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func FixHeader(input string) string {
|
||||
re_header := regexp.MustCompile(`(?m)^#.+$`)
|
||||
re_sub := regexp.MustCompile(`^(#+)\s*(.+)$`)
|
||||
fixer := func(header string) string {
|
||||
s := re_sub.FindStringSubmatch(header)
|
||||
return s[1] + " " + s[2]
|
||||
}
|
||||
return nil
|
||||
return re_header.ReplaceAllStringFunc(input, fixer)
|
||||
}
|
||||
|
||||
func RemoveFooterLink(input string) string {
|
||||
re_footer := regexp.MustCompile(`(?m)^#{2,} links.*?\n(.+\n)*`)
|
||||
return re_footer.ReplaceAllString(input, "")
|
||||
}
|
||||
|
||||
func main() {
|
||||
v := &Visitor{}
|
||||
err := filepath.Walk("./", func(path string, f os.FileInfo, err error) error {
|
||||
return v.visit(path, f, err)
|
||||
})
|
||||
tmp := os.Getenv("TMP")
|
||||
if tmp == "" {
|
||||
tmp = "."
|
||||
}
|
||||
|
||||
workdir := os.Getenv("WORKDIR")
|
||||
if workdir == "" {
|
||||
workdir = "."
|
||||
}
|
||||
|
||||
arg := map[string]string{
|
||||
"from": workdir,
|
||||
"to": tmp,
|
||||
}
|
||||
|
||||
v := &Visitor{}
|
||||
err := v.md2html(arg)
|
||||
if err != nil {
|
||||
fmt.Printf("filepath.Walk() returned %v\n", err)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,46 @@
|
||||
#!/bin/sh
|
||||
|
||||
rm -f *.html *~
|
||||
bn="`basename $0`"
|
||||
WORKDIR="$(cd $(dirname $0); pwd -P)"
|
||||
|
||||
export GOPATH=`pwd`
|
||||
#
|
||||
# Default language: zh
|
||||
# You can overwrite following variables in config file.
|
||||
#
|
||||
MSG_INSTALL_PANDOC_FIRST='请先安装pandoc,然后再次运行'
|
||||
MSG_SUCCESSFULLY_GENERATED='build-web-application-with-golang.epub 已经建立'
|
||||
MSG_CREATOR='Astaxie'
|
||||
MSG_DESCRIPTION='一本开源的Go Web编程书籍'
|
||||
MSG_LANGUAGE='zh-CN'
|
||||
MSG_TITLE='Go Web编程'
|
||||
[ -e "$WORKDIR/config" ] && . "$WORKDIR/config"
|
||||
|
||||
#go get -u github.com/russross/blackfriday
|
||||
|
||||
TMP=`mktemp -d 2>/dev/null || mktemp -d -t "${bn}"` || exit 1
|
||||
trap 'rm -rf "$TMP"' 0 1 2 3 15
|
||||
|
||||
|
||||
cd "$TMP"
|
||||
|
||||
(
|
||||
[ go list github.com/fairlyblank/md2min >/dev/null 2>&1 ] || export GOPATH="$PWD"
|
||||
go get -u github.com/fairlyblank/md2min
|
||||
WORKDIR="$WORKDIR" TMP="$TMP" go run "$WORKDIR/build.go"
|
||||
)
|
||||
|
||||
go run build.go
|
||||
if [ ! type -P pandoc >/dev/null 2>&1 ]; then
|
||||
echo "$MSG_INSTALL_PANDOC_FIRST"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
cat <<__METADATA__ > metadata.txt
|
||||
<dc:creator>$MSG_CREATOR</dc:creator>
|
||||
<dc:description>$MSG_DESCRIPTION</dc:description>
|
||||
<dc:language>$MSG_LANGUAGE</dc:language>
|
||||
<dc:rights>Creative Commons</dc:rights>
|
||||
<dc:title>$MSG_TITLE</dc:title>
|
||||
__METADATA__
|
||||
|
||||
pandoc --reference-links -S --toc -f html -t epub --epub-metadata=metadata.txt --epub-cover-image="$WORKDIR/../images/cover.png" -o "$WORKDIR/../build-web-application-with-golang.epub" `ls [0-9]*.html | sort`
|
||||
|
||||
echo "$MSG_SUCCESSFULLY_GENERATED"
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
#!/bin/bash
|
||||
if ! which pandoc >/dev/null ;then
|
||||
echo "请先安装pandoc,然后再次运行"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
sed -i 's!https://github.com/astaxie/build-web-application-with-golang/blob/master/!!g' README.md
|
||||
for i in *.md;do
|
||||
#重新格式化md文件
|
||||
sed -i '/^[#]\{1,\}/s!^\([#]\{1,\}\)\([^#]\{1,\}\)!\1 \2!' $i #以#开头的行,在#后增加空格
|
||||
sed -i '/^[#]\{1,\}/s! ! !' $i #以#开头的行, 删除多余的空格
|
||||
#sed -i '/!\[\](images/s#images\(.*\)?raw=true#../Images\1#' $i
|
||||
#sed -i '/!\[\](images/s#images\(.*\)?raw=true#../images\1#' $i #处理md文件中的image src属性
|
||||
sed -i '/[#]\{2,\} links/,/[ ]\{0,\}Id\$.*/d' $i #删除页面链接
|
||||
done
|
||||
list="`ls [0-9]*.html |sort `"
|
||||
cat > metadata.txt <<EOF
|
||||
<dc:creator>Astaxie</dc:creator>
|
||||
<dc:description>一本开源的Go Web编程书籍</dc:description>
|
||||
<dc:language>zh-CN</dc:language>
|
||||
<dc:rights>Creative Commons</dc:rights>
|
||||
<dc:title>Go Web编程</dc:title>
|
||||
EOF
|
||||
|
||||
pandoc --reference-links -S --toc -f html -t epub --epub-metadata=metadata.txt --epub-cover-image=../images/cover.png \
|
||||
-o ../build-web-application-with-golang.epub $list
|
||||
|
||||
rm -rf html
|
||||
echo "build-web-application-with-golang.epub 已经建立"
|
||||
6
ja/ebook/config
Normal file
6
ja/ebook/config
Normal file
@@ -0,0 +1,6 @@
|
||||
MSG_INSTALL_PANDOC_FIRST='pandocをインストール後、再度実行してください。'
|
||||
MSG_SUCCESSFULLY_GENERATED='build-web-application-with-golang.epub を作成しました'
|
||||
MSG_CREATOR='Astaxie'
|
||||
MSG_DESCRIPTION='オープンソースのGo Webプログラミング書籍'
|
||||
MSG_LANGUAGE='ja-JP'
|
||||
MSG_TITLE='Go Webプログラミング'
|
||||
Reference in New Issue
Block a user