lib/Gsg/MdParse.pm
1	package Gsg::MdParse;
2 use strict;
3 use warnings;
4 use Log::Log4perl qw(:easy);
5 use Exporter qw(import);
6 our @EXPORT_OK = qw(render_readme);
7
8 sub link_line($) {
9
10 my $line = shift;
11 if ( $line =~ m/(<(http.*)>)/ ) {
12 my $markup_link = $1;
13 my $link = $2;
14 my $link_replace = "<a href=\"$link\">$link</a>";
15 $line =~ s/\Q$1\E/$link_replace/g;
16 return $line;
17 }
18
19 return $line;
20
21 }
22
23 sub strike_line($) {
24
25 my $line = shift;
26 #if ( $line =~ m/~~(.*)~~/ ) {
27 if ( $line =~ m/^(.*)~~(.*)~~(.*)/ ) {
28 $line = "$1<s>$2</s>$3";
29 return $line;
30 }
31
32 return $line;
33
34 }
35
36 # README content is passed in as a var, sub returned an HTML version of the parsed markdown
37 sub render_readme($$) {
38
39 my $readme_content = shift;
40 my $logger = shift;
41
42 my $html_readme;
43 # Might be a better way to do this? TODO
44 open my $fh, '>>', \$html_readme or die "Can't open variable: $!";
45
46 # Main parsing logic, doing it line by line
47 # I have some ideas on how to make this more robust/efficient,
48 # but starting with below for POC
49 # TODO:
50 # Headers parsing can be one concise subroutine
51 # Started building a suite of simple subs to handle everything else,
52 # each line should be run through the 'sub suite' to handle all parsing
53 my @readme_lines = split("\n", $readme_content);
54 my $inside_code = 0;
55 foreach my $line ( @readme_lines ) {
56 # HEADERS
57 if ( $line =~ m/^#{1}(?!#)(.*)#$|^#{1}(?!#)(.*)$/ && $inside_code == 0 ) {
58 my $parsed_line;
59 if ( ! defined $1 || $1 eq "" ) {
60 $parsed_line = "<h1>$2</h1>";
61 } else {
62 $parsed_line = "<h1>$1</h1>";
63 }
64 print $fh "$parsed_line";
65 } elsif ( $line =~ m/^#{2}(?!#)(.*)#{2}$|^#{2}(?!#)(.*)$/ && $inside_code == 0) {
66 my $parsed_line;
67 if ( ! defined $1 || $1 eq "" ) {
68 $parsed_line = "<h2>$2</h2>";
69 } else {
70 $parsed_line = "<h2>$1</h2>";
71 }
72 print $fh "$parsed_line";
73 } elsif ( $line =~ m/^#{3}(?!#)(.*)#{3}$|^#{3}(?!#)(.*)$/ && $inside_code == 0) {
74 my $parsed_line;
75 if ( ! defined $1 || $1 eq "" ) {
76 $parsed_line = "<h3>$2</h3>";
77 } else {
78 $parsed_line = "<h3>$1</h3>";
79 }
80 print $fh "$parsed_line";
81 } elsif ( $line =~ m/^#{4}(?!#)(.*)#{4}$|^#{4}(?!#)(.*)$/ && $inside_code == 0) {
82 my $parsed_line;
83 if ( ! defined $1 || $1 eq "" ) {
84 $parsed_line = "<h4>$2</h4>";
85 } else {
86 $parsed_line = "<h4>$1</h4>";
87 }
88 print $fh "$parsed_line";
89 } elsif ( $line =~ m/^#{5}(?!#)(.*)#{5}$|^#{5}(?!#)(.*)$/ && $inside_code == 0) {
90 my $parsed_line;
91 if ( ! defined $1 || $1 eq "" ) {
92 $parsed_line = "<h5>$2</h5>";
93 } else {
94 $parsed_line = "<h5>$1</h5>";
95 }
96 print $fh "$parsed_line";
97 } elsif ( $line =~ m/^\*(.*)/ && $inside_code == 0) {
98 $line = strike_line($1);
99 $line = link_line($line);
100 my $parsed_line = "<ul><li>$line</li></ul>";
101 print $fh "$parsed_line";
102 } elsif ( $line =~ m/^```$/ ) {
103 if ( $inside_code == 0 ) {
104 $inside_code = 1;
105 print $fh "<br>";
106 } elsif ( $inside_code == 1 ) {
107 $inside_code = 0;
108 }
109 } elsif ( $inside_code == 1 ) {
110 print $fh "<code>$line</code><br>";
111 } elsif ( $line =~ m/(http.*)/ ) {
112 $line = link_line($line);
113 print $fh "$line<br>";
114 } elsif ( $line =~ m/^~~(.*)~~/ ) {
115 $line = strike_line($line);
116 print $fh "$line<br>";
117 }
118 else {
119 print $fh "$line<br>";
120 }
121 }
122
123 print $fh "<br>";
124 close $fh;
125 $logger->info("Parsed README");
126 return $html_readme;
127
128 }
129 1;