Bläddra i källkod

Convert text links and line feeds to HTML in YouTube channels

Frédéric Guillot 9 månader sedan
förälder
incheckning
9606126196

+ 3
- 3
locale/translations.go Visa fil

@@ -754,8 +754,8 @@ var translations = map[string]string{
754 754
     "error.feed_mandatory_fields": "L'URL et la catégorie sont obligatoire.",
755 755
     "error.user_mandatory_fields": "Le nom d'utilisateur est obligatoire.",
756 756
     "form.feed.label.title": "Titre",
757
-    "form.feed.label.site_url": "URL du flux",
758
-    "form.feed.label.feed_url": "URL du site web",
757
+    "form.feed.label.site_url": "URL du site web",
758
+    "form.feed.label.feed_url": "URL du flux",
759 759
     "form.feed.label.category": "Catégorie",
760 760
     "form.feed.label.crawler": "Récupérer le contenu original",
761 761
     "form.feed.label.feed_username": "Nom d'utilisateur du flux",
@@ -1993,7 +1993,7 @@ var translations = map[string]string{
1993 1993
 var translationsChecksums = map[string]string{
1994 1994
 	"de_DE": "099dea24a10c4f842674db2ae44f99e99b9c880a6f83e3a42502603fa228fd32",
1995 1995
 	"en_US": "c23d1f16d1dbea72c1e1ba558c7a9c25e0ee8ffda420d50c998efe2fb4d9aa55",
1996
-	"fr_FR": "b123c66c61cda3ae4a978465b7a0e332a83cf386032e1df11c033ed8def9203c",
1996
+	"fr_FR": "a5afa30bb63cba48fe0c2114a5e0bcb62dee7f1df0eb5748524decd280c80970",
1997 1997
 	"nl_NL": "b1e548c2b21f013b1b54a07df7df7c06c776cbd7d26fc1fed288bd6970e99c3c",
1998 1998
 	"pl_PL": "8cb856dede8b4f75e4c6aeb0a45f09507c5010f782692e887aae357e99674218",
1999 1999
 	"ru_RU": "0544db0800811fc678521b2e9a7141380919b9ae259b3158524619bf120600ab",

+ 2
- 2
locale/translations/fr_FR.json Visa fil

@@ -191,8 +191,8 @@
191 191
     "error.feed_mandatory_fields": "L'URL et la catégorie sont obligatoire.",
192 192
     "error.user_mandatory_fields": "Le nom d'utilisateur est obligatoire.",
193 193
     "form.feed.label.title": "Titre",
194
-    "form.feed.label.site_url": "URL du flux",
195
-    "form.feed.label.feed_url": "URL du site web",
194
+    "form.feed.label.site_url": "URL du site web",
195
+    "form.feed.label.feed_url": "URL du flux",
196 196
     "form.feed.label.category": "Catégorie",
197 197
     "form.feed.label.crawler": "Récupérer le contenu original",
198 198
     "form.feed.label.feed_username": "Nom d'utilisateur du flux",

+ 12
- 3
reader/rewrite/rewrite_functions.go Visa fil

@@ -13,8 +13,9 @@ import (
13 13
 )
14 14
 
15 15
 var (
16
-	youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
17
-	imgRegex     = regexp.MustCompile(`<img [^>]+>`)
16
+	youtubeRegex  = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
17
+	imgRegex      = regexp.MustCompile(`<img [^>]+>`)
18
+	textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
18 19
 )
19 20
 
20 21
 func addImageTitle(entryURL, entryContent string) string {
@@ -108,7 +109,7 @@ func addYoutubeVideo(entryURL, entryContent string) string {
108 109
 
109 110
 	if len(matches) == 2 {
110 111
 		video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
111
-		return video + "<p>" + entryContent + "</p>"
112
+		return video + "<p>" + replaceLineFeeds(replaceTextLinks(entryContent)) + "</p>"
112 113
 	}
113 114
 	return entryContent
114 115
 }
@@ -119,3 +120,11 @@ func addPDFLink(entryURL, entryContent string) string {
119 120
 	}
120 121
 	return entryContent
121 122
 }
123
+
124
+func replaceTextLinks(input string) string {
125
+	return textLinkRegex.ReplaceAllString(input, `<a href="${1}">${1}</a>`)
126
+}
127
+
128
+func replaceLineFeeds(input string) string {
129
+	return strings.Replace(input, "\n", "<br>", -1)
130
+}

+ 22
- 2
reader/rewrite/rewriter_test.go Visa fil

@@ -6,6 +6,26 @@ package rewrite // import "miniflux.app/reader/rewrite"
6 6
 
7 7
 import "testing"
8 8
 
9
+func TestReplaceTextLinks(t *testing.T) {
10
+	scenarios := map[string]string{
11
+		`This is a link to example.org`:                                              `This is a link to example.org`,
12
+		`This is a link to ftp://example.org`:                                        `This is a link to ftp://example.org`,
13
+		`This is a link to www.example.org`:                                          `This is a link to www.example.org`,
14
+		`This is a link to http://example.org`:                                       `This is a link to <a href="http://example.org">http://example.org</a>`,
15
+		`This is a link to http://example.org, end of sentence.`:                     `This is a link to <a href="http://example.org">http://example.org</a>, end of sentence.`,
16
+		`This is a link to https://example.org`:                                      `This is a link to <a href="https://example.org">https://example.org</a>`,
17
+		`This is a link to https://www.example.org/path/to?q=s`:                      `This is a link to <a href="https://www.example.org/path/to?q=s">https://www.example.org/path/to?q=s</a>`,
18
+		`This is a link to https://example.org/index#hash-tag, http://example.org/.`: `This is a link to <a href="https://example.org/index#hash-tag">https://example.org/index#hash-tag</a>, <a href="http://example.org/">http://example.org/</a>.`,
19
+	}
20
+
21
+	for input, expected := range scenarios {
22
+		actual := replaceTextLinks(input)
23
+		if actual != expected {
24
+			t.Errorf(`Unexpected link replacement, got "%s" instead of "%s"`, actual, expected)
25
+		}
26
+	}
27
+}
28
+
9 29
 func TestRewriteWithNoMatchingRule(t *testing.T) {
10 30
 	output := Rewriter("https://example.org/article", `Some text.`, ``)
11 31
 	expected := `Some text.`
@@ -16,8 +36,8 @@ func TestRewriteWithNoMatchingRule(t *testing.T) {
16 36
 }
17 37
 
18 38
 func TestRewriteWithYoutubeLink(t *testing.T) {
19
-	output := Rewriter("https://www.youtube.com/watch?v=1234", `Video Description`, ``)
20
-	expected := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><p>Video Description</p>`
39
+	output := Rewriter("https://www.youtube.com/watch?v=1234", "Video Description\nhttp://example.org/path", ``)
40
+	expected := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><p>Video Description<br><a href="http://example.org/path">http://example.org/path</a></p>`
21 41
 
22 42
 	if expected != output {
23 43
 		t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)

Laddar…
Avbryt
Spara