mirror of
https://github.com/documize/community.git
synced 2025-08-04 21:15:24 +02:00
Improved MS Word document name extraction
Use document filename and not H1 title inside document. Supports both Linux & MSFT.
This commit is contained in:
parent
f3e66b73c1
commit
7f2d2c01a6
2 changed files with 79 additions and 4 deletions
|
@ -17,6 +17,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
api "github.com/documize/community/core/convapi"
|
||||
|
@ -284,11 +285,11 @@ func convertFileResult(filename string, fileResult *api.DocumentConversionRespon
|
|||
document.Job = ""
|
||||
document.Location = filename
|
||||
|
||||
// Make document name from filename minus extension.
|
||||
document.Name = GetDocumentNameFromFilename(filename)
|
||||
document.Slug = stringutil.MakeSlug(document.Name)
|
||||
|
||||
if fileResult != nil {
|
||||
if len(fileResult.Pages) > 0 {
|
||||
document.Name = fileResult.Pages[0].Title
|
||||
document.Slug = stringutil.MakeSlug(fileResult.Pages[0].Title)
|
||||
}
|
||||
document.Excerpt = fileResult.Excerpt
|
||||
}
|
||||
|
||||
|
@ -296,3 +297,24 @@ func convertFileResult(filename string, fileResult *api.DocumentConversionRespon
|
|||
|
||||
return document
|
||||
}
|
||||
|
||||
// GetDocumentNameFromFilename strips path and extension.
|
||||
func GetDocumentNameFromFilename(filename string) (dn string) {
|
||||
dn = filename
|
||||
|
||||
// First try Linux separator.
|
||||
t := strings.SplitAfter(filename, "/")
|
||||
if len(t) > 1 {
|
||||
dn = t[len(t)-1]
|
||||
} else {
|
||||
// Now try Linux separator.
|
||||
t = strings.SplitAfter(filename, "\\")
|
||||
if len(t) > 1 {
|
||||
dn = t[len(t)-1]
|
||||
}
|
||||
}
|
||||
|
||||
// Remove file extension.
|
||||
dn = strings.TrimSuffix(dn, filepath.Ext(dn))
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue