Skip to content

Commit

Permalink
Add FileXXXParser
Browse files Browse the repository at this point in the history
  • Loading branch information
Vytek committed Sep 4, 2023
1 parent 31f997d commit 33abd9e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 16 deletions.
31 changes: 29 additions & 2 deletions opencrucible.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ func TXTParseToString(StreamToParse []byte) (string, error) {
return txt, err
}

func TXTFileParserToString(FileToParse string) (string, error) {
txt, err := os.ReadFile(FileToParse)
if err != nil {
return "", fmt.Errorf("error opening file: %s", err)
}
got, err := TXTParseToString(txt)
return got, err
}

func RTFParseToString(StreamToParse []byte) (string, error) {
// If no StreamToDetect was given, return an error with a message.
if len(StreamToParse) == 0 {
Expand All @@ -54,8 +63,17 @@ func RTFParseToString(StreamToParse []byte) (string, error) {
return rtf, err
}

func RTFFileParserToString(FileToParse string) (string, error) {
rtf, err := os.ReadFile(FileToParse)
if err != nil {
return "", fmt.Errorf("error opening file: %s", err)
}
got, err := RTFParseToString(rtf)
return got, err
}

func DOCXFileParseToString(FileToParse string) (string, error) {
// extract text from an XLSX file
// extract text from an DOCX file
file, err := os.Open(FileToParse)
if err != nil {
return "", fmt.Errorf("error opening file: %s", err)
Expand All @@ -74,4 +92,13 @@ func ODTParseToString(StreamToParse []byte) (string, error) {
}
odt, err := cat.FromBytes(StreamToParse)
return odt, err
}
}

func ODTFileParserToString(FileToParse string) (string, error) {
odt, err := os.ReadFile(FileToParse)
if err != nil {
return "", fmt.Errorf("error opening file: %s", err)
}
got, err := ODTParseToString(odt)
return got, err
}
64 changes: 50 additions & 14 deletions opencrucible_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"testing"
)

func TestTXTParse(t *testing.T) {
func TestTXTParser(t *testing.T) {
txt, err := os.ReadFile("test_file/test_file_txt.txt")
if err != nil {
panic(err)
t.Errorf("error loading file \n %s", err)
}
got, err := TXTParseToString(txt)
if err != nil {
panic(err)
t.Errorf("unable to parse file \n %s", err)
}
want := "This is a test file to test library"
t.Logf("Parsed: %s", got)
Expand All @@ -21,14 +21,38 @@ func TestTXTParse(t *testing.T) {
}
}

func TestODTarse(t *testing.T) {
func TestTXTFileParser(t *testing.T) {
got, err := TXTFileParserToString("test_file/test_file_txt.txt")
if err != nil {
t.Errorf("error loading file \n %s", err)
}
want := "This is a test file to test library"
t.Logf("Parsed: %s", got)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}

func TestODTParser(t *testing.T) {
odt, err := os.ReadFile("test_file/test_file_odt.odt")
if err != nil {
panic(err)
t.Errorf("error loading file \n %s", err)
}
got, err := ODTParseToString(odt)
if err != nil {
panic(err)
t.Errorf("unable to parse file \n %s", err)
}
want := "This is a test for library\n"
t.Logf("Parsed: %s", got)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}

func TestODTFileParser(t *testing.T) {
got, err := ODTFileParserToString("test_file/test_file_odt.odt")
if err != nil {
t.Errorf("error loading file \n %s", err)
}
want := "This is a test for library\n"
t.Logf("Parsed: %s", got)
Expand All @@ -37,14 +61,26 @@ func TestODTarse(t *testing.T) {
}
}

func TestRTFarse(t *testing.T) {
func TestRTFParser(t *testing.T) {
rtf, err := os.ReadFile("test_file/test_file_rtf.rtf")
if err != nil {
panic(err)
t.Errorf("error loading file \n %s", err)
}
got, err := RTFParseToString(rtf)
if err != nil {
panic(err)
t.Errorf("unable to parse file \n %s", err)
}
want := "Normalheading 1heading 2heading 3heading 4heading 5heading 6TitleSubtitleThis is a test file to test library "
t.Logf("Parsed: %s", got)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}

func TestRTFFileParser(t *testing.T) {
got, err := RTFFileParserToString("test_file/test_file_rtf.rtf")
if err != nil {
t.Errorf("error loading file \n %s", err)
}
want := "Normalheading 1heading 2heading 3heading 4heading 5heading 6TitleSubtitleThis is a test file to test library "
t.Logf("Parsed: %s", got)
Expand All @@ -53,10 +89,10 @@ func TestRTFarse(t *testing.T) {
}
}

func TestDOCXParse(t *testing.T) {
func TestDOCXFileParser(t *testing.T) {
got, err := DOCXFileParseToString("test_file/test_file_docx.docx")
if err != nil {
panic(err)
t.Errorf("error loading file \n %s", err)
}
want := "This is a test file to test library\n"
t.Logf("Parsed: %s", got)
Expand All @@ -65,14 +101,14 @@ func TestDOCXParse(t *testing.T) {
}
}

func TestDOCXMSParse(t *testing.T) {
func TestDOCXMSFileParser(t *testing.T) {
got, err := DOCXFileParseToString("test_file/test_file_docx_ms.docx")
if err != nil {
panic(err)
t.Errorf("error loading file \n %s", err)
}
want := "This is a test file to test library\n"
t.Logf("Parsed: %s", got)
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
}

0 comments on commit 33abd9e

Please sign in to comment.