Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Changed cover to a page flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Charlton committed Feb 26, 2017
1 parent 345aeb2 commit 230772c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
26 changes: 4 additions & 22 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

// A Document represents a single pdf document.
type Document struct {
cover *Page
pages []*Page
options []string

Expand All @@ -34,7 +33,8 @@ func (doc *Document) AddPages(pages ...*Page) {

// AddCover adds a cover page to the document.
func (doc *Document) AddCover(cover *Page) {
doc.cover = cover
doc.pages = append(doc.pages, cover)
cover.cover = true
}

// AddOptions allows the setting of options after document creation.
Expand All @@ -51,16 +51,9 @@ func (doc *Document) args() []string {
args := []string{}
args = append(args, doc.options...)

// coverpage
if doc.cover != nil {
args = append(args, "cover", doc.cover.filename)
args = append(args, doc.cover.options...)
}

// pages
for _, pg := range doc.pages {
args = append(args, pg.filename)
args = append(args, pg.options...)
args = append(args, pg.args()...)
}

return args
Expand All @@ -71,10 +64,6 @@ func (doc *Document) args() []string {
func (doc *Document) readers() int {

n := 0
if doc.cover != nil && doc.cover.reader {
n++
}

for _, pg := range doc.pages {
if pg.reader {
n++
Expand All @@ -93,18 +82,11 @@ func (doc *Document) writeTempPages() error {
return fmt.Errorf("Error creating temp directory")
}

n := 0
all_pages := []*Page{}
if doc.cover != nil {
all_pages = append(all_pages, doc.cover)
}
all_pages = append(all_pages, doc.pages...)
for _, pg := range all_pages {
for n, pg := range doc.pages {
if !pg.reader {
continue
}

n++
pg.filename = fmt.Sprintf("%v/%v/page%08d.html", TempDir, doc.tmp, n)
err := ioutil.WriteFile(pg.filename, pg.buf.Bytes(), 0666)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ func TestAddCover(t *testing.T) {
pg := NewPage("page1.html")
doc.AddPages(pg)

exp := Document{cover: cov, pages: []*Page{pg}, options: []string{}}
exp := Document{pages: []*Page{pg, cov}, options: []string{}}
if reflect.DeepEqual(exp, doc) {
t.Errorf("Wrong document produced. Expected: %v, Got: %v", exp, doc)
}

if cov.cover != true {
t.Errorf("Cover page not marked")
}
}

func TestArgs(t *testing.T) {
Expand Down Expand Up @@ -142,7 +146,7 @@ func TestWriteTemp(t *testing.T) {
t.Errorf("Unexpected error: %v", err)
}

exp := []string{"page00000001.html", "page00000002.html", "page00000003.html"}
exp := []string{"page00000001.html", "page00000002.html", "page00000004.html"}
files, err := ioutil.ReadDir(TempDir + "/" + doc.tmp)
if err != nil {
t.Errorf("Unexpected error: %v", err)
Expand Down
16 changes: 16 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Page struct {
buf *bytes.Buffer
reader bool
options []string
cover bool
}

// NewPage creates a new page from the given filename (which can be a url),
Expand Down Expand Up @@ -48,3 +49,18 @@ func (pg *Page) AddOptions(opts ...PageOption) {
pg.options = append(pg.options, opt.opts()...)
}
}

// args
func (pg *Page) args() []string {

args := []string{}

if pg.cover {
args = append(args, "cover")
}

args = append(args, pg.filename)
args = append(args, pg.options...)
return args

}

0 comments on commit 230772c

Please sign in to comment.