forked from DecidimAustria/decidim-enhanced_textwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word.rb
309 lines (249 loc) · 8.81 KB
/
word.rb
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# frozen_string_literal: true
require "caracal"
module Decidim
module Exporters
# Exports any serialized object (Hash) into a readable Excel file. It transforms
# the columns using slashes in a way that can be afterwards reconstructed
# into the original nested hash.
#
# For example, `{ name: { ca: "Hola", en: "Hello" } }` would result into
# the columns: `name/ca` and `name/es`.
#
# It will maintain types like Integers, Floats & Dates so Excel can deal with
# them.
class Word < Exporter
# Public: Exports a file in an Excel readable format.
#
# Returns an ExportData instance.
def export
# @docx
init_docx
@component = collection.first.component
@participatory_space = collection.first.participatory_space
print_titles @component.name
print_metadata
print_descriptions collection.first.participatory_space
collection.each do |paragraph|
next if paragraph.amended.present?
print_paragraph paragraph
end
# @docx.p collection.inspect
doxc_buffer = @docx.render
doxc_buffer.rewind
ExportData.new(doxc_buffer.sysread, "docx")
end
private
def init_docx
@docx = Caracal::Document.new("export.docx")
@docx.style id: "metadata", name: "Metadata" do
color "929292"
end
@docx.style id: "gray", name: "Gray" do
color "929292"
end
@docx.style id: "green", name: "Green" do
color "61D836"
end
@docx.style id: "red", name: "Red" do
color "EE220C"
end
end
# def print_titles(titles)
# if titles.keys.count > 1
# titles.each do |language, name|
# @docx.h1 "#{language}: #{name}"
# end
# else
# @docx.h1 titles.values.first
# end
# end
def print_titles(titles, paragraph: nil, heading: "h1", description: "")
description = "#{description}: " if description.present?
if titles.keys.count > 1
titles.each do |language, title|
@docx.send(heading, "#{description}#{language}: #{title}") if show_title_for(paragraph, title)
end
elsif show_title_for(paragraph)
@docx.send(heading, "#{description}#{titles.values.first}")
end
end
def print_content(content, description: "")
description = "#{description}: " if description.present?
if content.keys.count > 1
content.each do |language, body|
@docx.p do
text "#{description}#{language}:", bold: true if description.present?
text body.to_s
end
end
else
@docx.p do
text "#{description}:", bold: true if description.present?
text content.values.first.to_s
end
end
end
def print_metadata
metadata = [
"ID",
"participatory space id: #{@participatory_space.id}",
"component id: #{@component.id}"
]
@docx.p metadata.join(" | "), style: "metadata"
@docx.p
end
def print_descriptions(participatory_space)
participatory_space.short_description.each do |language, short_description|
@docx.p "short description (#{language}): #{short_description}"
end
participatory_space.description.each do |language, description|
@docx.p "description (#{language}): #{description}"
end
end
def print_paragraph(paragraph)
@docx.hr
print_titles(paragraph.title, paragraph: paragraph, heading: "h2", description: "title")
print_content(paragraph.body, description: "body")
# possible states: not_answered evaluating accepted rejected withdrawn
case paragraph.state
when "accepted"
@docx.p paragraph.state, bold: true, color: "61D836"
when "evaluating", ""
@docx.p paragraph.state, bold: true, color: "FFD932"
when "rejected"
@docx.p paragraph.state, bold: true, color: "EE220C"
when "not_answered", "withdrawn"
@docx.p paragraph.state, bold: true, color: "929292"
end
@docx.p "Reference: #{paragraph.reference}", style: "gray"
@docx.p "Followers: #{paragraph.followers.count}", style: "gray"
@docx.p do
text "Supports: ", bold: true
text paragraph.votes.count
end
@docx.p do
text "endorsements/total_count: ", bold: true
text paragraph.endorsements.count
end
@docx.p do
text "comments: ", bold: true
text paragraph.comments.count
end
@docx.p
if paragraph.comments.any?
@docx.h3 "Comments to this paragraph:"
paragraph.comments.where(depth: 0).each do |comment|
print_comment(comment)
end
@docx.p
end
@docx.h3 "Amendments:" if paragraph.amendments.any?
paragraph.amendments.each do |amendment|
print_amendment paragraph, amendment
end
end
def print_amendment(paragraph, amendment)
@docx.p "Amendment title: #{amendment.emendation.title.values.first}", bold: true if show_title_for(amendment.emendation)
@docx.p amendment.emendation.body.values.first
@docx.p do
text "Amendment ID: ", bold: true
text amendment.id
text ", "
text "Paragraph ID: ", bold: true
text amendment.emendation.id
end
@docx.p do
text "created: ", bold: true
text amendment.created_at
end
@docx.p do
text "author(s): ", bold: true
text amendment.emendation.authors.collect { |a| "#{a.name} (#{a.id})" }.join(", ")
end
case amendment.amendable.state
when "accepted"
@docx.p amendment.amendable.state, bold: true, color: "61D836"
when "evaluating", ""
@docx.p amendment.amendable.state, bold: true, color: "FFD932"
when "rejected"
@docx.p amendment.amendable.state, bold: true, color: "EE220C"
when "not_answered", "withdrawn"
@docx.p amendment.amendable.state, bold: true, color: "929292"
end
@docx.p "Reference: #{paragraph.reference}", style: "gray"
@docx.p "Followers: #{paragraph.followers.count}", style: "gray"
@docx.p do
text "Supports: ", bold: true
text paragraph.votes.count
end
@docx.p do
text "endorsements: ", bold: true
text paragraph.endorsements.count
end
@docx.p do
text "comments: ", bold: true
text paragraph.comments.count
end
end
def print_comment(comment)
@docx.p do
text " " * (comment.depth + 1)
text "Body: ", bold: true
text comment.body.values.first
end
@docx.p do
text " " * (comment.depth + 1)
text "ID: ", bold: true
text comment.id
end
@docx.p do
text " " * (comment.depth + 1)
text "created: ", bold: true
text comment.created_at
end
@docx.p do
text " " * (comment.depth + 1)
text "author: ", bold: true
text "#{comment.author.name} (#{comment.author.id})"
end
@docx.p do
text " " * (comment.depth + 1)
text "alignment: ", bold: true
case comment.alignment
when 1
text "in favor", color: "61D836"
when 0
text "neutral", color: "929292"
when -1
text "against", color: "EE220C"
end
end
if comment.user_group.present?
@docx.p do
text " " * (comment.depth + 1)
text "user-group id: ", bold: true
text comment.decidim_user_group_id.to_s
end
@docx.p do
text " " * (comment.depth + 1)
text "user-group name: ", bold: true
text comment.user_group.name
end
end
# find comments to this comment and print them recursively
Decidim::Comments::Comment.where(decidim_commentable_id: comment.id).each do |sub_comment|
print_comment(sub_comment)
end
end
def show_title_for(paragraph, title = "")
return true unless paragraph.instance_of?(Decidim::EnhancedTextwork::Paragraph)
!hide_title_for(paragraph, title)
end
def hide_title_for(paragraph, title = "")
return false unless paragraph.instance_of?(Decidim::EnhancedTextwork::Paragraph)
title = paragraph.title.values.first if title.blank?
paragraph.component.settings.hide_participatory_text_titles_enabled? && title !~ /\D/
end
end
end
end