summaryrefslogtreecommitdiff
path: root/NotesIndex.pm
blob: f547435ce225ad5f02f3a4be5ba892aaaf00f842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl

#
# NotesIndex.pm
# $Id: NotesIndex.pm,v 1.3 2003/05/23 16:26:19 johnh Exp $
#
# Copyright (C) 1996,2012  Free Software Foundation, Inc.
# Comments to <johnh@isi.edu>.
#
# This file is under the Gnu Public License, version 2.
# For details see the COPYING which accompanies this distribution.
#

#
# A Perl module implement a notes-index class. 
#

package NotesIndex;
use Carp;
use strict;

require 5.000;
$Notes::revsion = '$Id: NotesIndex.pm,v 1.3 2003/05/23 16:26:19 johnh Exp $';
$Notes::VERSION = 1.00;

=head1 NAME

NotesIndex - a simple class for notes-index files

=head1 Public Methods

new, read_from_file, subjects, by_subject, prelude

=cut
#' # font-lock hack

#----------------------------------------------------------------------


# public method
sub new {
    my ($class, $file) = @_;
    my $self = bless {};
    if (!defined($file)) {
        $self->init();
    } else {
	$self->read_from_file($file);
    };
    return $self;
}


# public method
sub read_from_file {
    my ($self, $filename) = @_;

    $self->init();

    open(FILE, "<$filename") || croak "Cannot open $filename";

    while (<FILE>) {
	chomp;
	$self->push_link($_);
    };	

    close FILE;
    return 1;
}

# public_method
sub subjects {
    my($self) = @_;
    return keys %{$self->{'links_by_subject'}};
}

# public method
sub by_subject {
    my($self, $subject) = @_;
    $subject = lc($subject);
    my($resref) = $self->{'links_by_subject'}{$subject};
    return wantarray ? () : undef
	if (!defined($resref));
    return wantarray ? @$resref : 1;
}


# private method
sub push_link {
    my($self, $link) = @_;
    my($subject) = ($link =~ m/\d{2}\#\* (.*)$/);
    $subject = lc($subject);
    push (@{ $self->{'links_by_subject'}{$subject} }, $link);
}


# private method
sub init {
    my($self) = @_;
}