11package controllers
22
33import (
4+ "bytes"
45 "encoding/json"
56 "errors"
67 "fmt"
@@ -64,9 +65,11 @@ func (this *BookController) Index() {
6465 this .Data ["SettingBook" ] = true
6566 this .TplName = "book/index.html"
6667 private , _ := this .GetInt ("private" , 1 ) //是否是私有文档
68+ wd := this .GetString ("wd" , "" )
6769 this .Data ["Private" ] = private
70+ this .Data ["Wd" ] = wd
6871 pageIndex , _ := this .GetInt ("page" , 1 )
69- books , totalCount , _ := models .NewBook ().FindToPager (pageIndex , conf .PageSize , this .Member .MemberId , private )
72+ books , totalCount , _ := models .NewBook ().FindToPager (pageIndex , conf .PageSize , this .Member .MemberId , wd , private )
7073 ebookStats := make (map [int ]map [string ]models.Ebook )
7174 modelEbook := models .NewEbook ()
7275 for _ , book := range books {
@@ -75,7 +78,7 @@ func (this *BookController) Index() {
7578 ebookJSON , _ := json .Marshal (ebookStats )
7679 this .Data ["EbookStats" ] = template .JS (string (ebookJSON ))
7780 if totalCount > 0 {
78- this .Data ["PageHtml" ] = utils .NewPaginations (conf .RollPage , totalCount , conf .PageSize , pageIndex , beego .URLFor ("BookController.Index" ), fmt .Sprintf ("&private=%v" , private ))
81+ this .Data ["PageHtml" ] = utils .NewPaginations (conf .RollPage , totalCount , conf .PageSize , pageIndex , beego .URLFor ("BookController.Index" ), fmt .Sprintf ("&private=%v&wd=%s " , private , url . QueryEscape ( wd ) ))
7982 } else {
8083 this .Data ["PageHtml" ] = ""
8184 }
@@ -1059,7 +1062,8 @@ func (this *BookController) unzipToData(bookId int, identify, zipFile, originFil
10591062 //读取文件,把图片文档录入oss
10601063 if files , err := filetil .ScanFiles (unzipPath ); err == nil {
10611064 projectRoot = this .getProjectRoot (files )
1062- this .replaceToAbs (projectRoot , identify )
1065+
1066+ this .fixFileLinks (projectRoot , identify )
10631067
10641068 ModelStore := new (models.DocumentStore )
10651069 //文档对应的标识
@@ -1069,11 +1073,11 @@ func (this *BookController) unzipToData(bookId int, identify, zipFile, originFil
10691073 if ok , _ := imgMap [ext ]; ok { //图片,录入oss
10701074 switch utils .StoreType {
10711075 case utils .StoreOss :
1072- if err := store .ModelStoreOss .MoveToOss (file .Path , "projects/" + identify + strings .TrimPrefix (file .Path , projectRoot ), false , false ); err != nil {
1076+ if err := store .ModelStoreOss .MoveToOss (file .Path , filepath . Join ( "projects/" + identify , strings .TrimPrefix (file .Path , projectRoot ) ), false , false ); err != nil {
10731077 beego .Error (err )
10741078 }
10751079 case utils .StoreLocal :
1076- if err := store .ModelStoreLocal .MoveToStore (file .Path , "uploads/projects/" + identify + strings .TrimPrefix (file .Path , projectRoot )); err != nil {
1080+ if err := store .ModelStoreLocal .MoveToStore (file .Path , filepath . Join ( "uploads/projects/" + identify , strings .TrimPrefix (file .Path , projectRoot ) )); err != nil {
10771081 beego .Error (err )
10781082 }
10791083 }
@@ -1082,13 +1086,9 @@ func (this *BookController) unzipToData(bookId int, identify, zipFile, originFil
10821086 var mdcont string
10831087 var htmlStr string
10841088 if b , err := ioutil .ReadFile (file .Path ); err == nil {
1085- if ext == ".md" || ext == ".markdown" {
1086- mdcont = strings .TrimSpace (string (b ))
1087- htmlStr = mdtil .Md2html (mdcont )
1088- } else {
1089- htmlStr = string (b )
1090- mdcont = html2md .Convert (htmlStr )
1091- }
1089+ mdcont = strings .TrimSpace (string (b ))
1090+ htmlStr = mdtil .Md2html (mdcont )
1091+
10921092 if ! strings .HasPrefix (mdcont , "[TOC]" ) {
10931093 mdcont = "[TOC]\r \n \r \n " + mdcont
10941094 }
@@ -1151,7 +1151,7 @@ func (this *BookController) loadByFolder(bookId int, identify, folder string) {
11511151 return
11521152 }
11531153
1154- this .replaceToAbs (folder , identify )
1154+ this .fixFileLinks (folder , identify )
11551155
11561156 ModelStore := new (models.DocumentStore )
11571157
@@ -1211,21 +1211,17 @@ func (this *BookController) loadByFolder(bookId int, identify, folder string) {
12111211
12121212//获取书籍的根目录
12131213func (this * BookController ) getProjectRoot (fl []filetil.FileList ) (root string ) {
1214- //获取书籍的根目录(感觉这个函数封装的不是很好,有更好的方法,请通过issue告知我,谢谢。)
1215- i := 1000
1214+ var strs []string
12161215 for _ , f := range fl {
12171216 if ! f .IsDir {
1218- if cnt := strings .Count (f .Path , "/" ); cnt < i {
1219- root = filepath .Dir (f .Path )
1220- i = cnt
1221- }
1217+ strs = append (strs , f .Path )
12221218 }
12231219 }
1224- return
1220+ return utils . LongestCommonPrefix ( strs )
12251221}
12261222
12271223//查找并替换markdown文件中的路径,把图片链接替换成url的相对路径,把文档间的链接替换成【$+文档标识链接】
1228- func (this * BookController ) replaceToAbs (projectRoot string , identify string ) {
1224+ func (this * BookController ) fixFileLinks (projectRoot string , identify string ) {
12291225 imgBaseUrl := "/uploads/projects/" + identify
12301226 switch utils .StoreType {
12311227 case utils .StoreLocal :
@@ -1236,67 +1232,74 @@ func (this *BookController) replaceToAbs(projectRoot string, identify string) {
12361232 }
12371233 files , _ := filetil .ScanFiles (projectRoot )
12381234 for _ , file := range files {
1239- if ext := strings .ToLower (filepath .Ext (file .Path )); ext == ".md" || ext == ".markdown" {
1240- //mdb ==> markdown byte
1241- mdb , _ := ioutil .ReadFile (file .Path )
1242- mdCont := string (mdb )
1243- basePath := filepath .Dir (file .Path )
1244- basePath = strings .Trim (strings .Replace (basePath , "\\ " , "/" , - 1 ), "/" )
1245- basePathSlice := strings .Split (basePath , "/" )
1246- l := len (basePathSlice )
1247- b , _ := ioutil .ReadFile (file .Path )
1248- output := blackfriday .Run (b )
1249- doc , _ := goquery .NewDocumentFromReader (strings .NewReader (string (output )))
1250-
1251- //图片链接处理
1252- doc .Find ("img" ).Each (func (i int , selection * goquery.Selection ) {
1253- //非http://、// 和 https:// 开头的图片地址,即是相对地址
1254- src , ok := selection .Attr ("src" )
1255- lowerSrc := strings .ToLower (src )
1256- if ok &&
1257- ! strings .HasPrefix (lowerSrc , "http://" ) &&
1258- ! strings .HasPrefix (lowerSrc , "https://" ) {
1259- newSrc := src //默认为旧地址
1260- if strings .HasPrefix (lowerSrc , "//" ) {
1261- newSrc = "https:" + newSrc
1262- } else {
1263- if cnt := strings .Count (src , "../" ); cnt < l { //以或者"../"开头的路径
1264- newSrc = strings .Join (basePathSlice [0 :l - cnt ], "/" ) + "/" + strings .TrimLeft (src , "./" )
1265- }
1266- newSrc = imgBaseUrl + "/" + strings .TrimLeft (strings .TrimPrefix (strings .TrimLeft (newSrc , "./" ), projectRoot ), "/" )
1235+ ext := strings .ToLower (filepath .Ext (file .Path ))
1236+ if ! (ext == ".md" || ext == ".markdown" || ext == ".html" || ext == ".xhtml" ) {
1237+ continue
1238+ }
1239+
1240+ //mdb ==> markdown byte
1241+ mdb , _ := ioutil .ReadFile (file .Path )
1242+ mdCont := string (mdb )
1243+ if ext == ".html" || ext == ".xhtml" {
1244+ mdCont = html2md .Convert (mdCont )
1245+ }
1246+
1247+ basePath := filepath .Dir (file .Path )
1248+ basePath = strings .Trim (strings .Replace (basePath , "\\ " , "/" , - 1 ), "/" )
1249+ basePathSlice := strings .Split (basePath , "/" )
1250+ l := len (basePathSlice )
1251+ b , _ := ioutil .ReadFile (file .Path )
1252+ output := blackfriday .Run (b )
1253+ doc , _ := goquery .NewDocumentFromReader (bytes .NewReader (output ))
1254+
1255+ //图片链接处理
1256+ doc .Find ("img" ).Each (func (i int , selection * goquery.Selection ) {
1257+ //非http://、// 和 https:// 开头的图片地址,即是相对地址
1258+ src , ok := selection .Attr ("src" )
1259+ lowerSrc := strings .ToLower (src )
1260+ if ok &&
1261+ ! strings .HasPrefix (lowerSrc , "http://" ) &&
1262+ ! strings .HasPrefix (lowerSrc , "https://" ) {
1263+ newSrc := src //默认为旧地址
1264+ if strings .HasPrefix (lowerSrc , "//" ) {
1265+ newSrc = "https:" + newSrc
1266+ } else {
1267+ if cnt := strings .Count (src , "../" ); cnt < l { //以或者"../"开头的路径
1268+ newSrc = strings .Join (basePathSlice [0 :l - cnt ], "/" ) + "/" + strings .TrimLeft (src , "./" )
12671269 }
1268- mdCont = strings .Replace ( mdCont , src , newSrc , - 1 )
1270+ newSrc = imgBaseUrl + "/" + strings .TrimLeft ( strings . TrimPrefix ( strings . TrimLeft ( newSrc , "./" ), projectRoot ), "/" )
12691271 }
1270- } )
1271-
1272- //a标签链接处理。要注意判断有锚点的情况
1273- doc . Find ( "a" ). Each ( func ( i int , selection * goquery. Selection ) {
1274- href , ok := selection . Attr ( "href" )
1275- lowerHref := strings . TrimSpace ( strings . ToLower ( href ))
1276- // 链接存在,且不以 // 、 http、https、mailto 开头
1277- if ok &&
1278- ! strings . HasPrefix ( lowerHref , "//" ) &&
1279- ! strings . HasPrefix ( lowerHref , "http://" ) &&
1280- ! strings .HasPrefix (lowerHref , "https: //" ) &&
1281- ! strings .HasPrefix (lowerHref , "mailto: " ) &&
1282- ! strings .HasPrefix (lowerHref , "# " ) {
1283- newHref := href //默认
1284- if cnt := strings .Count ( href , "../" ); cnt < l {
1285- newHref = strings . Join ( basePathSlice [ 0 : l - cnt ], "/" ) + "/" + strings . TrimLeft ( href , "./" )
1286- }
1287- newHref = strings .TrimPrefix ( strings . Trim ( newHref , "/" ), projectRoot )
1288- if ! strings . HasPrefix ( href , "$" ) { //原链接不包含$符开头,否则表示已经替换过了。
1289- newHref = "$" + strings .Replace (strings .Trim (newHref , "/" ), "/" , "-" , - 1 )
1290- slice := strings .Split ( newHref , "$" )
1291- if ll := len ( slice ); ll > 0 {
1292- newHref = "$" + slice [ ll - 1 ]
1293- }
1294- mdCont = strings . Replace ( mdCont , "](" + href , "](" + newHref , - 1 )
1272+ mdCont = strings . Replace ( mdCont , src , newSrc , - 1 )
1273+ }
1274+ })
1275+
1276+ //a标签链接处理。要注意判断有锚点的情况
1277+ doc . Find ( "a" ). Each ( func ( i int , selection * goquery. Selection ) {
1278+ href , ok := selection . Attr ( "href" )
1279+ lowerHref := strings . TrimSpace ( strings . ToLower ( href ))
1280+ // 链接存在,且不以 // 、 http、https、mailto 开头
1281+ if ok &&
1282+ ! strings .HasPrefix (lowerHref , "//" ) &&
1283+ ! strings .HasPrefix (lowerHref , "http:// " ) &&
1284+ ! strings .HasPrefix (lowerHref , "https:// " ) &&
1285+ ! strings . HasPrefix ( lowerHref , "mailto:" ) &&
1286+ ! strings .HasPrefix ( lowerHref , "#" ) {
1287+ newHref := href //默认
1288+ if cnt := strings . Count ( href , "../" ); cnt < l {
1289+ newHref = strings .Join ( basePathSlice [ 0 : l - cnt ] , "/" ) + "/" + strings . TrimLeft ( href , "./" )
1290+ }
1291+ newHref = strings .TrimPrefix (strings .Trim (newHref , "/" ), projectRoot )
1292+ if ! strings .HasPrefix ( href , "$" ) { //原链接不包含$符开头,否则表示已经替换过了。
1293+ newHref = "$" + strings . Replace ( strings . Trim ( newHref , "/" ), "/" , "-" , - 1 )
1294+ slice := strings . Split ( newHref , "$" )
1295+ if ll := len ( slice ); ll > 0 {
1296+ newHref = "$" + slice [ ll - 1 ]
12951297 }
1298+ mdCont = strings .Replace (mdCont , "](" + href , "](" + newHref , - 1 )
12961299 }
1297- })
1298- ioutil . WriteFile ( file . Path , [] byte ( mdCont ), os . ModePerm )
1299- }
1300+ }
1301+ } )
1302+ ioutil . WriteFile ( file . Path , [] byte ( mdCont ), os . ModePerm )
13001303 }
13011304}
13021305
0 commit comments