Move the perl part to add-link.pl

Signed-off-by: Yuan Fang <yuanfang@alauda.io>
This commit is contained in:
Yuan Fang 2024-01-05 21:38:37 +08:00
parent 5224403c15
commit 892008359f
No known key found for this signature in database
GPG Key ID: E02C5B28B3C4F6FB
2 changed files with 28 additions and 5 deletions

View File

@ -13,9 +13,9 @@ function add_github_link_to_examples {
EOF
)
# TODO use specific example link (e.g. https://github.com/roc-lang/examples/tree/main/examples/FizzBuzz) for every example
local examples_link="https://github.com/roc-lang/examples"
# Use specific example link (e.g. https://github.com/roc-lang/examples/tree/main/examples/FizzBuzz) for every example
local examples_link="https://github.com/roc-lang/examples/tree/main/examples"
# TODO move perl stuff to add-link.pl
find $examples_dir_path -type f -name "README.html" -exec perl -MFile::Basename -pi -e "(\$name = dirname(\$ARGV)); s!</h1>!</h1><a id='gh-example-link' href=\"\$name\" aria-label='view on github'>$github_logo_svg</a>!; s!href=\"$examples_dir_path!href=\"$examples_link!" {} \;
# Insert a github link to the example in HTML
find "$examples_dir_path" -type f -name "README.html" -exec perl scripts/add-link.pl "$examples_dir_path" "$examples_link" "$github_logo_svg" {} \;
}

View File

@ -1 +1,24 @@
# TODO
#!/usr/bin/perl
use File::Basename;
sub add_link {
my $examples_dir_path = shift;
my $examples_gh_link = shift;
my $github_logo_svg = shift;
my $file_path = shift;
my $file_dir = dirname($file_path);
open my $fh, '<', $file_path or die "Can't open $file_path: $!";
my $content = do { local $/; <$fh> };
close $fh;
$content =~ s!</h1>!</h1><a id="gh-example-link" href="$file_dir" aria-label="view on github">$github_logo_svg</a>!;
$content =~ s!href=\"$examples_dir_path!href=\"$examples_gh_link!;
open my $fh, '>', $file_path or die "Can't open $file_path: $!";
print $fh $content;
close $fh;
}
add_link @ARGV