Skip to content

Commit 58cfbca

Browse files
committed
Fix build by copying build files from English to Japanese version
1 parent 5dea1d8 commit 58cfbca

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

ja/build.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"regexp"
9+
"strings"
10+
11+
"github.com/fairlyblank/md2min"
12+
)
13+
14+
// 定义一个访问者结构体
15+
type Visitor struct{}
16+
17+
func (self *Visitor) md2html(arg map[string]string) error {
18+
from := arg["from"]
19+
to := arg["to"]
20+
err := filepath.Walk(from+"/", func(path string, f os.FileInfo, err error) error {
21+
if f == nil {
22+
return err
23+
}
24+
if f.IsDir() {
25+
return nil
26+
}
27+
if (f.Mode() & os.ModeSymlink) > 0 {
28+
return nil
29+
}
30+
if !strings.HasSuffix(f.Name(), ".md") {
31+
return nil
32+
}
33+
34+
file, err := os.Open(path)
35+
if err != nil {
36+
return err
37+
}
38+
39+
input_byte, _ := ioutil.ReadAll(file)
40+
input := string(input_byte)
41+
input = regexp.MustCompile(`\[(.*?)\]\(<?(.*?)\.md>?\)`).ReplaceAllString(input, "[$1](<$2.html>)")
42+
43+
if f.Name() == "README.md" {
44+
input = regexp.MustCompile(`https:\/\/github\.com\/astaxie\/build-web-application-with-golang\/blob\/master\/`).ReplaceAllString(input, "")
45+
}
46+
47+
// 以#开头的行,在#后增加空格
48+
// 以#开头的行, 删除多余的空格
49+
input = FixHeader(input)
50+
51+
// 删除页面链接
52+
input = RemoveFooterLink(input)
53+
54+
// remove image suffix
55+
input = RemoveImageLinkSuffix(input)
56+
57+
var out *os.File
58+
filename := strings.Replace(f.Name(), ".md", ".html", -1)
59+
fmt.Println(to + "/" + filename)
60+
if out, err = os.Create(to + "/" + filename); err != nil {
61+
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
62+
os.Exit(-1)
63+
}
64+
defer out.Close()
65+
md := md2min.New("none")
66+
err = md.Parse([]byte(input), out)
67+
if err != nil {
68+
fmt.Fprintln(os.Stderr, "Parsing Error", err)
69+
os.Exit(-1)
70+
}
71+
72+
return nil
73+
})
74+
return err
75+
}
76+
77+
func FixHeader(input string) string {
78+
re_header := regexp.MustCompile(`(?m)^#.+$`)
79+
re_sub := regexp.MustCompile(`^(#+)\s*(.+)$`)
80+
fixer := func(header string) string {
81+
s := re_sub.FindStringSubmatch(header)
82+
return s[1] + " " + s[2]
83+
}
84+
return re_header.ReplaceAllStringFunc(input, fixer)
85+
}
86+
87+
func RemoveFooterLink(input string) string {
88+
re_footer := regexp.MustCompile(`(?m)^#{2,} links.*?\n(.+\n)*`)
89+
return re_footer.ReplaceAllString(input, "")
90+
}
91+
92+
func RemoveImageLinkSuffix(input string) string {
93+
re_footer := regexp.MustCompile(`png\?raw=true`)
94+
return re_footer.ReplaceAllString(input, "png")
95+
}
96+
97+
func main() {
98+
tmp := os.Getenv("TMP")
99+
if tmp == "" {
100+
tmp = "."
101+
}
102+
103+
workdir := os.Getenv("WORKDIR")
104+
if workdir == "" {
105+
workdir = "."
106+
}
107+
108+
arg := map[string]string{
109+
"from": workdir,
110+
"to": tmp,
111+
}
112+
113+
v := &Visitor{}
114+
err := v.md2html(arg)
115+
if err != nil {
116+
fmt.Printf("filepath.Walk() returned %v\n", err)
117+
}
118+
}

ja/build.sh

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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='Astaxie'
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+
trap 'rm -rf "$TMP"' 0 1 2 3 15
27+
28+
29+
cd "$TMP"
30+
31+
(
32+
[ go list github.com/fairlyblank/md2min >/dev/null 2>&1 ] || export GOPATH="$PWD"
33+
go get -u github.com/fairlyblank/md2min
34+
WORKDIR="$WORKDIR" TMP="$TMP" go run "$WORKDIR/build.go"
35+
)
36+
37+
if [ ! type -P pandoc >/dev/null 2>&1 ]; then
38+
echo "$MSG_INSTALL_PANDOC_FIRST"
39+
exit 0
40+
fi
41+
42+
cat <<__METADATA__ > metadata.txt
43+
<dc:creator>$MSG_CREATOR</dc:creator>
44+
<dc:description>$MSG_DESCRIPTION</dc:description>
45+
<dc:language>$MSG_LANGUAGE</dc:language>
46+
<dc:rights>Creative Commons</dc:rights>
47+
<dc:title>$MSG_TITLE</dc:title>
48+
__METADATA__
49+
50+
mkdir -p $TMP/images
51+
cp -r $WORKDIR/images/* $TMP/images/
52+
ls [0-9]*.html | xargs $SED -i "s/png?raw=true/png/g"
53+
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+
56+
echo "$MSG_SUCCESSFULLY_GENERATED"

0 commit comments

Comments
 (0)