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.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.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.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.AddSection("Default config")
c.AddBoolParam(&c.Options.Show, "show", false, "Show your default parameters") c.AddBoolParam(&c.Options.Show, "show", false, "Show your default parameters")

View File

@ -32,6 +32,7 @@ type Options struct {
HasCover bool `yaml:"has_cover"` HasCover bool `yaml:"has_cover"`
AddPanelView bool `yaml:"add_panel_view"` AddPanelView bool `yaml:"add_panel_view"`
LimitMb int `yaml:"limit_mb"` LimitMb int `yaml:"limit_mb"`
StripFirstDirectoryFromToc bool `yaml:"strip_first_directory_from_toc"`
// Default Config // Default Config
Show bool `yaml:"-"` Show bool `yaml:"-"`
@ -60,6 +61,7 @@ func New() *Options {
HasCover: true, HasCover: true,
AddPanelView: false, AddPanelView: false,
LimitMb: 0, LimitMb: 0,
StripFirstDirectoryFromToc: false,
profiles: profiles.New(), profiles: profiles.New(),
} }
} }
@ -137,7 +139,8 @@ func (o *Options) ShowDefault() string {
Manga : %v Manga : %v
HasCover : %v HasCover : %v
AddPanelView : %v AddPanelView : %v
LimitMb : %s`, LimitMb : %s
StripFirstDirectoryFromToc: %v`,
profileDesc, profileDesc,
o.Quality, o.Quality,
o.Crop, o.Crop,
@ -150,6 +153,7 @@ func (o *Options) ShowDefault() string {
o.HasCover, o.HasCover,
o.AddPanelView, o.AddPanelView,
limitmb, limitmb,
o.StripFirstDirectoryFromToc,
) )
} }

View File

@ -37,6 +37,7 @@ type EpubOptions struct {
Title string Title string
Author string Author string
LimitMb int LimitMb int
StripFirstDirectoryFromToc bool
*ImageOptions *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) 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 []byte{}, nil
} }
return xml.MarshalIndent(paths["."].Children.Tags, " ", " ")
return xml.MarshalIndent(children.Tags, " ", " ")
} }
func (e *ePub) Write() error { func (e *ePub) Write() error {

View File

@ -101,6 +101,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
LimitMb: cmd.Options.LimitMb, LimitMb: cmd.Options.LimitMb,
Title: cmd.Options.Title, Title: cmd.Options.Title,
Author: cmd.Options.Author, Author: cmd.Options.Author,
StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc,
ImageOptions: &epub.ImageOptions{ ImageOptions: &epub.ImageOptions{
ViewWidth: profile.Width, ViewWidth: profile.Width,
ViewHeight: profile.Height, ViewHeight: profile.Height,