Custom.pm 8.61 KB
Newer Older
1 2
use strict;
use warnings;
3
package Software::License::Custom;
4
# ABSTRACT: custom license handler
5
$Software::License::Custom::VERSION = '0.103013';
6
use parent 'Software::License';
7

8 9 10
use Carp;
use Text::Template;

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
#pod =head1 DESCRIPTION
#pod
#pod This module extends L<Software::License> to give the possibility of specifying
#pod all aspects related to a software license in a custom file.  This allows for
#pod setting custom dates, notices, etc. while still preserving compatibility with
#pod all places where L<Software::License> is used, e.g. L<Dist::Zilla>.
#pod
#pod In this way, you should be able to customise some aspects of the licensing
#pod messages that would otherwise be difficult to tinker, e.g. adding a note
#pod in the notice, setting multiple years for the copyright notice or set multiple
#pod authors and/or copyright holders.
#pod
#pod The license details should be put inside a file that contains different
#pod sections. Each section has the following format:
#pod
#pod =begin :list
#pod
#pod = header line
#pod
#pod This is a line that begins and ends with two underscores C<__>. The string
#pod between the begin and the end of the line is first depured of any non-word
#pod character, then used as the name of the section;
#pod
#pod = body
#pod
#pod a L<Text::Template> (possibly a plain text file) where items to be
#pod expanded are enclosed between double braces
#pod
#pod =end :list
#pod
#pod Each section is terminated by the header of the following section or by
#pod the end of the file. Example:
#pod
#pod    __[ NAME ]__
#pod    The Foo-Bar License
#pod    __URL__
#pod    http://www.example.com/foo-bar.txt
#pod    __[ META_NAME ]__
#pod    foo_bar_meta
#pod    __{ META2_NAME }__
#pod    foo_bar_meta2
#pod    __[ NOTICE ]__
#pod    Copyright (C) 2000-2002 by P.R. Evious
#pod    Copyright (C) {{$self->year}} by {{$self->holder}}.
#pod
#pod    This is free software, licensed under {{$self->name}}.
#pod
#pod    __[ LICENSE ]__
#pod                The Foo-Bar License
#pod
#pod    Well... this is only some sample text.  Verily... only sample text!!!
#pod
#pod    Yes, spanning more lines and more paragraphs.
#pod
#pod The different formats for specifying the section name in the example
#pod above are only examples, you're invited to use a consistent approach.
#pod
#pod =method new
#pod
#pod    my $slc = Software::License::Custom->new({filename => 'LEGAL'});
#pod
#pod Create a new object. Arguments are passed through an anonymous hash, the
#pod following keys are allowed:
#pod
#pod   filename - the file where the custom software license details are stored
#pod
#pod =cut
78 79 80 81 82 83 84 85 86 87 88

sub new {
   my ($class, $arg) = @_;

   my $filename = delete $arg->{filename};

   my $self = $class->SUPER::new($arg);

   $self->load_sections_from($filename) if defined $filename;

   return $self;
89 90
}

91 92 93 94 95 96 97 98 99
#pod =method load_sections_from
#pod
#pod    $slc->load_sections_from('MY-LEGAL-ASPECTS');
#pod
#pod Loads the different sections of the license from the provided filename.
#pod
#pod Returns the input object.
#pod
#pod =cut
100 101 102 103 104 105 106 107

sub load_sections_from {
   my ($self, $filename) = @_;

   # Sections are kept inside a hash
   $self->{'Software::License::Custom'}{section_for} = \my %section_for;

   my $current_section = '';
108 109
   open my $fh, '<', $filename or croak "open('$filename'): $!";

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
   while (<$fh>) {
      if (my ($section) = m{\A __ (.*) __ \n\z}mxs) {
         ($current_section = $section) =~ s/\W+//gmxs;
      }
      else {
         $section_for{$current_section} .= $_;
      }
   }
   close $fh;

   # strip last newline from all items
   s{\n\z}{}mxs for values %section_for;

   return $self;
}

126 127 128 129 130 131 132 133 134
#pod =method section_data
#pod
#pod    my $notice_template_reference = $slc->section_data('NOTICE');
#pod
#pod Returns a reference to a textual template that can be fed to
#pod L<Text::Template> (it could be simple text), according to what is
#pod currently loaded in the object.
#pod
#pod =cut
135

