|
8 | 8 | class Atom::EntryContent
|
9 | 9 | include ActionView::Helpers::UrlHelper
|
10 | 10 |
|
11 |
| - # @param article [Sitepress::Model] the article to render for atom feed |
12 |
| - def initialize(article, request: nil) |
13 |
| - @article = article |
| 11 | + # @param page [Page] the Page instance to render for atom feed |
| 12 | + def initialize(page, request: nil) |
| 13 | + @page = page |
14 | 14 | @request = request
|
15 | 15 | end
|
16 | 16 |
|
17 | 17 | def render
|
18 |
| - html = render_template_html |
| 18 | + html = page.body_html(format: :atom) |
19 | 19 |
|
20 | 20 | doc = Nokogiri::HTML.fragment(html)
|
21 | 21 |
|
22 | 22 | doc.css("turbo-frame").each do |frame|
|
23 |
| - frame.add_next_sibling(link_to("Click here to see content", article_url)) |
| 23 | + frame_url = base_uri.merge(path: page.request_path, fragment: (frame["id"] if frame["id"])).to_s |
| 24 | + frame.add_next_sibling("(" + link_to("Go to the article to see dynamic content", frame_url) + ")") |
24 | 25 | frame.remove
|
25 | 26 | end
|
26 | 27 |
|
27 | 28 | doc.css("img").each do |img|
|
28 |
| - img["src"] = base_url + img["src"] if img["src"].start_with?("/") |
| 29 | + img["src"] = url(img["src"]) if img["src"].start_with?("/") |
29 | 30 | end
|
30 | 31 |
|
31 | 32 | doc.css("a").each do |a|
|
32 |
| - a["href"] = base_url + a["href"] if a["href"].start_with?("/") |
| 33 | + a["href"] = url(a["href"]) if a["href"].start_with?("/") |
33 | 34 | end
|
34 | 35 |
|
35 | 36 | doc.to_html
|
36 | 37 | end
|
37 | 38 |
|
38 | 39 | private
|
39 | 40 |
|
40 |
| - attr_reader :article, :request |
| 41 | + attr_reader :page, :request |
| 42 | + |
| 43 | + def base_uri |
| 44 | + @base_uri ||= Addressable::URI.parse(base_url) |
| 45 | + end |
| 46 | + |
| 47 | + def url(path) |
| 48 | + base_uri.join(path).to_s |
| 49 | + end |
41 | 50 |
|
42 | 51 | def base_url
|
43 | 52 | request&.base_url || ""
|
44 | 53 | end
|
45 | 54 |
|
46 | 55 | def article_url
|
47 |
| - base_url + article.request_path |
48 |
| - end |
49 |
| - |
50 |
| - def render_template_html |
51 |
| - ApplicationController.render( |
52 |
| - inline: article.body, |
53 |
| - type: :"mdrb-atom", |
54 |
| - layout: false, |
55 |
| - content_type: "application/atom+xml", |
56 |
| - assigns: { |
57 |
| - current_page: article, |
58 |
| - format: :atom |
59 |
| - } |
60 |
| - ) |
| 56 | + url page.request_path |
61 | 57 | end
|
62 | 58 | end
|
0 commit comments