diff --git a/src/manager/rss_generator.jl b/src/manager/rss_generator.jl index c866b6cf9..7c7c3c723 100644 --- a/src/manager/rss_generator.jl +++ b/src/manager/rss_generator.jl @@ -14,15 +14,17 @@ # 3. save the file struct RSSItem + # -- required fields title::String link::String - description::String - author::String + description::String # note: should not contain

+ # -- optional fields + author::String # note: should be a valid email category::String - comments::String + comments::String # note: should be a valid URL enclosure::String - # guid -- hash of link - pubDate::Date + # guid == link + pubDate::Date # note: should respect RFC822 (https://www.w3.org/Protocols/rfc822/) end const RSS_DICT = Dict{String,RSSItem}() @@ -31,6 +33,17 @@ const RSS_DICT = Dict{String,RSSItem}() """Convenience function for fallback fields""" jor(v::PageVars, a::String, b::String) = ifelse(isempty(first(v[a])), first(v[b]), first(v[a])) +"""Convenience function to remove

and

in RSS description (not supposed to happen)""" +remove_html_ps(s::String)::String = replace(s, r"" => "") + +""" +$SIGNATURES + +RSS should not contain relative links so this finds relative links and prepends them with the +canonical link. +""" +fix_relative_links(s::String, link::String) = + replace(s, r"(href|src)\s*?=\s*?\"\/" => SubstitutionString("\\1=\"$link")) """ $SIGNATURES @@ -41,10 +54,10 @@ function add_rss_item(jdv::PageVars)::RSSItem link = url_curpage() title = jor(jdv, "rss_title", "title") descr = jor(jdv, "rss", "rss_description") - author = jor(jdv, "rss_author", "author") - descr = jd2html(descr; internal=true) + descr = jd2html(descr; internal=true) |> remove_html_ps + author = jdv["rss_author"] |> first category = jdv["rss_category"] |> first comments = jdv["rss_comments"] |> first enclosure = jdv["rss_enclosure"] |> first @@ -61,8 +74,8 @@ function add_rss_item(jdv::PageVars)::RSSItem isnothing(title) && (title = "") isempty(title) && @warn "Found an RSS description but no title for page $link." - RSS_DICT[link] = RSSItem(title, link, descr, - author, category, comments, enclosure, pubDate) + RSS_DICT[link] = RSSItem(title, link, descr, author, + category, comments, enclosure, pubDate) end @@ -88,6 +101,10 @@ function rss_generator()::Nothing The feed will not be (re)generated.""" return nothing end + + endswith(rss_link, "/") || (rss_link *= "/") + rss_descr = jd2html(rss_descr; internal=true) |> remove_html_ps + # is there an RSS file already? if so remove it rss_path = joinpath(PATHS[:folder], "feed.xml") isfile(rss_path) && rm(rss_path) @@ -96,45 +113,39 @@ function rss_generator()::Nothing rss_buff = IOBuffer() write(rss_buff, """ - $rss_title - $rss_descr + $rss_link + """) # loop over items for (k, v) in RSS_DICT full_link = rss_link - # ends with / and next doesn't -> ok - # not ends with / and next doesn't -> add - # ends with / and next starts -> chop - # not ends with / and next does -> ok - if endswith(full_link, "/") - if startswith(v.link, "/") - full_link *= v.link[2:end] - else - full_link *= v.link - end + if startswith(v.link, "/") + full_link *= v.link[2:end] else - if startswith(v.link, "/") - full_link *= v.link - else - full_link *= "/" * v.link - end + full_link *= v.link end write(rss_buff, """ $(v.title) $(full_link) - $(v.description) - $(v.author) - $(v.category) - $(v.comments) - $(v.enclosure) - $(hash(v.link)) - $(Dates.format(v.pubDate, "e, d u Y")) 00:00:00 UTC + + """) + for elem in (:author, :category, :comments, :enclosure) + e = getproperty(v, elem) + isempty(e) || write(rss_buff, + """ + <$elem>$e + """) + end + write(rss_buff, + """ + $(full_link) + $(Dates.format(v.pubDate, "e, d u Y")) 00:00:00 UT """) end diff --git a/test/global/rss.jl b/test/global/rss.jl index 379d51f1e..e8fe31c9a 100644 --- a/test/global/rss.jl +++ b/test/global/rss.jl @@ -4,11 +4,11 @@ @test isfile(f) fc = prod(readlines(f, keep=true)) - @test occursin(raw"""""", fc) @test occursin(raw"""""", fc) @test occursin(raw"""JuDoc Template""", fc) - @test occursin(raw"""Example website using JuDoc""", fc) - @test occursin(raw"""Septimia Zenobia""", fc) + @test occursin(raw"""""", fc) + @test !occursin(raw"""""", fc) @test occursin(raw"""https://tlienart.github.io/JuDocTemplates.jl/pub/menu1.html""", fc) - @test occursin(raw"""

A short description of the page which would serve as blurb in a RSS feed;""", fc) + @test occursin(raw"""blurb in a RSS feed;""", fc) end diff --git a/test/manager/rss.jl b/test/manager/rss.jl index 3a45855c4..60c5740ac 100644 --- a/test/manager/rss.jl +++ b/test/manager/rss.jl @@ -1,13 +1,13 @@ @testset "RSSItem" begin rss = J.RSSItem( - "title", "link", "description", "author", "category", - "comments", "enclosure", Date(2012,12,12)) + "title", "www.link.com", "description", "author@author.com", "category", + "www.comments.com", "enclosure", Date(2012,12,12)) @test rss.title == "title" - @test rss.link == "link" + @test rss.link == "www.link.com" @test rss.description == "description" - @test rss.author == "author" + @test rss.author == "author@author.com" @test rss.category == "category" - @test rss.comments == "comments" + @test rss.comments == "www.comments.com" @test rss.enclosure == "enclosure" @test rss.pubDate == Date(2012,12,12) end @@ -21,12 +21,12 @@ end jdv = merge(J.GLOBAL_PAGE_VARS, copy(J.LOCAL_PAGE_VARS)) J.set_var!(jdv, "rss_title", "title") J.set_var!(jdv, "rss", "A **description** done.") - J.set_var!(jdv, "author", "Chuck") + J.set_var!(jdv, "rss_author", "chuck@norris.com") item = J.add_rss_item(jdv) @test item.title == "title" - @test item.description == "

A description done.

\n" - @test item.author == "Chuck" + @test item.description == "A description done.\n" + @test item.author == "chuck@norris.com" # unchanged bc all three fallbacks lead to Data(1) @test item.pubDate == Date(1) @@ -41,5 +41,5 @@ end feed = joinpath(J.PATHS[:folder], "feed.xml") @test isfile(feed) fc = prod(readlines(feed, keep=true)) - @test occursin("\n \n https://github.com/tlienart/JuDoc.jl/hey/ho.html\n

A description done.

\n
", fc) + @test occursin("description done.\n]]>", fc) end