From 892008359f8437e4dcff6a38182d7eeea9a618bc Mon Sep 17 00:00:00 2001 From: Yuan Fang Date: Fri, 5 Jan 2024 21:38:37 +0800 Subject: [PATCH] Move the perl part to add-link.pl Signed-off-by: Yuan Fang --- www/scripts/add-github-link-to-examples.sh | 8 +++---- www/scripts/add-link.pl | 25 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/www/scripts/add-github-link-to-examples.sh b/www/scripts/add-github-link-to-examples.sh index 570fbd96d8..0673a54140 100755 --- a/www/scripts/add-github-link-to-examples.sh +++ b/www/scripts/add-github-link-to-examples.sh @@ -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!!$github_logo_svg!; 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" {} \; } \ No newline at end of file diff --git a/www/scripts/add-link.pl b/www/scripts/add-link.pl index f87f5c14cb..c9b6e1581b 100644 --- a/www/scripts/add-link.pl +++ b/www/scripts/add-link.pl @@ -1 +1,24 @@ -# TODO \ No newline at end of file +#!/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!!$github_logo_svg!; + $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 \ No newline at end of file