修改目录结构
This commit is contained in:
62
ebook/build.go
Normal file
62
ebook/build.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/russross/blackfriday"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 定义一个访问者结构体
|
||||
type Visitor struct{}
|
||||
|
||||
func (self *Visitor) visit(path string, f os.FileInfo, err error) error {
|
||||
if f == nil {
|
||||
return err
|
||||
}
|
||||
if f.IsDir() {
|
||||
return nil
|
||||
} else if (f.Mode() & os.ModeSymlink) > 0 {
|
||||
return nil
|
||||
} else {
|
||||
if strings.HasSuffix(f.Name(), ".md") {
|
||||
fmt.Println(f)
|
||||
file, err := os.Open(f.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
input, _ := ioutil.ReadAll(file)
|
||||
input = regexp.MustCompile("\\[(.*?)\\]\\(<?(.*?)\\.md>?\\)").ReplaceAll(input, []byte("[$1](<$2.html>)"))
|
||||
output := blackfriday.MarkdownCommon(input)
|
||||
var out *os.File
|
||||
if out, err = os.Create(strings.Replace(f.Name(), ".md", ".html", -1)); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
defer out.Close()
|
||||
header := "<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\"/>\n</head>\n"
|
||||
footer := "</html>\n"
|
||||
out.Write([]byte(header))
|
||||
if _, err = out.Write(output); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Error writing output:", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
out.Write([]byte(footer))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
v := &Visitor{}
|
||||
err := filepath.Walk("./", func(path string, f os.FileInfo, err error) error {
|
||||
return v.visit(path, f, err)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("filepath.Walk() returned %v\n", err)
|
||||
}
|
||||
}
|
||||
10
ebook/build.sh
Normal file
10
ebook/build.sh
Normal file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/sh
|
||||
|
||||
rm -f *.html *~
|
||||
|
||||
export GOPATH=`pwd`
|
||||
|
||||
go get -u github.com/russross/blackfriday
|
||||
|
||||
go run build.go
|
||||
|
||||
32
ebook/genepub.sh
Normal file
32
ebook/genepub.sh
Normal file
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
if ! which pandoc >/dev/null ;then
|
||||
echo "请先安装pandoc,然后再次运行"
|
||||
exit 0
|
||||
fi
|
||||
[ -f build-web-application-with-golang ] || go build
|
||||
[ -d html ] || mkdir html
|
||||
pushd html >/dev/null; cp ../*.md .
|
||||
sed -i 's!https://github.com/astaxie/build-web-application-with-golang/blob/master/!!g' README.md
|
||||
for i in *.md;do
|
||||
#重新格式化md文件
|
||||
sed -i '/^[#]\{1,\}/s!^\([#]\{1,\}\)\([^#]\{1,\}\)!\1 \2!' $i #以#开头的行,在#后增加空格
|
||||
sed -i '/^[#]\{1,\}/s! ! !' $i #以#开头的行, 删除多余的空格
|
||||
#sed -i '/!\[\](images/s#images\(.*\)?raw=true#../Images\1#' $i
|
||||
sed -i '/!\[\](images/s#images\(.*\)?raw=true#../images\1#' $i #处理md文件中的image src属性
|
||||
sed -i '/[#]\{2,\} links/,/[ ]\{0,\}Id\$.*/d' $i #删除页面链接
|
||||
done
|
||||
../build-web-application-with-golang >/dev/null
|
||||
list="README.html `ls [0-9]*.html |sort -h` LICENSE.html"
|
||||
cat > metadata.txt <<EOF
|
||||
<dc:creator>Astaxie</dc:creator>
|
||||
<dc:description>一本开源的Go Web编程书籍</dc:description>
|
||||
<dc:language>zh-CN</dc:language>
|
||||
<dc:rights>Creative Commons</dc:rights>
|
||||
<dc:title>Go Web编程</dc:title>
|
||||
EOF
|
||||
|
||||
pandoc --reference-links -S --toc -f html -t epub --epub-metadata=metadata.txt --epub-cover-image=../images/cover.png \
|
||||
-o ../build-web-application-with-golang.epub $list
|
||||
popd >/dev/null
|
||||
rm -rf html
|
||||
echo "build-web-application-with-golang.epub 已经建立"
|
||||
13
ebook/src/1.2/main.go
Normal file
13
ebook/src/1.2/main.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// 章节 1.2
|
||||
// $GOPATH/src/mathapp/main.go
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mymath"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
|
||||
}
|
||||
11
ebook/src/1.2/sqrt.go
Normal file
11
ebook/src/1.2/sqrt.go
Normal file
@@ -0,0 +1,11 @@
|
||||
// 章节 1.2
|
||||
// $GOPATH/src/mymath/sqrt.go
|
||||
package mymath
|
||||
|
||||
func Sqrt(x float64) float64 {
|
||||
z := 0.0
|
||||
for i := 0; i < 1000; i++ {
|
||||
z -= (z*z - x) / (2 * x)
|
||||
}
|
||||
return z
|
||||
}
|
||||
Reference in New Issue
Block a user