MdParse.pm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. sub link_line($) {
  8. my $line = shift;
  9. if ( $line =~ m/(http.*)/ ) {
  10. my $link = $1;
  11. my $link_replace = "<a href=\"$link\">$link</a>";
  12. $line =~ s/\Q$1\E/$link_replace/g;
  13. return $line;
  14. }
  15. return $line;
  16. }
  17. # README content is passed in as a var, sub returned an HTML version of the parsed markdown
  18. sub render_readme($$) {
  19. my $readme_content = shift;
  20. my $logger = shift;
  21. my $html_readme;
  22. # Might be a better way to do this? TODO
  23. open my $fh, '>>', \$html_readme or die "Can't open variable: $!";
  24. # Main parsing logic, doing it line by line
  25. # I have some ideas on how to make this more robust/efficient,
  26. # but starting with below for POC
  27. my @readme_lines = split("\n", $readme_content);
  28. my $inside_code = 0;
  29. foreach my $line ( @readme_lines ) {
  30. # HEADERS
  31. if ( $line =~ m/^#{1}(?!#)(.*)#$|^#{1}(?!#)(.*)$/ && $inside_code == 0 ) {
  32. my $parsed_line;
  33. if ( ! defined $1 || $1 eq "" ) {
  34. $parsed_line = "<h1>$2</h1>";
  35. } else {
  36. $parsed_line = "<h1>$1</h1>";
  37. }
  38. print $fh "$parsed_line";
  39. } elsif ( $line =~ m/^#{2}(?!#)(.*)#{2}$|^#{2}(?!#)(.*)$/ && $inside_code == 0) {
  40. my $parsed_line;
  41. if ( ! defined $1 || $1 eq "" ) {
  42. $parsed_line = "<h2>$2</h2>";
  43. } else {
  44. $parsed_line = "<h2>$1</h2>";
  45. }
  46. print $fh "$parsed_line";
  47. } elsif ( $line =~ m/^#{3}(?!#)(.*)#{3}$|^#{3}(?!#)(.*)$/ && $inside_code == 0) {
  48. my $parsed_line;
  49. if ( ! defined $1 || $1 eq "" ) {
  50. $parsed_line = "<h3>$2</h3>";
  51. } else {
  52. $parsed_line = "<h3>$1</h3>";
  53. }
  54. print $fh "$parsed_line";
  55. } elsif ( $line =~ m/^#{4}(?!#)(.*)#{4}$|^#{4}(?!#)(.*)$/ && $inside_code == 0) {
  56. my $parsed_line;
  57. if ( ! defined $1 || $1 eq "" ) {
  58. $parsed_line = "<h4>$2</h4>";
  59. } else {
  60. $parsed_line = "<h4>$1</h4>";
  61. }
  62. print $fh "$parsed_line";
  63. } elsif ( $line =~ m/^#{5}(?!#)(.*)#{5}$|^#{5}(?!#)(.*)$/ && $inside_code == 0) {
  64. my $parsed_line;
  65. if ( ! defined $1 || $1 eq "" ) {
  66. $parsed_line = "<h5>$2</h5>";
  67. } else {
  68. $parsed_line = "<h5>$1</h5>";
  69. }
  70. print $fh "$parsed_line";
  71. } elsif ( $line =~ m/^\*(.*)/ && $inside_code == 0) {
  72. $line = link_line($1);
  73. my $parsed_line = "<ul><li>$line</li></ul>";
  74. print $fh "$parsed_line";
  75. } elsif ( $line =~ m/^```$/ ) {
  76. if ( $inside_code == 0 ) {
  77. $inside_code = 1;
  78. print $fh "<br>";
  79. } elsif ( $inside_code == 1 ) {
  80. $inside_code = 0;
  81. }
  82. } elsif ( $inside_code == 1 ) {
  83. print $fh "<code>$line</code><br>";
  84. } elsif ( $line =~ m/(http.*)/ ) {
  85. $line = link_line($line);
  86. print $fh "$line<br>";
  87. }
  88. else {
  89. print $fh "$line<br>";
  90. }
  91. }
  92. print $fh "<br>";
  93. close $fh;
  94. $logger->info("Parsed README");
  95. return $html_readme;
  96. }
  97. 1;