| 1 | # /=====================================================================\ # | ||
| 2 | # | NNexus Autolinker | # | ||
| 3 | # | Indexing Plug-in, encyclopediaofmath.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::Encyclopediaofmath; | ||
| 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 | # 1. We want to start from the All Pages entry point, containing the list of all categories | ||
| 25 | sub domain_root { "http://www.encyclopediaofmath.org/index.php/Special:AllPages"; } | ||
| 26 | our $eom_base = 'http://www.encyclopediaofmath.org'; | ||
| 27 | # 2. Candidate links to subcategories and concept pages | ||
| 28 | our $category_test = qr'&from='; | ||
| 29 | |||
| 30 | sub candidate_links { | ||
| 31 | my ($self)=@_; | ||
| 32 | my $url = $self->current_url; | ||
| 33 | # Add links from subcategory pages | ||
| 34 | if ($url =~ /$category_test/ ) { | ||
| 35 | # For now, we wont traverse into the specific concept pages, as there is nothing to be gained by doing so | ||
| 36 | # If they had e.g. metadata of some kind, traversing inwards would be useful. | ||
| 37 | # Right now the full information we need is the href and title attributes of the anchors | ||
| 38 | |||
| 39 | my $dom = $self->current_dom; | ||
| 40 | #my @anchors = $dom->find('table[class="mw-allpages-table-chunk"]')->[0]->find('a')->each; | ||
| 41 | #my @concept_pages = uniq(map {$nlab_base . $_} grep {defined} map {$_->{'href'}} @anchors); | ||
| 42 | |||
| 43 | # HOWEVER, some of the category pages actually have subcategories instead of leaf nodes! | ||
| 44 | my $category_table = $dom->find('table[class="allpageslist"]')->[0]; | ||
| 45 | my @category_pages=(); | ||
| 46 | if ($category_table) { | ||
| 47 | my @anchors = $category_table->find('a')->each; | ||
| 48 | @category_pages = uniq(map {$eom_base . uri_unescape($_)} grep {defined} map {$_->{'href'}} @anchors); } | ||
| 49 | return \@category_pages; } | ||
| 50 | elsif ($url =~ /Special\:AllPages$/) { | ||
| 51 | # First page, collect all categories: | ||
| 52 | my $dom = $self->current_dom; | ||
| 53 | my @anchors = $dom->find('table[class="allpageslist"]')->[0]->find('a')->each; | ||
| 54 | my @category_pages = uniq(map {$eom_base . uri_unescape($_)} grep {defined} map {$_->{'href'}} @anchors); | ||
| 55 | return \@category_pages; } | ||
| 56 | else {return [];} # skip leaves | ||
| 57 | } | ||
| 58 | |||
| 59 | # Index a concept page, ignore category pages | ||
| 60 | sub index_page { | ||
| 61 | my ($self) = @_; | ||
| 62 | my $url = $self->current_url; | ||
| 63 | # Nothing to do in the main page | ||
| 64 | return [] if ($url =~ /Special\:AllPages$/); | ||
| 65 | # Otherwise, all the data is inside the href and title attributes of the anchors | ||
| 66 | my $dom = $self->current_dom; | ||
| 67 | my $concept_table = $dom->find('table[class="mw-allpages-table-chunk"]')->[0]; | ||
| 68 | my @concepts = (); | ||
| 69 | if ($concept_table) { | ||
| 70 | my @anchors = $concept_table->find('a')->each; | ||
| 71 | @concepts = map { | ||
| 72 | { | ||
| 73 | url => $eom_base . uri_unescape($_->{'href'}), | ||
| 74 | concept => $_->{'title'}, | ||
| 75 | categories => ['XX-XX'] | ||
| 76 | }} | ||
| 77 | grep {defined $_->{'href'}} @anchors; | ||
| 78 | } | ||
| 79 | return \@concepts; } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | __END__ |