-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathics_line_unfolder.pl
executable file
·65 lines (54 loc) · 1.76 KB
/
ics_line_unfolder.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
#!/usr/bin/env perl
#===============================================================================
#
# FILE: ics_line_unfolder.pl
#
# USAGE: ./ics_line_unfolder.pl FOLDED.ICS > UNFOLDED.ICS
# ./ics_line_unfolder.pl < FOLDED.ICS > UNFOLDED.ICS
#
# DESCRIPTION: Code which prints iCalendar files with their content lines
# unfolded. Lines starting with either a SPACE or HTAB character
# have that character removed and are concatenated to the
# preceding line.
# NOTICE: the lines in the output are terminated by CRLF sequences,
# as dictated by the iCalendar specifications.
#
# AUTHOR: Fulvio Scapin
# VERSION: 1.0.0
# CREATED: 2019-06-14
#===============================================================================
# Compact one-liner which should be roughly equivalent to running this script
#
# PERLIO=:crlf perl -C -wnE 'state$a;$a//($a=$_,next);s///,substr($a,(length$a)-1)=$_,(eof(ARGV)and print$a),next if /^[\t ]/;print $a;$a=$_;$a=undef,print if eof(ARGV);'
#
use strict;
use warnings;
use utf8;
# Decode INPUT / encode OUTPUT with a CRLF line ending as per iCalendar SPECS
use open qw/:std :encoding(utf8)/, IO => ":mmap :crlf";
binmode STDIN, ":mmap :crlf";
binmode STDOUT, ":crlf";
# Variable holding the previous line
my $hold;
while (<>) {
# First line of a file
if (not defined $hold ) {
$hold = $_;
next;
}
# On a continuation line
if ( m/^[\t ]/ ) {
s/^[\t ]//;
substr( $hold, ( length $hold ) - 1 ) = $_;
print $hold if eof(ARGV);
next;
}
print $hold;
$hold = $_;
# Last line of a file
if ( eof(ARGV) ) {
$hold = undef;
print;
next;
}
}