实现导出Markdown源文件

pull/219/merge
Minho 2018-03-12 18:24:58 +08:00
parent 13b9e1a1cb
commit 34654ed4d4
5 changed files with 204 additions and 4 deletions

View File

@ -860,7 +860,6 @@ func (c *DocumentController) Content() {
// 导出
func (c *DocumentController) Export() {
c.Prepare()
c.TplName = "document/export.tpl"
identify := c.Ctx.Input.Param(":key")
if identify == "" {
@ -896,6 +895,21 @@ func (c *DocumentController) Export() {
bookResult.Cover = c.BaseUrl() + bookResult.Cover
}
if output == "markdown" {
if bookResult.Editor != "markdown"{
c.ShowErrorPage(500,"当前项目不支持Markdown编辑器")
}
p,err := bookResult.ExportMarkdown(c.CruSession.SessionID())
if err != nil {
c.ShowErrorPage(500,"导出文档失败")
}
c.Ctx.Output.Download(p, bookResult.BookName+".zip")
c.StopRun()
return
}
eBookResult, err := bookResult.Converter(c.CruSession.SessionID())
if err != nil {

View File

@ -16,6 +16,7 @@ import (
"strconv"
"github.com/lifei6671/mindoc/utils/pagination"
"math"
"gopkg.in/russross/blackfriday.v2"
)
type ManagerController struct {
@ -297,7 +298,9 @@ func (c *ManagerController) Books() {
} else {
c.Data["PageHtml"] = ""
}
for i,book := range books {
books[i].Description = utils.StripTags(string(blackfriday.Run([]byte(book.Description))))
}
c.Data["Lists"] = books
}

View File

@ -18,6 +18,7 @@ import (
"github.com/lifei6671/mindoc/converter"
"github.com/lifei6671/mindoc/utils"
"gopkg.in/russross/blackfriday.v2"
"github.com/lifei6671/mindoc/utils/ziptil"
)
type BookResult struct {
@ -360,3 +361,104 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) {
return convertBookResult, nil
}
//导出Markdown原始文件
func (m *BookResult) ExportMarkdown(sessionId string)(string, error){
outputPath := filepath.Join(conf.WorkingDirectory,"uploads","books", strconv.Itoa(m.BookId), "book.zip")
os.MkdirAll(filepath.Dir(outputPath),0644)
tempOutputPath := filepath.Join(os.TempDir(),sessionId,"markdown")
defer os.RemoveAll(tempOutputPath)
err := exportMarkdown(tempOutputPath,0,m.BookId)
if err != nil {
return "",err
}
if err := ziptil.Compress(outputPath,tempOutputPath);err != nil {
beego.Error("导出Markdown失败=>",err)
return "",err
}
return outputPath,nil
}
func exportMarkdown(p string,parentId int,bookId int) (error){
o := orm.NewOrm()
var docs []*Document
_,err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("book_id",bookId).Filter("parent_id",parentId).All(&docs)
if err != nil {
beego.Error("导出Markdown失败=>",err)
return err
}
for _,doc := range docs {
//获取当前文档的子文档数量如果数量不为0则将当前文档命名为READMD.md并设置成目录。
subDocCount,err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("parent_id",doc.DocumentId).Count()
if err != nil {
beego.Error("导出Markdown失败=>",err)
return err
}
var docPath string
if subDocCount > 0 {
if doc.Identify != "" {
docPath = filepath.Join(p, doc.Identify,"README.md")
} else {
docPath = filepath.Join(p, strconv.Itoa(doc.DocumentId),"README.md")
}
}else{
if doc.Identify != "" {
docPath = filepath.Join(p, doc.Identify + ".md")
} else {
docPath = filepath.Join(p, doc.DocumentName + ".md")
}
}
dirPath := filepath.Dir(docPath);
os.MkdirAll(dirPath,0766)
if err := ioutil.WriteFile(docPath,[]byte(doc.Markdown),0644);err != nil {
beego.Error("导出Markdown失败=>",err)
return err
}
if subDocCount > 0 {
if err = exportMarkdown(dirPath,doc.DocumentId,bookId);err != nil {
return err
}
}
}
return nil
}

View File

@ -87,10 +87,10 @@ func Zip(dest string, filepath ...string) (err error) {
if fw, err := w.Create(strings.TrimLeft(file.Path, "./")); err != nil {
return err
} else {
if filecontent, err := ioutil.ReadFile(file.Path); err != nil {
if fileContent, err := ioutil.ReadFile(file.Path); err != nil {
return err
} else {
if _, err = fw.Write(filecontent); err != nil {
if _, err = fw.Write(fileContent); err != nil {
return err
}
}
@ -99,3 +99,81 @@ func Zip(dest string, filepath ...string) (err error) {
}
return
}
func Compress(dst string,src string) (err error) {
d, _ := os.Create(dst)
defer d.Close()
w := zip.NewWriter(d)
defer w.Close()
src = strings.Replace(src,"\\","/",-1)
f, err := os.Open(src)
if err != nil {
return err
}
//prefix := src[strings.LastIndex(src,"/"):]
err = compress(f, "", w)
if err != nil {
return err
}
return nil
}
func compress(file *os.File, prefix string, zw *zip.Writer) error {
info, err := file.Stat()
if err != nil {
return err
}
if info.IsDir() {
if prefix != "" {
prefix = prefix + "/" + info.Name()
}else{
prefix = info.Name()
}
fileInfos, err := file.Readdir(-1)
if err != nil {
return err
}
for _, fi := range fileInfos {
f, err := os.Open(file.Name() + "/" + fi.Name())
if err != nil {
return err
}
err = compress(f, prefix, zw)
if err != nil {
return err
}
}
} else {
header, err := zip.FileInfoHeader(info)
if prefix != "" {
header.Name = prefix + "/" + header.Name
}
if err != nil {
return err
}
writer, err := zw.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, file)
file.Close()
if err != nil {
return err
}
}
return nil
}

View File

@ -74,6 +74,9 @@
<li><a href="{{urlfor "DocumentController.Export" ":key" .Model.Identify "output" "epub"}}" target="_blank">EPUB</a> </li>
<li><a href="{{urlfor "DocumentController.Export" ":key" .Model.Identify "output" "mobi"}}" target="_blank">MOBI</a> </li>
<li><a href="{{urlfor "DocumentController.Export" ":key" .Model.Identify "output" "docx"}}" target="_blank">Word</a> </li>
{{if eq .Model.Editor "markdown"}}
<li><a href="{{urlfor "DocumentController.Export" ":key" .Model.Identify "output" "markdown"}}" target="_blank">Markdown</a> </li>
{{end}}
</ul>
</div>
{{end}}