summaryrefslogtreecommitdiffstats
path: root/convert.pl
diff options
context:
space:
mode:
Diffstat (limited to 'convert.pl')
-rwxr-xr-xconvert.pl100
1 files changed, 100 insertions, 0 deletions
diff --git a/convert.pl b/convert.pl
new file mode 100755
index 0000000..f34f339
--- /dev/null
+++ b/convert.pl
@@ -0,0 +1,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;