add book name and author options

This commit is contained in:
lapwat
2021-12-19 18:20:54 +01:00
parent e7ffd8c66c
commit 4e9b0611e8
7 changed files with 118 additions and 55 deletions

View File

@@ -17,7 +17,7 @@ import (
)
var recursive, include, images bool
var format, output, selector string
var format, output, selector, name, author string
var limit, offset, delay, threads int
var getCmd = &cobra.Command{
@@ -77,7 +77,7 @@ var getCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
url := args[0]
b := book.NewBookFromURL(url, selector, recursive, include, images, limit, offset, delay, threads)
b := book.NewBookFromURL(url, selector, name, author, recursive, include, limit, offset, delay, threads)
if len(output) == 0 {
// set default output
@@ -136,27 +136,36 @@ var getCmd = &cobra.Command{
e.SetAuthor(b.Author())
for _, c := range b.Chapters() {
// parse content
var content string
if images == false {
content = c.Content()
}
// parse content
doc, err := goquery.NewDocumentFromReader(strings.NewReader(c.Content()))
if err != nil {
log.Fatal(err)
}
// retrieve images and download it
contentWithLocalImages := c.Content()
doc.Find("img").Each(func(i int, s *goquery.Selection) {
src, _ := s.Attr("src")
imagePath, _ := e.AddImage(src, "")
contentWithLocalImages = strings.ReplaceAll(contentWithLocalImages, src, imagePath)
if images {
imageTag, _ := goquery.OuterHtml(s)
content += imageTag
}
content = strings.ReplaceAll(content, src, imagePath)
})
html := fmt.Sprintf("<h1>%s</h1>%s", c.Name(), contentWithLocalImages)
html := fmt.Sprintf("<h1>%s</h1>%s", c.Name(), content)
_, err = e.AddSection(html, c.Name(), "", "")
if err != nil {
log.Fatal(err)
}
}
err := e.Write(output)
@@ -171,8 +180,37 @@ var getCmd = &cobra.Command{
e := epub.NewEpub(b.Name())
e.SetAuthor(b.Author())
for _, chapter := range b.Chapters() {
e.AddSection(chapter.Content(), chapter.Name(), "", "")
for _, c := range b.Chapters() {
var content string
if images == false {
content = c.Content()
}
// parse content
doc, err := goquery.NewDocumentFromReader(strings.NewReader(c.Content()))
if err != nil {
log.Fatal(err)
}
// retrieve images and download it
doc.Find("img").Each(func(i int, s *goquery.Selection) {
src, _ := s.Attr("src")
imagePath, _ := e.AddImage(src, "")
if images {
imageTag, _ := goquery.OuterHtml(s)
content += imageTag
}
content = strings.ReplaceAll(content, src, imagePath)
})
html := fmt.Sprintf("<h1>%s</h1>%s", c.Name(), content)
_, err = e.AddSection(html, c.Name(), "", "")
if err != nil {
log.Fatal(err)
}
}
outputEPUB := strings.ReplaceAll(output, ".mobi", ".epub")
@@ -183,16 +221,16 @@ var getCmd = &cobra.Command{
}
exec.Command("kindlegen", outputEPUB).Run()
// exec command always return status 1 even if it fails
// exec command always return status 1 even if it succeed
// if err != nil {
// log.Fatal(err)
// }
fmt.Printf("Ebook saved to \"%s\"\n", output)
err2 := os.Remove(outputEPUB)
if err2 != nil {
log.Fatal(err2)
err = os.Remove(outputEPUB)
if err != nil {
log.Fatal(err)
}
}
},

View File

@@ -27,7 +27,7 @@ var listCmd = &cobra.Command{
log.Fatal(err)
}
links, err := book.GetLinks(base, selector, limit, offset, include)
links, _, err := book.GetLinks(base, selector, limit, offset, include)
if err != nil {
log.Fatal(err)
}

View File

@@ -23,8 +23,10 @@ func Execute() {
}
func init() {
rootCmd.PersistentFlags().StringVarP(&name, "name", "n", "", "book name (default: page title)")
rootCmd.PersistentFlags().StringVarP(&author, "author", "a", "", "book author")
rootCmd.PersistentFlags().StringVarP(&format, "format", "f", "stdout", "file format [stdout, md, epub, mobi]")
rootCmd.PersistentFlags().StringVarP(&output, "output", "", "", "output file")
rootCmd.PersistentFlags().StringVarP(&output, "output", "", "", "file name (default: book name)")
rootCmd.PersistentFlags().StringVarP(&selector, "selector", "s", "", "table of content CSS selector, in resursive mode")
rootCmd.PersistentFlags().BoolVarP(&recursive, "recursive", "r", false, "create one chapter per natigation item")
rootCmd.PersistentFlags().BoolVarP(&include, "include", "i", false, "include URL as first chapter, in resursive mode")

View File

@@ -14,6 +14,6 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of papeer",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("papeer v0.3.0")
fmt.Println("papeer v0.3.1")
},
}