-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-draft-source.php
37 lines (36 loc) · 1.1 KB
/
extract-draft-source.php
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
<?php
/**
* Extracting source from draft-vos-silk-02.txt
* Contents neatened up a bit so I can programmatically acquire things
* Filenames found via grep -P '^A\.\d+\.' draft-vos-silk-02.txt
* interface/ src/ test/ need to be created cause yolo
*/
$src = fopen('draft-vos-silk-02.txt', 'rt');
$filename = '';
$contents = []; /* gonna explode() it into the file with EOLs */
do {
$line = fgets($src);
if (strlen(trim($line)) == 0) {
continue;
}
$line = rtrim($line, "\r\n"); /* Santitization */
/* Check if we've got a filename in the form of A.#.\s+\w+ */
if (preg_match('/^A\.\d+\.\s+(.*)/', $line, $matches) === 1) {
$fn = trim($matches[1]);
echo "Filename: " . $fn . "\n";
if ($filename == '') {
/* First filename */
$filename = $fn;
} else {
/* New filename. Write contents, clear things up, and then trudge along */
file_put_contents($filename, implode(PHP_EOL, $contents));
$filename = $fn;
$contents = [];
}
} else {
$contents[] = $line;
}
} while (!feof($src));
fclose($src);
/* At end of file just write last file */
file_put_contents($filename, implode(PHP_EOL, $contents));