Skip to content

Commit

Permalink
Return bytes from conversion functions
Browse files Browse the repository at this point in the history
This will allow more re-use
  • Loading branch information
semanticart committed Apr 15, 2024
1 parent 46ff85d commit a0b29c5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
17 changes: 13 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"fmt"
"log"
"os"
Expand All @@ -14,20 +15,28 @@ import (
func convert(story squire.Story, format string) {
fmt.Println("Converting to", format)

// TODO: make this configurable
const fileNameBase = "output"

var err error
var buf bytes.Buffer
var extension = "html"

switch {
case format == "html":
err = squire.ConvertToHtml(story, true)
buf, err = squire.ConvertToHtml(story, true)
case format == "html-inner":
err = squire.ConvertToHtml(story, false)
buf, err = squire.ConvertToHtml(story, false)
case format == "epub":
err = squire.ConvertToEpub(story)
extension = "epub"
buf, err = squire.ConvertToEpub(story)
default:
fmt.Println("Invalid conversion format. Allowed options are 'html', 'html-inner', or 'epub'")
}

if err != nil {
if err == nil {
os.WriteFile(fileNameBase+"."+extension, buf.Bytes(), 0644)
} else {
log.Fatalf("error converting to %s: %v", format, err)
}
}
Expand Down
26 changes: 18 additions & 8 deletions pkg/convert.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package squire

import (
"bytes"
_ "embed"
"encoding/base64"
"fmt"
"os"
"strings"

"github.com/go-shiori/go-epub"
Expand Down Expand Up @@ -56,7 +56,7 @@ func sortedChapters(chapters map[string]Chapter) []Chapter {
return sorted
}

func ConvertToHtml(story Story, full bool) error {
func ConvertToHtml(story Story, full bool) (bytes.Buffer, error) {
html := ""

if full {
Expand Down Expand Up @@ -90,22 +90,26 @@ func ConvertToHtml(story Story, full bool) error {
html += "</body>\n</html>"
}

return os.WriteFile(story.Title+".html", []byte(html), 0644)
buffer := bytes.NewBufferString(html)

return *buffer, nil
}

func ConvertToEpub(story Story) error {
func ConvertToEpub(story Story) (bytes.Buffer, error) {
var buffer bytes.Buffer

book, err := epub.NewEpub(story.Title)

if err != nil {
return err
return buffer, err
}

book.SetAuthor(story.Author)

cssPath, err := book.AddCSS(cssContent(), "")

if err != nil {
return err
return buffer, err
}

for _, chapter := range sortedChapters(story.Chapters) {
Expand All @@ -126,9 +130,15 @@ func ConvertToEpub(story Story) error {
_, err = book.AddSection(content, chapter.Title, chapter.Id, cssPath)

if err != nil {
return err
return buffer, err
}
}

return book.Write(story.Title + ".epub")
_, err = book.WriteTo(&buffer)

if err != nil {
return buffer, err
}

return buffer, nil
}
20 changes: 4 additions & 16 deletions pkg/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ func TestConvertStory(t *testing.T) {
t.Fatal(err)
}

err = ConvertToHtml(story, true)

if err != nil {
t.Fatal(err)
}

output, err := os.ReadFile(story.Title + ".html")
buf, err := ConvertToHtml(story, true)

if err != nil {
t.Fatal(err)
Expand All @@ -42,7 +36,7 @@ func TestConvertStory(t *testing.T) {
t.Fatal(err)
}

assertEqual(t, strings.TrimSpace(string(output)), strings.TrimSpace(string(expected)))
assertEqual(t, strings.TrimSpace(buf.String()), strings.TrimSpace(string(expected)))
})

t.Run("Converting a story to inner-html", func(t *testing.T) {
Expand All @@ -58,13 +52,7 @@ func TestConvertStory(t *testing.T) {
t.Fatal(err)
}

err = ConvertToHtml(story, false)

if err != nil {
t.Fatal(err)
}

output, err := os.ReadFile(story.Title + ".html")
buf, err := ConvertToHtml(story, false)

if err != nil {
t.Fatal(err)
Expand All @@ -76,6 +64,6 @@ func TestConvertStory(t *testing.T) {
t.Fatal(err)
}

assertEqual(t, strings.TrimSpace(string(output)), strings.TrimSpace(string(expected)))
assertEqual(t, strings.TrimSpace(buf.String()), strings.TrimSpace(string(expected)))
})
}

0 comments on commit a0b29c5

Please sign in to comment.