add option to strip first directory from toc

This commit is contained in:
Celogeek 2023-04-08 11:27:57 +02:00
parent a24bf0cfc8
commit 0e9ae95df0
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
4 changed files with 69 additions and 54 deletions

View File

@ -96,6 +96,7 @@ func (c *Converter) InitParse() {
c.AddBoolParam(&c.Options.HasCover, "hascover", c.Options.HasCover, "Has cover. Indicate if your comic have a cover. The first page will be used as a cover and include after the title.")
c.AddBoolParam(&c.Options.AddPanelView, "addpanelview", c.Options.AddPanelView, "Add an embeded panel view. On kindle you may not need this option as it is handled by the kindle.")
c.AddIntParam(&c.Options.LimitMb, "limitmb", c.Options.LimitMb, "Limit size of the ePub: Default nolimit (0), Minimum 20")
c.AddBoolParam(&c.Options.StripFirstDirectoryFromToc, "strip", c.Options.StripFirstDirectoryFromToc, "Strip first directory from the TOC if only 1")
c.AddSection("Default config")
c.AddBoolParam(&c.Options.Show, "show", false, "Show your default parameters")

View File

@ -20,18 +20,19 @@ type Options struct {
Dry bool `yaml:"-"`
// Config
Profile string `yaml:"profile"`
Quality int `yaml:"quality"`
Crop bool `yaml:"crop"`
Brightness int `yaml:"brightness"`
Contrast int `yaml:"contrast"`
AutoRotate bool `yaml:"auto_rotate"`
AutoSplitDoublePage bool `yaml:"auto_split_double_page"`
NoBlankPage bool `yaml:"no_blank_page"`
Manga bool `yaml:"manga"`
HasCover bool `yaml:"has_cover"`
AddPanelView bool `yaml:"add_panel_view"`
LimitMb int `yaml:"limit_mb"`
Profile string `yaml:"profile"`
Quality int `yaml:"quality"`
Crop bool `yaml:"crop"`
Brightness int `yaml:"brightness"`
Contrast int `yaml:"contrast"`
AutoRotate bool `yaml:"auto_rotate"`
AutoSplitDoublePage bool `yaml:"auto_split_double_page"`
NoBlankPage bool `yaml:"no_blank_page"`
Manga bool `yaml:"manga"`
HasCover bool `yaml:"has_cover"`
AddPanelView bool `yaml:"add_panel_view"`
LimitMb int `yaml:"limit_mb"`
StripFirstDirectoryFromToc bool `yaml:"strip_first_directory_from_toc"`
// Default Config
Show bool `yaml:"-"`
@ -48,19 +49,20 @@ type Options struct {
func New() *Options {
return &Options{
Profile: "",
Quality: 85,
Crop: true,
Brightness: 0,
Contrast: 0,
AutoRotate: false,
AutoSplitDoublePage: false,
NoBlankPage: false,
Manga: false,
HasCover: true,
AddPanelView: false,
LimitMb: 0,
profiles: profiles.New(),
Profile: "",
Quality: 85,
Crop: true,
Brightness: 0,
Contrast: 0,
AutoRotate: false,
AutoSplitDoublePage: false,
NoBlankPage: false,
Manga: false,
HasCover: true,
AddPanelView: false,
LimitMb: 0,
StripFirstDirectoryFromToc: false,
profiles: profiles.New(),
}
}
@ -72,11 +74,11 @@ Options:`
func (o *Options) String() string {
return fmt.Sprintf(`%s
Input : %s
Output : %s
Author : %s
Title : %s
Workers : %d%s
Input : %s
Output : %s
Author : %s
Title : %s
Workers : %d%s
`,
o.Header(),
o.Input,
@ -126,18 +128,19 @@ func (o *Options) ShowDefault() string {
}
return fmt.Sprintf(`
Profile : %s
Quality : %d
Crop : %v
Brightness : %d
Contrast : %d
AutoRotate : %v
AutoSplitDoublePage: %v
NoBlankPage : %v
Manga : %v
HasCover : %v
AddPanelView : %v
LimitMb : %s`,
Profile : %s
Quality : %d
Crop : %v
Brightness : %d
Contrast : %d
AutoRotate : %v
AutoSplitDoublePage : %v
NoBlankPage : %v
Manga : %v
HasCover : %v
AddPanelView : %v
LimitMb : %s
StripFirstDirectoryFromToc: %v`,
profileDesc,
o.Quality,
o.Crop,
@ -150,6 +153,7 @@ func (o *Options) ShowDefault() string {
o.HasCover,
o.AddPanelView,
limitmb,
o.StripFirstDirectoryFromToc,
)
}

View File

@ -32,11 +32,12 @@ type ImageOptions struct {
}
type EpubOptions struct {
Input string
Output string
Title string
Author string
LimitMb int
Input string
Output string
Title string
Author string
LimitMb int
StripFirstDirectoryFromToc bool
*ImageOptions
}
@ -166,10 +167,18 @@ func (e *ePub) getToc(title string, images []*Image) ([]byte, error) {
paths[parentPath].Children.Tags = append(paths[parentPath].Children.Tags, part)
}
}
if paths["."].Children == nil {
children := paths["."].Children
if children != nil && e.StripFirstDirectoryFromToc && len(children.Tags) == 1 {
children = children.Tags[0].Children
}
if children == nil {
return []byte{}, nil
}
return xml.MarshalIndent(paths["."].Children.Tags, " ", " ")
return xml.MarshalIndent(children.Tags, " ", " ")
}
func (e *ePub) Write() error {

11
main.go
View File

@ -96,11 +96,12 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
profile := cmd.Options.GetProfile()
if err := epub.NewEpub(&epub.EpubOptions{
Input: cmd.Options.Input,
Output: cmd.Options.Output,
LimitMb: cmd.Options.LimitMb,
Title: cmd.Options.Title,
Author: cmd.Options.Author,
Input: cmd.Options.Input,
Output: cmd.Options.Output,
LimitMb: cmd.Options.LimitMb,
Title: cmd.Options.Title,
Author: cmd.Options.Author,
StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc,
ImageOptions: &epub.ImageOptions{
ViewWidth: profile.Width,
ViewHeight: profile.Height,