Blame


1 1c1e3e26 2022-09-08 op #!/usr/bin/env perl
2 1c1e3e26 2022-09-08 op #
3 1c1e3e26 2022-09-08 op # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 1c1e3e26 2022-09-08 op #
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.
8 1c1e3e26 2022-09-08 op #
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.
16 1c1e3e26 2022-09-08 op
17 1c1e3e26 2022-09-08 op use v5.10;
18 1c1e3e26 2022-09-08 op use strict;
19 1c1e3e26 2022-09-08 op use warnings;
20 1c1e3e26 2022-09-08 op
21 1c1e3e26 2022-09-08 op my $in_pre = 0;
22 1c1e3e26 2022-09-08 op my $in_list = 0;
23 1c1e3e26 2022-09-08 op
24 1c1e3e26 2022-09-08 op while (<>) {
25 1c1e3e26 2022-09-08 op chomp;
26 1c1e3e26 2022-09-08 op if ($in_pre && m/^```/) {
27 1c1e3e26 2022-09-08 op $in_pre = 0;
28 1c1e3e26 2022-09-08 op say "</pre>";
29 1c1e3e26 2022-09-08 op } elsif (!$in_pre && m/^```/) {
30 1c1e3e26 2022-09-08 op if ($in_list) {
31 1c1e3e26 2022-09-08 op $in_list = 0;
32 1c1e3e26 2022-09-08 op say "</ul>";
33 1c1e3e26 2022-09-08 op }
34 1c1e3e26 2022-09-08 op $in_pre = 1;
35 1c1e3e26 2022-09-08 op print "<pre>";
36 1c1e3e26 2022-09-08 op } elsif ($in_pre) {
37 1c1e3e26 2022-09-08 op say san($_);
38 1c1e3e26 2022-09-08 op } elsif (m/^</) {
39 1c1e3e26 2022-09-08 op say;
40 1c1e3e26 2022-09-08 op } elsif ($in_list && m/^$/) {
41 1c1e3e26 2022-09-08 op say "</ul>";
42 1c1e3e26 2022-09-08 op $in_list = 0;
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";
46 1c1e3e26 2022-09-08 op say "<ul>";
47 1c1e3e26 2022-09-08 op } elsif ($in_list eq "link") {
48 1c1e3e26 2022-09-08 op $in_list = "item";
49 1c1e3e26 2022-09-08 op say "</ul>";
50 1c1e3e26 2022-09-08 op say "<ul>";
51 1c1e3e26 2022-09-08 op }
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;
56 1c1e3e26 2022-09-08 op
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) {
60 1c1e3e26 2022-09-08 op say "</ul>";
61 1c1e3e26 2022-09-08 op $in_list = 0;
62 1c1e3e26 2022-09-08 op }
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' />";
66 1c1e3e26 2022-09-08 op print "</a>";
67 1c1e3e26 2022-09-08 op print "<figcaption>$alt</figcaption>";
68 1c1e3e26 2022-09-08 op print "</figure>\n";
69 1c1e3e26 2022-09-08 op next;
70 1c1e3e26 2022-09-08 op }
71 1c1e3e26 2022-09-08 op
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";
77 1c1e3e26 2022-09-08 op say "</ul>";
78 1c1e3e26 2022-09-08 op say "<ul class='link-list'>";
79 1c1e3e26 2022-09-08 op }
80 1c1e3e26 2022-09-08 op $_ = $alt;
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);
90 1c1e3e26 2022-09-08 op } else {
91 1c1e3e26 2022-09-08 op output("p", $_);
92 1c1e3e26 2022-09-08 op }
93 1c1e3e26 2022-09-08 op }
94 1c1e3e26 2022-09-08 op
95 1c1e3e26 2022-09-08 op say "</ul>" if $in_list;
96 1c1e3e26 2022-09-08 op say "</pre>" if $in_pre;
97 1c1e3e26 2022-09-08 op
98 1c1e3e26 2022-09-08 op sub san {
99 1c1e3e26 2022-09-08 op s/&/\&amp;/g;
100 1c1e3e26 2022-09-08 op s/</\&lt;/g;
101 1c1e3e26 2022-09-08 op s/>/\&gt;/g;
102 1c1e3e26 2022-09-08 op return $_;
103 1c1e3e26 2022-09-08 op }
104 1c1e3e26 2022-09-08 op
105 1c1e3e26 2022-09-08 op sub output {
106 1c1e3e26 2022-09-08 op my ($tn, $content) = @_;
107 1c1e3e26 2022-09-08 op
108 1c1e3e26 2022-09-08 op if (!$in_list && $tn eq "li") {
109 1c1e3e26 2022-09-08 op $in_list = 1;
110 1c1e3e26 2022-09-08 op say "<ul>";
111 1c1e3e26 2022-09-08 op }
112 1c1e3e26 2022-09-08 op
113 1c1e3e26 2022-09-08 op if ($in_list && $tn ne "li") {
114 1c1e3e26 2022-09-08 op $in_list = 0;
115 1c1e3e26 2022-09-08 op say "</ul>";
116 1c1e3e26 2022-09-08 op }
117 1c1e3e26 2022-09-08 op
118 1c1e3e26 2022-09-08 op if ($tn eq "p" && $content eq "") {
119 1c1e3e26 2022-09-08 op return;
120 1c1e3e26 2022-09-08 op }
121 1c1e3e26 2022-09-08 op
122 1c1e3e26 2022-09-08 op $_ = $content;
123 1c1e3e26 2022-09-08 op say "<$tn>". san() ."</$tn>";
124 1c1e3e26 2022-09-08 op }