Skip to content

Commit

Permalink
mark 库格式化 md
Browse files Browse the repository at this point in the history
  • Loading branch information
M2shad0w committed Apr 17, 2017
1 parent 9e52360 commit 1e72a58
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
Binary file modified zh/build-web-application-with-golang.epub
Binary file not shown.
42 changes: 39 additions & 3 deletions zh/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"path/filepath"
"regexp"
"strings"
"bufio"
// "net/http"
// "github.com/fairlyblank/md2min"
"github.com/a8m/mark"

"github.com/fairlyblank/md2min"
)

// 定义一个访问者结构体
Expand All @@ -17,6 +20,8 @@ type Visitor struct{}
func (self *Visitor) md2html(arg map[string]string) error {
from := arg["from"]
to := arg["to"]
s := `<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
`
err := filepath.Walk(from+"/", func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
Expand Down Expand Up @@ -62,8 +67,39 @@ func (self *Visitor) md2html(arg map[string]string) error {
os.Exit(-1)
}
defer out.Close()
md := md2min.New("none")
err = md.Parse([]byte(input), out)
opts := mark.DefaultOptions()
opts.Smartypants = true
opts.Fractions = true
// r1 := []rune(s1)
m := mark.New(input, opts)
// md := md2min.New("none")
// err = md.Parse([]byte(input), out)
// client := &http.Client{}

// req, err := http.NewRequest("POST", "https://api.github.com/markdown/raw", strings.NewReader(input))
// if err != nil {
// handle error
// }

// req.Header.Set("Content-Type", "text/plain")
// req.Header.Set("charset", "utf-8")
// req.Header.Set("User-Agent", "m2shad0w")
//
// resp, err := client.Do(req)

// defer resp.Body.Close()

// body, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// handle error
// }

w := bufio.NewWriter(out)
n4, err := w.WriteString(s + m.Render())
fmt.Printf("wrote %d bytes\n", n4)
// fmt.Printf("wrote %d bytes\n", n4)
//使用 Flush 来确保所有缓存的操作已写入底层写入器。
w.Flush()
if err != nil {
fmt.Fprintln(os.Stderr, "Parsing Error", err)
os.Exit(-1)
Expand Down
13 changes: 8 additions & 5 deletions zh/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ WORKDIR="$(cd $(dirname $0); pwd -P)"
#
MSG_INSTALL_PANDOC_FIRST='请先安装pandoc,然后再次运行'
MSG_SUCCESSFULLY_GENERATED='build-web-application-with-golang.epub 已经建立'
MSG_CREATOR='Astaxie'
MSG_CREATOR='M2shad0w'
MSG_DESCRIPTION='一本开源的Go Web编程书籍'
MSG_LANGUAGE='zh-CN'
MSG_TITLE='Go Web编程'
[ -e "$WORKDIR/config" ] && . "$WORKDIR/config"


TMP=`mktemp -d 2>/dev/null || mktemp -d -t "${bn}"` || exit 1
trap 'rm -rf "$TMP"' 0 1 2 3 15
# TMP=./build
# 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
[ go list github.com/a8m/mark >/dev/null 2>&1 ] || export GOPATH="$PWD"
go get -u github.com/a8m/mark
WORKDIR="$WORKDIR" TMP="$TMP" go run "$WORKDIR/build.go"
)

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

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 "工作目录$WORKDIR, 临时目录$TMP"

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"

0 comments on commit 1e72a58

Please sign in to comment.