发布文档内容为HTML
(bookId int, baseUrl string, force ...bool)
| 202 | |
| 203 | //发布文档内容为HTML |
| 204 | func (m *Document) ReleaseContent(bookId int, baseUrl string, force ...bool) { |
| 205 | var ( |
| 206 | o = orm.NewOrm() |
| 207 | docs []*Document |
| 208 | book Book |
| 209 | tableBooks = "md_books" |
| 210 | releaseTime = time.Now() //发布的时间戳 |
| 211 | ) |
| 212 | |
| 213 | qs := o.QueryTable(tableBooks).Filter("book_id", bookId) |
| 214 | qs.One(&book) |
| 215 | |
| 216 | //全部重新发布。查询该书籍的所有文档id |
| 217 | _, err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).Limit(20000).All(&docs, "document_id", "identify", "modify_time") |
| 218 | if err != nil { |
| 219 | beego.Error("发布失败 => ", err) |
| 220 | return |
| 221 | } |
| 222 | docMap := make(map[string]bool) |
| 223 | for _, item := range docs { |
| 224 | docMap[item.Identify] = true |
| 225 | } |
| 226 | |
| 227 | ModelStore := new(DocumentStore) |
| 228 | for _, item := range docs { |
| 229 | ds, err := ModelStore.GetById(item.DocumentId) |
| 230 | if err != nil { |
| 231 | beego.Error(err) |
| 232 | continue |
| 233 | } |
| 234 | |
| 235 | if len(force) > 0 && force[0] { |
| 236 | //内容为空,渲染一下文档,然后再重新获取 |
| 237 | utils.RenderDocumentById(item.DocumentId) |
| 238 | ds, _ = ModelStore.GetById(item.DocumentId) |
| 239 | } else { |
| 240 | if strings.TrimSpace(utils.GetTextFromHtml(strings.Replace(ds.Markdown, "[TOC]", "", -1))) == "" { |
| 241 | // 如果markdown内容为空,则查询下一级目录内容来填充 |
| 242 | _, ds.Content = item.BookStackAuto(bookId, ds.DocumentId) |
| 243 | } else if len(utils.GetTextFromHtml(ds.Content)) == 0 { |
| 244 | //内容为空,渲染一下文档,然后再重新获取 |
| 245 | utils.RenderDocumentById(item.DocumentId) |
| 246 | ds, _ = ModelStore.GetById(item.DocumentId) |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | item.Release = ds.Content |
| 251 | |
| 252 | // 采集图片与文档内容链接替换 |
| 253 | if gq, err := goquery.NewDocumentFromReader(strings.NewReader(item.Release)); err == nil { |
| 254 | images := gq.Find("img") |
| 255 | if images.Length() > 0 { |
| 256 | images.Each(func(i int, selection *goquery.Selection) { |
| 257 | if src, ok := selection.Attr("src"); ok { |
| 258 | lowerSrc := strings.ToLower(src) |
| 259 | if strings.HasPrefix(lowerSrc, "https://") || strings.HasPrefix(lowerSrc, "http://") { |
| 260 | tmpFile, err := utils.DownImage(src) |
| 261 | if err == nil { |
no test coverage detected