~ubuntu-branches/debian/sid/libpandoc-wrapper-perl/sid

« back to all changes in this revision

Viewing changes to lib/Pandoc/Version.pm

  • Committer: Package Import Robot
  • Author(s): Jonas Smedegaard
  • Date: 2017-03-29 18:16:34 UTC
  • Revision ID: package-import@ubuntu.com-20170329181634-oyx1tvs2gyoe6210
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Pandoc::Version;
 
2
use strict;
 
3
use warnings;
 
4
use 5.010;
 
5
 
 
6
use utf8;
 
7
 
 
8
=head1 NAME
 
9
 
 
10
Pandoc::Version - version number of pandoc and its libraries
 
11
 
 
12
=cut
 
13
 
 
14
our $VERSION = '0.6.0';
 
15
 
 
16
use overload '""' => 'string', '0+' => 'number', 
 
17
    cmp => 'cmp', '<=>' => 'cmp', fallback => 1;
 
18
use Carp qw(croak);
 
19
use Scalar::Util qw(reftype blessed);
 
20
 
 
21
our @CARP_NOT = ('Pandoc');
 
22
 
 
23
sub new {
 
24
    my $class = shift;
 
25
 
 
26
    # We accept array or string input
 
27
    # (or mixed but let's not document that!)
 
28
    my @nums = 
 
29
        map {
 
30
            my $num = $_;
 
31
            $num =~ /^\d+$/ or croak 'invalid version number';
 
32
            $num =~ s/^0+(?=\d)//; # ensure decimal interpretation
 
33
            $num = 0+ $num;
 
34
            $num 
 
35
        } 
 
36
        map { s/^v//i; split /\./ } ## no critic
 
37
        map { 'ARRAY' CORE::eq (reftype $_ // "") ? @$_ : $_ }
 
38
        map { $_ // '' } @_;
 
39
 
 
40
    croak 'invalid version number' unless @nums;
 
41
 
 
42
    return bless \@nums => $class;
 
43
}
 
44
 
 
45
sub string { join '.', @{ $_[0] } }
 
46
 
 
47
sub number {
 
48
    my ($major, @minors) = @{ $_[0] };
 
49
    no warnings qw(uninitialized numeric);
 
50
    if ( @minors ) {
 
51
        my $minor = join '', map { sprintf '%03d', $_ } @minors;
 
52
        return 0+ "$major.$minor";    # return a true number
 
53
    }
 
54
    return 0+ $major;
 
55
}
 
56
 
 
57
sub cmp {
 
58
    my ($a, $b) = map {
 
59
        (blessed $_ and $_->isa('Pandoc::Version'))
 
60
            ? $_ : Pandoc::Version->new($_ // ())   
 
61
    } ($_[0], $_[1]);
 
62
    return $a->number <=> $b->number;
 
63
}
 
64
 
 
65
sub match {
 
66
    my ($a, $b) = map { Pandoc::Version->new($_) } @_;
 
67
    pop @$a while @$a > @$b;
 
68
    pop @$b while @$b > @$a;
 
69
 
 
70
    return $a->number == $b->number;
 
71
}
 
72
 
 
73
my %cmp_truth_table = (
 
74
    '==' => [0,1,0],
 
75
    '!=' => [1,0,1],
 
76
    '>=' => [0,1,1],
 
77
    '<=' => [1,1,0],
 
78
    '<'  => [1,0,0],
 
79
    '>'  => [0,0,1]
 
80
);
 
81
 
 
82
sub fulfills {
 
83
    my ($self, $req) = @_;
 
84
    return 1 unless $req;
 
85
 
 
86
    my @parts = split qr{\s*,\s*}, $req;
 
87
    for my $part (@parts) {
 
88
        my ($op, $ver) = $part =~ m{^\s*(==|>=|>|<=|<|!=)?\s*v?(\d+(\.\d+)*)$};
 
89
        croak "invalid version requirement: $req" unless defined $ver;
 
90
        
 
91
        my $cmp = $self->cmp($ver) + 1; # will be 0 for <, 1 for ==, 2 for >
 
92
        return unless $cmp_truth_table{$op || '>='}->[$cmp];
 
93
    }
 
94
 
 
95
    1;
 
96
}
 
97
 
 
98
sub TO_JSON {
 
99
    my ($self) = @_;
 
100
    return [ map { 0+ $_ } @$self ];
 
101
}
 
102
 
 
103
1;
 
104
 
 
105
__END__
 
106
 
 
107
=head1 SYNOPSIS
 
108
 
 
109
  $version = Pandoc::Version->new("1.17.2");     # create version
 
110
  $version = bless [1,17,2], 'Pandoc::Version';  # equivalent
 
111
 
 
112
  "$version";       # stringify to "1.17.2"
 
113
  $version > 1.9;   # compare
 
114
  $version->[0];    # major
 
115
  $version->[1];    # minor
 
116
 
 
117
  $version->match('1.17');   # true for 1.17, 1.17.x, 1.17.x.y...
 
118
 
 
119
=head1 DESCRIPTION
 
120
 
 
121
This module is used to store and compare version numbers of pandoc executable
 
122
and Haskell libraries compiled into pandoc. A Pandoc::Version object is an
 
123
array reference of one or more non-negative integer values.
 
124
 
 
125
In most cases there is no need to create Pandoc::Version objects by hand. Just
 
126
use the instances returned by methods C<version> and C<libs> of module
 
127
L<Pandoc> and trust in overloading.
 
128
 
 
129
=head1 METHODS
 
130
 
 
131
=head2 string
 
132
 
 
133
Return a string representation of a version, for instance C<"1.17.0.4">. This
 
134
method is automatically called by overloading in string context.
 
135
 
 
136
=head2 number
 
137
 
 
138
Return a number representation of a version, for instance C<1.017000004>. This
 
139
method is automatically called by overloading in number context.
 
140
 
 
141
=head2 cmp( $version )
 
142
 
 
143
Compare two version numbers. This is method is used automatically by
 
144
overloading to compare version objects with strings or numbers (operators
 
145
C<eq>, C<lt>, C<le>, C<ge>, C<==>, C<< < >>, C<< > >>, C<< <= >>, and C<< >=
 
146
>>).
 
147
 
 
148
=head2 match( $version )
 
149
 
 
150
Return whether a version number matches another version number if cut to the
 
151
same number of parts. For instance C<1.2.3> matches C<1>, C<1.2>, and C<1.2.3>.
 
152
 
 
153
=head2 fulfills( $version_requirement )
 
154
 
 
155
Return whether a version number fullfills a version requirement, such as
 
156
C<=1.16, !=1.17>'. See L<CPAN::Meta::Spec/Version Ranges> for possible values.
 
157
 
 
158
=head2 TO_JSON
 
159
 
 
160
Return an array reference of the version number to serialize in JSON format.
 
161
 
 
162
=head1 SEE ALSO
 
163
 
 
164
L<version> is a similar module for Perl version numbers.
 
165
 
 
166
L<SemVer> extends versions to Semantic Versioning as described at L<http://semver.org/>.
 
167
 
 
168
=cut