Skip to content

Commit cbf428d

Browse files
committed
Merge branches 'master' and 'master' of github.com:M2shad0w/build-web-application-with-golang
2 parents cff418a + 8137b13 commit cbf428d

6 files changed

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

zh/build.go

+39-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import (
77
"path/filepath"
88
"regexp"
99
"strings"
10+
"bufio"
11+
// "net/http"
12+
// "github.com/fairlyblank/md2min"
13+
"github.com/a8m/mark"
1014

11-
"github.com/fairlyblank/md2min"
1215
)
1316

1417
// 定义一个访问者结构体
@@ -17,6 +20,8 @@ type Visitor struct{}
1720
func (self *Visitor) md2html(arg map[string]string) error {
1821
from := arg["from"]
1922
to := arg["to"]
23+
s := `<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
24+
`
2025
err := filepath.Walk(from+"/", func(path string, f os.FileInfo, err error) error {
2126
if f == nil {
2227
return err
@@ -62,8 +67,39 @@ func (self *Visitor) md2html(arg map[string]string) error {
6267
os.Exit(-1)
6368
}
6469
defer out.Close()
65-
md := md2min.New("none")
66-
err = md.Parse([]byte(input), out)
70+
opts := mark.DefaultOptions()
71+
opts.Smartypants = true
72+
opts.Fractions = true
73+
// r1 := []rune(s1)
74+
m := mark.New(input, opts)
75+
// md := md2min.New("none")
76+
// err = md.Parse([]byte(input), out)
77+
// client := &http.Client{}
78+
79+
// req, err := http.NewRequest("POST", "https://api.github.com/markdown/raw", strings.NewReader(input))
80+
// if err != nil {
81+
// handle error
82+
// }
83+
84+
// req.Header.Set("Content-Type", "text/plain")
85+
// req.Header.Set("charset", "utf-8")
86+
// req.Header.Set("User-Agent", "m2shad0w")
87+
//
88+
// resp, err := client.Do(req)
89+
90+
// defer resp.Body.Close()
91+
92+
// body, err := ioutil.ReadAll(resp.Body)
93+
// if err != nil {
94+
// handle error
95+
// }
96+
97+
w := bufio.NewWriter(out)
98+
n4, err := w.WriteString(s + m.Render())
99+
fmt.Printf("wrote %d bytes\n", n4)
100+
// fmt.Printf("wrote %d bytes\n", n4)
101+
//使用 Flush 来确保所有缓存的操作已写入底层写入器。
102+
w.Flush()
67103
if err != nil {
68104
fmt.Fprintln(os.Stderr, "Parsing Error", err)
69105
os.Exit(-1)

zh/build.sh

+8-5
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@ WORKDIR="$(cd $(dirname $0); pwd -P)"
1515
#
1616
MSG_INSTALL_PANDOC_FIRST='请先安装pandoc,然后再次运行'
1717
MSG_SUCCESSFULLY_GENERATED='build-web-application-with-golang.epub 已经建立'
18-
MSG_CREATOR='Astaxie'
18+
MSG_CREATOR='M2shad0w'
1919
MSG_DESCRIPTION='一本开源的Go Web编程书籍'
2020
MSG_LANGUAGE='zh-CN'
2121
MSG_TITLE='Go Web编程'
2222
[ -e "$WORKDIR/config" ] && . "$WORKDIR/config"
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

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

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)