lib/NNexus/Index/Nlab.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 73 CC 4 SCAR 15.9
CC
4
Cov
0%
index_page
12.0
candidate_links
6.0
domain_root
2.0
CRAP 4.9
1 # /=====================================================================\ #
2 # | NNexus Autolinker | #
3 # | Indexing Plug-in, nCatLab.org domain | #
4 # |=====================================================================| #
5 # | Part of the Planetary project: http://trac.mathweb.org/planetary | #
6 # | Research software, produced as part of work done by: | #
7 # | the KWARC group at Jacobs University | #
8 # | Copyright (c) 2012 | #
9 # | Released under the MIT License (MIT) | #
10 # |---------------------------------------------------------------------| #
11 # | Adapted from the original NNexus code by | #
12 # | James Gardner and Aaron Krowne | #
13 # |---------------------------------------------------------------------| #
14 # | Deyan Ginev <d.ginev@jacobs-university.de> #_# | #
15 # | http://kwarc.info/people/dginev (o o) | #
16 # \=========================================================ooo==U==ooo=/ #
17 package NNexus::Index::Nlab;
18 use warnings;
19 use strict;
20 use base qw(NNexus::Index::Template);
21 use List::MoreUtils qw(uniq);
22 use URI::Escape;
23  
24 # nLab has a pretty unfriendly organization of their pages,
25 # so we just ask politely for them to give us all of them at once
26 sub domain_root { "ncatlab.org/nlab/search?query="; }
27 our $nlab_base = 'http://ncatlab.org';
28  
29 sub candidate_links {
30 my ($self)=@_;
31 my $url = $self->current_url;
32 if ($url =~ /search\?query=$/) {
33 # First page, collect all categories:
34 my $dom = $self->current_dom;
35 my @anchors = $dom->find('ul')->[0]->find('a')->each;
36 my @pages = uniq(map {$nlab_base . uri_unescape($_)} grep {defined} map {$_->{'href'}} @anchors);
37 return \@pages; }
38 else {return [];} # skip leaves
39 }
40  
41 # Index a concept page, ignore category pages
42 sub index_page {
43 my ($self) = @_;
44 my $url = uri_unescape($self->current_url);
45 # Nothing to do in category pages
46 return [] if ((! $self->leaf_test($url)) || ($url =~ /search\?query=$/));
47 my $dom = $self->current_dom;
48 my $h1 = $dom->find('h1')->[0];
49 my $concept = $h1 && lc($h1->text);
50 my @categories = map {$_->text} $dom->find('a.category_link')->each;
51 push @categories, 'XX-XX' unless @categories;
52  
53 return [{ url => $url,
54 concept => $concept,
55 scheme => 'nlab',
56 categories => \@categories,
57 }]; }
58  
59 sub candidate_categories {}
60 sub leaf_test { $_[1] =~ /^ncatlab\.org\/nlab\/show\/(?:.+)$/ }
61  
62 1;
63  
64 __END__