MdParse.pm 2.9 KB

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