| 1 | package Zeal; |
| 2 | |
| 3 | use 5.014000; |
| 4 | use strict; |
| 5 | use warnings; |
| 6 | use re '/s'; |
| 7 | |
| 8 | our $VERSION = '0.000_002'; |
| 9 | |
| 10 | use File::Spec::Functions qw/catfile/; |
| 11 | |
| 12 | use Zeal::Docset; |
| 13 | use Zeal::Document; |
| 14 | |
| 15 | sub new { |
| 16 | my ($class, $path) = @_; |
| 17 | $path //= $ENV{ZEAL_PATH}; |
| 18 | my $self = bless {sets => {}}, $class; |
| 19 | if ($path) { |
| 20 | $self->add($_) for split /:/, $path; |
| 21 | } |
| 22 | $self |
| 23 | } |
| 24 | |
| 25 | sub add { |
| 26 | my ($self, $path) = @_; |
| 27 | return unless -d $path; |
| 28 | if ($path =~ /[.]docset$/) { |
| 29 | my $ds = Zeal::Docset->new($path); |
| 30 | $self->{sets}{$ds->family} //= []; |
| 31 | push @{$self->{sets}{$ds->family}}, $ds; |
| 32 | } else { |
| 33 | my $dir; |
| 34 | opendir $dir, $path; |
| 35 | my @entries = grep { !/^[.]{1,2}$/ } readdir $dir; |
| 36 | closedir $dir; |
| 37 | $self->add(catfile $path, $_) for @entries |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | sub sets { |
| 42 | my ($self, $family) = @_; |
| 43 | return map { @$_ } values %{$self->{sets}} unless $family; |
| 44 | @{$self->{sets}{$family}} |
| 45 | } |
| 46 | |
| 47 | sub query { |
| 48 | my ($self, $query, $family) = @_; |
| 49 | ($family, $query) = split /:/, $query, 2 if !$family && $query =~ /:/; |
| 50 | my @res = map { $_->query($query) } $self->sets($family); |
| 51 | wantarray ? @res : $res[0] |
| 52 | } |
| 53 | |
| 54 | 1; |
| 55 | __END__ |
| 56 | |
| 57 | =encoding utf-8 |
| 58 | |
| 59 | =head1 NAME |
| 60 | |
| 61 | Zeal - Read and query Dash/Zeal docsets |
| 62 | |
| 63 | =head1 SYNOPSIS |
| 64 | |
| 65 | use Zeal; |
| 66 | my $zeal = Zeal->new("/home/mgv/docsets/:/home/mgv/something.docset"); |
| 67 | $zeal->add('/home/mgv/somethingelse.docset'); # Add another docset |
| 68 | $zeal->add('/home/mgv/moredocsets/'); # Add a directory containing docsets |
| 69 | |
| 70 | my $doc = $zeal->query('length'); # Documentation for 'length' in all docsets |
| 71 | my @docs = $zeal->query('Test::%', 'perl'); # Documentation for all Test:: perl modules |
| 72 | @docs = $zeal->query('perl:Test::%); # Alternative syntax |
| 73 | |
| 74 | =head1 DESCRIPTION |
| 75 | |
| 76 | Dash is an offline API documentation browser. Zeal.pm is a module for |
| 77 | reading and querying Dash documentation sets. |
| 78 | |
| 79 | This module queries multiple docsets. If you only have one docset, you |
| 80 | should use the L<Zeal::Docset> module directly. |
| 81 | |
| 82 | Available methods: |
| 83 | |
| 84 | =over |
| 85 | |
| 86 | =item Zeal->B<new>([I<$path>]) |
| 87 | |
| 88 | Create a new Zeal object. I<$path> is an optional colon delimited |
| 89 | string for initializing the object. Each of its components is |
| 90 | recursively scanned for docsets (and can also be a docset itself). If |
| 91 | I<$path> is not provided, the value of I<$ENV{ZEAL_PATH}> (if defined) |
| 92 | is used instead. |
| 93 | |
| 94 | =item $zeal->B<add>(I<$path>) |
| 95 | |
| 96 | Recursively scan a path for docsets, adding them to this object. |
| 97 | |
| 98 | =item $zeal->B<sets>([I<$family>]) |
| 99 | |
| 100 | Return a list of docsets (L<Zeal::Docset> objects) in the given |
| 101 | family, or in all families if I<$family> is not provided. |
| 102 | |
| 103 | =item $zeal->B<query>(I<"$family:$query">) |
| 104 | |
| 105 | =item $zeal->B<query>(I<$query>, [I<$family>]) |
| 106 | |
| 107 | Return a list of documents (L<Zeal::Document> objects) matching a |
| 108 | query, optionally restricted to a family. In scalar context only one |
| 109 | such document is returned. I<$query> is a SQL LIKE condition. |
| 110 | |
| 111 | =back |
| 112 | |
| 113 | =head1 SEE ALSO |
| 114 | |
| 115 | L<http://kapeli.com/dash>, L<http://zealdocs.org> |
| 116 | |
| 117 | =head1 AUTHOR |
| 118 | |
| 119 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> |
| 120 | |
| 121 | =head1 COPYRIGHT AND LICENSE |
| 122 | |
| 123 | Copyright (C) 2014 by Marius Gavrilescu |
| 124 | |
| 125 | This library is free software; you can redistribute it and/or modify |
| 126 | it under the same terms as Perl itself, either Perl version 5.20.1 or, |
| 127 | at your option, any later version of Perl 5 you may have available. |
| 128 | |
| 129 | |
| 130 | =cut |