Browse Source

Added backtick code block support to markdown parser

spesk1 4 years ago
parent
commit
3c476b885b
2 changed files with 21 additions and 10 deletions
  1. 1 1
      README.md
  2. 20 9
      lib/Gsg/MdParse.pm

+ 1 - 1
README.md

@@ -12,7 +12,7 @@
 DONE:
 * Generate top level index with links to all projects
 * Generate project specific index with links to git log/diffs and file content of repo
-* Extremely basic markdown parser (only supports headings and non nested bullets)
+* Extremely basic markdown parser (only supports headings, non nested bullets and backticks code blocks)
 * Line numbers for file browsing
 * Diff highlighting for log
 * Show git:// address for cloning (configurable via config file)

+ 20 - 9
lib/Gsg/MdParse.pm

@@ -18,9 +18,11 @@ sub render_readme($$) {
 	# Main parsing logic, doing it line by line
 	# I have some ideas on how to make this more robust/efficient,
 	# but starting with below for POC
-	foreach my $line ( split("\n", $readme_content) ) {
+	my @readme_lines = split("\n", $readme_content);
+	my $inside_code = 0;
+	foreach my $line ( @readme_lines ) {
 		# HEADERS
-		if ( $line =~ m/^#{1}(?!#)(.*)#$|^#{1}(?!#)(.*)$/ ) {
+		if ( $line =~ m/^#{1}(?!#)(.*)#$|^#{1}(?!#)(.*)$/ && $inside_code == 0 ) {
 			my $parsed_line;
 			if ( ! defined $1 || $1 eq "" ) {
 				$parsed_line = "<h1>$2</h1>";
@@ -28,7 +30,7 @@ sub render_readme($$) {
 				$parsed_line = "<h1>$1</h1>";
 			}
 			print $fh "$parsed_line";
-		} elsif ( $line =~ m/^#{2}(?!#)(.*)#{2}$|^#{2}(?!#)(.*)$/ ) {
+		} elsif ( $line =~ m/^#{2}(?!#)(.*)#{2}$|^#{2}(?!#)(.*)$/ && $inside_code == 0) {
 			my $parsed_line;
 			if ( ! defined $1 || $1 eq "" ) {
 				$parsed_line = "<h2>$2</h2>";
@@ -36,7 +38,7 @@ sub render_readme($$) {
 				$parsed_line = "<h2>$1</h2>";
 			}
 			print $fh "$parsed_line";
-		} elsif ( $line =~ m/^#{3}(?!#)(.*)#{3}$|^#{3}(?!#)(.*)$/ ) {
+		} elsif ( $line =~ m/^#{3}(?!#)(.*)#{3}$|^#{3}(?!#)(.*)$/ && $inside_code == 0) {
 			my $parsed_line;
 			if ( ! defined $1 || $1 eq "" ) {
 				$parsed_line = "<h3>$2</h3>";
@@ -44,7 +46,7 @@ sub render_readme($$) {
 				$parsed_line = "<h3>$1</h3>";
 			}
 			print $fh "$parsed_line";
-		} elsif ( $line =~ m/^#{4}(?!#)(.*)#{4}$|^#{4}(?!#)(.*)$/ ) {
+		} elsif ( $line =~ m/^#{4}(?!#)(.*)#{4}$|^#{4}(?!#)(.*)$/ && $inside_code == 0) {
 			my $parsed_line;
 			if ( ! defined $1 || $1 eq "" ) {
 				$parsed_line = "<h4>$2</h4>";
@@ -52,7 +54,7 @@ sub render_readme($$) {
 				$parsed_line = "<h4>$1</h4>";
 			}
 			print $fh "$parsed_line";
-		} elsif ( $line =~ m/^#{5}(?!#)(.*)#{5}$|^#{5}(?!#)(.*)$/ ) {
+		} elsif ( $line =~ m/^#{5}(?!#)(.*)#{5}$|^#{5}(?!#)(.*)$/ && $inside_code == 0) {
 			my $parsed_line;
 			if ( ! defined $1 || $1 eq "" ) {
 				$parsed_line = "<h5>$2</h5>";
@@ -60,16 +62,25 @@ sub render_readme($$) {
 				$parsed_line = "<h5>$1</h5>";
 			}
 			print $fh "$parsed_line";
-		} elsif ( $line =~ m/^\*(.*)/ ) {
+		} elsif ( $line =~ m/^\*(.*)/ && $inside_code == 0) {
 			my $parsed_line = "<ul><li>$1</li></ul>";
 			print $fh "$parsed_line";
+		} elsif ( $line =~ m/^```$/ ) {
+			if ( $inside_code == 0 ) {
+				$inside_code = 1;
+				print $fh "<br>";
+			} elsif ( $inside_code == 1 ) {
+				$inside_code = 0;
+			}
+		} elsif ( $inside_code == 1 ) { 
+			print $fh "<code>$line</code><br>";
 		}
+
 		else {
-			print $fh "$line</br>";
+			print $fh "$line<br>";
 		}
 	}
 
-	print $fh "</br><hr\>";
 	close $fh;
 	$logger->info("Parsed README");
 	return $html_readme;