Skip to content

Commit 06a2440

Browse files
authored
Merge pull request astaxie#828 from M2shad0w/master
通过 github markdown api 格式化生成 html,并生成 kindle 图书
2 parents 962b530 + ff21610 commit 06a2440

6 files changed

+225
-8
lines changed
6.17 MB
Binary file not shown.
38.8 KB
Binary file not shown.

zh/build.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package main
22

33
import (
4+
"bufio"
45
"fmt"
6+
"github.com/a8m/mark"
57
"io/ioutil"
68
"os"
79
"path/filepath"
810
"regexp"
911
"strings"
10-
11-
"github.com/fairlyblank/md2min"
1212
)
1313

1414
// 定义一个访问者结构体
@@ -17,6 +17,8 @@ type Visitor struct{}
1717
func (self *Visitor) md2html(arg map[string]string) error {
1818
from := arg["from"]
1919
to := arg["to"]
20+
s := `<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21+
`
2022
err := filepath.Walk(from+"/", func(path string, f os.FileInfo, err error) error {
2123
if f == nil {
2224
return err
@@ -62,8 +64,15 @@ func (self *Visitor) md2html(arg map[string]string) error {
6264
os.Exit(-1)
6365
}
6466
defer out.Close()
65-
md := md2min.New("none")
66-
err = md.Parse([]byte(input), out)
67+
opts := mark.DefaultOptions()
68+
opts.Smartypants = true
69+
opts.Fractions = true
70+
// r1 := []rune(s1)
71+
m := mark.New(input, opts
72+
w := bufio.NewWriter(out)
73+
n4, err := w.WriteString(s + m.Render())
74+
fmt.Printf("wrote %d bytes\n", n4)
75+
w.Flush()
6776
if err != nil {
6877
fmt.Fprintln(os.Stderr, "Parsing Error", err)
6978
os.Exit(-1)

zh/build.sh

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ MSG_TITLE='Go Web编程'
2323

2424

2525
TMP=`mktemp -d 2>/dev/null || mktemp -d -t "${bn}"` || exit 1
26-
trap 'rm -rf "$TMP"' 0 1 2 3 15
26+
# TMP=./build
27+
# trap 'rm -rf "$TMP"' 0 1 2 3 15
2728

2829

2930
cd "$TMP"
3031

3132
(
32-
[ go list github.com/fairlyblank/md2min >/dev/null 2>&1 ] || export GOPATH="$PWD"
33-
go get -u github.com/fairlyblank/md2min
33+
[ go list github.com/a8m/mark >/dev/null 2>&1 ] || export GOPATH="$PWD"
34+
go get -u github.com/a8m/mark
3435
WORKDIR="$WORKDIR" TMP="$TMP" go run "$WORKDIR/build.go"
3536
)
3637

@@ -51,6 +52,8 @@ mkdir -p $TMP/images
5152
cp -r $WORKDIR/images/* $TMP/images/
5253
ls [0-9]*.html | xargs $SED -i "s/png?raw=true/png/g"
5354

54-
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`
55+
echo "工作目录$WORKDIR, 临时目录$TMP"
56+
57+
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`
5558

5659
echo "$MSG_SUCCESSFULLY_GENERATED"

zh/build_new.go

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
"os"
8+
"path/filepath"
9+
"regexp"
10+
"strings"
11+
)
12+
13+
// 开发者 github token
14+
const token = ""
15+
16+
// 定义一个访问者结构体
17+
type Visitor struct{}
18+
19+
func (self *Visitor) md2html(arg map[string]string) error {
20+
from := arg["from"]
21+
to := arg["to"]
22+
s := `<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
23+
`
24+
err := filepath.Walk(from+"/", func(path string, f os.FileInfo, err error) error {
25+
if f == nil {
26+
return err
27+
}
28+
if f.IsDir() {
29+
return nil
30+
}
31+
if (f.Mode() & os.ModeSymlink) > 0 {
32+
return nil
33+
}
34+
if !strings.HasSuffix(f.Name(), ".md") {
35+
return nil
36+
}
37+
38+
file, err := os.Open(path)
39+
if err != nil {
40+
return err
41+
}
42+
43+
input_byte, _ := ioutil.ReadAll(file)
44+
input := string(input_byte)
45+
input = regexp.MustCompile(`\[(.*?)\]\(<?(.*?)\.md>?\)`).ReplaceAllString(input, "[$1](<$2.html>)")
46+
47+
if f.Name() == "README.md" {
48+
input = regexp.MustCompile(`https:\/\/github\.com\/astaxie\/build-web-application-with-golang\/blob\/master\/`).ReplaceAllString(input, "")
49+
}
50+
51+
// 以#开头的行,在#后增加空格
52+
// 以#开头的行, 删除多余的空格
53+
input = FixHeader(input)
54+
55+
// 删除页面链接
56+
input = RemoveFooterLink(input)
57+
58+
// remove image suffix
59+
input = RemoveImageLinkSuffix(input)
60+
61+
var out *os.File
62+
filename := strings.Replace(f.Name(), ".md", ".html", -1)
63+
fmt.Println(to + "/" + filename)
64+
if out, err = os.Create(to + "/" + filename); err != nil {
65+
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
66+
os.Exit(-1)
67+
}
68+
defer out.Close()
69+
client := &http.Client{}
70+
71+
req, err := http.NewRequest("POST", "https://api.github.com/markdown/raw", strings.NewReader(input))
72+
if err != nil {
73+
// handle error
74+
}
75+
76+
req.Header.Set("Content-Type", "text/plain")
77+
req.Header.Set("charset", "utf-8")
78+
req.Header.Set("Authorization", "token "+token)
79+
//
80+
resp, err := client.Do(req)
81+
82+
defer resp.Body.Close()
83+
84+
body, err := ioutil.ReadAll(resp.Body)
85+
if err != nil {
86+
// handle error
87+
}
88+
89+
w := bufio.NewWriter(out)
90+
n4, err := w.WriteString(s + string(body)) //m.Render()
91+
fmt.Printf("wrote %d bytes\n", n4)
92+
// fmt.Printf("wrote %d bytes\n", n4)
93+
//使用 Flush 来确保所有缓存的操作已写入底层写入器。
94+
w.Flush()
95+
if err != nil {
96+
fmt.Fprintln(os.Stderr, "Parsing Error", err)
97+
os.Exit(-1)
98+
}
99+
100+
return nil
101+
})
102+
return err
103+
}
104+
105+
func FixHeader(input string) string {
106+
re_header := regexp.MustCompile(`(?m)^#.+$`)
107+
re_sub := regexp.MustCompile(`^(#+)\s*(.+)$`)
108+
fixer := func(header string) string {
109+
s := re_sub.FindStringSubmatch(header)
110+
return s[1] + " " + s[2]
111+
}
112+
return re_header.ReplaceAllStringFunc(input, fixer)
113+
}
114+
115+
func RemoveFooterLink(input string) string {
116+
re_footer := regexp.MustCompile(`(?m)^#{2,} links.*?\n(.+\n)*`)
117+
return re_footer.ReplaceAllString(input, "")
118+
}
119+
120+
func RemoveImageLinkSuffix(input string) string {
121+
re_footer := regexp.MustCompile(`png\?raw=true`)
122+
return re_footer.ReplaceAllString(input, "png")
123+
}
124+
125+
func main() {
126+
tmp := os.Getenv("TMP")
127+
if tmp == "" {
128+
tmp = "."
129+
}
130+
131+
workdir := os.Getenv("WORKDIR")
132+
if workdir == "" {
133+
workdir = "."
134+
}
135+
136+
arg := map[string]string{
137+
"from": workdir,
138+
"to": tmp,
139+
}
140+
141+
v := &Visitor{}
142+
err := v.md2html(arg)
143+
if err != nil {
144+
fmt.Printf("filepath.Walk() returned %v\n", err)
145+
}
146+
}

zh/build_new.sh

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
SED='sed'
4+
5+
if [ `uname -s` == 'Darwin' ] ; then
6+
SED='gsed'
7+
fi
8+
9+
bn="`basename $0`"
10+
WORKDIR="$(cd $(dirname $0); pwd -P)"
11+
12+
#
13+
# Default language: zh
14+
# You can overwrite following variables in config file.
15+
#
16+
MSG_INSTALL_PANDOC_FIRST='请先安装pandoc,然后再次运行'
17+
MSG_SUCCESSFULLY_GENERATED='build-web-application-with-golang.epub 已经建立'
18+
MSG_CREATOR='M2shad0w'
19+
MSG_DESCRIPTION='一本开源的Go Web编程书籍'
20+
MSG_LANGUAGE='zh-CN'
21+
MSG_TITLE='Go Web编程'
22+
[ -e "$WORKDIR/config" ] && . "$WORKDIR/config"
23+
24+
25+
TMP=`mktemp -d 2>/dev/null || mktemp -d -t "${bn}"` || exit 1
26+
# TMP=./build
27+
trap 'rm -rf "$TMP"' 0 1 2 3 15
28+
29+
30+
cd "$TMP"
31+
32+
(
33+
# [ go list github.com/a8m/mark >/dev/null 2>&1 ] || export GOPATH="$PWD"
34+
# go get -u github.com/a8m/mark
35+
WORKDIR="$WORKDIR" TMP="$TMP" go run "$WORKDIR/build_new.go"
36+
)
37+
38+
if [ ! type -P pandoc >/dev/null 2>&1 ]; then
39+
echo "$MSG_INSTALL_PANDOC_FIRST"
40+
exit 0
41+
fi
42+
43+
cat <<__METADATA__ > metadata.txt
44+
<dc:creator>$MSG_CREATOR</dc:creator>
45+
<dc:description>$MSG_DESCRIPTION</dc:description>
46+
<dc:language>$MSG_LANGUAGE</dc:language>
47+
<dc:rights>Creative Commons</dc:rights>
48+
<dc:title>$MSG_TITLE</dc:title>
49+
__METADATA__
50+
51+
mkdir -p $TMP/images
52+
cp -r $WORKDIR/images/* $TMP/images/
53+
ls [0-9]*.html | xargs $SED -i "s/png?raw=true/png/g"
54+
55+
echo "工作目录$WORKDIR, 临时目录$TMP"
56+
57+
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`
58+
59+
echo "$MSG_SUCCESSFULLY_GENERATED"

0 commit comments

Comments
 (0)