my $named_link= q{ \s* (?[^|]*?) \s* (?: (?\|) (?.*?) \s* )? }; sub build_parser_rules { my @parts; push @parts, [ 70, q{ \{{3} \s* (?.*?) \s* \}{3} (*:nowiki) } ]; push @parts, [ 80, qr{ \{{2} $named_link \}{2} (*:image) }xs ]; push @parts, [ 90, qr{ \<{3} \s* (?.*?) \s* \>{3} (*:placeholder) }xs ]; push @parts, [ 40, q{// \s* (?(?: (?&link) | . )*?) \s* (?: (?: (? (?&link)|.|\Z ) (*:escape)} ]; push @parts, [ 60, qr{\\\\ (*:break)}x ]; return \@parts; } method formulate_link_rule { # formulate the 'link' rule, which includes link_prefixes which are set after construction. # So this is used at the last moment before the parser is created. my $linkprefix= join "|", map { quotemeta($_) } @{$self->link_prefixes}; my $blend= $self->get_parse_option('blended_links') ? q{ (?()|(? \w+)? ) } : ''; my $link= qr{ (? (?: \[{2} $named_link \]{2} # explicit use of brackets $blend # blend suffix ) | (?: (?(?: $linkprefix )://\S+?) (?= [,.?!:;"']? # ❝Single punctuation characters (,.?!:;"') at the end of URLs should not be considered part of the URL.❞ (?: \Z|\s ) ) # since I used a lazy quantifier to allow the trailing punctuation, need to know how to end. ) )(*:link) }x; return [ 10, $link ]; } method formulate_simples_rule { # formulate the 'simples' rule, which includes simple_format_tags which are set after construction. # simple_format_tags are qw[** //] for standard Creole, and can have extensions such as qw/__ ## ^^/ for underlined, monospaced, superscript, etc. "simple" means same open and close and maps to a html tag. # So this is used at the last moment before the parser is created. my $simples= join "|", map { $_ eq '//' ? () : quotemeta($_) } (keys %{$self->simple_format_tags}); return [ 50, q{(? (?:} . $simples . q{))\s*(?.*?) \s* (?: (?: (?) | \Z) (*:simple)} ]; } method get_final_parser_rules { my $parser_rules= $self->parser_rules; push @$parser_rules, $self->formulate_link_rule, $self->formulate_simples_rule; return $parser_rules } method _build_parser_spec { my $parser_rules= $self->get_final_parser_rules; my $branches_string= join "\n | ", map { my $x= $$_[1]; ref $x ? $x : "(?: $x )" } (sort { $a->[0] <=> $b->[0] } @$parser_rules); my $ps= qr{(?.*?) (?: $branches_string | \Z (*:nada) # must be the last branch ) }xs; return $ps; } #### method do_format (Str $line) { my @results; my $ps= $self->_parser_spec; while ($line =~ /$ps/g) { my %captures= %+; my $regmark= $REGMARK; my $prematch= $captures{prematch}; push @results, $self->escape($self->filter($prematch)) unless length($prematch)==0; unless ($regmark eq 'nada') { my $meth= "grammar_branch_$regmark"; push @results, $self->$meth (\%captures); } } return join ('', @results); }