Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for -layout option #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func Convert(r io.Reader, mimeType string, readability bool) (*Response, error)
body, meta, err = ConvertODT(r)

case "application/vnd.apple.pages", "application/x-iwork-pages-sffpages":
body, meta, err = ConvertPages(r)
body, meta, err = ConvertPages(r, readability)

case "application/pdf":
body, meta, err = ConvertPDF(r)
body, meta, err = ConvertPDF(r, readability)

case "application/rtf", "application/x-rtf", "text/rtf", "text/richtext":
body, meta, err = ConvertRTF(r)
Expand Down
4 changes: 2 additions & 2 deletions pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// ConvertPages converts a Pages file to text.
func ConvertPages(r io.Reader) (string, map[string]string, error) {
func ConvertPages(r io.Reader, readability bool) (string, map[string]string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ravf95 this is a backwards incompatible change. Instead, we might have to explorer a solution like in my comment here: https://github.com/sajari/docconv/pull/75/files#r449309398

meta := make(map[string]string)
var textBody string

Expand All @@ -35,7 +35,7 @@ func ConvertPages(r io.Reader) (string, map[string]string, error) {
if strings.HasSuffix(f.Name, "Preview.pdf") {
// There is a preview PDF version we can use
if rc, err := f.Open(); err == nil {
return ConvertPDF(rc)
return ConvertPDF(rc, readability)
}
}
if f.Name == "index.xml" {
Expand Down
4 changes: 2 additions & 2 deletions pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"io"
)

func ConvertPDF(r io.Reader) (string, map[string]string, error) {
func ConvertPDF(r io.Reader, readability bool) (string, map[string]string, error) {

f, err := NewLocalFile(r)
if err != nil {
return "", nil, fmt.Errorf("error creating local file: %v", err)
}
defer f.Done()

bodyResult, metaResult, convertErr := ConvertPDFText(f.Name())
bodyResult, metaResult, convertErr := ConvertPDFText(f.Name(), readability)
if convertErr != nil {
return "", nil, convertErr
}
Expand Down
2 changes: 1 addition & 1 deletion pdf_ocr.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func ConvertPDF(r io.Reader) (string, map[string]string, error) {
}
defer f.Done()

bodyResult, metaResult, textConvertErr := ConvertPDFText(f.Name())
bodyResult, metaResult, textConvertErr := ConvertPDFText(f.Name(), false)
if textConvertErr != nil {
return "", nil, textConvertErr
}
Expand Down
11 changes: 9 additions & 2 deletions pdf_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type BodyResult struct {

// Convert PDF

func ConvertPDFText(path string) (BodyResult, MetaResult, error) {
func ConvertPDFText(path string, readability bool) (BodyResult, MetaResult, error) {
metaResult := MetaResult{meta: make(map[string]string)}
bodyResult := BodyResult{}
mr := make(chan MetaResult, 1)
Expand Down Expand Up @@ -56,7 +56,14 @@ func ConvertPDFText(path string) (BodyResult, MetaResult, error) {

br := make(chan BodyResult, 1)
go func() {
body, err := exec.Command("pdftotext", "-q", "-nopgbrk", "-enc", "UTF-8", "-eol", "unix", path, "-").Output()
var body []byte
var err error
if readability {
body, err = exec.Command("pdftotext", "-q", "-nopgbrk", "-enc", "UTF-8", "-eol", "unix", "-layout", path, "-").Output()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd want to collapse the shared arguments into one location and pass in -layout if readability is true.

} else {
body, err = exec.Command("pdftotext", "-q", "-nopgbrk", "-enc", "UTF-8", "-eol", "unix", path, "-").Output()
}

if err != nil {
bodyResult.err = err
}
Expand Down