Skip to content

Commit

Permalink
fix: support non-word characters in link names for ePUB generation
Browse files Browse the repository at this point in the history
Closes: chromatic#249
Inspired by @sd43 in 2684d56
  • Loading branch information
jnahmias committed Jun 6, 2024
1 parent 76224f1 commit 79643dc
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions build/tools/build_epub.pl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
my $anchors = get_anchors(@chapters);
my $table_of_contents = [];
my %entries;
my $link_pos;

sub Pod::PseudoPod::HTML::begin_X
{
Expand Down Expand Up @@ -84,19 +85,22 @@ sub clean_name
return 'i' . $name;
}

sub Pod::PseudoPod::HTML::start_L
{
my $self = shift;
$link_pos = length($self->{scratch});
}

sub Pod::PseudoPod::HTML::end_L
{
my $self = shift;
if ($self->{scratch} =~ s/\b(\w+)$//)
{
my $link = $1;
die "Unknown link $link\n" unless exists $anchors->{$link};
$self->{scratch} .=
'<a href="'
. $anchors->{$link}[0]
. '#' . $link . '">'
. $anchors->{$link}[1] . "</a>($link)";
}
my $link = substr($self->{scratch}, $link_pos);

die "Unknown link $link\n" unless exists $anchors->{$link};

substr($self->{scratch}, $link_pos, length($link),
'<a href="' . $anchors->{$link}[0] . "#$link\">"
. $anchors->{$link}[1] . '</a>');
}

for my $chapter (@chapters)
Expand Down

0 comments on commit 79643dc

Please sign in to comment.