comment_paragraph_plugin.rb
2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class CommentParagraphPlugin < Noosfero::Plugin
def self.plugin_name
"Comment Paragraph"
end
def self.plugin_description
_("A plugin that display comments divided by paragraphs.")
end
def unavailable_comments(scope)
scope.without_paragraph
end
def comment_form_extra_contents(args)
comment = args[:comment]
paragraph_id = comment.paragraph_id || args[:paragraph_id]
proc {
arr = []
arr << hidden_field_tag('comment[id]', comment.id)
arr << hidden_field_tag('comment[paragraph_id]', paragraph_id) if paragraph_id
arr << hidden_field_tag('comment[comment_paragraph_selected_area]', comment.comment_paragraph_selected_area) if comment.comment_paragraph_selected_area
arr
}
end
def comment_extra_contents(args)
comment = args[:comment]
proc {
render :file => 'comment/comment_extra', :locals => {:comment => comment}
}
end
def js_files
['comment_paragraph_macro', 'rangy-core', 'rangy-cssclassapplier', 'rangy-serializer']
end
def stylesheet?
true
end
def cms_controller_filters
block = proc do
if params['commit'] == 'Save'
settings = Noosfero::Plugin::Settings.new(environment, CommentParagraphPlugin, params[:settings])
extend CommentParagraphPlugin::CommentParagraphHelper
if !@article.id.blank? && self.auto_marking_enabled?(settings, @article.class.name)
parsed_paragraphs = []
paragraph_id = 0
doc = Hpricot(@article.body)
paragraphs = doc.search("/*").each do |paragraph|
if paragraph.to_html =~ /^<div(.*)paragraph_comment(.*)$/ || paragraph.to_html =~ /^<p>\W<\/p>$/
parsed_paragraphs << paragraph.to_html
else
if paragraph.to_html =~ /^(<div|<table|<p|<ul).*/
parsed_paragraphs << CommentParagraphPlugin.parse_paragraph(paragraph.to_html, paragraph_id)
else
parsed_paragraphs << paragraph.to_html
end
end
paragraph_id += 1
end
@article.body = parsed_paragraphs.join()
@article.save
end
end
end
{ :type => 'after_filter',
:method_name => 'new',
:block => block }
end
private
def self.parse_paragraph( paragraph_content, paragraph_id )
"<div class='macro article_comments paragraph_comment' " +
"data-macro='comment_paragraph_plugin/allow_comment' " +
"data-macro-paragraph_id='#{paragraph_id}'>#{paragraph_content}</div>\r\n" +
"<p> </p>"
end
end
require_dependency 'comment_paragraph_plugin/macros/allow_comment'