Repository navigation
Expand file tree
/
Copy pathqbformat.pl
More file actions
99 lines (81 loc) · 2.43 KB
/
Copy pathqbformat.pl
File metadata and controls
99 lines (81 loc) · 2.43 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use open ':std', ':encoding(utf8)';
use File::Basename;
my ($h1, $h2, $h3, $h4) = (1, 1, 1, 1);
my @part1 = qw{十 二十 三十 四十 五十 六十 七十 八十 九十};
my @part2 = qw{一 二 三 四 五 六 七 八 九};
unshift(@part1, '');
unshift(@part2, '');
sub get_heading_text {
my ($arg) = @_;
my $quotient = int($arg / 10);
my $remainder = $arg % 10;
return $part1[$quotient] . $part2[$remainder];
}
sub process_file {
open my $rfh, '<:encoding(utf8)', @_;
my @lines = <$rfh>;
close $rfh;
my $wfilename = basename(@_, '.txt') . '_temp.txt';
open my $wfh, '>>', $wfilename;
for my $line (@lines) {
chomp($line);
$line =~ s/^\s+|\s+$//g;
# Ignore empty lines
next if ($line =~ m/^$/);
# Headings
if ($line =~ m/^#\s+/) {
print $wfh join('', $&, get_heading_text($h1), '、', "$'\n\n");
$h1++;
($h2, $h3, $h4) = (1, 1, 1);
next;
}
if ($line =~ m/^##\s+/) {
print $wfh join('', $&, '(', get_heading_text($h2), ')', "$'\n\n");
$h2++;
($h3, $h4) = (1, 1);
next;
}
if ($line =~ m/^###\s+/) {
print $wfh join('', $&, $h3, '.', "$'\n\n");
$h3++;
$h4 = 1;
next;
}
if ($line =~ m/^####\s+/) {
print $wfh join('', $&, '(', $h4, ')', "$'\n\n");
$h4++;
next;
}
# Attachments
if ($line =~ m/^######\s+附件:/) {
print $wfh "$line\n\n";
next;
}
if ($line =~ m/^######\s+(?!附件:)/) {
print $wfh join('', '######## ', "$'\n\n");
next;
}
print $wfh "$line\n\n";
}
close $wfh;
}
sub main() {
# Remove existing temp files
my @tempfiles = glob('*_temp.txt');
for my $tempfile (@tempfiles) {
unlink $tempfile or warn "Could not unlink file $tempfile: $!\n";
}
my @txtfiles = glob('*.txt');
for my $file (@txtfiles) {
($h1, $h2, $h3, $h4) = (1, 1, 1, 1);
process_file($file);
my $filename = basename($file, '.txt');
system("pandoc -o ${filename}.docx ${filename}_temp.txt --reference-doc t.docx");
unlink "${filename}_temp.txt" or warn "Could not unlink temp file ${filename}_temp.txt: $!\n";
}
}
main();