https://git.spwbk.site/swatson/git-site-gen/raw/master/lib/Gsg/MdParse.pm
___________________________________
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 = "$link";
$line =~ s/\Q$1\E/$link_replace/g;
return $line;
}
return $line;
}
sub strike_line($) {
my $line = shift;
#if ( $line =~ m/~~(.*)~~/ ) {
if ( $line =~ m/^(.*)~~(.*)~~(.*)/ ) {
$line = "$1$2$3";
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
# TODO:
# Headers parsing can be one concise subroutine
# Started building a suite of simple subs to handle everything else,
# each line should be run through the 'sub suite' to handle all parsing
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 = "
$line