Repository: tlienart/Franklin.jl
Pull request #687 (2020-11-02 00:12:53)

tag feeds: fix pretty URL and handle tags with special characters

fredrikekre: Hmm. Actually, let me check some more stuff before merging.
fredrikekre: Alright, so since the feed reader will most likely use the `<link>` element to generate a link for the user I think it is nice to keep them all "pretty". I don't think it should cause any problems.
tlienart: cool! thanks Fredrik 

Patch:
diff --git a/src/manager/rss_generator.jl b/src/manager/rss_generator.jl
index be87a0cb..b29b946c 100644
--- a/src/manager/rss_generator.jl
+++ b/src/manager/rss_generator.jl
@@ -61,7 +61,7 @@ $SIGNATURES
 Create an `RSSItem` out of the provided fields defined in the page vars.
 """
 function add_rss_item()
-    link  = get_url(url_curpage())
+    link  = url_curpage()
     title = jor("rss_title", "title")
     descr = jor("rss", "rss_description")
 
@@ -141,11 +141,11 @@ function rss_generator()::Nothing
     tags = Set{String}()
     foreach(x -> union!(tags, x[2]), values(RSS_DICT))
     for tag in tags
-        rss_path = joinpath(path(:tag), tag, "feed.xml")
+        rss_path = joinpath(path(:tag), refstring(tag), "feed.xml")
         ## Filter items containing this tag only
         rss_items = OrderedDict{String,RSSItem}(k => v[1] for (k, v) in RSS_DICT_SORTED if tag ∈ v[2])
         ## Find the relative path for the tag-feeds
-        rss_rel = strip(globvar("tag_page_path"), '/') * "/" * tag * "/"
+        rss_rel = strip(globvar("tag_page_path"), '/') * "/" * refstring(tag) * "/"
         ## Write the file
         write_rss_xml(rss_path, rss_title, rss_descr, rss_link, rss_items, rss_rel)
     end
@@ -174,18 +174,14 @@ function write_rss_xml(rss_path, rss_title, rss_descr, rss_link, rss_items, rss_
 
     # loop over items
     for (k, v) in rss_items
-        full_link = rss_link
-        if startswith(v.link, "/")
-            full_link *= v.link[2:end]
-        else
-            full_link *= v.link
-        end
+        full_link = rss_link * lstrip(v.link, '/')
+        full_link = replace(full_link, r"index\.html$" => "")
         write(rss_buff,
           """
             <item>
               <title>$(v.title)</title>
               <link>$(full_link)</link>
