1 1c1e3e26 2022-09-08 op #!/usr/bin/env perl
3 1c1e3e26 2022-09-08 op # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
5 1c1e3e26 2022-09-08 op # Permission to use, copy, modify, and distribute this software for any
6 1c1e3e26 2022-09-08 op # purpose with or without fee is hereby granted, provided that the above
7 1c1e3e26 2022-09-08 op # copyright notice and this permission notice appear in all copies.
9 1c1e3e26 2022-09-08 op # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 1c1e3e26 2022-09-08 op # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 1c1e3e26 2022-09-08 op # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 1c1e3e26 2022-09-08 op # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 1c1e3e26 2022-09-08 op # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 1c1e3e26 2022-09-08 op # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 1c1e3e26 2022-09-08 op # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 1c1e3e26 2022-09-08 op my $in_pre = 0;
22 1c1e3e26 2022-09-08 op my $in_list = 0;
26 1c1e3e26 2022-09-08 op if ($in_pre && m/^```/) {
29 1c1e3e26 2022-09-08 op } elsif (!$in_pre && m/^```/) {
30 1c1e3e26 2022-09-08 op if ($in_list) {
35 1c1e3e26 2022-09-08 op print "<pre>";
36 1c1e3e26 2022-09-08 op } elsif ($in_pre) {
38 1c1e3e26 2022-09-08 op } elsif (m/^</) {
40 1c1e3e26 2022-09-08 op } elsif ($in_list && m/^$/) {
43 1c1e3e26 2022-09-08 op } elsif (m/^\*\s+(.*)/) { # NB: at least one space
44 1c1e3e26 2022-09-08 op if (!$in_list) {
45 1c1e3e26 2022-09-08 op $in_list = "item";
47 1c1e3e26 2022-09-08 op } elsif ($in_list eq "link") {
48 1c1e3e26 2022-09-08 op $in_list = "item";
52 1c1e3e26 2022-09-08 op output("li", $1);
53 1c1e3e26 2022-09-08 op } elsif (m/^=>\s*([^\s]*)\s*(.*)$/) {
54 1c1e3e26 2022-09-08 op my $href = $1;
55 1c1e3e26 2022-09-08 op my $alt = $2 || $1;
57 1c1e3e26 2022-09-08 op # special case: images
58 1c1e3e26 2022-09-08 op if ($1 =~ /\.(png|jpg|svg)$/) {
59 1c1e3e26 2022-09-08 op if ($in_list) {
63 1c1e3e26 2022-09-08 op print "<figure>";
64 1c1e3e26 2022-09-08 op print "<a href='$href'>";
65 1c1e3e26 2022-09-08 op print "<img src='$href' alt='$alt' />";
67 1c1e3e26 2022-09-08 op print "<figcaption>$alt</figcaption>";
68 1c1e3e26 2022-09-08 op print "</figure>\n";
72 1c1e3e26 2022-09-08 op if (!$in_list) {
73 1c1e3e26 2022-09-08 op $in_list = "link";
74 1c1e3e26 2022-09-08 op say "<ul class='link-list'>";
75 1c1e3e26 2022-09-08 op } elsif ($in_list eq "item") {
76 1c1e3e26 2022-09-08 op $in_list = "link";
78 1c1e3e26 2022-09-08 op say "<ul class='link-list'>";
81 1c1e3e26 2022-09-08 op say "<li><a href='$href'>". san() ."</a></li>";
82 1c1e3e26 2022-09-08 op } elsif (m/^###\s*(.*)$/) {
83 1c1e3e26 2022-09-08 op output("h3", $1);
84 1c1e3e26 2022-09-08 op } elsif (m/^##\s*(.*)$/) {
85 1c1e3e26 2022-09-08 op output("h2", $1);
86 1c1e3e26 2022-09-08 op } elsif (m/^#\s*(.*)$/) {
87 1c1e3e26 2022-09-08 op output("h1", $1);
88 1c1e3e26 2022-09-08 op } elsif (m/^>\s*(.*)$/) {
89 1c1e3e26 2022-09-08 op output("blockquote", $1);
91 1c1e3e26 2022-09-08 op output("p", $_);
95 1c1e3e26 2022-09-08 op say "</ul>" if $in_list;
96 1c1e3e26 2022-09-08 op say "</pre>" if $in_pre;
106 1c1e3e26 2022-09-08 op my ($tn, $content) = @_;
108 1c1e3e26 2022-09-08 op if (!$in_list && $tn eq "li") {
109 1c1e3e26 2022-09-08 op $in_list = 1;
113 1c1e3e26 2022-09-08 op if ($in_list && $tn ne "li") {
114 1c1e3e26 2022-09-08 op $in_list = 0;
118 1c1e3e26 2022-09-08 op if ($tn eq "p" && $content eq "") {
122 1c1e3e26 2022-09-08 op $_ = $content;
123 1c1e3e26 2022-09-08 op say "<$tn>". san() ."</$tn>";