136 137
sub section_data {
   my ($self, $name) = @_;
138 139 140 141
   my $section_for = $self->{'Software::License::Custom'}{section_for} ||= {};
   return unless exists $section_for->{$name};
   return unless defined $section_for->{$name};
   return \$section_for->{$name};
142 143
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#pod =head1 MORE METHODS
#pod
#pod The following methods, found in all software license classes, look up and
#pod render the template with the capitalized form of their name.  In other words,
#pod the C<license> method looks in the C<LICENSE> template.
#pod
#pod For now, the C<meta_name> and C<meta2_name> methods return C<custom> if called
#pod on the class.  This may become fatal in the future.
#pod
#pod =for :list
#pod * name
#pod * url
#pod * meta_name
#pod * meta2_name
#pod * license
#pod * notice
#pod * fulltext
#pod * version
#pod
#pod =cut
164

165 166 167 168 169 170 171 172 173
sub name       { shift->_fill_in('NAME') }
sub url        { shift->_fill_in('URL') }

sub meta_name  {
   my $self = shift;
   return 'custom' unless ref $self;
   return $self->_fill_in('META_NAME')
}

174 175 176 177 178 179 180 181
sub meta2_name {
  my $self = shift;
  return 'custom' unless ref $self;
  $self->_fill_in('META2_NAME')
}

sub license    { shift->_fill_in('LICENSE') }
sub notice     { shift->_fill_in('NOTICE') }
182

183 184 185 186
sub fulltext {
   my ($self) = @_;
   return join "\n", $self->notice, $self->license;
}
187

188 189 190 191 192 193 194 195 196
sub version {
   my ($self) = @_;
   return unless $self->section_data('VERSION');
   return $self->_fill_in('VERSION')
}

1;

__END__
197

198 199
=pod

200 201
=encoding UTF-8

202 203 204 205 206 207
=head1 NAME

Software::License::Custom - custom license handler

=head1 VERSION

208
version 0.103013
209 210 211

=head1 DESCRIPTION

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
This module extends L<Software::License> to give the possibility of specifying
all aspects related to a software license in a custom file.  This allows for
setting custom dates, notices, etc. while still preserving compatibility with
all places where L<Software::License> is used, e.g. L<Dist::Zilla>.

In this way, you should be able to customise some aspects of the licensing
messages that would otherwise be difficult to tinker, e.g. adding a note
in the notice, setting multiple years for the copyright notice or set multiple
authors and/or copyright holders.

The license details should be put inside a file that contains different
sections. Each section has the following format:

=over 4

=item header line

This is a line that begins and ends with two underscores C<__>. The string
between the begin and the end of the line is first depured of any non-word
character, then used as the name of the section;

=item body

a L<Text::Template> (possibly a plain text file) where items to be
expanded are enclosed between double braces

=back

Each section is terminated by the header of the following section or by
the end of the file. Example:

   __[ NAME ]__
   The Foo-Bar License
   __URL__
   http://www.example.com/foo-bar.txt
   __[ META_NAME ]__
   foo_bar_meta
   __{ META2_NAME }__
   foo_bar_meta2
   __[ NOTICE ]__
   Copyright (C) 2000-2002 by P.R. Evious
   Copyright (C) {{$self->year}} by {{$self->holder}}.

   This is free software, licensed under {{$self->name}}.

   __[ LICENSE ]__
               The Foo-Bar License

   Well... this is only some sample text.  Verily... only sample text!!!

   Yes, spanning more lines and more paragraphs.

The different formats for specifying the section name in the example
above are only examples, you're invited to use a consistent approach.

=head1 METHODS

=head2 new

   my $slc = Software::License::Custom->new({filename => 'LEGAL'});

Create a new object. Arguments are passed through an anonymous hash, the
following keys are allowed:

  filename - the file where the custom software license details are stored

=head2 load_sections_from

   $slc->load_sections_from('MY-LEGAL-ASPECTS');

Loads the different sections of the license from the provided filename.

Returns the input object.

=head2 section_data

   my $notice_template_reference = $slc->section_data('NOTICE');

Returns a reference to a textual template that can be fed to
L<Text::Template> (it could be simple text), according to what is
currently loaded in the object.

=head1 MORE METHODS

The following methods, found in all software license classes, look up and
render the template with the capitalized form of their name.  In other words,
the C<license> method looks in the C<LICENSE> template.

For now, the C<meta_name> and C<meta2_name> methods return C<custom> if called
on the class.  This may become fatal in the future.

=over 4

=item *

name

=item *

url

=item *

meta_name

=item *

meta2_name

=item *

license

=item *

notice

=item *

fulltext

=item *

version

=back
338 339 340 341 342 343 344

=head1 AUTHOR

Ricardo Signes <rjbs@cpan.org>

=head1 COPYRIGHT AND LICENSE

345
This software is copyright (c) 2017 by Ricardo Signes.
346 347 348 349 350

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut