| 1 | package Net::Nostr::Wiki; | ||
| 2 | |||
| 3 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use Net::Nostr::_ConstructorArgs (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use feature 'unicode_strings'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Net::Nostr::Event; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | use constant WIKI_RELAY_LIST_KIND => 10102; | ||
| 12 | |||
| 13 | use Class::Tiny qw( | ||
| 14 | identifier | ||
| 15 | title | ||
| 16 | summary | ||
| 17 | fork_a | ||
| 18 | fork_e | ||
| 19 | defer_a | ||
| 20 | defer_e | ||
| 21 | target | ||
| 22 | target_relay | ||
| 23 | destination | ||
| 24 | source | ||
| 25 | source_relay | ||
| 26 | base_version | ||
| 27 | base_relay | ||
| 28 | ); | ||
| 29 | |||
| 30 | my %KINDS = ( | ||
| 31 | 30818 => 'article', | ||
| 32 | 818 => 'merge_request', | ||
| 33 | 30819 => 'redirect', | ||
| 34 | ); | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my $class = shift; | ||
| 38 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 39 | my $self = bless { %args }, $class; | ||
| 40 | my %known; @known{Class::Tiny->get_all_attributes_for($class)} = (); | ||
| 41 | my @unknown = grep { !exists $known{$_} } keys %$self; | ||
| 42 | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub normalize_dtag { | ||
| 47 | my ($class, $str) = @_; | ||
| 48 | |||
| 49 | $str = lc($str); | ||
| 50 | |||
| 51 | # Whitespace to dash | ||
| 52 | $str =~ s/\s+/-/g; | ||
| 53 | |||
| 54 | # Remove punctuation and symbols (keep letters, numbers, dashes) | ||
| 55 | $str =~ s/[^\p{Letter}\p{Number}\-]//g; | ||
| 56 | |||
| 57 | # Collapse consecutive dashes | ||
| 58 | $str =~ s/-{2,}/-/g; | ||
| 59 | |||
| 60 | # Remove leading/trailing dashes | ||
| 61 | $str =~ s/^-//; | ||
| 62 | $str =~ s/-$//; | ||
| 63 | |||
| 64 | return $str; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub article { | ||
| 68 | my $class = shift; | ||
| 69 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 70 | |||
| 71 | my $pubkey = delete $args{pubkey} | ||
| 72 | // croak "article requires 'pubkey'"; | ||
| 73 | my $identifier = delete $args{identifier} | ||
| 74 | // croak "article requires 'identifier'"; | ||
| 75 | my $content = delete $args{content} | ||
| 76 | // croak "article requires 'content'"; | ||
| 77 | my $title = delete $args{title}; | ||
| 78 | my $summary = delete $args{summary}; | ||
| 79 | my $fork_a = delete $args{fork_a}; | ||
| 80 | my $fork_e = delete $args{fork_e}; | ||
| 81 | my $defer_a = delete $args{defer_a}; | ||
| 82 | my $defer_e = delete $args{defer_e}; | ||
| 83 | |||
| 84 | $identifier = $class->normalize_dtag($identifier); | ||
| 85 | |||
| 86 | my @tags; | ||
| 87 | push @tags, ['d', $identifier]; | ||
| 88 | push @tags, ['title', $title] if defined $title; | ||
| 89 | push @tags, ['summary', $summary] if defined $summary; | ||
| 90 | |||
| 91 | if ($fork_a) { | ||
| 92 | push @tags, ['a', @$fork_a, 'fork']; | ||
| 93 | } | ||
| 94 | if ($fork_e) { | ||
| 95 | push @tags, ['e', @$fork_e, 'fork']; | ||
| 96 | } | ||
| 97 | if ($defer_a) { | ||
| 98 | push @tags, ['a', @$defer_a, 'defer']; | ||
| 99 | } | ||
| 100 | if ($defer_e) { | ||
| 101 | push @tags, ['e', @$defer_e, 'defer']; | ||
| 102 | } | ||
| 103 | |||
| 104 | return Net::Nostr::Event->new( | ||
| 105 | %args, | ||
| 106 | pubkey => $pubkey, | ||
| 107 | kind => 30818, | ||
| 108 | content => $content, | ||
| 109 | tags => \@tags, | ||
| 110 | ); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub merge_request { | ||
| 114 | my $class = shift; | ||
| 115 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 116 | |||
| 117 | my $pubkey = delete $args{pubkey} | ||
| 118 | // croak "merge_request requires 'pubkey'"; | ||
| 119 | my $target = delete $args{target} | ||
| 120 | // croak "merge_request requires 'target'"; | ||
| 121 | my $source = delete $args{source} | ||
| 122 | // croak "merge_request requires 'source'"; | ||
| 123 | my $destination = delete $args{destination} | ||
| 124 | // croak "merge_request requires 'destination'"; | ||
| 125 | |||
| 126 | my $target_relay = delete $args{target_relay}; | ||
| 127 | my $base_version = delete $args{base_version}; | ||
| 128 | my $base_relay = delete $args{base_relay}; | ||
| 129 | my $source_relay = delete $args{source_relay}; | ||
| 130 | my $content = delete $args{content} // ''; | ||
| 131 | |||
| 132 | my @tags; | ||
| 133 | |||
| 134 | # a tag: target article | ||
| 135 | my @a_tag = ('a', $target); | ||
| 136 | push @a_tag, $target_relay if defined $target_relay; | ||
| 137 | push @tags, \@a_tag; | ||
| 138 | |||
| 139 | # e tag: base version (optional, no marker) | ||
| 140 | if (defined $base_version) { | ||
| 141 | my @e_tag = ('e', $base_version); | ||
| 142 | push @e_tag, $base_relay if defined $base_relay; | ||
| 143 | push @tags, \@e_tag; | ||
| 144 | } | ||
| 145 | |||
| 146 | # p tag: destination pubkey | ||
| 147 | push @tags, ['p', $destination]; | ||
| 148 | |||
| 149 | # e tag: source with "source" marker | ||
| 150 | my @source_tag = ('e', $source); | ||
| 151 | push @source_tag, ($source_relay // ''); | ||
| 152 | push @source_tag, 'source'; | ||
| 153 | push @tags, \@source_tag; | ||
| 154 | |||
| 155 | return Net::Nostr::Event->new( | ||
| 156 | %args, | ||
| 157 | pubkey => $pubkey, | ||
| 158 | kind => 818, | ||
| 159 | content => $content, | ||
| 160 | tags => \@tags, | ||
| 161 | ); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub redirect { | ||
| 165 | my $class = shift; | ||
| 166 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 167 | |||
| 168 | my $pubkey = delete $args{pubkey} | ||
| 169 | // croak "redirect requires 'pubkey'"; | ||
| 170 | my $identifier = delete $args{identifier} | ||
| 171 | // croak "redirect requires 'identifier'"; | ||
| 172 | my $target = delete $args{target} | ||
| 173 | // croak "redirect requires 'target'"; | ||
| 174 | my $target_relay = delete $args{target_relay}; | ||
| 175 | |||
| 176 | $identifier = $class->normalize_dtag($identifier); | ||
| 177 | |||
| 178 | my @tags; | ||
| 179 | push @tags, ['d', $identifier]; | ||
| 180 | |||
| 181 | my @a_tag = ('a', $target); | ||
| 182 | push @a_tag, $target_relay if defined $target_relay; | ||
| 183 | push @tags, \@a_tag; | ||
| 184 | |||
| 185 | return Net::Nostr::Event->new( | ||
| 186 | %args, | ||
| 187 | pubkey => $pubkey, | ||
| 188 | kind => 30819, | ||
| 189 | content => '', | ||
| 190 | tags => \@tags, | ||
| 191 | ); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub from_event { | ||
| 195 | my ($class, $event) = @_; | ||
| 196 | my $kind = $event->kind; | ||
| 197 | |||
| 198 | return undef unless exists $KINDS{$kind}; | ||
| 199 | |||
| 200 | my %attrs; | ||
| 201 | |||
| 202 | if ($kind == 30818) { | ||
| 203 | for my $tag (@{$event->tags}) { | ||
| 204 | my $t = $tag->[0]; | ||
| 205 | if ($t eq 'd') { $attrs{identifier} = $tag->[1] } | ||
| 206 | elsif ($t eq 'title') { $attrs{title} = $tag->[1] } | ||
| 207 | elsif ($t eq 'summary') { $attrs{summary} = $tag->[1] } | ||
| 208 | elsif ($t eq 'a') { | ||
| 209 | my $marker = $tag->[-1]; | ||
| 210 | if ($marker eq 'fork') { | ||
| 211 | $attrs{fork_a} = [@{$tag}[1 .. $#$tag - 1]]; | ||
| 212 | } elsif ($marker eq 'defer') { | ||
| 213 | $attrs{defer_a} = [@{$tag}[1 .. $#$tag - 1]]; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | elsif ($t eq 'e') { | ||
| 217 | my $marker = $tag->[-1]; | ||
| 218 | if ($marker eq 'fork') { | ||
| 219 | $attrs{fork_e} = [@{$tag}[1 .. $#$tag - 1]]; | ||
| 220 | } elsif ($marker eq 'defer') { | ||
| 221 | $attrs{defer_e} = [@{$tag}[1 .. $#$tag - 1]]; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | elsif ($kind == 818) { | ||
| 227 | for my $tag (@{$event->tags}) { | ||
| 228 | my $t = $tag->[0]; | ||
| 229 | if ($t eq 'a') { | ||
| 230 | $attrs{target} = $tag->[1]; | ||
| 231 | $attrs{target_relay} = $tag->[2] if defined $tag->[2]; | ||
| 232 | } | ||
| 233 | elsif ($t eq 'e') { | ||
| 234 | if (defined $tag->[3] && $tag->[3] eq 'source') { | ||
| 235 | $attrs{source} = $tag->[1]; | ||
| 236 | $attrs{source_relay} = $tag->[2] | ||
| 237 | if defined $tag->[2] && $tag->[2] ne ''; | ||
| 238 | } else { | ||
| 239 | $attrs{base_version} = $tag->[1]; | ||
| 240 | $attrs{base_relay} = $tag->[2] | ||
| 241 | if defined $tag->[2] && $tag->[2] ne ''; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | elsif ($t eq 'p') { | ||
| 245 | $attrs{destination} = $tag->[1]; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | } | ||
| 249 | elsif ($kind == 30819) { | ||
| 250 | for my $tag (@{$event->tags}) { | ||
| 251 | my $t = $tag->[0]; | ||
| 252 | if ($t eq 'd') { $attrs{identifier} = $tag->[1] } | ||
| 253 | elsif ($t eq 'a') { | ||
| 254 | $attrs{target} = $tag->[1]; | ||
| 255 | $attrs{target_relay} = $tag->[2] if defined $tag->[2]; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | return $class->new(%attrs); | ||
| 261 | } | ||
| 262 | |||
| 263 | sub validate { | ||
| 264 | my ($class, $event) = @_; | ||
| 265 | my $kind = $event->kind; | ||
| 266 | |||
| 267 | croak "wiki event MUST be kind 30818, 818, or 30819" | ||
| 268 | unless exists $KINDS{$kind}; | ||
| 269 | |||
| 270 | my (%has, $has_source_e); | ||
| 271 | for my $tag (@{$event->tags}) { | ||
| 272 | $has{$tag->[0]} = 1; | ||
| 273 | $has_source_e = 1 | ||
| 274 | if $tag->[0] eq 'e' && defined $tag->[3] && $tag->[3] eq 'source'; | ||
| 275 | } | ||
| 276 | |||
| 277 | if ($kind == 30818) { | ||
| 278 | croak "wiki article MUST have a 'd' tag" unless $has{d}; | ||
| 279 | } | ||
| 280 | |||
| 281 | if ($kind == 818) { | ||
| 282 | croak "merge request MUST have an 'a' tag" unless $has{a}; | ||
| 283 | croak "merge request MUST have a 'p' tag" unless $has{p}; | ||
| 284 | croak "merge request MUST have an 'e' tag with 'source' marker" | ||
| 285 | unless $has_source_e; | ||
| 286 | } | ||
| 287 | |||
| 288 | if ($kind == 30819) { | ||
| 289 | croak "wiki redirect MUST have a 'd' tag" unless $has{d}; | ||
| 290 | croak "wiki redirect MUST have an 'a' tag" unless $has{a}; | ||
| 291 | } | ||
| 292 | |||
| 293 | return 1; | ||
| 294 | } | ||
| 295 | |||
| 296 | sub resolve_wikilinks { | ||
| 297 | my ($class, $content) = @_; | ||
| 298 | |||
| 299 | # Extract defined references: [label]: target | ||
| 300 | my %defined; | ||
| 301 | while ($content =~ /^\[([^\]]+)\]:\s*(\S+)/mg) { | ||
| 302 | $defined{$1} = $2; | ||
| 303 | } | ||
| 304 | |||
| 305 | # Replace [text][Label] style (explicit reference) where Label is not defined | ||
| 306 | $content =~ s{ | ||
| 307 | \[([^\]]+)\]\[([^\]]+)\] | ||
| 308 | }{ | ||
| 309 | if (exists $defined{$2}) { | ||
| 310 | "[$1][$2]" # keep as-is | ||
| 311 | } else { | ||
| 312 | my $norm = $class->normalize_dtag($2); | ||
| 313 | "[$1](nostr:30818:$norm)" | ||
| 314 | } | ||
| 315 | }gex; | ||
| 316 | |||
| 317 | # Replace [text][] style (implicit reference) where text is not defined | ||
| 318 | $content =~ s{ | ||
| 319 | \[([^\]]+)\]\[\] | ||
| 320 | }{ | ||
| 321 | if (exists $defined{$1}) { | ||
| 322 | "[$1][]" # keep as-is | ||
| 323 | } else { | ||
| 324 | my $norm = $class->normalize_dtag($1); | ||
| 325 | "[$1](nostr:30818:$norm)" | ||
| 326 | } | ||
| 327 | }gex; | ||
| 328 | |||
| 329 | return $content; | ||
| 330 | } | ||
| 331 | |||
| 332 | 1; | ||
| 333 | |||
| 334 | __END__ | ||