package Gsg::MdParse; use strict; use warnings; use Log::Log4perl qw(:easy); use Exporter qw(import); our @EXPORT_OK = qw(render_readme); # README content is passed in as a var, sub returned an HTML version of the parsed markdown sub render_readme($$) { my $readme_content = shift; my $logger = shift; my $html_readme; # Might be a better way to do this? TODO open my $fh, '>>', \$html_readme or die "Can't open variable: $!"; # 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) ) { # HEADERS if ( $line =~ m/^#{1}(?!#)(.*)#$|^#{1}(?!#)(.*)$/ ) { my $parsed_line; if ( ! defined $1 || $1 eq "" ) { $parsed_line = "

$2

"; } else { $parsed_line = "

$1

"; } print $fh "$parsed_line"; } elsif ( $line =~ m/^#{2}(?!#)(.*)#{2}$|^#{2}(?!#)(.*)$/ ) { my $parsed_line; if ( ! defined $1 || $1 eq "" ) { $parsed_line = "

$2

"; } else { $parsed_line = "

$1

"; } print $fh "$parsed_line"; } elsif ( $line =~ m/^#{3}(?!#)(.*)#{3}$|^#{3}(?!#)(.*)$/ ) { my $parsed_line; if ( ! defined $1 || $1 eq "" ) { $parsed_line = "

$2

"; } else { $parsed_line = "

$1

"; } print $fh "$parsed_line"; } elsif ( $line =~ m/^#{4}(?!#)(.*)#{4}$|^#{4}(?!#)(.*)$/ ) { my $parsed_line; if ( ! defined $1 || $1 eq "" ) { $parsed_line = "

$2

"; } else { $parsed_line = "

$1

"; } print $fh "$parsed_line"; } elsif ( $line =~ m/^#{5}(?!#)(.*)#{5}$|^#{5}(?!#)(.*)$/ ) { my $parsed_line; if ( ! defined $1 || $1 eq "" ) { $parsed_line = "
$2
"; } else { $parsed_line = "
$1
"; } print $fh "$parsed_line"; } elsif ( $line =~ m/^\*(.*)/ ) { my $parsed_line = ""; print $fh "$parsed_line"; } else { print $fh "$line
"; } } print $fh "
"; close $fh; $logger->info("Parsed README"); return $html_readme; } 1;