调用 github markdown api 实现 md 文件格式化 html
This commit is contained in:
145
zh/build_new.go
Normal file
145
zh/build_new.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"bufio"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const token = ""
|
||||
// 定义一个访问者结构体
|
||||
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
|
||||
}
|
||||
if f.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if (f.Mode() & os.ModeSymlink) > 0 {
|
||||
return nil
|
||||
}
|
||||
if !strings.HasSuffix(f.Name(), ".md") {
|
||||
return nil
|
||||
}
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input_byte, _ := ioutil.ReadAll(file)
|
||||
input := string(input_byte)
|
||||
input = regexp.MustCompile(`\[(.*?)\]\(<?(.*?)\.md>?\)`).ReplaceAllString(input, "[$1](<$2.html>)")
|
||||
|
||||
if f.Name() == "README.md" {
|
||||
input = regexp.MustCompile(`https:\/\/github\.com\/astaxie\/build-web-application-with-golang\/blob\/master\/`).ReplaceAllString(input, "")
|
||||
}
|
||||
|
||||
// 以#开头的行,在#后增加空格
|
||||
// 以#开头的行, 删除多余的空格
|
||||
input = FixHeader(input)
|
||||
|
||||
// 删除页面链接
|
||||
input = RemoveFooterLink(input)
|
||||
|
||||
// remove image suffix
|
||||
input = RemoveImageLinkSuffix(input)
|
||||
|
||||
var out *os.File
|
||||
filename := strings.Replace(f.Name(), ".md", ".html", -1)
|
||||
fmt.Println(to + "/" + filename)
|
||||
if out, err = os.Create(to + "/" + filename); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
defer out.Close()
|
||||
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("Authorization", "token " + token)
|
||||
//
|
||||
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 + string(body)) //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)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func FixHeader(input string) string {
|
||||
re_header := regexp.MustCompile(`(?m)^#.+$`)
|
||||
re_sub := regexp.MustCompile(`^(#+)\s*(.+)$`)
|
||||
fixer := func(header string) string {
|
||||
s := re_sub.FindStringSubmatch(header)
|
||||
return s[1] + " " + s[2]
|
||||
}
|
||||
return re_header.ReplaceAllStringFunc(input, fixer)
|
||||
}
|
||||
|
||||
func RemoveFooterLink(input string) string {
|
||||
re_footer := regexp.MustCompile(`(?m)^#{2,} links.*?\n(.+\n)*`)
|
||||
return re_footer.ReplaceAllString(input, "")
|
||||
}
|
||||
|
||||
func RemoveImageLinkSuffix(input string) string {
|
||||
re_footer := regexp.MustCompile(`png\?raw=true`)
|
||||
return re_footer.ReplaceAllString(input, "png")
|
||||
}
|
||||
|
||||
func main() {
|
||||
tmp := os.Getenv("TMP")
|
||||
if tmp == "" {
|
||||
tmp = "."
|
||||
}
|
||||
|
||||
workdir := os.Getenv("WORKDIR")
|
||||
if workdir == "" {
|
||||
workdir = "."
|
||||
}
|
||||
|
||||
arg := map[string]string{
|
||||
"from": workdir,
|
||||
"to": tmp,
|
||||
}
|
||||
|
||||
v := &Visitor{}
|
||||
err := v.md2html(arg)
|
||||
if err != nil {
|
||||
fmt.Printf("filepath.Walk() returned %v\n", err)
|
||||
}
|
||||
}
|
||||
59
zh/build_new.sh
Executable file
59
zh/build_new.sh
Executable file
@@ -0,0 +1,59 @@
|
||||
#!/bin/bash
|
||||
|
||||
SED='sed'
|
||||
|
||||
if [ `uname -s` == 'Darwin' ] ; then
|
||||
SED='gsed'
|
||||
fi
|
||||
|
||||
bn="`basename $0`"
|
||||
WORKDIR="$(cd $(dirname $0); pwd -P)"
|
||||
|
||||
#
|
||||
# Default language: zh
|
||||
# You can overwrite following variables in config file.
|
||||
#
|
||||
MSG_INSTALL_PANDOC_FIRST='请先安装pandoc,然后再次运行'
|
||||
MSG_SUCCESSFULLY_GENERATED='build-web-application-with-golang.epub 已经建立'
|
||||
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
|
||||
# TMP=./build
|
||||
trap 'rm -rf "$TMP"' 0 1 2 3 15
|
||||
|
||||
|
||||
cd "$TMP"
|
||||
|
||||
(
|
||||
# [ 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_new.go"
|
||||
)
|
||||
|
||||
if [ ! type -P pandoc >/dev/null 2>&1 ]; then
|
||||
echo "$MSG_INSTALL_PANDOC_FIRST"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
cat <<__METADATA__ > metadata.txt
|
||||
<dc:creator>$MSG_CREATOR</dc:creator>
|
||||
<dc:description>$MSG_DESCRIPTION</dc:description>
|
||||
<dc:language>$MSG_LANGUAGE</dc:language>
|
||||
<dc:rights>Creative Commons</dc:rights>
|
||||
<dc:title>$MSG_TITLE</dc:title>
|
||||
__METADATA__
|
||||
|
||||
mkdir -p $TMP/images
|
||||
cp -r $WORKDIR/images/* $TMP/images/
|
||||
ls [0-9]*.html | xargs $SED -i "s/png?raw=true/png/g"
|
||||
|
||||
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"
|
||||
Reference in New Issue
Block a user