Skip to content

Commit

Permalink
Merge pull request #21 from sajari/master
Browse files Browse the repository at this point in the history
Changed all log.Fatal to return errors instead
  • Loading branch information
JalfResi committed Jul 29, 2013
2 parents 3bcac0e + db9b91f commit 9673d08
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
4 changes: 1 addition & 3 deletions paragraphObjectModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package justext
import (
"github.com/peterbourgon/exp-html"
"io"
"log"
"regexp"
"strings"
"fmt"
Expand Down Expand Up @@ -95,8 +94,7 @@ func paragraphObjectModel(htmlStr string) ([]*Paragraph, error) {
return paragraphs, nil
}
if matchToDoErrors.MatchString(fmt.Sprintf("%s", z.Err())) {
log.Println("ToDo Error")
log.Fatal(z.Err())
return nil, z.Err()
}
continue

Expand Down
7 changes: 3 additions & 4 deletions preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ package justext

import (
"github.com/peterbourgon/exp-html"
"log"
"regexp"
"strings"
)

func preprocess(htmlStr, encoding, defaultEncoding, encErrors string) *html.Node {
func preprocess(htmlStr, encoding, defaultEncoding, encErrors string) (*html.Node, error) {

root, err := html.Parse(strings.NewReader(htmlStr))
if err != nil {
log.Fatal(err)
return nil, err
}

addKwTags(root)
removeElements(root, []string{"head", "script", "style"})

return root
return root, nil
}

type nodeIterator func(n *html.Node)
Expand Down
14 changes: 8 additions & 6 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/peterbourgon/exp-html"
"io"
"io/ioutil"
"log"
"strings"
"errors"
)

type Reader struct {
Expand Down Expand Up @@ -40,23 +40,25 @@ func (r *Reader) ReadAll() ([]*Paragraph, error) {
return nil, err
}

root := preprocess(string(in), "utf-8", "utf-8", "errors")
root, err := preprocess(string(in), "utf-8", "utf-8", "errors")
if err != nil {
return nil, err
}
if root == nil {
log.Fatal("Preprocess has resulted in nil")
return nil, errors.New("Preprocess has resulted in nil")
}

htmlSource := nodesToString(root)
if len(htmlSource) == 0 {
log.Fatal("MAIN: perprocess has returned an empty string")
return nil, errors.New("MAIN: perprocess has returned an empty string")
}

p, err := paragraphObjectModel(htmlSource)
if err != nil {
return nil, err
}
if p == nil {
log.Println(htmlSource)
log.Fatal("MAIN: P is nil", err)
return nil, errors.New("MAIN: P is nil")
}

classifyParagraphs(p, r.Stoplist, r.LengthLow, r.LengthHigh, r.StopwordsLow, r.StopwordsHigh, r.MaxLinkDensity, r.NoHeadings)
Expand Down
9 changes: 4 additions & 5 deletions stoplists.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package justext
import (
"bytes"
"errors"
"log"
"fmt"
"io/ioutil"
)
Expand Down Expand Up @@ -121,10 +120,10 @@ func RegisterStoplist(name string, resourceFunc ResourceFunc) {
}
*/

func ReadStoplist(filename string) map[string]bool {
func ReadStoplist(filename string) (map[string]bool, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
return nil, err
}

db := bytes.Split(data, []uint8("\n"))
Expand All @@ -135,7 +134,7 @@ func ReadStoplist(filename string) map[string]bool {
list[string(val)] = true
}

return list
return list, nil
}

func GetStoplist(language string) (map[string]bool, error) {
Expand All @@ -145,7 +144,7 @@ func GetStoplist(language string) (map[string]bool, error) {

data, err := stoplists[language]()
if err != nil {
log.Fatal(err)
return nil, err
}
db := bytes.Split(data, []uint8("\n"))

Expand Down
4 changes: 2 additions & 2 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (w *Writer) outputDefault(paragraphs []*Paragraph) error {

templ, err := t.Parse(string(templateData))
if err != nil {
log.Fatal(err)
return err
}

var data = struct {
Expand Down Expand Up @@ -104,7 +104,7 @@ func (w *Writer) outputDetailed(paragraphs []*Paragraph) error {

templ, err := t.Parse(string(templateData))
if err != nil {
log.Fatal(err)
return err
}

var data = struct {
Expand Down

0 comments on commit 9673d08

Please sign in to comment.