mirror of
https://github.com/sajari/docconv.git
synced 2026-10-16 21:21:59 +08:00
3dad0cb02c
Documentation still needs a lot of work.
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package docconv
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// ConvertRTF converts RTF files to text.
|
|
func ConvertRTF(r io.Reader) (string, map[string]string, error) {
|
|
f, err := NewLocalFile(r, "/tmp", "sajari-convert-")
|
|
if err != nil {
|
|
return "", nil, fmt.Errorf("error creating local file: %v", err)
|
|
}
|
|
defer f.Done()
|
|
|
|
var output string
|
|
tmpOutput, err := exec.Command("unrtf", "--nopict", "--text", f.Name()).Output()
|
|
if err != nil {
|
|
return "", nil, fmt.Errorf("unrtf error: %v", err)
|
|
}
|
|
|
|
// Step through content looking for meta data and stripping out comments
|
|
meta := make(map[string]string)
|
|
for _, line := range strings.Split(string(tmpOutput), "\n") {
|
|
if parts := strings.SplitN(line, ":", 2); len(parts) > 1 {
|
|
meta[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
|
|
}
|
|
if len(line) > 4 && line[:4] != "### " {
|
|
output += line + "\n"
|
|
}
|
|
}
|
|
|
|
// Identify meta data
|
|
if tmp, ok := meta["AUTHOR"]; ok {
|
|
meta["Author"] = tmp
|
|
}
|
|
if tmp, ok := meta["### creation date"]; ok {
|
|
if t, err := time.Parse("02 January 2006 15:04", tmp); err == nil {
|
|
meta["CreatedDate"] = fmt.Sprintf("%d", t.Unix())
|
|
}
|
|
}
|
|
if tmp, ok := meta["### revision date"]; ok {
|
|
if t, err := time.Parse("02 January 2006 15:04", tmp); err == nil {
|
|
meta["ModifiedDate"] = fmt.Sprintf("%d", t.Unix())
|
|
}
|
|
}
|
|
|
|
return output, meta, nil
|
|
}
|