Bump version and update Changes
[zeal.git] / lib / Zeal / Docset.pm
CommitLineData
c1c9c817
MG
1package Zeal::Docset;
2
3use 5.014000;
4use strict;
5use warnings;
6
164f653c 7our $VERSION = '0.000_002';
c1c9c817
MG
8
9use parent qw/Class::Accessor::Fast/;
10__PACKAGE__->mk_ro_accessors(qw/path plist dbh name id family/);
11
12use Carp qw/carp/;
13use Cwd qw/realpath/;
14use File::Spec::Functions qw/catfile catdir rel2abs/;
15use HTTP::Tiny;
16
17use DBI;
18use File::Slurp qw/read_file/;
19use Mac::PropertyList::SAX qw/parse_plist_file/;
20
21use Zeal::Document;
22
23sub new {
24 my ($class, $path) = @_;
25 $path = realpath $path;
26 my $plpath = catfile $path, 'Contents', 'Info.plist';
27 my $dbpath = catfile $path, 'Contents', 'Resources', 'docSet.dsidx';
28 my $plist = parse_plist_file($plpath)->as_perl;
29 carp 'This is not a Dash docset' unless $plist->{isDashDocset};
30
31 bless {
32 path => $path,
33 plist => $plist,
34 dbh => DBI->connect("dbi:SQLite:dbname=$dbpath", '', ''),
35 name => $plist->{CFBundleName},
36 id => $plist->{CFBundleIdentifier},
37 family => $plist->{DocSetPlatformFamily},
38 }, $class
39}
40
41sub _blessdocs {
42 my ($self, $docsref) = @_;
43 map { Zeal::Document->new(+{%$_, docset => $self}) } @$docsref;
44}
45
46sub fetch {
47 my ($self, $path) = @_;
9f3addb5 48 return HTTP::Tiny->new->get($path)->{content} if $path =~ /^http:/s;
c1c9c817
MG
49 my $docroot = catdir $self->path, 'Contents', 'Resources', 'Documents';
50 $path = rel2abs $path, $docroot;
51 scalar read_file $path
52}
53
54sub query {
55 my ($self, $cond) = @_;
56 my $query = 'SELECT * FROM searchIndex WHERE name LIKE ?';
57 my $res = $self->dbh->selectall_arrayref($query, {Slice => {}}, $cond);
58 my @results = $self->_blessdocs($res);
59 wantarray ? @results : $results[0]
60}
61
62sub get {
63 my ($self, $cond) = @_;
64 $self->query($cond)->fetch
65}
66
67sub list {
68 my ($self) = @_;
69 my $query = 'SELECT * FROM searchIndex';
70 my $res = $self->dbh->selectall_arrayref($query, {Slice => {}});
71 $self->_blessdocs($res)
72}
73
741;
75__END__
76
77=encoding utf-8
78
79=head1 NAME
80
81Zeal::Docset - Class representing a Dash/Zeal docset
82
83=head1 SYNOPSIS
84
85 use Zeal::Docset;
0469af4b
MG
86 my $ds = Zeal::Docset->new('/home/mgv/docsets/Perl.docset');
87 say $ds->$path; # /home/mgv/docsets/Perl.docset
88 say $ds->name; # Perl
89 say $ds->id; # perl
90 say $ds->family; # perl
91
92 # In SQL LIKE, % is .* and _ is .
93 my @matches = $ds->query('perlopen%'); # finds perlopenbsd and perlopentut
94 my $doc = $ds->query('perlsec'); # A Zeal::Document object for perlsec
95 my $html = $ds->get('perls_c'); # HTML documentation of perlsec
96 my @docs = $ds->list; # all documents
c1c9c817
MG
97
98=head1 DESCRIPTION
99
100Dash is an offline API documentation browser. Zeal::Docset is a class
101representing a Dash/Zeal docset.
102
103Available methods:
104
105=over
106
107=item Zeal::Docset->B<new>(I<$path>)
108
109Create a Zeal::Docset object from a given docset. I<$path> should be
110the path to a F<something.docset> directory.
111
112=item $ds->B<path>
113
114The path to the docset folder.
115
116=item $ds->B<plist>
117
118A hashref with the contents of Info.plist.
119
120=item $ds->B<dbh>
121
122A DBI database handle to the docSet.dsidx index.
123
124=item $ds->B<name>
125
126The name of this docset. Equivalent to
127C<< $ds->plist->{CFBundleName} >>
128
129=item $ds->B<id>
130
131The identifier of this docset. Equivalent to
132C<< $ds->plist->{CFBundleIdentifier} >>
133
134=item $ds->B<family>
135
136The family this docset belongs to. Dash uses this as the keyword for
137restricting searches to a particular family of docsets. Equivalent to
138C<< $ds->plist->{DocSetPlatformFamily} >>
139
140=item $ds->B<fetch>(I<$path>)
141
142Internal method for fetching the HTML content of a document. I<$path>
143is either the path to the document relative to C<< $ds->B<path> >> or
144a HTTP URL.
145
146=item $ds->B<query>(I<$cond>)
147
148In list context, return all documents (L<Zeal::Document> instances)
149matching I<$cond>. In scalar context, return one such document.
150I<$cond> is a SQL LIKE condition.
151
152=item $ds->B<get>(I<$cond>)
153
154The HTML content of one document that matches I<$cond>.
155I<$cond> is a SQL LIKE condition.
156
157This method is shorthand for C<< $ds->query(I<$cond>)->fetch >>.
158
159=item $ds->B<list>
160
161The list of all documents (L<Zeal::Document> instances) in this
162docset.
163
164=back
165
166=head1 SEE ALSO
167
168L<Zeal>, L<http://kapeli.com/dash>, L<http://zealdocs.org>
169
170=head1 AUTHOR
171
172Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
173
174=head1 COPYRIGHT AND LICENSE
175
176Copyright (C) 2014 by Marius Gavrilescu
177
178This library is free software; you can redistribute it and/or modify
179it under the same terms as Perl itself, either Perl version 5.20.1 or,
180at your option, any later version of Perl 5 you may have available.
181
182
183=cut
This page took 0.021256 seconds and 4 git commands to generate.