blib/lib/App/DuckPAN/Web.pm untested

filter: Statementstmt 0.0% 0 / 388 Branchbran 0.0% 0 / 136 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 628 CC 69 SCAR 79.4
CC
69
Cov
0%
request
4032.0
BUILD
30.0
run_psgi
6.0
CRAP 2793.7
1 package App::DuckPAN::Web;
2 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: Webserver for duckpan server
4 $App::DuckPAN::Web::VERSION = '1021';
5 use Moo;
6 use DDG::Request;
7 use DDG::Test::Location;
8 use DDG::Test::Language;
9 use DDG::Meta::Data;
10 use Path::Tiny;
11 use Plack::Request;
12 use Plack::Response;
13 use Plack::MIME;
14 use HTML::Entities;
15 use HTML::TreeBuilder;
16 use HTML::Element;
17 use Data::Printer return_value => 'dump';
18 use HTTP::Request;
19 use HTTP::Headers;
20 use LWP::UserAgent;
21 use URI::Escape;
22 use JSON;
23  
24 has blocks => ( is => 'ro', required => 1 );
25 has page_root => ( is => 'ro', required => 1 );
26 has page_spice => ( is => 'ro', required => 1 );
27 has page_css => ( is => 'ro', required => 1 );
28 has page_js => ( is => 'ro', required => 1 );
29 has page_locales => ( is => 'ro', required => 1 );
30 has page_templates => ( is => 'ro', required => 1 );
31 has server_hostname => ( is => 'ro', required => 0 );
32  
33 has _our_hostname => ( is => 'rw' );
34 has _share_dir_hash => ( is => 'rw' );
35 has _path_hash => ( is => 'rw' );
36 has _rewrite_hash => ( is => 'rw' );
37  
38 has ua => (
39 is => 'ro',
40 default => sub {
41 LWP::UserAgent->new(
42 agent => "Mozilla/5.0", #User Agent required for some API's (eg. Vimeo, IsItUp)
43 timeout => 5,
44 ssl_opts => { verify_hostname => 0 },
45 env_proxy => 1,
46 );
47 },
48 );
49  
50 sub BUILD {
51 my ( $self ) = @_;
52 my %share_dir_hash;
53 my %path_hash;
54 my %rewrite_hash;
55 for (@{$self->blocks}) {
56 for (@{$_->only_plugin_objs}) {
57 if ($_->does('DDG::IsSpice')) {
58 $rewrite_hash{ref $_} = $_->rewrite if $_->has_rewrite;
59 my $rewrites = $_->alt_rewrites;
60 while(my ($short_name, $rewrite) = each %$rewrites){
61 $rewrite_hash{$short_name} = $rewrite;
62 $path_hash{$rewrite->path} = $short_name;
63 }
64 }
65 $share_dir_hash{$_->module_share_dir} = ref $_ if $_->can('module_share_dir');
66 $path_hash{$_->path} = ref $_ if $_->can('path');
67  
68 }
69 }
70  
71 $self->_share_dir_hash(\%share_dir_hash);
72 $self->_path_hash(\%path_hash);
73 $self->_rewrite_hash(\%rewrite_hash);
74 }
75  
76 sub run_psgi {
77 my ( $self, $app, $env ) = @_;
78 $self->_our_hostname($env->{HTTP_HOST}) unless $self->_our_hostname;
79 my $request = Plack::Request->new($env);
80 my $response = $self->request($app, $request);
81 return $response->finalize;
82 }
83  
84 my $has_common_js = 0;
85 sub request {
86 my ( $self, $app, $request ) = @_;
87 my $hostname = $self->server_hostname;
88 my @path_parts = split(/\/+/,$request->request_uri);
89 shift @path_parts;
90 my $response = Plack::Response->new(200);
91 my $body;
92  
93 if ($request->request_uri eq "/"){
94 $response->content_type("text/html");
95 $body = $self->page_root;
96 }
97 elsif (@path_parts && $path_parts[0] eq 'share') {
98 my $share_dir;
99 for (keys %{$self->_share_dir_hash}) {
100 if ($request->path =~ m|^/$_/|g) {
101  
102 $share_dir = $_;
103 # Get filename from path and url unescape
104 my $filename = uri_unescape( pop @path_parts );
105 # Trim path from left to right to find parent dir of filename
106 # e.g /share/goodie/foo/foo_imgs/image.png -> "foo_imgs"
107 my $remainder = $request->path_info;
108 $remainder =~ s|$share_dir||;
109 $remainder =~ s|$filename||;
110 $remainder =~ s|//|/|;
111 $remainder =~ s|^/\d{3,4}||;
112  
113 # if valid remainder exists, prepend to filename
114 $filename = "$remainder$filename" if $remainder ne $filename;
115  
116 if (my $filename_path = $self->_share_dir_hash->{$share_dir}->can('share')->($filename)) {
117  
118 my $content_type = Plack::MIME->mime_type($filename);
119 $response->content_type($content_type);
120  
121 if ($filename =~ /\.js$/ && $has_common_js &&
122 $request->path =~ /(share\/spice\/([^\/]+)\/?)(.*)/){
123  
124 my $parent_dir = $1;
125 my $parent_name = $2;
126 my $common_js = $parent_dir."$parent_name.js";
127  
128 $body = path($common_js)->slurp;
129 print "\nAppended $common_js to $filename\n\n";
130 }
131  
132 $body .= path($filename_path)->slurp;
133 }
134 else {
135 $share_dir = undef;
136 }
137 }
138 }
139 unless ($share_dir){
140 $response->status(404);
141 my $path = join "/", @path_parts;
142 my $errormsg = "ERROR: File not found - $path";
143 print "\n" . $errormsg . "\n";
144 $body = $errormsg;
145 }
146 }
147 elsif (@path_parts && $path_parts[0] eq 'js' && $path_parts[1] eq 'spice') {
148 my $rewrite;
149 for (keys %{$self->_path_hash}) {
150 if ($request->request_uri =~ m/^$_/g) {
151 my $path_remainder = $request->request_uri;
152 $path_remainder =~ s/^$_//;
153 $path_remainder =~ s/\/+/\//g;
154 $path_remainder =~ s/^\///;
155 my $spice_class = $self->_path_hash->{$_};
156 $rewrite = $self->_rewrite_hash->{$spice_class};
157 die "Spice tested here must have a rewrite..." unless $rewrite;
158 my $from = $rewrite->from;
159 my $re = $rewrite->has_from ? qr{$from} : qr{(.*)};
160 if (my @captures = $path_remainder =~ m/$re/) {
161 my $to = $rewrite->parsed_to;
162 my $post_body = $rewrite->post_body;
163 for (1..@captures) {
164 my $index = $_-1;
165 my $cap_from = '\$'.$_;
166 my $cap_to = $captures[$index];
167 if (defined $cap_to) {
168 $to =~ s/$cap_from/$cap_to/g;
169 $post_body =~ s/$cap_from/$cap_to/g if $post_body;
170 }
171 else {
172 $to =~ s/$cap_from//g;
173 $post_body =~ s/$cap_from//g;
174 }
175 }
176 # Make sure we replace "${dollar}" with "$".
177 $to =~ s/\$\{dollar\}/\$/g;
178  
179 my ($wrap_jsonp_callback, $callback, $wrap_string_callback, $missing_envs, $headers) =
180 ($rewrite->wrap_jsonp_callback, $rewrite->callback, $rewrite->wrap_string_callback, defined($rewrite->missing_envs), $rewrite->headers);
181  
182 # Check if environment variables (most likely the API key) is missing.
183 # If it is missing, switch to the DDG endpoint.
184 my ($use_ddh, $request_uri);
185 if ($missing_envs) {
186 ++$use_ddh;
187 $request_uri = $request->request_uri;
188 # Display the URL that we used.
189 print "\nAPI key not found. Using DuckDuckGo's endpoint:\n";
190 }
191  
192 $to = "https://beta.duckduckgo.com$request_uri" if $use_ddh;
193  
194 my $h = HTTP::Headers->new( %$headers );
195 my $res;
196 my $req;
197  
198 if ( $post_body && !$use_ddh ) {
199 $req = HTTP::Request->new(
200 POST => $to,
201 $h,
202 $post_body
203 );
204 }
205 else {
206 $req = HTTP::Request->new(
207 GET => $to,
208 $h
209 );
210 }
211  
212 p($req->as_string);
213 $res = $self->ua->request($req);
214  
215 if ($res->is_success) {
216 $body = $res->decoded_content;
217 # Encode utf8 api_responses to bytestream for Plack.
218 utf8::encode $body if utf8::is_utf8 $body;
219 warn "Cannot use wrap_jsonp_callback and wrap_string callback at the same time!" if $wrap_jsonp_callback && $wrap_string_callback;
220 if ($wrap_jsonp_callback && $callback) {
221 $body = $callback.'('.$body.');' unless $missing_envs;
222 }
223 elsif ($wrap_string_callback && $callback) {
224 $body =~ s/"/\\"/g;
225 $body =~ s/\n/\\n/g;
226 $body =~ s/\R//g;
227 $body = qq{$callback("'.$body.'");} unless $missing_envs;
228 }
229 $response->code($res->code);
230 $response->content_type($res->content_type);
231 }
232 else {
233 p($res->status_line, color => { string => 'red' });
234 my $errormsg = (pop @{[split'::', $spice_class]}). ": ".$res->status_line;
235 $body = '$("#message").removeClass("is-hidden").append("<div class=\"msg msg--warning\">'. $errormsg .'</div>");';
236 }
237 }
238 }
239 }
240 unless ($rewrite){
241 $response->status(404);
242 my $path = join "/", @path_parts;
243 my $errormsg = "ERROR: Rewrite not found - $path";
244 print "\n" . $errormsg . "\n";
245 $body = $errormsg;
246 }
247 }
248 elsif ($request->param('duckduckhack_ignore')) {
249 $response->status(204);
250 $body = "";
251 }
252 elsif ($request->param('duckduckhack_css')) {
253 $response->content_type('text/css');
254 $body = $self->page_css;
255 }
256 elsif ($request->param('duckduckhack_js')) {
257 $response->content_type('text/javascript');
258 $body = $self->page_js;
259 }
260 elsif ($request->param('duckduckhack_locales')) {
261 $response->content_type('text/javascript');
262 $body = $self->page_locales;
263 }
264 elsif ($request->param('duckduckhack_templates')) {
265 $response->content_type('text/javascript');
266 $body = $self->page_templates;
267 }
268 elsif ($request->param('q') && $request->path_info eq '/') {
269 my $query = $request->param('q');
270 $query =~ s/^\s+|\s+$//g; # strip leading & trailing whitespace
271 Encode::_utf8_on($query);
272 my $ddg_request = DDG::Request->new(
273 query_raw => $query,
274 location => test_location_by_env(),
275 language => test_language_by_env(),
276 );
277  
278 my @results = ();
279 my @calls_nrj = ();
280 my @calls_nrc = ();
281 my @calls_script = ();
282 my %calls_template = ();
283 my @calls_goodie;
284 my @calls_fathead;
285 my @ids;
286  
287 my $page = $self->page_spice;
288 my $uri_encoded_query = uri_escape_utf8($query, "^A-Za-z");
289 my $html_encoded_query = encode_entities($query);
290 my $uri_encoded_ddh = quotemeta(uri_escape('duckduckhack-template-for-spice2', "^A-Za-z0-9"));
291 $page =~ s/duckduckhack-template-for-spice2/$html_encoded_query/g;
292 $page =~ s/$uri_encoded_ddh/$uri_encoded_query/g;
293  
294 # For debugging query replacement.
295 #p($uri_encoded_ddh);
296 #p($page);
297  
298 my $root = HTML::TreeBuilder->new;
299 $root->parse($page);
300  
301  
302 ##########
303 # FATHEAD
304 my $repo = $app->get_ia_type;
305 if ($repo->{name} eq "Fathead") {
306  
307 my $output_txt = $app->fathead->output_txt;
308 if (my $result = $app->fathead->structured_answer_for_query($query)) {
309 p($result, colored => $app->colors);
310 push @calls_fathead, $result;
311 }
312 else {
313 # TODO Fallback to DDG API?
314 return $self->_no_results_error($query, $root);
315 }
316 }
317  
318 ###################
319 # SPICE & GOODIES
320 for (@{$self->blocks}) {
321 push(@results,$_->request($ddg_request));
322 }
323 if ($request->param('format') && $request->param('format') eq 'json') {
324 $response->content_type('application/x-javascript');
325 my %flattened = ();
326 if (defined $results[0]) {
327 my %result = %{$results[0]};
328 my $sa = delete $result{structured_answer};
329 %flattened = (%result, %{$sa});
330 $flattened{from} = $sa->{id};
331 };
332 my $result = %flattened ? \%flattened : '';
333 $body = to_json({ 'Answer' => $result }, { ascii => 1 });
334 $response->body($body);
335 return $response;
336 }
337  
338 # Check for no results
339 return $self->_no_results_error($query, $root) if !scalar(@results) && $repo->{name} ne "Fathead";
340  
341 # Iterate over results,
342 # checking if result is a Spice or Goodie
343 # and sets up the page content accordingly
344 foreach my $result (@results) {
345  
346 my $caller = $result->caller;
347 my $id;
348 #exlude parent/child IAs like CheatSheets
349 if (my $ia = DDG::Meta::Data->get_ia(module => $caller)){
350 if (@$ia == 1) {
351 $id = $ia->[0]{id};
352 }
353 }
354 else {
355 my $clean_module = $caller;
356 $clean_module =~ s/DDG::[^:]+:://;
357 $id = App::DuckPAN->camel_to_underscore($clean_module);
358 }
359 push @ids, $id;
360  
361 # Info for terminal.
362 p($result) if $result;
363  
364 # NOTE -- this isn't designed to have both goodies and spice at once.
365  
366 my $res_ref = ref $result;
367 my $result_type = ($res_ref eq 'DDG::ZeroClickInfo::Spice') ? 'spice' :
368 ($res_ref eq 'DDG::ZeroClickInfo') ? 'goodie' :
369 'other';
370 my $is_goodie = $result_type eq 'goodie';
371 if (($result_type eq 'spice' || $is_goodie)
372 && $caller->can('module_share_dir')) {
373 # grab associated JS, Handlebars and CSS
374 # and add them to correct arrays for injection into page
375 my $share_dir = path($caller->module_share_dir);
376 my @path = split(/\/+/, $share_dir);
377 my $ia_name = join("_", @path[2..$#path]);
378  
379 foreach ($share_dir->children) {
380 my $name = $_->basename;
381 if ($name =~ /$ia_name\.js$/){
382 push (@calls_script, $_);
383  
384 }
385 elsif ($name =~ /$ia_name\.css$/){
386 push (@calls_nrc, $_);
387  
388 }
389 elsif ($name =~ /handlebars$/){
390 $name =~ s/\.handlebars//;
391 $calls_template{$ia_name}{$name}{"content"} = $_;
392 $calls_template{$ia_name}{$name}{"is_ct_self"} = $is_goodie ? 1 : $result->call_type eq 'self';
393 }
394 }
395 push (@calls_nrj, $result->call_path) if ($result->can('call_path'));
396 }
397 if ($is_goodie){
398 # We have a Goodie result so modify HTML and return content
399 # Grab ZCI div, push in required HTML
400 my $zci_container = HTML::Element->new('div', id => "zci-answer", class => "zci zci--answer is-active");
401 if ($result->has_structured_answer) {
402 # Inject a script which prints out what we want.
403 # There is no error-checking or support for non-auto-templates here.
404 my $structured = $result->structured_answer;
405 push @ids, $structured->{dynamic_id} if exists $structured->{dynamic_id};
406 if(exists $structured->{templates}){ # user-specified templates
407 $structured->{id} = $id if $id;
408 push @calls_goodie, $structured;
409 last;
410 }
411 else{ # auto-template
412 my $input_size = @{$structured->{input}};
413 die "Auto-templating with input of size $input_size called for '$structured->{operation}', "
414 . "while only sizes up to 2 are supported" if $input_size >= 3;
415 my $template_name = 'goodie_'.$input_size.'_inputs';
416 my $json_string = encode_json({Answer => $structured});
417 $zci_container->push_content(HTML::TreeBuilder->new_from_content("<script>\$(window).load(function(){"
418 . "document.getElementById('zci-answer').innerHTML = DDG.exec_template('$template_name', $json_string);"
419 . "});</script>")->guts);
420 }
421 }
422 # HTML Goodie
423 # TODO Remove when all HTML Goodies return structured_answer
424 else {
425 $zci_container->push_content(
426 HTML::TreeBuilder->new_from_content(
427 q(<div class="cw">
428 <div class="zci__main zci__main--detail">
429 <div class="zci__body"></div>
430 </div>
431 </div>)
432 )->guts
433 );
434 my $zci_body = $zci_container->look_down(class => 'zci__body');
435  
436 # Stick the answer inside $zci_body
437 my $answer = $result->answer;
438 if ($result->has_html) {
439 my $tb = HTML::TreeBuilder->new();
440 # Specifically allow unknown tags to support <svg> and <canvas>
441 $tb->ignore_unknown(0);
442 # Allow empty tags
443 $tb->empty_element_tags(1);
444 $answer = $tb->parse_content($result->html)->guts;
445 }
446 $zci_body->push_content($answer);
447 }
448  
449 my $zci_wrapper = $root->look_down(id => "zero_click_wrapper");
450 $zci_wrapper->insert_element($zci_container);
451  
452 my $duckbar_home = $root->look_down(id => "duckbar_home");
453 $duckbar_home->delete_content();
454 $duckbar_home->attr(class => "zcm__menu");
455 $duckbar_home->push_content(
456 HTML::TreeBuilder->new_from_content(
457 q(<li class="zcm__item">
458 <a data-zci-link="answer" class="zcm__link zcm__link--answer is-active" href="javascript:;">Answer</a>
459 </li>)
460 )->guts
461 );
462  
463 my $duckbar_static_sep = $root->look_down(id => "duckbar_static_sep");
464 $duckbar_static_sep->attr(class => "zcm__sep--h");
465  
466 my $html = $root->look_down(_tag => "html");
467 $html->attr(class => "set-header--fixed has-zcm js no-touch csstransforms3d csstransitions svg use-opts has-active-zci");
468  
469 # Make sure we only show one Goodie (this will change down the road)
470 last;
471 }
472 if ($result_type eq 'other') {
473 # Not Spice or Goodie, inject raw Dumper() output from into page
474  
475 my $content = $root->look_down(id => "bottom_spacing2");
476 my $dump = HTML::Element->new('pre');
477 $dump->push_content(Dumper $result);
478 $content->insert_element($dump);
479 $page = $root->as_HTML;
480 }
481 }
482  
483 # Setup various script tags for IAs that can template:
484 # calls_script : js files
485 # calls_nrj : proxied spice api calls or goodie future
486 # calls_nrc : css calls
487 # calls_template : handlebars templates
488  
489 my $calls_nrj = join('', map {
490 DDG::Meta::Data->get_js(id => $_)
491 || qq(DDH.$_=DDH.$_||{};DDH.$_.meta={"tab":"Answer", "id":"$_"};)
492 } @ids);
493 my $calls_script = join('', map { q|<script type='text/JavaScript' src='| . $_ . q|'></script>| } @calls_script);
494 # For now we only allow a single goodie. If that changes, we will do the
495 # same join/map as with spices.
496 if(@calls_goodie){
497 my $goodie = shift @calls_goodie;
498 $calls_nrj .= "DDG.duckbar.future_signal_tab({signal:'high',from:'$goodie->{id}'});",
499 # Uncomment following line and remove "setTimeout" line when javascript race condition is addressed
500 # $calls_script = q|<script type="text/JavaScript">/*DDH.add(| . encode_json($goodie) . q|);*/</script>|;
501 $calls_script .= q|<script type="text/JavaScript">DDG.ready(function(){ window.setTimeout(DDH.add.bind(DDH, | . encode_json($goodie) . q|), 100)});</script>|;
502 }
503 elsif(@calls_fathead){
504 my $fathead = shift @calls_fathead;
505 # $calls_nrj .= "DDG.duckbar.future_signal_tab({signal:'high',from:'$fathead->{id}'});",
506 # Uncomment following line and remove "setTimeout" line when javascript race condition is addressed
507 # $calls_script = q|<script type="text/JavaScript">/*DDH.add(| . encode_json($fathead) . q|);*/</script>|;
508 $calls_script .= q|<script type="text/JavaScript">DDG.ready(function(){ window.setTimeout(DDH.add.bind(DDH, | . encode_json($fathead) . q|), 100)});</script>|;
509 }
510 else{
511 $calls_nrj .= @calls_nrj ? join(';', map { "nrj('".$_."')" } @calls_nrj) . ';' : '';
512 }
513 my $calls_nrc = @calls_nrc ? join(';', map { "nrc('".$_."')" } @calls_nrc) . ';' : '';
514  
515 if (%calls_template) {
516 foreach my $spice_name ( keys %calls_template ){
517 $calls_script .= join("",map {
518 my $template_name = $_;
519 my $is_ct_self = $calls_template{$spice_name}{$template_name}{"is_ct_self"};
520 my $template_content = $calls_template{$spice_name}{$template_name}{"content"}->slurp;
521 "<script class='duckduckhack_spice_template' spice-name='$spice_name' template-name='$template_name' is-ct-self='$is_ct_self' type='text/plain'>$template_content</script>"
522  
523 } keys %{ $calls_template{$spice_name} });
524 }
525 }
526  
527  
528 $self->_inject_mock_content($root);
529  
530 $page = $root->as_HTML;
531  
532 $page =~ s/####DUCKDUCKHACK-CALL-NRJ####/$calls_nrj/g;
533 $page =~ s/####DUCKDUCKHACK-CALL-NRC####/$calls_nrc/g;
534 $page =~ s/####DUCKDUCKHACK-CALL-SCRIPT####/$calls_script/g;
535  
536 $response->content_type('text/html');
537 $body = $page;
538  
539 }
540 else {
541 my $res = $self->ua->request(HTTP::Request->new(GET => "http://".$hostname.$request->request_uri));
542 if ($res->is_success) {
543 $body = $res->decoded_content;
544 $response->code($res->code);
545 $response->content_type($res->content_type);
546 }
547 else {
548 p($res->status_line, color => { string => 'red' });
549 $body = "";
550 }
551 }
552  
553 $response->body($body);
554 return $response;
555 }
556  
557 sub _no_results_error {
558 my ($self, $query) = @_;
559  
560 my $response = Plack::Response->new(200);
561 $response->content_type('text/html');
562 my $error = "Sorry, no results were returned from Instant Answer";
563 my $root = HTML::TreeBuilder->new;
564  
565 $root->parse($self->page_root);
566 my $text_field = $root->look_down( "name", "q" );
567 $text_field->attr( value => $query );
568 $root->find_by_tag_name('body')->push_content(
569 HTML::TreeBuilder->new_from_content(
570 qq(<script type="text/javascript">seterr('$error')</script>)
571 )->guts
572 );
573 p($error, color => { string => 'red' });
574  
575 my $body = $root->as_HTML;
576 $response->body($body);
577 return $response;
578 }
579  
580 #inject some mock results into the SERP to make it look a little more real
581 sub _inject_mock_content {
582  
583 my ($self, $root) = @_;
584  
585 # ensure results and ad containers exist
586 my $ad_container = $root->look_down(id => "ads");
587 my $links_container = $root->look_down(id => "links");
588 return unless $ad_container && $links_container;
589  
590 #inject a mock ad into the page
591 $ad_container->attr("style", "display: block");
592  
593 $ad_container->push_content(
594 HTML::TreeBuilder->new_from_content(
595 q(<div id="ra-0" class="result results_links highlight_a result--ad highlight_sponsored sponsored highlight highlight_sponsored_hover" data-nir="1">
596 <div class="result__body links_main links_deep">
597 <a href="#" class="result__badge badge--ad">Ad</a>
598 <h2 class="result__title">
599 <a class="result__a" href="#">Lorem ipsum Culpa ex adipisicing.</a>
600 <a class="result__check" href="#">
601 <span class="result__check__tt">Lorem ipsum Consectetur nostrud id quis in ut.</span>
602 </a>
603 </h2>
604 <div class="result__snippet">
605 <a href="#">Lorem ipsum Nisi aute velit sit dolore sit amet fugiat consequat aute reprehenderit in dolore deserunt.</a>
606 </div>
607 <div class="result__extras">
608 <div class="result__extras__url">
609 <a class="result__url" href="#">duckduckgo.com</a>
610 </div>
611 </div>
612 </div>
613 </div>)
614 )->guts
615 );
616  
617 #inject some mock ad into the page
618 for (1..4){
619 $links_container->push_content(
620 HTML::TreeBuilder->new_from_content(
621 qq(<div id="r$_-0" class="result results_links_deep " data-nir="$_"
622 <div class="result__body links_main links_deep">
623 <h2 class="result__title">
624 <a class="result__a" href="#">
625 Lorem ipsum Duis elit voluptate in ut sed culpa nostrud sint est occaecat in irure veniam exercitation
626 </a>
627 <a class="result__check" href="#">
628 <span class="result__check__tt">Your browser indicates if you've visited this link</span>
629 </a>
630 </h2>
631 <div class="result__snippet">
632 Lorem ipsum Mollit ut voluptate in id laborum nulla adipisicing ad ea do do nisi nulla qui quis do nisi pariatur voluptate minim dolore enim commodo cillum ullamco pariatur culpa.
633 </div>
634 <div class="result__extras">
635 <div class="result__extras__url">
636 <span class="result__icon">
637 <a href="#">
638 <img title="Search www.duckduckgo.com" id="i101" height="16" width="16" class="result__icon__img" src="//icons.duckduckgo.com/ip2/www.duckduckgo.com.ico">
639 </a>
640 </span>
641 <a class="result__url" href="#">
642 <span class="result__url__domain">Lorem.ipsum.com</span>
643 <span class="result__url__full">/Incididunt%20reprehenderit%20ullamco.</span>
644 </a>
645 </div>
646 <a href="#">More results</a>
647 </div>
648 </div>
649 </div>)
650 )->guts
651 );
652 }
653 }
654  
655 1;
656  
657 __END__