Skip to content

Commit 7c461e8

Browse files
committedMay 30, 2016
The changes were made to be able to render text as markdown
- Gem 'redcarpet' (markdown to html parser) has been added - route :markdown has been added - 'markdown' method has been added to the posts_controller - 'markdown' method that uses the redcarpet gem has been added to the posts_helper - markdown.html.erb has been added - a button that calls for the markdown view has been added to the gui - posts.css.scss got modified for a cleaner look of the markdown view - one test has been added to post_controller_tests that tries to access a /markdown route
1 parent 84a37f5 commit 7c461e8

File tree

9 files changed

+71
-1
lines changed

9 files changed

+71
-1
lines changed
 

‎Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ gem 'rails', '~>4.0.1'
66
# gem 'rails', :git => 'git://github.com/rails/rails.git'
77

88
gem 'sqlite3'
9-
9+
gem 'redcarpet'
1010
gem 'sass-rails'
1111
gem 'railties'
1212
gem 'coffee-rails'

‎Gemfile.lock

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ GEM
9696
rake (>= 0.8.7)
9797
thor (>= 0.18.1, < 2.0)
9898
rake (10.1.0)
99+
redcarpet (3.3.4)
99100
sass (3.2.9)
100101
sass-rails (4.0.0)
101102
railties (>= 4.0.0.beta, < 5.0)
@@ -143,7 +144,11 @@ DEPENDENCIES
143144
pygments.rb!
144145
rails (~> 4.0.1)
145146
railties
147+
redcarpet
146148
sass-rails
147149
sqlite3
148150
thin
149151
uglifier
152+
153+
BUNDLED WITH
154+
1.12.3

‎app/assets/stylesheets/posts.css.scss

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@
8181
.diff li.diff-comment { display: none; }
8282
.diff li.diff-block-info { background: none repeat scroll 0 0 gray; }
8383

84+
.markdown {
85+
border-bottom: 1px solid darkgrey;
86+
padding-bottom: 20px;
87+
}
88+
8489
pre.line-pre {
8590
margin: 0;
8691
padding: 0;

‎app/controllers/posts_controller.rb

+7
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ def dislike
238238
end
239239
end
240240

241+
def markdown
242+
@post = Post.find(params[:id])
243+
respond_to do |format|
244+
format.html {render :markdown}
245+
end
246+
end
247+
241248
private
242249

243250
def post_params

‎app/helpers/posts_helper.rb

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
module PostsHelper
22

3+
def markdown(post)
4+
options = {
5+
filter_html: true,
6+
hard_wrap: true,
7+
link_attributes: { rel: 'nofollow', target: "_blank" },
8+
space_after_headers: true,
9+
fenced_code_blocks: true
10+
}
11+
12+
extensions = {
13+
autolink: true,
14+
superscript: true,
15+
disable_indented_code_blocks: true
16+
}
17+
18+
renderer = Redcarpet::Render::HTML.new(options)
19+
markdown = Redcarpet::Markdown.new(renderer, extensions)
20+
21+
markdown.render(post.content).html_safe
22+
end
23+
324
def preview_content(post)
425
options = { options: {encoding: 'utf-8'} }
526
if Pygments::Lexer.find(post.content_type)

‎app/views/posts/_options.html.erb

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
<i class="icon-download"></i>
3131
<% end %>
3232

33+
<%= link_to markdown_post_path(@post), :class => "btn btn-mini",
34+
:title => "Markdown" do %>
35+
<i class="icon-flag"></i>
36+
<% end %>
37+
38+
3339
<% unless current_page?(root_url) || current_page?(posts_path) %>
3440
<%= link_to posts_path, :class => "btn btn-mini", :title => "Posts" do %>
3541
<i class="icon-align-justify"></i>

‎app/views/posts/markdown.html.erb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div class="row">
2+
<div class="markdown">
3+
<%= markdown(@post) %>
4+
</div>
5+
6+
<% if @post.author.present? %>
7+
<p class="author">- <%= @post.author %></p>
8+
<% end %>
9+
10+
<%= render 'options' %>
11+
12+
<input id="post_url" class="input-xlarge uneditable-input" type="text"
13+
value="<%= post_url(@post) %>"></input>
14+
15+
<br/><br/>
16+
<%= render 'like_dislike' %>
17+
<%= render 'comments/comment' %>
18+
</div>

‎config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
get :diff
1111
get :like
1212
get :dislike
13+
get :markdown
1314
end
1415
end
1516
resources :posts, :as => :p

‎test/functional/posts_controller_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ class PostsControllerTest < ActionController::TestCase
157157
assert_template(:diff)
158158
end
159159

160+
test "should show markdown for post" do
161+
get :markdown, :id => 106
162+
assert_response :success
163+
assert_not_nil(:post)
164+
assert_template(:markdown)
165+
end
166+
160167
test "should upload file from form" do
161168
test_image = "test/fixtures/test.txt"
162169
file = Rack::Test::UploadedFile.new(test_image, "text/plain")

0 commit comments

Comments
 (0)
Please sign in to comment.