~mwshinn/+junk/notes

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
#!/usr/bin/perl -w

use Regexp::Grammars;
use File::Basename;
$filename = $ARGV[0];
local $/ = undef;
open(FILE, $filename);
$content = <FILE>;
close(FILE);
$id = "";
do { 
    no Regexp::Grammars; 
    if ($filename =~ m/^.*?([^\/\\]+)\.([nr])$/) {
        $id = $1;
    }
    else {
        die "Invalid file name.";
    }
};
$path = (fileparse($filename))[1];
if (exists $ARGV[1]) {
    $onlyshow = $ARGV[1];
}
else {
    $onlyshow = "";
}
qr{
    <grammar: notes>
    <token: Paragraph>          <.bls> <lines> <.ble>
    <token: bls>               (?<=\n\n)
    <token: ble>               (?=\n\n)
    <token: partbls>           (?<=\n)
    <token: partble>           (?=\n)
    <token: line>              [^\n]+
    <token: lines>             (<[line]> \n)* <[line]>
    <token: Code>              <.bls> <optionallabel>? <.delimitcode> \n <codelines> \n <.delimitcode> <.ble>
    <token: codeline>          [^\n]*
    <token: codelines>         (<[codeline]> \n)*? <[codeline]>
    <token: delimitcode>       [\<]?===[\>]?

    <token: Label>             \<\< <linklabeltext> \>\>
    <token: optionallabel>     <Label> \n
    <token: linklabeltext>     [^\>]+

}xms;

$code = qr{ <extends: notes> <Code> }xms;
%sourcetexts = ();
$last = "";
while (1) {
    if ($content =~ s/$code//) {
        my $line = join("\n", @{$/{Code}->{codelines}->{codeline}}) . "\n";
        if (exists $/{Code}->{optionallabel}) {
            my $label = $/{Code}->{optionallabel}->{Label}->{linklabeltext};
            if (exists $sourcetexts{$label}) {
                $sourcetexts{$label} .= $line;
                $last = $label;
            }
            else {
                $sourcetexts{$label} = $line;
                $last = $label;
            }
        }
        elsif ($last ne "") {
            $sourcetexts{$last} .= $line;
        }
        else {
            $sourcetexts{$id} = $line;
            $last = $id;
        }
    }
    else {last;}
}
while(($k,$v)=each(%sourcetexts)) {
    open HANDLE, ">",  $path . "/" . $k;
    print HANDLE $v;
    close HANDLE;
    if ($onlyshow eq $k) {
        print $v;
    }
}