-              <description><![CDATA[$(fix_relative_links(v.description, rss_link))<br><a href=\"$full_link\">Read more</a>]]></description>
+              <description><![CDATA[$(fix_relative_links(v.description, rss_link))<br><a href=\"$(full_link)\">Read more</a>]]></description>
           """)
         for elem in (:author, :category, :comments, :enclosure)
             e = getproperty(v, elem)


Test patch:
diff --git a/test/manager/rss.jl b/test/manager/rss.jl
index 23941c57..fce339a1 100644
--- a/test/manager/rss.jl
+++ b/test/manager/rss.jl
@@ -20,22 +20,22 @@ end
     F.set_var!(F.GLOBAL_VARS, "website_descr", "Website descr")
     F.set_var!(F.GLOBAL_VARS, "website_url", "https://github.com/tlienart/Franklin.jl/")
 
-    # Page 1 with tags = ["foo"]
+    # Page 1 with tags = ["foo", "t.a g"]
     F.def_LOCAL_VARS!()
     set_curpath("hey/hello.md")
     F.set_var!(F.LOCAL_VARS, "rss_title", "title 1")
     F.set_var!(F.LOCAL_VARS, "rss", "Page with tag foo.")
     F.set_var!(F.LOCAL_VARS, "rss_pubdate", Date(2020, 10, 27))
-    F.set_var!(F.LOCAL_VARS, "tags", ["foo"])
+    F.set_var!(F.LOCAL_VARS, "tags", ["foo", "t.a g"])
 
     item, tags = F.add_rss_item()
     @test item.title == "title 1"
     @test item.description == "Page with tag foo.\n"
     @test item.author == ""
     @test item.pubDate == Date(2020, 10, 27)
-    @test tags == ["foo"]
-    @test F.RSS_DICT["/hey/hello/"][1].description == item.description
-    @test F.RSS_DICT["/hey/hello/"][2] == ["foo"]
+    @test tags == ["foo", "t.a g"]
+    @test F.RSS_DICT["/hey/hello/index.html"][1].description == item.description
+    @test F.RSS_DICT["/hey/hello/index.html"][2] == ["foo", "t.a g"]
 
     # Page 2 with tags = ["foo", "bar"]
     F.def_LOCAL_VARS!()
@@ -52,8 +52,8 @@ end
     @test item.author == "chuck@norris.com"
     @test item.pubDate == Date(2020, 10, 30)
     @test tags == ["foo", "bar"]
-    @test F.RSS_DICT["/hey/ho/"][1].description == item.description
-    @test F.RSS_DICT["/hey/ho/"][2] == ["foo", "bar"]
+    @test F.RSS_DICT["/hey/ho/index.html"][1].description == item.description
+    @test F.RSS_DICT["/hey/ho/index.html"][2] == ["foo", "bar"]
 
     # Generation
     F.PATHS[:folder] = td
@@ -66,6 +66,10 @@ end
     @test occursin("<description><![CDATA[Page with tag foo.", fc)
     @test occursin("<link>https://github.com/tlienart/Franklin.jl/hey/ho/</link>", fc)
     @test occursin("<link>https://github.com/tlienart/Franklin.jl/hey/hello/</link>", fc)
+    @test occursin("<guid>https://github.com/tlienart/Franklin.jl/hey/ho/</guid>", fc)
+    @test occursin("<guid>https://github.com/tlienart/Franklin.jl/hey/hello/</guid>", fc)
+    @test occursin("<br><a href=\"https://github.com/tlienart/Franklin.jl/hey/ho/\">Read more</a>", fc)
+    @test occursin("<br><a href=\"https://github.com/tlienart/Franklin.jl/hey/hello/\">Read more</a>", fc)
     @test occursin("<pubDate>Fri, 30 Oct 2020 00:00:00 UT</pubDate>", fc)
     @test occursin("<pubDate>Tue, 27 Oct 2020 00:00:00 UT</pubDate>", fc)
     @test occursin("<atom:link href=\"https://github.com/tlienart/Franklin.jl/feed.xml\" rel=\"self\" type=\"application/rss+xml\" />", fc)
@@ -91,4 +95,10 @@ end
     @test occursin("<pubDate>Fri, 30 Oct 2020 00:00:00 UT</pubDate>", bar_feed)
     @test !occursin("<pubDate>Tue, 27 Oct 2020 00:00:00 UT</pubDate>", bar_feed)
     @test occursin("<atom:link href=\"https://github.com/tlienart/Franklin.jl/tag/bar/feed.xml\" rel=\"self\" type=\"application/rss+xml\" />", bar_feed)
+
+    ### t.a g tag (check handling of special characters)
+    feed = joinpath(F.PATHS[:tag], "ta_g", "feed.xml")
+    @test isfile(feed)
+    tag_feed = read(feed, String)
+    @test occursin("<atom:link href=\"https://github.com/tlienart/Franklin.jl/tag/ta_g/feed.xml\" rel=\"self\" type=\"application/rss+xml\" />", tag_feed)
 end
diff --git a/test/utils/warnings.jl b/test/utils/warnings.jl
index 39eb0967..561cce02 100644
--- a/test/utils/warnings.jl
+++ b/test/utils/warnings.jl
@@ -28,7 +28,7 @@ end
     s = @capture_out F.add_rss_item()
     @test occursin("Warning: in <input string>", s)
     @test occursin("An RSS description was found but without title", s)
-    @test occursin("for page '/hey/ho/'.", s)
+    @test occursin("for page '/hey/ho/index.html'.", s)
 end
 
 @testset "warn-paginate" begin