@@ -35,6 +35,23 @@ type DocumentController struct {
3535 BaseController
3636}
3737
38+ // 解析并提取版本控制的commit内容
39+ func parseGitCommit (str string ) (cont , commit string ) {
40+ var slice []string
41+ arr := strings .Split (str , "<git>" )
42+ if len (arr ) > 1 {
43+ slice = append (slice , arr [0 ])
44+ }
45+ str = strings .Join (arr [1 :], "" )
46+ arr = strings .Split (str , "</git>" )
47+ if len (arr ) > 1 {
48+ slice = append (slice , arr [1 :]... )
49+ commit = arr [0 ]
50+ }
51+ cont = strings .Join (slice , "" )
52+ return
53+ }
54+
3855//判断用户是否可以阅读文档.
3956func isReadable (identify , token string , this * DocumentController ) * models.BookResult {
4057 book , err := models .NewBook ().FindByFieldFirst ("identify" , identify )
@@ -877,14 +894,24 @@ func (this *DocumentController) Content() {
877894 content = this .replaceLinks (identify , content , is_summary )
878895
879896 var ds = models.DocumentStore {}
897+ var actionName string
880898
899+ // 替换掉<git></git>标签内容
881900 if markdown == "" && content != "" {
882901 ds .Markdown = content
883902 } else {
884903 ds .Markdown = markdown
885904 }
905+
906+ ds .Markdown , actionName = parseGitCommit (ds .Markdown )
907+ ds .Content , _ = parseGitCommit (content )
908+ if actionName == "" {
909+ actionName = "修改文档"
910+ } else {
911+ is_auto = true
912+ }
913+
886914 doc .Version = time .Now ().Unix ()
887- ds .Content = content
888915 if doc_id , err := doc .InsertOrUpdate (); err != nil {
889916 beego .Error ("InsertOrUpdate => " , err )
890917 this .JsonResult (6006 , "保存失败" )
@@ -894,33 +921,41 @@ func (this *DocumentController) Content() {
894921 beego .Error (err )
895922 }
896923 }
924+
897925 // TODO:内容存储为文档
898926 //如果启用了文档历史,则添加历史文档
899927 if this .EnableDocumentHistory > 0 {
900- history := models .NewDocumentHistory ()
901- history .DocumentId = doc_id
902- history .DocumentName = doc .DocumentName
903- history .ModifyAt = this .Member .MemberId
904- history .MemberId = doc .MemberId
905- history .ParentId = doc .ParentId
906- history .Version = time .Now ().Unix ()
907- history .Action = "modify"
908- history .ActionName = "修改文档"
909-
910- vc := models .NewVersionControl (doc_id , history .Version )
911- vc .SaveVersion (ds .Content , ds .Markdown )
912-
913- _ , err = history .InsertOrUpdate ()
914- if err != nil {
915- beego .Error ("DocumentHistory InsertOrUpdate => " , err )
928+ dom , err := goquery .NewDocumentFromReader (strings .NewReader (ds .Content ))
929+ if err == nil && len (dom .Text ()) > 0 { //空内容不存储版本
930+ history := models .NewDocumentHistory ()
931+ history .DocumentId = doc_id
932+ history .DocumentName = doc .DocumentName
933+ history .ModifyAt = this .Member .MemberId
934+ history .MemberId = doc .MemberId
935+ history .ParentId = doc .ParentId
936+ history .Version = time .Now ().Unix ()
937+ history .Action = "modify"
938+ history .ActionName = actionName
939+
940+ vc := models .NewVersionControl (doc_id , history .Version )
941+ vc .SaveVersion (ds .Content , ds .Markdown )
942+
943+ _ , err = history .InsertOrUpdate ()
944+ if err != nil {
945+ beego .Error ("DocumentHistory InsertOrUpdate => " , err )
946+ }
916947 }
948+
917949 }
950+
918951 if is_auto {
919952 errMsg = "auto"
920953 } else if is_summary {
921954 errMsg = "true"
922955 }
923956
957+ beego .Error ("IS_AUTO====" , is_auto , actionName , ds .Content )
958+
924959 //
925960 //doc.Content = ""
926961 doc .Release = ""
@@ -989,117 +1024,6 @@ func (this *DocumentController) Export() {
9891024
9901025}
9911026
992- //导出文件
993- //func (this *DocumentController) Export() {
994- // this.TplName = "document/export.html"
995- // identify := this.Ctx.Input.Param(":key")
996- // output := this.GetString("output")
997- // token := this.GetString("token")
998- // if identify == "" {
999- // this.Abort("404")
1000- // }
1001- //
1002- // //如果没有开启你们访问则跳转到登录
1003- // if !this.EnableAnonymous && this.Member == nil {
1004- // this.Redirect(beego.URLFor("AccountController.Login"), 302)
1005- // return
1006- // }
1007- // bookResult := models.NewBookResult()
1008- // if this.Member != nil && this.Member.IsAdministrator() {
1009- // book, err := models.NewBook().FindByIdentify(identify)
1010- // if err != nil {
1011- // beego.Error(err)
1012- // this.Abort("500")
1013- // }
1014- // bookResult = book.ToBookResult()
1015- // } else {
1016- // bookResult = isReadable(identify, token, this)
1017- // }
1018- //
1019- // if bookResult.PrivatelyOwned == 0 {
1020- // //TODO 私有项目禁止导出
1021- // }
1022- //
1023- // docs, err := models.NewDocument().FindListByBookId(bookResult.BookId)
1024- //
1025- // if err != nil {
1026- // beego.Error(err)
1027- // this.Abort("500")
1028- // }
1029- //
1030- // if output == "pdf" {
1031- //
1032- // exe := beego.AppConfig.String("wkhtmltopdf")
1033- //
1034- // if exe == "" {
1035- // this.TplName = "errors/error.html"
1036- // this.Data["ErrorMessage"] = "没有配置PDF导出程序"
1037- // this.Data["ErrorCode"] = 50010
1038- // return
1039- // }
1040- // dpath := "cache/" + bookResult.Identify
1041- //
1042- // os.MkdirAll(dpath, 0766)
1043- //
1044- // pathList := list.New()
1045- //
1046- // RecursiveFun(0, "", dpath, this, bookResult, docs, pathList)
1047- //
1048- // defer os.RemoveAll(dpath)
1049- //
1050- // os.MkdirAll("./cache", 0766)
1051- // pdfpath := filepath.Join("cache", identify+"_"+this.CruSession.SessionID()+".pdf")
1052- //
1053- // if _, err := os.Stat(pdfpath); os.IsNotExist(err) {
1054- //
1055- // wkhtmltopdf.SetPath(beego.AppConfig.String("wkhtmltopdf"))
1056- // pdfg, err := wkhtmltopdf.NewPDFGenerator()
1057- // pdfg.PageSize.Set(wkhtmltopdf.PageSizeA4)
1058- // pdfg.MarginBottom.Set(25)
1059- //
1060- // if err != nil {
1061- // beego.Error(err)
1062- // this.Abort("500")
1063- // }
1064- //
1065- // for e := pathList.Front(); e != nil; e = e.Next() {
1066- // if page, ok := e.Value.(string); ok {
1067- // pdfg.AddPage(wkhtmltopdf.NewPage(page))
1068- // }
1069- // }
1070- // pdfg.MoreArgs = append(pdfg.MoreArgs,
1071- // "--header-font-size", "8",
1072- // "--footer-right", "[page] / [toPage]",
1073- // "--footer-spacing", "5",
1074- // "--footer-html", "views/widgets/pdf_footer.html",
1075- // "--footer-font-size", "8",
1076- // )
1077- // //beego.Debug(pdfg.ArgString())
1078- // //TODO 处理页码和footer、header问题
1079- // //this.JsonResult(0, "1", pdfg.ArgString())
1080- //
1081- // err = pdfg.Create()
1082- // if err != nil {
1083- // beego.Error(err)
1084- // this.Abort("500")
1085- // }
1086- //
1087- // err = pdfg.WriteFile(pdfpath)
1088- // if err != nil {
1089- // beego.Error(err)
1090- // }
1091- // }
1092- //
1093- // this.Ctx.Output.Download(pdfpath, bookResult.BookName+".pdf")
1094- //
1095- // defer os.Remove(pdfpath)
1096- //
1097- // this.StopRun()
1098- // }
1099- //
1100- // this.Abort("404")
1101- //}
1102-
11031027//生成项目访问的二维码.
11041028
11051029func (this * DocumentController ) QrCode () {
0 commit comments