| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 | 
							- package Gsg::MdParse;
 
- use strict;
 
- use warnings;
 
- 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 $markup_link = $1;
 
- 		my $link = $2;
 
- 		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($$) {
 
- 	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
 
- 	my @readme_lines = split("\n", $readme_content);
 
- 	my $inside_code = 0;
 
- 	foreach my $line ( @readme_lines ) {
 
- 		# HEADERS
 
- 		if ( $line =~ m/^#{1}(?!#)(.*)#$|^#{1}(?!#)(.*)$/ && $inside_code == 0 ) {
 
- 			my $parsed_line;
 
- 			if ( ! defined $1 || $1 eq "" ) {
 
- 				$parsed_line = "<h1>$2</h1>";
 
- 			} else {
 
- 				$parsed_line = "<h1>$1</h1>";
 
- 			}
 
- 			print $fh "$parsed_line";
 
- 		} elsif ( $line =~ m/^#{2}(?!#)(.*)#{2}$|^#{2}(?!#)(.*)$/ && $inside_code == 0) {
 
- 			my $parsed_line;
 
- 			if ( ! defined $1 || $1 eq "" ) {
 
- 				$parsed_line = "<h2>$2</h2>";
 
- 			} else {
 
- 				$parsed_line = "<h2>$1</h2>";
 
- 			}
 
- 			print $fh "$parsed_line";
 
- 		} elsif ( $line =~ m/^#{3}(?!#)(.*)#{3}$|^#{3}(?!#)(.*)$/ && $inside_code == 0) {
 
- 			my $parsed_line;
 
- 			if ( ! defined $1 || $1 eq "" ) {
 
- 				$parsed_line = "<h3>$2</h3>";
 
- 			} else {
 
- 				$parsed_line = "<h3>$1</h3>";
 
- 			}
 
- 			print $fh "$parsed_line";
 
- 		} elsif ( $line =~ m/^#{4}(?!#)(.*)#{4}$|^#{4}(?!#)(.*)$/ && $inside_code == 0) {
 
- 			my $parsed_line;
 
- 			if ( ! defined $1 || $1 eq "" ) {
 
- 				$parsed_line = "<h4>$2</h4>";
 
- 			} else {
 
- 				$parsed_line = "<h4>$1</h4>";
 
- 			}
 
- 			print $fh "$parsed_line";
 
- 		} elsif ( $line =~ m/^#{5}(?!#)(.*)#{5}$|^#{5}(?!#)(.*)$/ && $inside_code == 0) {
 
- 			my $parsed_line;
 
- 			if ( ! defined $1 || $1 eq "" ) {
 
- 				$parsed_line = "<h5>$2</h5>";
 
- 			} else {
 
- 				$parsed_line = "<h5>$1</h5>";
 
- 			}
 
- 			print $fh "$parsed_line";
 
- 		} elsif ( $line =~ m/^\*(.*)/ && $inside_code == 0) {
 
- 			$line = link_line($1);
 
- 			my $parsed_line = "<ul><li>$line</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>";
 
- 		} elsif ( $line =~ m/(http.*)/ ) {
 
- 			$line = link_line($line);
 
- 			print $fh "$line<br>";
 
- 		} elsif ( $line =~ m/^~~(.*)~~/ ) {
 
- 			$line = "<s>$1</s>";
 
- 			print $fh "$line<br>";
 
- 		}
 
- 		else {
 
- 			print $fh "$line<br>";
 
- 		}
 
- 	}
 
- 	print $fh "<br>";
 
- 	close $fh;
 
- 	$logger->info("Parsed README");
 
- 	return $html_readme;
 
- }
 
- 1;
 
 
  |