-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter.go
More file actions
144 lines (117 loc) · 3.38 KB
/
chapter.go
File metadata and controls
144 lines (117 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package pub
import (
"errors"
"path/filepath"
"strings"
)
var (
ErrChapterMissingBookPointer = errors.New("missing pointer to Book")
ErrChapterMissingUniqueID = errors.New("missing UniqueID (must have at least 1 non-space character)")
)
// Chapter represents a division in a [Book] that contains its primary [Content].
type Chapter struct {
UniqueID string `json:"unique_id"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Authors []Profile `json:"authors"`
Contributors []Profile `json:"contributors"`
Publishers []Profile `json:"publishers"`
ContentFileName string `json:"content_file_name"`
Content Content `json:"content"`
AuthorsNotePrefix Content `json:"authors_note_prefix"`
AuthorsNoteSuffix Content `json:"authors_note_suffix"`
URL string `json:"url"`
LanguageCode string `json:"language_code"`
DatePublished *DateTime `json:"date_published"`
DateUpdated *DateTime `json:"date_updated"`
IDs map[string]string `json:"ids"`
Copyright Copyright `json:"copyright"`
Extra map[string]any `json:"extra"`
Chapters []Chapter `json:"chapters"`
Previous *Chapter
Next *Chapter
Book *Book
InputPath string
}
func (c *Chapter) SetBook(book *Book) error {
if book == nil {
return ErrChapterMissingBookPointer
}
c.Book = book
return nil
}
func (c Chapter) GetAbsoluteInputPath() (string, error) {
absPath, err := filepath.Abs(c.InputPath)
if err != nil {
return c.InputPath, err
}
return absPath, nil
}
func (c *Chapter) SetInputPath(inputPath string) error {
absPath, err := filepath.Abs(inputPath)
if err != nil {
return err
}
c.InputPath = absPath
return nil
}
func (c *Chapter) SetUniqueID(uniqueID string) {
c.UniqueID = strings.ToLower(strings.TrimSpace(uniqueID))
}
func (c *Chapter) SetDatePublishedFromString(input string) error {
t, err := dateFromString(input)
if err != nil {
return err
}
*c.DatePublished = t
return nil
}
func (c *Chapter) SetDateUpdatedFromString(input string) error {
t, err := dateFromString(input)
if err != nil {
return err
}
*c.DateUpdated = t
return nil
}
func (c Chapter) HasSubchapters() bool {
return len(c.Chapters) > 0
}
func (c Chapter) Subchapters() []*Chapter {
return allChapters(&c.Chapters)
}
func (c *Chapter) EnsureValid() error {
if c.Book == nil {
return ErrChapterMissingBookPointer
}
c.SetUniqueID(c.UniqueID)
if c.UniqueID == "" && c.Title == "" && c.ContentFileName == "" {
return ErrChapterMissingUniqueID
}
if c.UniqueID == "" && c.Title != "" {
c.SetUniqueID(c.Title)
}
if c.UniqueID == "" && c.ContentFileName != "" {
base := filepath.Base(c.ContentFileName)
ext := filepath.Ext(base)
c.SetUniqueID(strings.TrimSuffix(base, ext))
}
if c.Title == "" {
c.Title = c.UniqueID
}
var profiles []Profile
profiles = append(profiles, c.Authors...)
profiles = append(profiles, c.Publishers...)
profiles = append(profiles, c.Contributors...)
for _, profile := range profiles {
if err := profile.EnsureValid(); err != nil {
return err
}
}
for _, subchapter := range c.Chapters {
if err := subchapter.EnsureValid(); err != nil {
return err
}
}
return nil
}