| 1 | # Copyright (c) 2024-2025 Philipp Schafft | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: module for reading and writing ValueFile files | ||
| 6 | |||
| 7 | package File::ValueFile::Simple::Writer; | ||
| 8 | |||
| 9 | use v5.10; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use parent qw(Data::Identifier::Interface::Userdata Data::Identifier::Interface::Subobjects); | ||
| 14 | |||
| 15 | use Carp; | ||
| 16 | use URI::Escape qw(uri_escape uri_escape_utf8); | ||
| 17 | use Data::Identifier v0.03; | ||
| 18 | |||
| 19 | use File::ValueFile; | ||
| 20 | |||
| 21 | use constant { | ||
| 22 | FORMAT_ISE => '54bf8af4-b1d7-44da-af48-5278d11e8f32', | ||
| 23 | SF_ISE => 'e5da6a39-46d5-48a9-b174-5c26008e208e', # tagpool-source-format | ||
| 24 | TLv1_ISE => 'afdb46f2-e13f-4419-80d7-c4b956ed85fa', # tagpool-taglist-format-v1 | ||
| 25 | F_M_L_ISE => 'f06c2226-b33e-48f2-9085-cd906a3dcee0', # tagpool-source-format-modern-limited | ||
| 26 | F_M_F_ISE => '1c71f5b1-216d-4a9b-81a1-54dc22d8a067', # tagpool-source-format-modern-full | ||
| 27 | DOT_REPEAT_ISE => '2ec67bbe-4698-4a0c-921d-1f0951923ee6', | ||
| 28 | }; | ||
| 29 | |||
| 30 | my %_default_style = ( | ||
| 31 | generator_hint => 'auto', | ||
| 32 | tag_relation => 'tag-relation', | ||
| 33 | degenerate_generator_hint => undef, | ||
| 34 | tag_ise_no_ise_retry => undef, | ||
| 35 | tag_ise_no_ise_one => 1, | ||
| 36 | tag_ise_no_ise_no_uriid => 1, | ||
| 37 | comment_column => 168, | ||
| 38 | ); | ||
| 39 | |||
| 40 | my %_generator_comments = ( | ||
| 41 | '97b7f241-e1c5-4f02-ae3c-8e31e501e1dc' => 'gregorian-date', | ||
| 42 | '7da02209-0cf5-4bd2-9ce9-50bd0b7d76e3' => 'owner', | ||
| 43 | '5714e8d8-913a-4193-9de0-eb6ba9e13e1f' => 'favourite', | ||
| 44 | '283511f7-1c0d-44d4-abd8-6ed1e1fc5d66' => 'like', | ||
| 45 | 'fd7eb1c9-2840-467c-a77b-afa6f925f0c3' => 'dislike', | ||
| 46 | 'f2bd1194-d658-4741-b1b1-b4cfd0d6ddea' => 'todo', | ||
| 47 | 'bd471aca-8b37-483c-8569-a84444b34cfc' => 'background', | ||
| 48 | 'd511f370-0e49-42d5-ad18-bf280dc97e08' => 'body', | ||
| 49 | 'bd1a1966-2e71-43cc-a7ce-f7a4547df450' => 'character', | ||
| 50 | 'f616e3e2-7779-4172-ae26-865b979b4d0c' => 'artist', | ||
| 51 | 'ee60545c-5415-474d-abe9-6de941045460' => 'fertilization', | ||
| 52 | '2b85ca08-921c-4683-a102-f18748c88fda' => 'birth', | ||
| 53 | '3e1c709e-32bf-4943-a9fa-8c25cb37dc92' => 'death', | ||
| 54 | '8b864ce6-e034-432d-8803-c95cd6da53b6' => 'tagcombiner', | ||
| 55 | ); | ||
| 56 | |||
| 57 | my %_old_style_relation = ( | ||
| 58 | '7f265548-81dc-4280-9550-1bd0aa4bf748' => 'tag-type', | ||
| 59 | 'e48cd5c6-83d7-411e-9640-cb370f3502fc' => 'tag-implies', | ||
| 60 | 'd926eb95-6984-415f-8892-233c13491931' => 'tag-links', | ||
| 61 | ); | ||
| 62 | |||
| 63 | our $VERSION = v0.10; | ||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | sub new { | ||
| 68 | my ($pkg, $out, %opts) = @_; | ||
| 69 | my $fh; | ||
| 70 | my $self = bless \%opts; | ||
| 71 | my $_is_utf8; | ||
| 72 | my %features; | ||
| 73 | |||
| 74 | if (ref $out) { | ||
| 75 | $fh = $out; | ||
| 76 | } else { | ||
| 77 | open($fh, '>', $out) or croak $!; | ||
| 78 | } | ||
| 79 | |||
| 80 | { | ||
| 81 | my $style = delete $opts{style}; | ||
| 82 | $self->{style} = {%_default_style}; | ||
| 83 | $self->style(%{$style}) if defined $style; | ||
| 84 | } | ||
| 85 | |||
| 86 | $self->{fh} = $fh; | ||
| 87 | $self->{features} = \%features; | ||
| 88 | $self->{escape} = \&uri_escape; # set here, so we can write the markers. | ||
| 89 | |||
| 90 | if (defined $opts{format}) { | ||
| 91 | my $format = $self->{format} = Data::Identifier->new(from => $opts{format}); | ||
| 92 | |||
| 93 | $self->_write_marker(required => 'ValueFile', FORMAT_ISE, $format); | ||
| 94 | $_is_utf8 ||= File::ValueFile->_is_utf8($format); | ||
| 95 | } | ||
| 96 | |||
| 97 | foreach my $type (qw(required copy optional)) { | ||
| 98 | my $list = $opts{$type.'_feature'} // next; | ||
| 99 | $list = [$list] unless ref($list) eq 'ARRAY'; | ||
| 100 | foreach my $entry (@{$list}) { | ||
| 101 | my $feature = Data::Identifier->new(from => $entry); | ||
| 102 | |||
| 103 | $self->{dot_repreat} ||= $feature->eq(DOT_REPEAT_ISE); | ||
| 104 | |||
| 105 | $self->_write_marker($type, 'Feature', $feature); | ||
| 106 | $_is_utf8 ||= File::ValueFile->_is_utf8($feature); | ||
| 107 | $features{$feature->ise} = $feature; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | $opts{utf8} //= 'auto'; | ||
| 112 | |||
| 113 | if ($opts{utf8} eq 'auto') { | ||
| 114 | $opts{utf8} = $_is_utf8; | ||
| 115 | } | ||
| 116 | |||
| 117 | $self->{escape} = $opts{utf8} ? \&uri_escape_utf8 : \&uri_escape; | ||
| 118 | |||
| 119 | return $self; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | sub format { | ||
| 124 | my ($self, %opts) = @_; | ||
| 125 | |||
| 126 | if (defined $self->{format}) { | ||
| 127 | return $self->{format}->as($opts{as}, so => $self) if defined $opts{as}; | ||
| 128 | return $self->{format}; | ||
| 129 | } | ||
| 130 | |||
| 131 | return $opts{default} if exists $opts{default}; | ||
| 132 | croak 'No value for format'; | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | sub features { | ||
| 137 | my ($self, %opts) = @_; | ||
| 138 | return values %{$self->{features}}; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | sub style { | ||
| 143 | my ($self, @args) = @_; | ||
| 144 | |||
| 145 | if (scalar(@args) == 1) { | ||
| 146 | return $self->{style}{$args[0]}; | ||
| 147 | } elsif ((scalar(@args) & 1) == 0) { | ||
| 148 | my %merge = @args; | ||
| 149 | foreach my $key (keys %merge) { | ||
| 150 | croak 'Invalid style: '.$key unless exists $_default_style{$key}; | ||
| 151 | $self->{style}{$key} = $merge{$key}; | ||
| 152 | } | ||
| 153 | return undef; | ||
| 154 | } | ||
| 155 | |||
| 156 | croak 'Invalid call (bad arguments?)'; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _escape { | ||
| 160 | my ($self, $in) = @_; | ||
| 161 | |||
| 162 | return '!null' if !defined $in; | ||
| 163 | return '!empty' if $in eq ''; | ||
| 164 | |||
| 165 | return $self->{escape}->($in); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub _write_marker { | ||
| 169 | my ($self, $type, @line) = @_; | ||
| 170 | |||
| 171 | $self->{last_line} = undef; | ||
| 172 | |||
| 173 | if ($type eq 'required') { | ||
| 174 | $self->{fh}->print('!!'); | ||
| 175 | } elsif ($type eq 'copy') { | ||
| 176 | $self->{fh}->print('!&'); | ||
| 177 | } elsif ($type eq 'optional') { | ||
| 178 | $self->{fh}->print('!?'); | ||
| 179 | } else { | ||
| 180 | croak 'Bug: Bad marker: '.$type; | ||
| 181 | } | ||
| 182 | |||
| 183 | @line = map {$self->_escape($_)} map {ref($_) ? $_->ise : $_} @line; | ||
| 184 | |||
| 185 | local $, = ' '; | ||
| 186 | $self->{fh}->say(@line); | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | sub write { | ||
| 191 | my ($self, @line) = @_; | ||
| 192 | |||
| 193 | unless (scalar @line) { | ||
| 194 | $self->{fh}->say(''); | ||
| 195 | return; | ||
| 196 | } | ||
| 197 | |||
| 198 | @line = map {ref($_) ? $_->ise : $_} @line; | ||
| 199 | |||
| 200 | if ($self->{dot_repreat}) { | ||
| 201 | my $line = [@line]; | ||
| 202 | if (defined(my $last_line = $self->{last_line})) { | ||
| 203 | my $x = 0; | ||
| 204 | foreach my $e (@line) { | ||
| 205 | if (defined $e) { | ||
| 206 | if (defined($last_line->[$x]) && $e eq $last_line->[$x]) { | ||
| 207 | $e = '.'; | ||
| 208 | } elsif ($e =~ /^\.+$/) { | ||
| 209 | $e .= '.'; | ||
| 210 | } | ||
| 211 | } elsif (!defined($last_line->[$x])) { | ||
| 212 | $e = '.'; | ||
| 213 | } | ||
| 214 | $x++; | ||
| 215 | } | ||
| 216 | } else { | ||
| 217 | foreach my $e (@line) { | ||
| 218 | if (defined $e) { | ||
| 219 | if ($e =~ /^\.+$/) { | ||
| 220 | $e .= '.'; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | $self->{last_line} = $line; | ||
| 226 | } | ||
| 227 | |||
| 228 | @line = map {$self->_escape($_)} @line; | ||
| 229 | |||
| 230 | { | ||
| 231 | my $l = length($line[0]); | ||
| 232 | $line[0] .= ' ' x (19 - $l) if $l < 19; | ||
| 233 | } | ||
| 234 | |||
| 235 | local $, = ' '; | ||
| 236 | if ($self->{no_eol}) { | ||
| 237 | if (defined(my $width = $self->{style}{comment_column})) { | ||
| 238 | my $line = join($,, @line); | ||
| 239 | my $l = length($line); | ||
| 240 | $width--; | ||
| 241 | $line .= ' ' x ($width - $l) if $width > $l; | ||
| 242 | @line = ($line); | ||
| 243 | } | ||
| 244 | $self->{fh}->print(@line); | ||
| 245 | } else { | ||
| 246 | $self->{fh}->say(@line); | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | sub write_with_comment { | ||
| 252 | my ($self, @line) = @_; | ||
| 253 | my $comment = pop(@line); | ||
| 254 | my $valid_comment = defined($comment) && length($comment); | ||
| 255 | |||
| 256 | croak 'Unsupported comment: Bad characters' if $valid_comment && $comment =~ /[\x00-\x1F]/; | ||
| 257 | |||
| 258 | if (scalar(@line)) { | ||
| 259 | local $self->{no_eol} = $valid_comment; | ||
| 260 | $self->write(@line); | ||
| 261 | $self->{fh}->print(' ') if $valid_comment; | ||
| 262 | } | ||
| 263 | |||
| 264 | if ($valid_comment) { | ||
| 265 | $self->{fh}->say('# ', $comment); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | |||
| 270 | sub write_blank { | ||
| 271 | my ($self) = @_; | ||
| 272 | return $self->write; | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | sub write_comment { | ||
| 277 | my ($self, @comment) = @_; | ||
| 278 | |||
| 279 | foreach my $comment_line (map {split /[\r\n]/} grep {defined} @comment) { | ||
| 280 | croak 'Unsupported comment: Bad characters' if $comment_line =~ /[\x00-\x1F]/; | ||
| 281 | $self->{fh}->say('# ', $comment_line); | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | sub write_hash { | ||
| 287 | my ($self, $hash) = @_; | ||
| 288 | |||
| 289 | foreach my $key (keys %{$hash}) { | ||
| 290 | my $value = $hash->{$key}; | ||
| 291 | |||
| 292 | $value = [$value] unless ref($value) eq 'ARRAY'; | ||
| 293 | |||
| 294 | foreach my $entry (@{$value}) { | ||
| 295 | $self->write($key => $entry); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | |||
| 301 | sub write_taglist { | ||
| 302 | my ($self, @list) = @_; | ||
| 303 | my $format = $self->format; | ||
| 304 | my $mode; | ||
| 305 | |||
| 306 | if ($format->eq(SF_ISE)) { # tagpool-source-format | ||
| 307 | if (defined $self->{features}{F_M_L_ISE()}) { # tagpool-source-format-modern-limited | ||
| 308 | $mode = 'tag-ise'; | ||
| 309 | } elsif (defined $self->{features}{F_M_F_ISE()}) { # tagpool-source-format-modern-full | ||
| 310 | $mode = 'full'; | ||
| 311 | } else { | ||
| 312 | $mode = 'tag'; | ||
| 313 | } | ||
| 314 | } elsif ($format->eq(TLv1_ISE)) { # tagpool-taglist-format-v1 | ||
| 315 | $mode = 'tag'; | ||
| 316 | } else { | ||
| 317 | croak 'Unsupported format'; | ||
| 318 | } | ||
| 319 | |||
| 320 | if ($mode eq 'full') { | ||
| 321 | foreach my $sublist (@list) { | ||
| 322 | $sublist = [$sublist] unless ref($sublist) eq 'ARRAY'; | ||
| 323 | foreach my $id (@{$sublist}) { | ||
| 324 | $self->write_tag_ise($id); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | } else { | ||
| 328 | foreach my $sublist (@list) { | ||
| 329 | $sublist = [$sublist] unless ref($sublist) eq 'ARRAY'; | ||
| 330 | foreach my $id (@{$sublist}) { | ||
| 331 | $self->write($mode, Data::Identifier->new(from => $id)->uuid); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | } | ||
| 335 | } | ||
| 336 | |||
| 337 | |||
| 338 | sub write_tag_ise { | ||
| 339 | my ($self, @ids) = @_; | ||
| 340 | my $displayname; | ||
| 341 | my %collected = (uuid => {}, oid => {}, uri => {}); | ||
| 342 | |||
| 343 | @ids = map {ref($_) eq 'ARRAY' ? @{$_} : $_} @ids; | ||
| 344 | |||
| 345 | foreach my $id (@ids) { | ||
| 346 | my $found_for_id; | ||
| 347 | |||
| 348 | $id = Data::Identifier->new(ise => $id) unless ref $id; | ||
| 349 | |||
| 350 | $displayname //= $id->displayname(default => undef, no_defaults => 1); | ||
| 351 | |||
| 352 | foreach my $key (qw(uuid oid uri)) { | ||
| 353 | my $func = $id->can($key); | ||
| 354 | my $value = $id->$func(default => undef, no_defaults => 1, as => $key); | ||
| 355 | if (defined $value) { | ||
| 356 | $collected{$key}{$value} = undef; | ||
| 357 | $found_for_id = 1; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | unless ($found_for_id) { | ||
| 362 | if (defined(my $retry = $self->{style}{tag_ise_no_ise_retry})) { | ||
| 363 | my @list = map {$_ eq 'all' || $_ eq 'ise' ? qw(uuid oid uri) : ($_)} ref($retry) ? @{$retry} : split(/\s*,\s*|\s+/, $retry); | ||
| 364 | |||
| 365 | foreach my $key (@list) { | ||
| 366 | my $func = $id->can($key) // croak 'Bad value for retry: '.$key; | ||
| 367 | my $value = $id->$func(default => undef, as => $key); | ||
| 368 | if (defined $value) { | ||
| 369 | next if $value =~ m#^https://uriid\.org/# && $self->{style}{tag_ise_no_ise_no_uriid}; | ||
| 370 | $collected{$key}{$value} = undef; | ||
| 371 | $found_for_id = 1; | ||
| 372 | last if $self->{style}{tag_ise_no_ise_one}; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | croak 'No ISE found (after retry) for one of the ids' unless $found_for_id; | ||
| 377 | } else { | ||
| 378 | croak 'No ISE found for one of the ids' unless $found_for_id; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | croak 'No ISEs found' unless scalar(keys(%{$collected{uuid}})) + scalar(keys(%{$collected{oid}})) + scalar(keys(%{$collected{uri}})); | ||
| 384 | |||
| 385 | local $self->{style}{comment_column} = undef; | ||
| 386 | $self->write_with_comment('tag-ise', keys(%{$collected{uuid}}), keys(%{$collected{oid}}), keys(%{$collected{uri}}), $displayname); | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | sub write_tag_relation { | ||
| 391 | my ($self, @args) = @_; | ||
| 392 | my ($tag, $relation, $related, $context, $filter); | ||
| 393 | my %opts; | ||
| 394 | my $comment; | ||
| 395 | |||
| 396 | if (scalar(@args) == 1) { | ||
| 397 | my $ref = ref($args[0]); | ||
| 398 | if ($ref eq 'HASH') { | ||
| 399 | return $self->write_tag_relation(%{$args[0]}); | ||
| 400 | } elsif ($ref eq 'ARRAY') { | ||
| 401 | return $self->write_tag_relation(@{$args[0]}); | ||
| 402 | } else { | ||
| 403 | $tag = $args[0]->tag(default => undef, no_defaults => 1); | ||
| 404 | $relation = $args[0]->relation(default => undef, no_defaults => 1); | ||
| 405 | $related = $args[0]->related(default => undef, no_defaults => 1); | ||
| 406 | $context = $args[0]->context(default => undef, no_defaults => 1); | ||
| 407 | $filter = $args[0]->filter(default => undef, no_defaults => 1); | ||
| 408 | } | ||
| 409 | } elsif (scalar(@args) == 3 || scalar(@args) == 5) { | ||
| 410 | ($tag, $relation, $related, $context, $filter) = @args; | ||
| 411 | } elsif ((scalar(@args) % 2) == 0) { | ||
| 412 | %opts = @args; | ||
| 413 | |||
| 414 | $tag = $opts{tag}; | ||
| 415 | $relation = $opts{relation}; | ||
| 416 | $related = $opts{related}; | ||
| 417 | $context = $opts{context}; | ||
| 418 | $filter = $opts{filter}; | ||
| 419 | } else { | ||
| 420 | croak 'Invalid argument configuration'; | ||
| 421 | } | ||
| 422 | |||
| 423 | croak 'No tag given' unless defined $tag; | ||
| 424 | croak 'No relation given' unless defined $relation; | ||
| 425 | croak 'No related given' unless defined $related; | ||
| 426 | |||
| 427 | # Ensure types and well formatting: | ||
| 428 | foreach my $ent ($tag, $relation, $related, $context, $filter) { | ||
| 429 | next unless defined $ent; | ||
| 430 | $ent = Data::Identifier->new(ise => $ent) unless ref $ent; | ||
| 431 | } | ||
| 432 | |||
| 433 | { | ||
| 434 | my $displayname_relation = $relation->displayname(default => undef, no_defaults => 1); | ||
| 435 | my $displayname_related = $related->displayname(default => undef, no_defaults => 1); | ||
| 436 | |||
| 437 | if (defined($displayname_relation) && defined($displayname_related)) { | ||
| 438 | $comment = sprintf('%s: %s', $displayname_relation, $displayname_related); | ||
| 439 | } elsif (defined($displayname_relation)) { | ||
| 440 | $comment = $displayname_relation; | ||
| 441 | } elsif (defined($displayname_related)) { | ||
| 442 | $comment = '?: '.$displayname_related; | ||
| 443 | } | ||
| 444 | } | ||
| 445 | |||
| 446 | if ($self->{style}{tag_relation} eq 'mixed' && !defined($context) && !defined($filter) && defined(my $old_style = $_old_style_relation{$relation->ise})) { | ||
| 447 | $self->write_with_comment($old_style, $tag, $related, $comment); | ||
| 448 | return; | ||
| 449 | } | ||
| 450 | |||
| 451 | $self->write_with_comment('tag-relation', $tag, $relation, $related, $context, $filter, $comment); | ||
| 452 | } | ||
| 453 | |||
| 454 | |||
| 455 | sub write_tag_metadata { | ||
| 456 | my ($self, @args) = @_; | ||
| 457 | my ($tag, $relation, $context, $type, $encoding, $data_raw); | ||
| 458 | my %opts; | ||
| 459 | my $comment; | ||
| 460 | |||
| 461 | if (scalar(@args) == 1) { | ||
| 462 | my $ref = ref($args[0]); | ||
| 463 | if ($ref eq 'HASH') { | ||
| 464 | return $self->write_tag_metadata(%{$args[0]}); | ||
| 465 | } elsif ($ref eq 'ARRAY') { | ||
| 466 | return $self->write_tag_metadata(@{$args[0]}); | ||
| 467 | } else { | ||
| 468 | $tag = $args[0]->tag(default => undef, no_defaults => 1); | ||
| 469 | $relation = $args[0]->relation(default => undef, no_defaults => 1); | ||
| 470 | $context = $args[0]->context(default => undef, no_defaults => 1); | ||
| 471 | $type = $args[0]->type(default => undef, no_defaults => 1); | ||
| 472 | $encoding = $args[0]->encoding(default => undef, no_defaults => 1); | ||
| 473 | $data_raw = $args[0]->data_raw(default => undef, no_defaults => 1); | ||
| 474 | } | ||
| 475 | } elsif (scalar(@args) == 3) { | ||
| 476 | ($tag, $relation, $data_raw) = @args; | ||
| 477 | } elsif (scalar(@args) == 5) { | ||
| 478 | ($tag, $relation, $type, $encoding, $data_raw) = @args; | ||
| 479 | } elsif ((scalar(@args) % 2) == 0) { | ||
| 480 | %opts = @args; | ||
| 481 | |||
| 482 | $tag = $opts{tag}; | ||
| 483 | $relation = $opts{relation}; | ||
| 484 | $context = $opts{context}; | ||
| 485 | $type = $opts{type}; | ||
| 486 | $encoding = $opts{encoding}; | ||
| 487 | $data_raw = $opts{data_raw}; | ||
| 488 | } else { | ||
| 489 | croak 'Invalid argument configuration'; | ||
| 490 | } | ||
| 491 | |||
| 492 | croak 'No tag given' unless defined $tag; | ||
| 493 | croak 'No relation given' unless defined $relation; | ||
| 494 | croak 'No data_raw given' unless defined $data_raw; | ||
| 495 | |||
| 496 | # Ensure types and well formatting: | ||
| 497 | foreach my $ent ($tag, $relation, $context, $type, $encoding) { | ||
| 498 | next unless defined $ent; | ||
| 499 | $ent = Data::Identifier->new(ise => $ent) unless ref $ent; | ||
| 500 | } | ||
| 501 | |||
| 502 | $comment = $relation->displayname(default => undef, no_defaults => 1); | ||
| 503 | if (defined($comment) && defined($type) && defined(my $type_displayname = $type->displayname(default => undef, no_defaults => 1))) { | ||
| 504 | $comment .= '('.$type_displayname.')'; | ||
| 505 | } | ||
| 506 | |||
| 507 | $self->write_with_comment('tag-metadata', $tag, $relation, $context, $type, $encoding, $data_raw, $comment); | ||
| 508 | } | ||
| 509 | |||
| 510 | |||
| 511 | sub write_tag_generator_hint { | ||
| 512 | my ($self, $tag, $generator, $hint, $comment) = @_; | ||
| 513 | |||
| 514 | $generator //= Data::Identifier->new(from => $tag)->generator(default => undef); | ||
| 515 | $hint //= Data::Identifier->new(from => $tag)->request(default => undef); | ||
| 516 | |||
| 517 | if (ref($hint) eq 'ARRAY') { | ||
| 518 | $hint = join('--', sort map {$_->uuid} @{$hint}); | ||
| 519 | } | ||
| 520 | |||
| 521 | if ((!defined($generator) || !defined($hint)) && ($self->{style}{degenerate_generator_hint} // '') eq 'auto') { | ||
| 522 | $self->write_tag_ise($tag); | ||
| 523 | return; | ||
| 524 | } | ||
| 525 | |||
| 526 | croak 'No generator given' unless defined $generator; | ||
| 527 | croak 'No hint given' unless defined $hint; | ||
| 528 | |||
| 529 | if ( | ||
| 530 | $self->{style}{generator_hint} eq 'auto' && | ||
| 531 | ( | ||
| 532 | defined($self->{features}{F_M_L_ISE()}) || # tagpool-source-format-modern-limited | ||
| 533 | defined($self->{features}{F_M_F_ISE()}) # tagpool-source-format-modern-full | ||
| 534 | ) | ||
| 535 | ) { | ||
| 536 | state $generator_request = Data::Identifier->new(uuid => 'ab573786-73bc-4f5c-9b03-24ef8a70ae45')->register; | ||
| 537 | state $generated_by = Data::Identifier->new(uuid => '8efbc13b-47e5-4d92-a960-bd9a2efa9ccb')->register; | ||
| 538 | |||
| 539 | $self->write_tag_metadata($tag, $generator_request, $hint); | ||
| 540 | $self->write_tag_relation($tag, $generated_by, Data::Identifier->new(from => $generator)); | ||
| 541 | } else { | ||
| 542 | if (defined $generator) { | ||
| 543 | $comment //= $_generator_comments{ref($generator) ? $generator->ise : $generator}; | ||
| 544 | } | ||
| 545 | |||
| 546 | if (defined $comment) { | ||
| 547 | $self->write_with_comment('tag-generator-hint', $tag, $generator, $hint, $comment); | ||
| 548 | } else { | ||
| 549 | $self->write('tag-generator-hint', $tag, $generator, $hint); | ||
| 550 | } | ||
| 551 | } | ||
| 552 | } | ||
| 553 | |||
| 554 | |||
| 555 | sub write_tagname { | ||
| 556 | my ($self, $tag, $tagname) = @_; | ||
| 557 | |||
| 558 | return unless defined($tagname) && length($tagname); | ||
| 559 | |||
| 560 | if ( | ||
| 561 | defined($self->{features}{F_M_L_ISE()}) || # tagpool-source-format-modern-limited | ||
| 562 | defined($self->{features}{F_M_F_ISE()}) # tagpool-source-format-modern-full | ||
| 563 | ) { | ||
| 564 | state $wk_asi = Data::Identifier->new(uuid => 'ddd60c5c-2934-404f-8f2d-fcb4da88b633')->register; | ||
| 565 | state $wk_tagname = Data::Identifier->new(uuid => 'bfae7574-3dae-425d-89b1-9c087c140c23')->register; | ||
| 566 | |||
| 567 | |||
| 568 | $self->write_tag_metadata($tag, $wk_asi, $wk_tagname, undef, $tagname); | ||
| 569 | } else { | ||
| 570 | $self->write('tag', $tag, $tagname); | ||
| 571 | } | ||
| 572 | } | ||
| 573 | |||
| 574 | 1; | ||
| 575 | |||
| 576 | __END__ |