Browse Source

Added link parsing to md parser

spesk1 4 years ago
parent
commit
9130b2f18c
2 changed files with 21 additions and 2 deletions
  1. 1 0
      README.md
  2. 20 2
      lib/Gsg/MdParse.pm

+ 1 - 0
README.md

@@ -1,4 +1,5 @@
 # git-site-gen #
+### AKA gsg
 * Tool used to generate a bare bones, pure HTML static website from information availble in a git project dir. See: https://spwbk.site/git.
 * Inspired by the much better/full-featured cgit.
 * Not sure if this will be widely useful, mostly written for learning purposes.

+ 20 - 2
lib/Gsg/MdParse.pm

@@ -5,6 +5,20 @@ use Log::Log4perl qw(:easy);
 use Exporter qw(import);
 our @EXPORT_OK = qw(render_readme);
 
+sub link_line($) {
+
+	my $line = shift;
+	if ( $line =~ m/(http.*)/ ) {
+		my $link = $1;
+		my $link_replace = "<a href=\"$link\">$link</a>";
+		$line =~ s/\Q$1\E/$link_replace/g;
+		return $line;
+	}
+
+	return $line;
+
+}
+
 # README content is passed in as a var, sub returned an HTML version of the parsed markdown
 sub render_readme($$) {
 
@@ -63,7 +77,8 @@ sub render_readme($$) {
 			}
 			print $fh "$parsed_line";
 		} elsif ( $line =~ m/^\*(.*)/ && $inside_code == 0) {
-			my $parsed_line = "<ul><li>$1</li></ul>";
+			$line = link_line($1);
+			my $parsed_line = "<ul><li>$line</li></ul>";
 			print $fh "$parsed_line";
 		} elsif ( $line =~ m/^```$/ ) {
 			if ( $inside_code == 0 ) {
@@ -74,13 +89,16 @@ sub render_readme($$) {
 			}
 		} elsif ( $inside_code == 1 ) { 
 			print $fh "<code>$line</code><br>";
+		} elsif ( $line =~ m/(http.*)/ ) {
+			$line = link_line($line);
+			print $fh "$line<br>";
 		}
-
 		else {
 			print $fh "$line<br>";
 		}
 	}
 
+	print $fh "<br>";
 	close $fh;
 	$logger->info("Parsed README");
 	return $html_readme;