8fab14a733d2c7c25f16e8692196cdc64ed5d9fa
[zeal.git] / lib / Zeal.pm
1 package Zeal;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6 use re '/s';
7
8 our $VERSION = '0.000_001';
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 # Add another docset
68 $zeal->add('/home/mgv/somethingelse.docset');
69 # Add a directory containing docsets
70 $zeal->add('/home/mgv/moredocsets/');
71 # Documentation for 'length' in all docsets
72 my $doc = $zeal->query('length');
73 # Documentation for all Test:: perl modules
74 my @docs = $zeal->query('Test::%', 'perl');
75 # Alternative syntax
76 @docs = $zeal->query('perl:Test::%);
77
78 =head1 DESCRIPTION
79
80 Dash is an offline API documentation browser. Zeal.pm is a module for
81 reading and querying Dash documentation sets.
82
83 This module queries multiple docsets. If you only have one docset, you
84 should use the L<Zeal::Docset> module directly.
85
86 Available methods:
87
88 =over
89
90 =item Zeal->B<new>([I<$path>])
91
92 Create a new Zeal object. I<$path> is an optional colon delimited
93 string for initializing the object. Each of its components is
94 recursively scanned for docsets (and can also be a docset itself). If
95 I<$path> is not provided, the value of I<$ENV{ZEAL_PATH}> (if defined)
96 is used instead.
97
98 =item $zeal->B<add>(I<$path>)
99
100 Recursively scan a path for docsets, adding them to this object.
101
102 =item $zeal->B<sets>([I<$family>])
103
104 Return a list of docsets (L<Zeal::Docset> objects) in the given
105 family, or in all families if I<$family> is not provided.
106
107 =item $zeal->B<query>(I<"$family:$query">)
108
109 =item $zeal->B<query>(I<$query>, [I<$family>])
110
111 Return a list of documents (L<Zeal::Document> objects) matching a
112 query, optionally restricted to a family. In scalar context only one
113 such document is returned. I<$query> is a SQL LIKE condition.
114
115 =back
116
117 =head1 SEE ALSO
118
119 L<http://kapeli.com/dash>, L<http://zealdocs.org>
120
121 =head1 AUTHOR
122
123 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
124
125 =head1 COPYRIGHT AND LICENSE
126
127 Copyright (C) 2014 by Marius Gavrilescu
128
129 This library is free software; you can redistribute it and/or modify
130 it under the same terms as Perl itself, either Perl version 5.20.1 or,
131 at your option, any later version of Perl 5 you may have available.
132
133
134 =cut
This page took 0.02878 seconds and 3 git commands to generate.