1 |
#!/usr/bin/perl -w |
2 |
|
3 |
my $debug = 1; |
4 |
|
5 |
my $in_dir = shift @ARGV || die "usage: $0 [in_dir] [st/]"; |
6 |
my $out_dir = shift @ARGV || 'st'; |
7 |
|
8 |
opendir(DIR, $in_dir) || die "can't opendir $in_dir: $!"; |
9 |
my @pages = grep { ! /^\./ && -f "$in_dir/$_" } readdir(DIR); |
10 |
closedir DIR; |
11 |
|
12 |
my $pages_re = '(' . join('|', @pages) . ')'; |
13 |
|
14 |
foreach $file (@pages) { |
15 |
warn "converting $file\n"; |
16 |
|
17 |
open(my $in, '<', "$in_dir/$file") || die "can't open $in_dir/file: $!"; |
18 |
open(my $out, '>', "$out_dir/$file") || die "can't write to $out_dir/$file: $!"; |
19 |
|
20 |
my $seen; |
21 |
|
22 |
while(<$in>) { |
23 |
# headings |
24 |
s/^(\s*)(=)\s*([^=]+.*?)\2*(.*)$/^ $3 $4/; |
25 |
s/^(\s*)(==)\s*([^=]+.*?)\2*(.*)$/^^ $3 $4/; |
26 |
s/^(\s*)(===)\s*([^=]+.*?)\2*(.*)$/^^^ $3 $4/; |
27 |
s/^(\s*)(====)\s*([^=]+.*?)\2*(.*)$/^^^^ $3 $4/; |
28 |
s/^(\s*)(=====)\s*([^=]+.*?)\2*(.*)$/^^^^^ $3 $4/; |
29 |
s/^(\s*)(======)\s*([^=]+.*?)\2*(.*)$/^^^^^^ $3 $4/; |
30 |
|
31 |
$seen->{vim} = not $seen->{vim} if (s/^\.vim/.pre/); |
32 |
$seen->{pre} = not $seen->{pre} if (m/^.pre/); |
33 |
next if ( $seen->{pre} && m/^filetype:/ ); |
34 |
|
35 |
if ( |
36 |
! $seen->{pre} && |
37 |
( ! /^\s+/ && $seen->{space} ) # end of space preformat |
38 |
|| |
39 |
( /^\s+\S+/ && ! $seen->{space} ) # begin of space preformat |
40 |
) { |
41 |
|
42 |
print $out ".pre\n"; |
43 |
warn "## PRE\n" if ($debug); |
44 |
$seen->{space} = not $seen->{space}; |
45 |
} |
46 |
|
47 |
# fixspace |
48 |
s/\[=(.+?)\]/`$1`/g; |
49 |
|
50 |
# italics, risky |
51 |
s!\s/([\w\s-]+)/[\s\.\!\,]! _$1_ !g; |
52 |
|
53 |
# links |
54 |
s!\[([\w\s]+)\s(http://.+?)\]!"$1"<$2>!g; |
55 |
s!\[(http://.+?)\s([\w\s]+)\]!"$1"<$2>!g; |
56 |
|
57 |
# link PagesWithCamelCaps |
58 |
s!$pages_re![$1]!g; |
59 |
|
60 |
warn join(",", grep { $seen->{$_} } keys %$seen), " ## $_" if ($debug); |
61 |
print $out $_; |
62 |
} |
63 |
|
64 |
print $out ".pre\n" if ($seen->{space}); |
65 |
|
66 |
close($out) || die "can't close $out_dir/$file: $!"; |
67 |
close($in); |
68 |
|
69 |
} |