Skip to content

Commit dfe2104

Browse files
committed
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.
1 parent a0ca9d3 commit dfe2104

File tree

4 files changed

+130
-68
lines changed

4 files changed

+130
-68
lines changed

ebook/build.go

Lines changed: 85 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,98 @@ import (
1111
)
1212

1313
// 定义一个访问者结构体
14-
type Visitor struct {}
14+
type Visitor struct{}
1515

16-
func (self *Visitor) visit(path string, f os.FileInfo, err error) error {
17-
if f == nil {
18-
return err
19-
}
20-
if f.IsDir() {
21-
return nil
22-
} else if (f.Mode() & os.ModeSymlink) > 0 {
23-
return nil
24-
} else {
25-
if strings.HasSuffix(f.Name(), ".md") {
26-
fmt.Println(f)
27-
file, err := os.Open(f.Name())
28-
if err != nil {
29-
return err
30-
}
31-
input, _ := ioutil.ReadAll(file)
32-
input = regexp.MustCompile("\\[(.*?)\\]\\(<?(.*?)\\.md>?\\)").ReplaceAll(input, []byte("[$1](<$2.html>)"))
33-
var out *os.File
34-
if out, err = os.Create(strings.Replace(f.Name(), ".md", ".html", -1)); err != nil {
35-
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
36-
os.Exit(-1)
37-
}
38-
defer out.Close()
39-
md := md2min.New("none")
40-
err = md.Parse(input, out)
41-
if err != nil {
42-
fmt.Fprintln(os.Stderr, "Parsing Error", err)
43-
os.Exit(-1)
44-
}
16+
func (self *Visitor) md2html(arg map[string]string) error {
17+
from := arg["from"]
18+
to := arg["to"]
19+
err := filepath.Walk(from+"/", func(path string, f os.FileInfo, err error) error {
20+
if f == nil {
21+
return err
22+
}
23+
if f.IsDir() {
24+
return nil
25+
}
26+
if (f.Mode() & os.ModeSymlink) > 0 {
27+
return nil
28+
}
29+
if !strings.HasSuffix(f.Name(), ".md") {
30+
return nil
31+
}
32+
33+
file, err := os.Open(path)
34+
if err != nil {
35+
return err
36+
}
37+
38+
input_byte, _ := ioutil.ReadAll(file)
39+
input := string(input_byte)
40+
input = regexp.MustCompile(`\[(.*?)\]\(<?(.*?)\.md>?\)`).ReplaceAllString(input, "[$1](<$2.html>)")
41+
42+
if f.Name() == "README.md" {
43+
input = regexp.MustCompile(`https:\/\/github\.com\/astaxie\/build-web-application-with-golang\/blob\/master\/`).ReplaceAllString(input, "")
44+
}
45+
46+
// 以#开头的行,在#后增加空格
47+
// 以#开头的行, 删除多余的空格
48+
input = FixHeader(input)
49+
50+
// 删除页面链接
51+
input = RemoveFooterLink(input)
52+
53+
var out *os.File
54+
filename := strings.Replace(f.Name(), ".md", ".html", -1)
55+
fmt.Println(to + "/" + filename)
56+
if out, err = os.Create(to + "/" + filename); err != nil {
57+
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
58+
os.Exit(-1)
59+
}
60+
defer out.Close()
61+
md := md2min.New("none")
62+
err = md.Parse([]byte(input), out)
63+
if err != nil {
64+
fmt.Fprintln(os.Stderr, "Parsing Error", err)
65+
os.Exit(-1)
4566
}
67+
68+
return nil
69+
})
70+
return err
71+
}
72+
73+
func FixHeader(input string) string {
74+
re_header := regexp.MustCompile(`(?m)^#.+$`)
75+
re_sub := regexp.MustCompile(`^(#+)\s*(.+)$`)
76+
fixer := func(header string) string {
77+
s := re_sub.FindStringSubmatch(header)
78+
return s[1] + " " + s[2]
4679
}
47-
return nil
80+
return re_header.ReplaceAllStringFunc(input, fixer)
81+
}
82+
83+
func RemoveFooterLink(input string) string {
84+
re_footer := regexp.MustCompile(`(?m)^#{2,} links.*?\n(.+\n)*`)
85+
return re_footer.ReplaceAllString(input, "")
4886
}
4987

5088
func main() {
51-
v := &Visitor{}
52-
err := filepath.Walk("./", func(path string, f os.FileInfo, err error) error {
53-
return v.visit(path, f, err)
54-
})
89+
tmp := os.Getenv("TMP")
90+
if tmp == "" {
91+
tmp = "."
92+
}
93+
94+
workdir := os.Getenv("WORKDIR")
95+
if workdir == "" {
96+
workdir = "."
97+
}
5598

99+
arg := map[string]string{
100+
"from": workdir,
101+
"to": tmp,
102+
}
103+
104+
v := &Visitor{}
105+
err := v.md2html(arg)
56106
if err != nil {
57107
fmt.Printf("filepath.Walk() returned %v\n", err)
58108
}

ebook/build.sh

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
#!/bin/sh
22

3-
rm -f *.html *~
3+
bn="`basename $0`"
4+
WORKDIR="$(cd $(dirname $0); pwd -P)"
45

5-
export GOPATH=`pwd`
6+
#
7+
# Default language: zh
8+
# You can overwrite following variables in config file.
9+
#
10+
MSG_INSTALL_PANDOC_FIRST='请先安装pandoc,然后再次运行'
11+
MSG_SUCCESSFULLY_GENERATED='build-web-application-with-golang.epub 已经建立'
12+
MSG_CREATOR='Astaxie'
13+
MSG_DESCRIPTION='一本开源的Go Web编程书籍'
14+
MSG_LANGUAGE='zh-CN'
15+
MSG_TITLE='Go Web编程'
16+
[ -e "$WORKDIR/config" ] && . "$WORKDIR/config"
617

7-
#go get -u github.com/russross/blackfriday
18+
19+
TMP=`mktemp -d 2>/dev/null || mktemp -d -t "${bn}"` || exit 1
20+
trap 'rm -rf "$TMP"' 0 1 2 3 15
21+
22+
23+
cd "$TMP"
24+
25+
(
26+
[ go list github.com/fairlyblank/md2min >/dev/null 2>&1 ] || export GOPATH="$PWD"
827
go get -u github.com/fairlyblank/md2min
28+
WORKDIR="$WORKDIR" TMP="$TMP" go run "$WORKDIR/build.go"
29+
)
30+
31+
if [ ! type -P pandoc >/dev/null 2>&1 ]; then
32+
echo "$MSG_INSTALL_PANDOC_FIRST"
33+
exit 0
34+
fi
35+
36+
cat <<__METADATA__ > metadata.txt
37+
<dc:creator>$MSG_CREATOR</dc:creator>
38+
<dc:description>$MSG_DESCRIPTION</dc:description>
39+
<dc:language>$MSG_LANGUAGE</dc:language>
40+
<dc:rights>Creative Commons</dc:rights>
41+
<dc:title>$MSG_TITLE</dc:title>
42+
__METADATA__
943

10-
go run build.go
44+
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`
1145

46+
echo "$MSG_SUCCESSFULLY_GENERATED"

ebook/genepub.sh

Lines changed: 0 additions & 29 deletions
This file was deleted.

ja/ebook/config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MSG_INSTALL_PANDOC_FIRST='pandocをインストール後、再度実行してください。'
2+
MSG_SUCCESSFULLY_GENERATED='build-web-application-with-golang.epub を作成しました'
3+
MSG_CREATOR='Astaxie'
4+
MSG_DESCRIPTION='オープンソースのGo Webプログラミング書籍'
5+
MSG_LANGUAGE='ja-JP'
6+
MSG_TITLE='Go Webプログラミング'

0 commit comments

Comments
 (0)