-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b47ba0
commit b6d724d
Showing
7 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package filekit | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/arimatakao/mdx/filekit/metadata" | ||
"github.com/go-shiori/go-epub" | ||
) | ||
|
||
const imageSectionTemplate = `<img src="%s" alt="%s" />` | ||
|
||
type epubArchive struct { | ||
b *epub.Epub | ||
tempDir string | ||
filesPaths []string | ||
} | ||
|
||
func newEpubArchive() (*epubArchive, error) { | ||
book, err := epub.NewEpub("") | ||
if err != nil { | ||
return &epubArchive{}, err | ||
} | ||
|
||
dir, err := os.MkdirTemp("", "mdxepubfiles") | ||
if err != nil { | ||
return &epubArchive{}, err | ||
} | ||
|
||
return &epubArchive{ | ||
b: book, | ||
tempDir: dir, | ||
filesPaths: []string{}, | ||
}, nil | ||
} | ||
|
||
func (e epubArchive) WriteOnDiskAndClose(outputDir string, outputFileName string, | ||
m metadata.Metadata) error { | ||
|
||
for i, filePath := range e.filesPaths { | ||
indexStr := fmt.Sprint(i + 1) | ||
imageEpubPath, err := e.b.AddImage(filePath, indexStr) | ||
if err != nil { | ||
return err | ||
} | ||
sectionStr := fmt.Sprintf(imageSectionTemplate, imageEpubPath, indexStr) | ||
_, err = e.b.AddSection(sectionStr, indexStr, "", "") | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
bookTitle := fmt.Sprintf("%s vol%s ch%s", m.CI.Title, m.CI.Volume, m.CI.Number) | ||
e.b.SetTitle(bookTitle) | ||
|
||
authors := m.P.Authors + " | " + m.P.Artists | ||
e.b.SetAuthor(authors) | ||
|
||
e.b.SetLang(m.CI.LanguageISO) | ||
|
||
err := os.MkdirAll(outputDir, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = e.b.Write(filepath.Join(outputDir, outputFileName)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return os.RemoveAll(e.tempDir) | ||
} | ||
|
||
func (e *epubArchive) AddFile(fileName string, imageBytes []byte) error { | ||
filePath := filepath.Join(e.tempDir, fileName) | ||
err := os.WriteFile(filePath, imageBytes, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
e.filesPaths = append(e.filesPaths, filePath) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters