-
Notifications
You must be signed in to change notification settings - Fork 0
/
hem2md.pl
191 lines (143 loc) · 3.54 KB
/
hem2md.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/perl
use strict;
# Declare this script as UTF-8
use utf8;
use JSON;
use Getopt::Std;
# Declare output as UTF-8
binmode(STDOUT, ":utf8");
sub usage() {
die "Usage: $0 [-nq] file\n";
}
my $warning=1;
my $htmlblkquote=0;
my $markua_bq = ">";
my %opts;
usage unless getopts('nqh', \%opts);
usage if (%opts{'h'});
$warning=0 if (%opts{'n'});
$htmlblkquote=1 if (%opts{'q'});
usage unless (@ARGV);
my $buffer;
my $lc=1;
# Parse input as UTF-8
#
open (IN, "<:encoding(UTF-8)", $ARGV[0]);
while(<IN>) {
$buffer = $buffer.$_;
}
close IN;
my $json = JSON->new->allow_nonref;
my $hd = $json->decode( $buffer );
my $bl_hd = $hd->{'blocks'};
my $entitymap = $hd->{'entityMap'};
print "<!-- This file has been automatically generated by hem2md -->\n\n\n"
if $warning;
# Traverse the Blocks
#
my $endlist = 0;
for my $k (@$bl_hd) {
my $type = $k->{'type'};
my $text = $k->{'text'};
my @offset;
my @do;
my $idx = 0;
if ($text) {
my $idx = 0;
# Find inline styles offsets for bold and italic
#
my $inl = $k->{'inlineStyleRanges'};
for my $i (@$inl) {
my $i_style = $i->{'style'};
my $i_length = $i->{'length'};
my $i_offset = $i->{'offset'};
# Attempt to handle whitespace
#
my $segment = substr $text, $i_offset, $i_length;
$segment =~ s/^\s+//;
$i_offset = $i_offset + $i_length - length($segment);
$segment =~ s/\s+$//;
$i_length = length($segment);
$offset[$idx] = $i_offset ;
$offset[$idx+1] = $i_offset + $i_length;
if ($i_style eq 'ITALIC') {
$do[$idx] = "*";
$do[$idx+1] = "*";
} elsif ($i_style eq 'BOLD') {
$do[$idx] = "**";
$do[$idx+1] = "**";
} elsif ($i_style eq 'UNDERLINE') {
$do[$idx] = "____";
$do[$idx+1] = "____";
} elsif ($i_style eq 'CODE') {
warn "Code not implemented yet\n";
# Do nothing for now
$do[$idx] = "";
$do[$idx+1] = "";
} else {
warn "Unknown style $i_style!\n";
}
$idx++;
$idx++;
}
# Find Link offsets
#
my $entr = $k->{'entityRanges'};
for my $e (@$entr) {
my $e_key = $e->{'key'};
my $e_length = $e->{'length'};
my $e_offset = $e->{'offset'};
next unless $entitymap->{$e_key}->{'type'};
my $e_type = $entitymap->{$e_key}->{'type'};
$offset[$idx] = $e_offset ;
$offset[$idx+1] = $e_offset + $e_length;
if ($e_type eq 'LINK') {
my $link = $entitymap->{$e_key}->{'data'}->{'href'};
$do[$idx] = "[";
$do[$idx+1] = "]\($link\)";
} else {
warn "Unknown entity type $e_type\n";
}
$idx++;
$idx++;
}
# Go through the string byte by byte and insert formatting
#
my $newtext;
for (my $i = 0; $i <= length($text); $i++) {
my $byte="";
$byte = substr $text, $i, 1 if $i < length($text);
for (my $j = 0; $j<(@offset); $j++) {
$newtext = $newtext.$do[$j] if $offset[$j] == $i;
}
$newtext = $newtext.$byte;
}
$text = $newtext;
if ($type eq 'unordered-list-item') {
print "- " if $type eq 'unordered-list-item';
$endlist = 2;
} elsif ($type eq 'ordered-list-item') {
print "$lc".". ";
$lc++;
$endlist = 2;
} else {
# Reset counters
$lc=1;
$endlist = 1 if ($endlist == 2);
}
if ($endlist == 1) {
# Ensure there is a newline to break lists
# from other text types
print "\n";
$endlist = 0;
}
if ($type eq 'blockquote') {
print "$markua_bq " if $htmlblkquote == 0;
$text = "<blockquote>$text</blockquote>" if $htmlblkquote == 1;
}
print "# " if $type eq 'header-one';
print "## " if $type eq 'header-two';
print "### " if $type eq 'header-three';
}
print "$text\n";
}