diff --git a/controllers/document.go b/controllers/document.go index cd35e52d..af986f99 100644 --- a/controllers/document.go +++ b/controllers/document.go @@ -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 { diff --git a/controllers/manager.go b/controllers/manager.go index 63f26084..1421032c 100644 --- a/controllers/manager.go +++ b/controllers/manager.go @@ -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 } diff --git a/models/book_result.go b/models/book_result.go index 57a5e27e..9d6227a2 100644 --- a/models/book_result.go +++ b/models/book_result.go @@ -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 +} + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/utils/ziptil/ziptil.go b/utils/ziptil/ziptil.go index 3711c8dd..f4a4a2f8 100644 --- a/utils/ziptil/ziptil.go +++ b/utils/ziptil/ziptil.go @@ -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 +} + + + + + + diff --git a/views/document/default_read.tpl b/views/document/default_read.tpl index 8e3a9f7c..bb41927c 100644 --- a/views/document/default_read.tpl +++ b/views/document/default_read.tpl @@ -74,6 +74,9 @@
  • EPUB
  • MOBI
  • Word
  • + {{if eq .Model.Editor "markdown"}} +
  • Markdown
  • + {{end}} {{end}}