Fix haml preview (#3610)

* HAML

* rewrite

* Fix specs - but likely still wrong

* Return temple

* Trailing line

* Fix specs

* This was rearranged, apparently.

* Fix tests

* Retain escaping

* Fix specs

* Rubocop

* Attempt to fix rendering

* Fix output

* Move away from filter

* Move away from filter

* Fix spec

* Fix specs

* Fix structure to avoid nested paragraph tags
This commit is contained in:
Daniel O'Connor
2024-02-04 15:08:18 +10:30
committed by GitHub
parent 3738d7cc21
commit 49284eb169
22 changed files with 48 additions and 63 deletions

View File

@@ -34,6 +34,13 @@ module ApplicationHelper
tag.div(asterisk + ' '.html_safe + text, class: ['margin-bottom'])
end
# A helper to replace the complex template compilation mess
# of HAML, Tilt, and dynamic compilation with interpolated ruby.
def markdownify(text)
translator = Haml::Filters::GrowstuffMarkdown.new
translator.expand_members!(translator.expand_crops!(text.to_s))
end
#
# Returns an image uri for a given member.
#

View File

@@ -1,12 +1,7 @@
# frozen_string_literal: true
module PostsHelper
def display_post_truncated(post, length: 300)
truncate(strip_tags(post.body), length:,
separator: ' ', omission: '... ') { link_to "Read more", post_path(post) }
end
def post_stripped_tags(post, length: 300)
truncate(strip_tags(post.body), length:)
truncate(strip_tags(markdownify(post.body)), length:)
end
end

View File

@@ -31,6 +31,6 @@
= comment.updated_at
.comment-body
:growstuff_markdown
#{ strip_tags comment.body }
:markdown
#{ strip_tags markdownify(comment.body) }

View File

@@ -14,7 +14,7 @@
</p>
:escaped_markdown
#{ strip_tags comment.body }
#{ strip_tags markdownify(comment.body) }
%pubdate= comment.created_at.to_fs(:rfc822)
%link= post_url(comment.post)

View File

@@ -26,8 +26,8 @@
= render 'members/tiny', member: message.sender
.col-12
%p.text-justify
:growstuff_markdown
#{ strip_tags(message.body) }
:markdown
#{ strip_tags markdownify(message.body) }
%li.list-group-item
= icon 'fas', 'reply'
= render 'messages/form', conversation: @conversation

View File

@@ -16,8 +16,8 @@
= link_to @forum.owner, @forum.owner
%div
:growstuff_markdown
#{ strip_tags(@forum.description) }
:markdown
#{ strip_tags markdownify(@forum.description) }
- if can? :edit, @forum
= link_to "Edit", edit_forum_path(@forum), class: 'btn btn-default btn-xs'

View File

@@ -29,8 +29,8 @@
to plant something in this garden.
%div
%p
:growstuff_markdown
#{strip_tags @garden.description}
:markdown
#{strip_tags markdownify(@garden.description)}
- unless @garden.description
.row-fluid
%p No description available yet.

View File

@@ -58,8 +58,8 @@
.card-header
%h2 Notes
.card-body
:growstuff_markdown
#{strip_tags(@harvest.description)}
:markdown
#{strip_tags markdownify(@harvest.description)}
.col-md-4.col-xs-12
= render 'harvests/owner', harvest: @harvest

View File

@@ -5,5 +5,5 @@
- else
#{member.login_name} hasn't written a bio yet.
- else
:growstuff_markdown
#{ strip_tags member.bio }
:markdown
#{ strip_tags markdownify(member.bio) }

View File

@@ -11,4 +11,4 @@
%guid= post_url(post)
%description
:escaped_markdown
#{ strip_tags post.body }
#{ strip_tags markdownify(post.body) }

View File

@@ -12,8 +12,8 @@
\.
%blockquote
:growstuff_markdown
#{strip_tags @notification.body}
:markdown
#{strip_tags markdownify(@notification.body)}
%p
= link_to "Reply to this message", @reply_link

View File

@@ -15,6 +15,6 @@
<p>Sunniness: #{planting['sunniness'] ? planting['sunniness'] : 'unknown' }</p>
<p>Planted from: #{planting['planted_from'] ? planting['planted_from'] : 'unknown' }</p>
:escaped_markdown
#{ strip_tags planting['description'] }
#{ strip_tags markdownify(planting['description']) }
%link= planting_url(slug: planting['slug'])
%guid= planting_url(slug: planting['slug'])

View File

@@ -52,8 +52,8 @@
.card-header
%h2 Notes
.card-body
:growstuff_markdown
#{strip_tags(@planting.description)}
:markdown
#{strip_tags markdownify(@planting.description)}
%section= render 'plantings/photos', photos: @photos, planting: @planting
%section.harvests

View File

@@ -10,7 +10,9 @@
by
= succeed "," do
%a.font-weight-bold= post.author
%p.post-body= post_stripped_tags(post, length: 300)
.post-body
:markdown
#{post_stripped_tags(post, length: 300)}
%h6.font-weight-bold.mb-3
- post.crops.each do |crop|
= link_to crop do

View File

@@ -15,5 +15,5 @@
= link_to "Permalink", post
:growstuff_markdown
#{ strip_tags @post.body }
:markdown
#{ strip_tags markdownify(@post.body) }

View File

@@ -9,7 +9,7 @@
%title #{post.subject} by #{post.author.login_name}
%description
:escaped_markdown
#{ strip_tags post.body }
#{ strip_tags markdownify(post.body) }
%pubdate= post.created_at.to_fs(:rfc822)
%link= post_url(post)
%guid= post_url(post)

View File

@@ -15,7 +15,7 @@
</p>
:escaped_markdown
#{ strip_tags comment.body }
#{ strip_tags markdownify(comment.body) }
%pubdate= comment.created_at.to_fs(:rfc822)
%link= post_url(@post)

View File

@@ -45,8 +45,8 @@
.card-header
%h2 Notes
.card-body
:growstuff_markdown
#{strip_tags(@seed.description)}
:markdown
#{strip_tags markdownify(@seed.description)}
- if current_member
- if @seed.tradable && current_member != @seed.owner

View File

@@ -4,7 +4,7 @@ require 'bluecloth'
require 'haml/filters/growstuff_markdown'
class Haml::Filters
class EscapedMarkdown < Haml::Filters::GrowstuffMarkdown
class EscapedMarkdown < Haml::Filters::Markdown
def compile(node)
[:escape, true, super(node)]
end

View File

@@ -1,29 +1,17 @@
# frozen_string_literal: true
require 'bluecloth'
# TODO: Move this file/helper elsewhere, as it is used as a pre filter rather than plugging into the haml architecture
class Haml::Filters
class GrowstuffMarkdown < Haml::Filters::Markdown
def compile(node)
@expanded = node.value[:text]
expand_crops!
expand_members!
node.value[:text] = @expanded
compile_with_tilt(node, 'markdown')
end
private
class GrowstuffMarkdown
CROP_REGEX = /(?<!\\)\[([^\[\]]+?)\]\(crop\)/
MEMBER_REGEX = /(?<!\\)\[([^\[\]]+?)\]\(member\)/
MEMBER_AT_REGEX = /(?<!\\)(@\w+)/
MEMBER_ESCAPE_AT_REGEX = /(?<!\\)\\(?=@\w+)/
HOST = Rails.application.config.host
def expand_crops!
def expand_crops!(text)
# turn [tomato](crop) into [tomato](http://growstuff.org/crops/tomato)
@expanded = @expanded.gsub(CROP_REGEX) do
text.gsub(CROP_REGEX) do
crop_str = Regexp.last_match(1)
# find crop case-insensitively
crop = Crop.where('lower(name) = ?', crop_str.downcase).first
@@ -31,18 +19,18 @@ class Haml::Filters
end
end
def expand_members!
def expand_members!(text)
# turn [jane](member) into [jane](http://growstuff.org/members/jane)
# turn @jane into [@jane](http://growstuff.org/members/jane)
[MEMBER_REGEX, MEMBER_AT_REGEX].each do |re|
@expanded = @expanded.gsub(re) do
text = text.gsub(re) do
member_str = Regexp.last_match(1)
member = find_member(member_str)
member_link(member, member_str)
end
end
@expanded = @expanded.gsub(MEMBER_ESCAPE_AT_REGEX, '')
text.gsub(MEMBER_ESCAPE_AT_REGEX, '')
end
def member_link(member, link_text)
@@ -69,8 +57,4 @@ class Haml::Filters
Member.case_insensitive_login_name(login_name).first
end
end
# Register it as the handler for the :growstuff_markdown HAML command.
# The automatic system gives us :growstuffmarkdown, which is ugly.
Haml::Filters.registered[:growstuff_markdown] = GrowstuffMarkdown
end

View File

@@ -25,10 +25,7 @@ def output_member_link(member, name = nil)
end
describe 'Haml::Filters::Growstuff_Markdown' do
it 'is registered as the handler for :growstuff_markdown' do
Haml::Filters.registered[:growstuff_markdown].should ==
Haml::Filters::GrowstuffMarkdown
end
include ApplicationHelper
it 'converts quick crop links' do
@crop = FactoryBot.create(:crop)
@@ -120,8 +117,8 @@ describe 'Haml::Filters::Growstuff_Markdown' do
def haml_template(input)
<<~HTML
:growstuff_markdown
#{input}
:markdown
#{markdownify input}
HTML
end

View File

@@ -24,7 +24,7 @@ describe "posts/index" do
it "renders a list of posts" do
assert_select "div.post", count: 2
assert_select "h4", text: "A Post", count: 2
assert_select "p.post-body", text: "This is some text.", count: 2
assert_select ".post-body p", text: "This is some text.", count: 2
end
it "contains two gravatar icons" do