commit 3c476b885bb36b7c28af597ab436ef4b0ec82362
Author: spesk1 <spesk@pm.me>
Date: Mon Jun 24 01:02:45 2019 +0000
Added backtick code block support to markdown parser
diff --git a/README.md b/README.md
index a989b15..674b5b9 100644
--- a/README.md
+++ b/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)
diff --git a/lib/Gsg/MdParse.pm b/lib/Gsg/MdParse.pm
index 483a626..9a2333c 100644
--- a/lib/Gsg/MdParse.pm
+++ b/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;