Skip to content

Commit 24b3a2c

Browse files
authored
Merge pull request #193 from joyofrails/feat/atom-app-file-code-blocks
Render basic code block from app file within atom
2 parents cdc4b56 + 090eaff commit 24b3a2c

File tree

8 files changed

+34
-7
lines changed

8 files changed

+34
-7
lines changed

app/controllers/feed_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ def index
33
@articles = ArticlePage.published
44
respond_to do |format|
55
format.html { redirect_to feed_path(format: :atom), status: :moved_permanently }
6-
# format.xml { headers["Content-Type"] = 'application/rss+xml; charset=utf-8'}
7-
format.atom { headers["Content-Type"] = "application/atom+xml; charset=utf-8" }
6+
format.atom
87
end
98
end
109
end

app/models/examples/app_file.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Examples
24
class AppFile
35
class << self

app/views/components/atom/entry_content.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def render_template_html
5050
inline: article.body,
5151
type: :"mdrb-atom",
5252
layout: false,
53-
content_type: article.mime_type.to_s,
53+
content_type: "application/atom+xml",
5454
assigns: {
5555
format: :atom
5656
}

app/views/components/code_block/app_file.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ def initialize(filename, lines: nil, revision: "HEAD", **attributes)
1010
end
1111

1212
def view_template
13+
# When an AppFile code block renders within an Atom feed, we want to render the basic code block
14+
if content_type?("application/atom+xml")
15+
return render ::CodeBlock::Basic.new(source, **attributes)
16+
end
17+
1318
render ::CodeBlock::Article.new(**attributes) do |code_block|
1419
code_block.title do
1520
link(app_file.repo_url, "Source code on Github", class: "nc") {
@@ -31,4 +36,8 @@ def view_template
3136
def source
3237
app_file.source(lines: lines)
3338
end
39+
40+
def content_type?(type)
41+
helpers.headers["Content-Type"].to_s =~ %r{#{Regexp.escape(type)}}
42+
end
3443
end

app/views/components/markdown/allows_erb.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# DANGER! This parses Erb, which means arbitrary Ruby can be run. Make sure
2+
# you trust the source of your markdown and that its not user input.
3+
14
module Markdown::AllowsErb
25
ERB_TAGS = %r{s*<%.*?%>}
36
ERB_TAGS_START = %r{\A<%.*?%>}

app/views/components/markdown/atom.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# DANGER! This parses Erb, which means arbitrary Ruby can be run. Make sure
2-
# you trust the source of your markdown and that its not user input.
3-
41
class Markdown::Atom < Markdown::Application
52
prepend Markdown::AllowsErb
63
end

app/views/feed/index.atom.builder

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ atom_feed do |feed|
99
url: request.base_url + article.request_path
1010
) do |entry|
1111
entry.title(article.title)
12-
entry.content(Atom::EntryContent.new(article).render, type: "html")
12+
entry.content(Atom::EntryContent.new(article, request: request).render, type: "html")
1313

1414
entry.author do |author|
1515
author.name(article.author)

spec/views/components/code_block/app_file_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ def render_page(*, **)
55
Capybara.string(render(*, **))
66
end
77

8+
before do
9+
allow(view).to receive(:headers).and_return({"Content-Type" => "text/html"})
10+
end
11+
812
it "renders contents of file" do
913
page = render_page(CodeBlock::AppFile.new("config/database.yml"))
1014
expect(page).to have_content(<<~YAML)
1115
default: &default
1216
adapter: sqlite3
1317
YAML
18+
expect(page).to have_content("config/database.yml")
1419
end
1520

1621
it "renders contents of file by line number" do
@@ -42,4 +47,16 @@ def render_page(*, **)
4247
expect(page).to have_content("database: storage/production/data.sqlite3")
4348
expect(page).not_to have_content("database: storage/development/data.sqlite3")
4449
end
50+
51+
it "renders basic code block when content type is atom" do
52+
allow(view).to receive(:headers).and_return({"Content-Type" => "application/atom+xml"})
53+
54+
page = render_page(CodeBlock::AppFile.new("config/database.yml"))
55+
expect(page).to have_content(<<~YAML)
56+
default: &default
57+
adapter: sqlite3
58+
YAML
59+
60+
expect(page).not_to have_content("config/database.yml")
61+
end
4562
end

0 commit comments

Comments
 (0)