-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacroProcess.pm
270 lines (223 loc) · 8.29 KB
/
MacroProcess.pm
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
package MacroProcess;
use strict;
use warnings;
use experimental "switch";
use Encode qw(decode encode);
use HTML::Entities;
use XML::LibXML;
binmode STDERR, ':utf8';
my $DEBUG="false";
my $debug = sub {
my $message = shift @_;
print STDERR $message . "\n" if $DEBUG eq "true";
};
my $macro_images = sub {
my $tree = shift @_;
my $subtree = shift @_;
foreach my $image_node ( $tree->findnodes('//ac:image') ){
$debug->("Found image node ".$image_node->toString.".");
my $image_tag = XML::LibXML::Element->new("img");
###
#
# collecting all attributes for the image tag
#
my %image_attributes;
if ( $image_node->hasAttributes() ){
foreach my $attribute ( $image_node->attributes() ){
$image_tag->setAttribute( $attribute->localName(),
$attribute->value );
}
}
###
#
# looking up the filename
# then getting the extension via a regex
#
if ( $image_node->exists('./ri:attachment/@ri:filename') ){
my $file_name = $image_node->findvalue('./ri:attachment/@ri:filename');
my ($file_format) = $file_name =~ /\.([^.]+)$/;
my $src_string = "data:image/"
. $file_format
. ";base64,"
. $subtree->{ "attachments" }->{ $file_name };
$image_tag->setAttribute( "src",
$src_string );
} elsif ( $image_node->exists('./ri:url/@ri:value')) {
$image_tag->setAttribute( "src",
$image_node->findvalue('./ri:url/@ri:value')
);
} else {
$debug->("No image source or date was found!");
}
$image_node->replaceNode( $image_tag );
#my ( $image_node_body ) = $image_node->findnodes('/body');
#$subtree->{ "body" } = decode_entities( $image_node_body->toString );
}
};
my $macro_symbols = sub {
my $tree = shift @_;
my $subtree = shift @_;
foreach my $symbol_node ( $tree->findnodes('//ac:emoticon') ){
$debug->("Found emoticon node ".$symbol_node->toString.".");
my $symbol_node_parent = $symbol_node->parentNode;
my $symbol_name = $symbol_node->findvalue('./@ac:name');
my $symbol_unicode;
for ($symbol_name) {
when (/^tick$/) { $symbol_unicode = "\N{CHECK MARK}" }
when (/^cross$/) { $symbol_unicode = "\N{BALLOT X}" }
when (/^question$/) { $symbol_unicode = "?" }
when (/^warning$/) { $symbol_unicode = "!" }
when (/^information$/) { $symbol_unicode = "\N{INFORMATION SOURCE}" }
when (/^smile$/) { $symbol_unicode = "\N{SLIGHTLY SMILING FACE}" }
when (/^sad$/) { $symbol_unicode = "\N{SLIGHTLY FROWNING FACE}" }
when (/^checky$/) { $symbol_unicode = "\N{FACE WITH STUCK-OUT TONGUE}" }
when (/^laugh$/) { $symbol_unicode = "\N{SMILING FACE WITH OPEN MOUTH AND SMILING EYES}" }
when (/^wink$/) { $symbol_unicode = "\N{WINKING FACE}" }
when (/^thumbs-up$/) { $symbol_unicode = "\N{THUMBS UP SIGN}" }
when (/^thumbs-down$/) { $symbol_unicode = "\N{THUMBS DOWN SIGN}" }
default {
$symbol_unicode = " ";
$debug->("Unknown symbol name: $symbol_name");
}
}
my $symbol_new = XML::LibXML::Text->new( $symbol_unicode );
$symbol_node_parent->insertBefore( $symbol_new, $symbol_node );
$symbol_node->unbindNode;
print "";
}
};
my $macro_tasklist = sub {
my $tree = shift @_;
my $subtree = shift @_;
=comment
it seems that the returned list containes the nodes in
order of the depth.
assuming that this is always the case the list is being
processed in reverse order.
=cut
my $task_node_list = $tree->findnodes('//ac:task-list');
if ( $task_node_list->size ) {
my $task_node_list_size = $task_node_list->size;
while ( $task_node_list->size ){
my $tasklist_node = $task_node_list->pop;
$debug->("Found tasklist.");
my $list_tag = XML::LibXML::Element->new("ul");
foreach my $tasklist_element ( $tasklist_node->findnodes('.//ac:task') ){
# $debug->("Processing task element ".$tasklist_element->toString.".");
my $list_element = XML::LibXML::Element->new("li");
my $list_element_body = ( $tasklist_element->findnodes('./ac:task-body') )[0]->toString;
=comment
because the html tags inside the body are treaded as nodes by the xml library
the whole node is taken as a string ans the outer ac:task:body tag is stripped.
a more xml-ly method is unknown.
=cut
$list_element_body=~ s/<\/ac:task-body>//;
$list_element_body=~ s/<ac:task-body>//;
$list_element->appendTextNode( $list_element_body );
$list_tag->appendChild( $list_element );
}
$tasklist_node->replaceNode( $list_tag );
}
}
};
my $macro_usermention = sub {
my $tree = shift @_;
my $subtree = shift @_;
my $users = shift @_;
foreach my $usermention_node ( $tree->findnodes('//ac:link/ri:user/..') ){
$debug->("Found user mention macro.");
my $userkey = $usermention_node->findvalue('./ri:user/@ri:userkey');
my $username = "@" . $users->{ $userkey }->{ "name" };
my $user_tag = XML::LibXML::Element->new("strong");
$user_tag->appendTextNode( $username );
$usermention_node->replaceNode( $user_tag );
}
};
my $macro_code = sub {
my $tree = shift @_;
my $subtree = shift @_;
foreach my $code_node (
$tree->findnodes('//ac:structured-macro[@ac:name="code"]')
){
$debug->("Found code macro.");
my $paragraph_tag = XML::LibXML::Element->new("p");
my $pre_tag = XML::LibXML::Element->new("pre");
my $code_tag = XML::LibXML::Element->new("code");
$pre_tag->appendChild( $code_tag );
$paragraph_tag->appendChild( $pre_tag );
my $code_body = $code_node->findvalue('./ac:plain-text-body');
=comment
because the code might contain HTML it is encoded using url coding
to prevent being decoded or encoded by the ampersand coding.
=cut
#$code_body = url_encode_utf8( $code_body );
$code_tag->appendTextNode( $code_body );
$code_tag->setAttribute( "class",
$code_node->findvalue('./ac:parameter[@ac:name="language"]')
);
$code_node->replaceNode( $paragraph_tag );
}
};
sub macros_convert {
my $subtree = shift @_;
my $users = shift @_;
=comment
adding body tags and namespace information
otherwise the xml library is going to have trouble processing the
page content.
=cut
$debug->("Adding outer body tag with namespace information.");
my $body_tag_string = '<body xmlns:ac="http://atlassian.confluence" xmlns:ri="http://atlassian.ressource">';
my $body = $body_tag_string . $subtree->{ "body" } . "</body>";
=comment
the ampersand (&) encoded string are entities inside the xml document
yet disabling their expansion still causes errors inside libxml.
therefor the Entities library is used to convert these to unicode.
all ampersand symbols are then encoded. the libxml library is then not
going to complain about these.
then the document is converted to UTF8 because otherwise the libxml
throws an error.
this process is finicky and not completely understood.
=cut
#$body = encode( "utf8" ,decode_entities( $body ));
$body = encode( "utf8" ,$body );
$body =~ s/&/&/g;
$body =~ s/\]\]\ >/\]\]>/g;
my $tree=XML::LibXML->load_xml(
string => $body,
expand_entities =>0,
recover => 1
) || die "Could not load xml document from string!" ;
$debug->("Processing possible image macros.");
$macro_images->($tree, $subtree);
$debug->("Processing possible emoticon macros.");
$macro_symbols->($tree, $subtree);
$debug->("Processing possible tasklist macros.");
$macro_tasklist->($tree, $subtree);
$debug->("Processing possible user mention macros.");
$macro_usermention->($tree, $subtree, $users);
$debug->("Processing possible code macros.");
$macro_code->($tree, $subtree);
$subtree->{ "body" } = $tree->toString;
=comment
for some unknown reasons not all ampersand encoded entities
are being decodes during the function call.
it is necessary to call this function multiple.
=cut
while ( $subtree->{ "body" } =~ /<|>|&/
|| $subtree->{ "body" } =~ /&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});/ig){
$debug->("Found encoded entities, decoding these.");
decode_entities $subtree->{ "body" };
}
#$subtree->{ "body" } = url_decode_utf8( $subtree->{ "body" } );
###
#
# removing the xml declaration, the body tags
# and the namespace information
#
$debug->("Removing outer body tags with namespaces.");
$subtree->{ "body" } =~ s#<\?xml\ version\="1\.0"\?>##g;
$subtree->{ "body" } =~ s#\Q$body_tag_string\E##g;
$subtree->{ "body" } =~ s#</body>##g;
}
1;