Skip to content

Commit 1e72a58

Browse files
committed
mark 库格式化 md
1 parent 9e52360 commit 1e72a58

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed
38.8 KB
Binary file not shown.

zh/build.go

Lines changed: 39 additions & 3 deletions
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

Lines changed: 8 additions & 5 deletions
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"

0 commit comments

Comments
 (0)