The code on this page does the following:

We define 「old_print_html」 to do what 「print_html」 currently does and we define 「print_html」 to do what 「new_print_html」 does. Writing it this way avoids the error “Deep recursion on subroutine …”

「new_print_html」 gets the output of 「old_print_html」 and it uses 「link_social_media」 to fiddle with the HTML, and then returns that instead.

「link_social_media」 changes the HTML output using search and replace: every time a social media handle gets mentioned, like “@kensanata@twitter.com” or “@kensanata@octodon.social” it gets shown as “@kensanata” and is linked to an appropriate target URL.

This would be a way to add fancier HTML to the HTML side without having to add extra markup to the gemtext.

package MyConfig;
use Modern::Perl;
use App::Phoebe::Web;



sub new_to_html {
  my $html = old_to_html(@_);
  return link_social_media($html);
}

sub link_social_media {
  my $str = shift;
  $str =~ s{\@([-a-z_0-9]+)\@((?:[-a-z0-9]+\.)+[a-z0-9]+)}{
    my ($username, $host) = ($1, $2);
    if ($host eq "twitter.com") {
      # boooo!
      qq{<a href="https://nitter.net/$username">\@$username</a>};
    } else {
      # works for Mastodon, Pleroma, etc.
      qq{<a href="https://$host/users/$username">\@$username</a>};
    }
  }gie;
  return $str;
}