Skip to content

Commit 3216871

Browse files
committed
fix tests, fix hidden images, remove source label under title
1 parent 403fdcc commit 3216871

File tree

5 files changed

+25
-23
lines changed

5 files changed

+25
-23
lines changed

book/format.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func ToMarkdownString(c chapter) string {
3232

3333
// url
3434
if c.config.PrintURL {
35-
markdown += fmt.Sprintf("_Source: %s_\n\n", c.URL())
35+
markdown += fmt.Sprintf("_%s_\n\n", c.URL())
3636
}
3737

3838
// convert content to markdown
@@ -82,7 +82,7 @@ func ToHtmlString(c chapter) string {
8282

8383
// url
8484
if c.config.PrintURL {
85-
html += fmt.Sprintf("<p><i>Source: %s</i></p>\n", c.URL())
85+
html += fmt.Sprintf("<p><i>%s</i></p>\n", c.URL())
8686
}
8787

8888
// content
@@ -177,9 +177,9 @@ func AppendToEpub(e *epub.Epub, c chapter) {
177177

178178
if c.config.ImagesOnly {
179179
imageTag, _ := goquery.OuterHtml(s)
180-
content += strings.Replace(imageTag, src, imagePath, 1)
180+
content += strings.ReplaceAll(imageTag, src, imagePath)
181181
} else {
182-
content = strings.Replace(content, src, imagePath, 1)
182+
content = strings.ReplaceAll(content, src, imagePath)
183183
}
184184
})
185185

@@ -191,7 +191,7 @@ func AppendToEpub(e *epub.Epub, c chapter) {
191191

192192
// url
193193
if c.config.PrintURL {
194-
html += fmt.Sprintf("<p><i>Source: %s</i></p>\n", c.URL())
194+
html += fmt.Sprintf("<p><i>%s</i></p>\n", c.URL())
195195
}
196196

197197
// content

book/format_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestToMarkdownPrintURL(t *testing.T) {
3838
c := NewChapterFromURL("https://example.com/", "", []*ScrapeConfig{config}, 0, func(index int, name string) {})
3939

4040
got := ToMarkdownString(c)
41-
want := "Example Domain\n==============\n\n_Source: https://example.com/_\n\nThis domain is for use in illustrative examples in documents. You may use this\ndomain in literature without prior coordination or asking for permission.\n\n[More information...](https://www.iana.org/domains/example)\n\n\n"
41+
want := "Example Domain\n==============\n\n_https://example.com/_\n\nThis domain is for use in illustrative examples in documents. You may use this\ndomain in literature without prior coordination or asking for permission.\n\n[More information...](https://www.iana.org/domains/example)\n\n\n"
4242

4343
if got != want {
4444
t.Errorf("got %v, wanted %v", got, want)
@@ -99,7 +99,7 @@ func TestToHtmlPrintURL(t *testing.T) {
9999
c := NewChapterFromURL("https://example.com/", "", []*ScrapeConfig{config}, 0, func(index int, name string) {})
100100

101101
got := ToHtmlString(c)
102-
want := "<h1>Example Domain</h1>\n<p><i>Source: https://example.com/</i></p>\n<div>\n \n <p>This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.</p>\n <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\n</div>"
102+
want := "<h1>Example Domain</h1>\n<p><i>https://example.com/</i></p>\n<div>\n \n <p>This domain is for use in illustrative examples in documents. You may use this\n domain in literature without prior coordination or asking for permission.</p>\n <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\n</div>"
103103

104104
if got != want {
105105
t.Errorf("got %q, wanted %q", got, want)

book/scraper.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ func NewChapterFromURL(url, linkName string, configs []*ScrapeConfig, index int,
242242
content = ""
243243
doc.Find("img").Each(func(i int, s *goquery.Selection) {
244244
imageTag, _ := goquery.OuterHtml(s)
245-
// imageTag = strings.ReplaceAll(imageTag, "\n", "")
246245
content += imageTag
247246
})
248247

book/scraper_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ func TestSubChapters(t *testing.T) {
117117
config0 := NewScrapeConfigQuiet()
118118
config1 := NewScrapeConfigQuiet()
119119

120-
c := NewChapterFromURL("https://html5example.com/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
120+
c := NewChapterFromURL("https://12factor.net/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
121121

122122
got := len(c.SubChapters())
123-
want := 14
123+
want := 21
124124

125125
if got != want {
126126
t.Errorf("got %v, wanted %v", got, want)
@@ -133,10 +133,10 @@ func TestSubChaptersRSS(t *testing.T) {
133133
config0 := NewScrapeConfigQuiet()
134134
config1 := NewScrapeConfigQuiet()
135135

136-
c := NewChapterFromURL("https://www.nginx.com/feed/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
136+
c := NewChapterFromURL("https://blog.nginx.org/feed", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
137137

138138
got := len(c.SubChapters())
139-
want := 14
139+
want := 10
140140

141141
if got != want {
142142
t.Errorf("got %v, wanted %v", got, want)
@@ -147,14 +147,14 @@ func TestSubChaptersRSS(t *testing.T) {
147147
func TestSubChaptersSelector(t *testing.T) {
148148

149149
config0 := NewScrapeConfigQuiet()
150-
config0.Selector = "body > aside > p > a"
150+
config0.Selector = "section.concrete>article>h2>a"
151151

152152
config1 := NewScrapeConfigQuiet()
153153

154-
c := NewChapterFromURL("https://html5example.com/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
154+
c := NewChapterFromURL("https://12factor.net/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
155155

156156
got := len(c.SubChapters())
157-
want := 14
157+
want := 12
158158

159159
if got != want {
160160
t.Errorf("got %v, wanted %v", got, want)
@@ -165,11 +165,12 @@ func TestSubChaptersSelector(t *testing.T) {
165165
func TestSubChaptersLimit(t *testing.T) {
166166

167167
config0 := NewScrapeConfigQuiet()
168+
config0.Selector = "section.concrete>article>h2>a"
168169
config0.Limit = 1
169170

170171
config1 := NewScrapeConfigQuiet()
171172

172-
c := NewChapterFromURL("https://html5example.com/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
173+
c := NewChapterFromURL("https://12factor.net/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
173174

174175
got := len(c.SubChapters())
175176
want := 1
@@ -183,14 +184,15 @@ func TestSubChaptersLimit(t *testing.T) {
183184
func TestSubChaptersLimitOver(t *testing.T) {
184185

185186
config0 := NewScrapeConfigQuiet()
187+
config0.Selector = "section.concrete>article>h2>a"
186188
config0.Limit = 15
187189

188190
config1 := NewScrapeConfigQuiet()
189191

190-
c := NewChapterFromURL("https://html5example.com/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
192+
c := NewChapterFromURL("https://12factor.net/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
191193

192194
got := len(c.SubChapters())
193-
want := 14
195+
want := 12
194196

195197
if got != want {
196198
t.Errorf("got %v, wanted %v", got, want)
@@ -201,14 +203,15 @@ func TestSubChaptersLimitOver(t *testing.T) {
201203
func TestReverse(t *testing.T) {
202204

203205
config0 := NewScrapeConfigQuiet()
206+
config0.Selector = "section.concrete>article>h2>a"
204207
config0.Reverse = true
205208

206209
config1 := NewScrapeConfigQuiet()
207210

208-
c := NewChapterFromURL("https://html5example.com/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
211+
c := NewChapterFromURL("https://12factor.net/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {})
209212

210-
got := c.SubChapters()[0].Name()
211-
want := "The W3C Markup Validation Service"
213+
got := c.SubChapters()[0].URL()
214+
want := "https://12factor.net/admin-processes"
212215

213216
if got != want {
214217
t.Errorf("got %v, wanted %v", got, want)
@@ -221,7 +224,7 @@ func TestNotInclude(t *testing.T) {
221224
config := NewScrapeConfigQuiet()
222225
config.Include = false
223226

224-
c := NewChapterFromURL("https://example.com/", "", []*ScrapeConfig{config}, 0, func(index int, name string) {})
227+
c := NewChapterFromURL("https://12factor.net/", "", []*ScrapeConfig{config}, 0, func(index int, name string) {})
225228

226229
got := c.Content()
227230
want := ""

cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ var versionCmd = &cobra.Command{
1414
Use: "version",
1515
Short: "Print the version number of papeer",
1616
Run: func(cmd *cobra.Command, args []string) {
17-
fmt.Println("papeer v0.8.2")
17+
fmt.Println("papeer v0.8.3")
1818
},
1919
}

0 commit comments

Comments
 (0)