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