-
Notifications
You must be signed in to change notification settings - Fork 0
/
txt2rsms.pl
executable file
·138 lines (101 loc) · 2.99 KB
/
txt2rsms.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
#!/usr/bin/perl
use strict;
use Getopt::Long;
use POSIX qw(strftime);
my $maxchar = 25;
# Verzeichniss fuer die Ausgabe der Files
my $outdir;
# Logbuch was eingelesen und verarbeitet wird
my $infile;
GetOptions ("outdir=s" => \$outdir, "infile=s" => \$infile);
if(! $infile || ! $outdir){
print STDOUT "Usage: ./txt2rsms.pl --infile <filename> --outdir <dirpath>\n";
exit 0;
}
if( ! -e $infile || -z $infile){
print STDERR "$infile ist leer oder nicht vorhanden, breche Verarbeitung ab.\n";
exit 1;
}
if( ! -e $outdir || ! -d $outdir){
print STDERR "$outdir ist kein Verzeichniss oder nicht vorhanden, breche Verarbeitung ab.\n";
exit 1;
}
# das Logbuch wird hier eingelesen:
my $logbuch;
open(IN, '<', $infile);
$logbuch = join("", <IN>);
close(IN);
my @tmp = split(/(\d\d\d\d \d\d \d\d)/, $logbuch);
shift @tmp if ($tmp[0] =~ /^\s*$/);
my %logbuch = @tmp;
while(my ($date, $post) = each %logbuch){
if($date =~ /^(\d\d\d\d) (\d\d) (\d\d)\s*$/){
$date = make_date($1, $2, $3);
} else {
print STDERR "unbekanntes Datumformat $date, Eintrag wird uebersprungen.\n";
next;
}
my @post = split(/\n/, $post);
shift @post if($post[0] =~ /^\s*$/);
my $filename = make_filename($date, make_filename_postfix($post[0]));
next if similar_file_exists($filename);
open(OUT, '>', $outdir.'/'.$filename);
print OUT "---\n";
print OUT "title: \"".$post[0]."\"\n";
print OUT "description: \"".$post[0]." ".$post[1]." ".$post[2]."\"\n";
print OUT "layout: post\n";
print OUT "tags: untagged\n";
print OUT "category: uncategorized\n";
print OUT "comments: no\n";
print OUT "---\n\n";
print OUT join("\n", @post);
close(OUT);
print STDOUT "file $filename geschrieben\n";
}
sub make_date {
my ($year, $mon, $day) = @_;
return $year.'-'.$mon.'-'.$day;
}
sub make_filename_postfix {
my ($line) = @_;
$line =~ s/[^a-zA-Z ]//g;
my @words = split(/ /, lc($line));
my @tmp;
foreach my $w (@words){
next if($w =~ /^\s*$/);
if(length(join('-', @tmp)) + length($w) + 1 <= $maxchar){
push @tmp, $w;
} else {
last;
}
}
return join('-', @tmp);
}
sub make_filename {
my ($date, $filename_postfix) = @_;
return $date .'-'.$filename_postfix.'.md';
}
sub today {
return strftime("%Y-%m-%d", localtime());
}
sub similar_file_exists {
my ($filename) = @_;
# I guess it would be nice to cache the file list at startup ..
opendir (my $dh, $outdir);
# no while loop, stay compatible with perl < 5.12
my @files = readdir($dh);
foreach (@files){
next if(! defined $_ || $_ =~ /^\./);
my ($date, $title) = $filename =~ /^(\d\d\d\d-\d\d-\d\d)(.*)$/;
my $today = today();
if($date eq $today && $_ eq $filename){
print STDOUT "$filename existiert bereits und stammt von heute, wird ueberschrieben.\n";
return 0;
} elsif($date ne $today && $_ eq $filename){
print STDOUT "$filename existiert bereits und ist mindestens 1 Tag alt, wird uebersprungen.\n";
return 1;
}
}
closedir($dh);
return 0;
}