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
|
#!/usr/bin/env perl
use strict;
use warnings;
my $infile = $ARGV[0] or die "Usage: $0 infile";
my $line;
my $fh;
## Count prefix a specific character in the start of a string
## to know where in a list we are.
sub count_char {
my ($ch, $line) = @_;
my $i = 1;
while (substr($line, $i, 1) eq "$ch") {
$i += 1;
}
return $i;
}
## Generate a sequese of spaces for use in front of tables.
sub spaces {
my $count = shift;
my $spaces = "";
while ($count > 0) {
$spaces .= " ";
$count -= 1;
}
return $spaces;
}
## Handle unordered lists.
sub u_list {
my $line = shift;
return 0 unless (substr($line, 0, 1) eq '*');
my $len = count_char('*', $line);
print spaces($len) . "*" . substr($line, $len) . "\n";
return 1;
}
## Handle ordered lists.
sub o_list {
my $line = shift;
return 0 unless (substr($line, 0, 1) eq '#');
my $len = count_char('#', $line);
print spaces($len) . "1." . substr($line, $len) . "\n";
return 1;
}
## A very primitive aproach to table conversion,
## but it should work for pandoc output.
sub we_haz_tables {
print "||";
while (($line = <$fh>)) {
chomp $line;
if (substr($line, 0, 2) eq '|-') {
print "||\n";
} elsif (substr($line, 0, 2) eq '|}') {
print "||\n";
return;
} elsif (substr($line, 0, 1) eq '!') {
print "||" . substr($line, 1);
} elsif (substr($line, 0, 1) eq '|') {
print "||" . substr($line, 1);
} else {
print $line;
}
}
}
open $fh, "<", "$infile" or die $!;
while (($line = <$fh>)) {
chomp $line;
next if (u_list $line); # Unordered list
next if (o_list $line); # Ordered list
if (substr($line, 0, 2) eq '{|') {
we_haz_tables $line;
next;
}
print $line . "\n";
}
close $fh;
|