-------------------------------------------------------- #! /usr/pkg/bin/perl -wi use strict; #margemain.pl - outputs an html file for an annotated page # Outputs a table with three columns - the margin, a load of a's and >'s, # and the page proper # Works line by line from the .marge file passed to it as "pageroot". #***CONFIGURATION*** my $god = "god"; #give a "user=$god" cookie for super-special powers #(deletion, creation, editing) my $defaultformatting = "bgcolor='#202000' text='#3080FF' link='#802020' vlink='#FF1010'"; #set default colours here (used if the page hasn't set any). #Could also use to insert other HTML just after the body tag, #if you so wish. my $nologin = 0; #set this to 1 if you don't want a link to a login.marge page use CGI qw/:standard/; my $pageroot = param(pageroot); #the root page name, #so $pageroot.marge exists my $pagepath = param('pagepath'); #empty for root page, or something like -4-6 #for an annotation (-4-6 would be an annotation on the sixth #line of a note to the 4th line of the $pageroot page). my $message = param('message'); #pass message=text to have it printed at the top. my $user = cookie(-name=>'user'); my $cloaked = cookie(-name=>'cloaked'); print "Content-type: text/html\n\n"; #***A simple-minded per-page hit counter*** if ($cloaked==0){ my $count; open(COUNTIN, "$pageroot.count") or $count=0; flock(COUNTIN,2); seek(COUNTIN,0,0); $count= + 1; close(COUNTIN); open(COUNTOUT, ">$pageroot.count"); flock(COUNTOUT,2); seek(COUNTOUT,0,0); print COUNTOUT "$count"; close(COUNTOUT); } #***Reading .marge file*** my @page; open(MARGE, "$pageroot.marge") or die("Can't open $pageroot.marge"); flock(MARGE,2); seek(MARGE,0,0); @page = ; close(MARGE); #***Basic processing of and reading off variables from .marge file foreach (@page[ 0 .. $#page]) { s/\\n|\\p//g } #make disappear the \n&\p my $lines = $page[0]; # first line should give the number of lines of the rest my $notewidth = $page[1]; #then width of notes in characters my $longname = $page[2]; #page title my $formatting = $page[3]; #stuff in body tag my $mainwidth = $page[4]; #main page width my $note = 0; my($pagestart,$pagelength,$notes,@afline,@ano,@apline,@anotes); for (my $i=0;$i<=$lines;$i++) {my $line = $page[$i]; if ("$line" eq "***$pageroot$pagepath***\n") #make sure parent always comes before child in .marge file { $pagestart = $i+3; #not the *** line nor the ano line nor the notes $pagelength = $page[$i+1]; $notes = $page[$i+2]; $afline[0] = $notes; #afline is line in file annotation starts on (from 0) $ano[0] = $notes; #ano is number of lines in note, given as first line $apline[0] = $notes; #apline is line in page annotation starts on (from 1) $anotes[0] = $notes; #no of annotations the annotation has $i+=2; } if ("$line" =~ /\*\*\*$pageroot$pagepath-(\d+)\*\*\*\n/) { $note +=1; $afline[$note] = $i+3; #not the *** line nor the ano line nor the notes $ano[$note] = $page[$i+1]; $apline[$note] = $1; $anotes[$note] = $page[$i+2]; $i+=2; } } #***print header & heading*** print "marge - $pageroot$pagepath"; if ($formatting =="") {$formatting=$defaultformatting;} print ""; my $upnote; if ($pagepath == ""){ print "

$longname

";} else { print "

An annotation to $longname

"; $upnote = $pagepath; while (chop($upnote) ne "-"){;} print "back up";} if ("$user" eq "$god") { #print "
godly instructions"; print "
edit"; print "
create"; } else { #If you're going to have multiple gods, you might want to make a #login page which passes a cookie "user=$god". Else set $nologin=1. if ($nologin==0) {print "
login";} } if ($message ne '') {print "
$message";} if ("$user" eq "$god" && $notes==0 && "$pagepath" ne "") { print "
delete(!)"; } print "

"; print ""; print ""; #***print the "a"'s (mid page) and*** #***printing the main page (RHS)*** #The mid page gets printed directly, and simulatenously the RHS gets put into #$maintext, which is printed afterwards. print ""; print ""; print "
"; #the margin #***Sorting the margin*** my @start; $start[0] = $notes; #line to start printing from (from 1) #starts with everything central, and goes down expanding each up and down #until all are blocked. my @end; $end[0] = $notes; my $up=0; my $blocked=0; for ($note=1;$note<=$notes;$note++) {$start[$note]=$apline[$note]; $end[$note]=$apline[$note];} while ($blocked != 2) {$blocked++; for ($note=1;$note<=$notes;$note++) {#print "$note: start=$start[$note], end=$end[$note], apl=$apline[$note]"; #Some rather messy conditionals - they work, though. if ($up==0) {if (($end[$note]-$apline[$note])<=($apline[$note]-$start[$note]) && ($end[$note]-$start[$note]+1)<$ano[$note] && (($note != $notes && $end[$note]<($start[$note+1]-2)) || ($note == $notes && $end[$note]<($pagelength)))) {$end[$note]++;$blocked=0;}} else {if (($end[$note]-$apline[$note])>=($apline[$note]-$start[$note]) && ($end[$note]-$start[$note]+1)<$ano[$note] && (($note != 1 && $start[$note]>($end[$note-1]+2)) || ($note == 1 && $start[$note]>1))) {$start[$note]--;$blocked=0;} } } $up = 1-$up; } #***Printing the margin*** $note=1; my $nobreak; my $filelineno; for (my $i=1;(($i<=$pagelength) && ($note <= $notes)); $i++) # $i goes down the main page itself, line by line {if ($nobreak==0) {print " \;";} $nobreak=0; if ($i == $start[$note]) { $filelineno = $afline[$note]; #filelineno is line of file we're on if ($anotes[$note] == 0) {print "";} #if unannotated, print in italics # print ""; } if ($i >= $start[$note] && $i < $end[$note]) {my $line = $page[$filelineno++]; if ("$line" =~ /\*\\\s*$/) { #end a line with *\ to prevent breaking #(This is a leftover from a previous approach and #shouldn't be needed any more, but is left in just in #case). $nobreak=1; $line =~ s/\*\\\s*/ /; } print "$line"; } if ($i == $end[$note]) {#end of note my $line = $page[$filelineno++]; if (($end[$note]-$start[$note]+1)<$ano[$note]) { #chop $line; chop $line; chop $line; print "$line..."; } else {print "$line";} if ($anotes[$note]==0) {print "";} #print ""; $note++; } if ($nobreak==0) {print "
";} } print "
"; my $maintext = ''; $filelineno = $pagestart; my $line = $page[$filelineno++]; my $i=1; while ($line !~ /^\*\*\*/) { #Provision for simple includes - one-line only, only one per line #Syntax - e.g. #i/marge/poll.dat#/i if ("$line" =~ /\#i(.+)\#\/i/) {open(INCLUDE, "$1"); my $include =; $line =~ s/\#i.+\#\/i/$include/; close(INCLUDE); } my $test=0; for (my $j=1;$j<=$notes;$j++) {if ($i == $apline[$j]) { #A note is centred on this line $test=1;}} if ($test == 0) { if ($nobreak==0) {print " a
";} $nobreak=0; if ($line =~ /\*\\\s*$/) {$nobreak=1; $line =~ s/\*\\\s*/ /;} #Again, this whole $nobreak and *\ stuff should be ignored $maintext = $maintext."$line"; if ($nobreak==0) {$maintext=$maintext."
";} } else { if ($nobreak==0) {print " >
";} $nobreak=0; if ($line =~ /\*\\\s*$/) {$nobreak=1; $line =~ s/\*\\\s*/ /;} $maintext = $maintext."$line"; if ($nobreak==0) {$maintext=$maintext."
";} } $line = $page[$filelineno++]; if ($nobreak==0) {$i++;} } print "
$maintext
"; --------------------------------------------------------- #! /usr/pkg/bin/perl -w #margeannotate.pl - outputs a form for annotation use strict; #***CONFIGURE*** my $defaultformatting = "bgcolor='#202000' text='#3080FF' link='#802020' vlink='#FF1010'"; use CGI qw/:standard/; my $pageroot = param('pageroot'); #the root page name, #so $pageroot.marge exists my $pagepath = param('pagepath'); #empty for root page, or something like -4-6 my $user = cookie(-name=>'user'); my $noteline = param('line'); my $width = param('width'); print "Content-type: text/html\n\n"; print "Annotating $pageroot$pagepath around line $noteline"; print ""; print "

Annotating

"; print "

(Annotating the following piece of text - the line in italics is the particular line your annotation will be centred on)

"; open(MARGE, "$pageroot.marge"); flock(MARGE,2); seek(MARGE,0,0); my $lines = ; my ($pagestart, $pagelength); for (my $i=1;$i<=$lines;$i++) {my $line = ; if ("$line" eq "***$pageroot$pagepath***\n") #make sure parent always comes before child in .marge file { $pagestart = $i+3; #not the *** line nor the ano line nor the notes $pagelength = ; ; $line = ; $i+=3; } #Prints some context lines from the annotated text around the line annotated my $diminish=0; if ("$line" =~ /\*\\\s*$/) {$diminish=1; $line =~ s/\*\\\s*/ /;} if ($pagestart !=0 && $i+1-$pagestart >= $noteline-2) { $line =~ s/\\n|\\p//g; if ($i+1-$pagestart == $noteline) {print "$line
";} else {print "$line
";}} if ($pagestart !=0 && ($i+1-$pagestart >= $noteline+2 || $i >= $pagestart+$pagelength-1)) {last;} if ($diminish == 1) {$i--;} } close(MARGE); print <

(Type your annotation into the box below, then click the Annotate! button)

EOF --------------------------------------------------------- #! /usr/pkg/bin/perl -w #margedonote.pl - messes with the .marge file for the # addition and deletion of notes and for editing stuff use strict; #***CONFIGURATION*** my $god = "god"; #As in margemain.pl use CGI qw/:standard/; my $pageroot = param('pageroot'); #the root page name, #so $pageroot.marge exists my $pagepath = param('pagepath'); #empty for root page, or something like -4-6 my $noteline = param('line'); my $user; my $user = cookie(-name=>'user'); my $mode = param('mode'); if ("$mode" ne "delete" && "$mode" ne "edit" && "$mode" ne "create") {$mode = "add";} my $text = param('text'); my $width = param('width'); if ($mode eq "add") {$text =~ s//>/g;} #Disallow all HTML in notes if ($mode eq "create" && param('convhtml') eq "yes") { $text =~ s/\/\\n/g; $text =~ s/\/\\p/g; } $text =~ s/[\n\r]//g; #Kills all new line characters $text =~ s/\s+/ /g; #Cuts down extended white space #$text =~ s/\+\\\s*\n/ /g; # +\ stops wrapping at all $text =~ s/\*\>/  /g; #*> becomes 2 spaces $text =~ s/\t/\ \;\ \;\ \;\ \;\ \;/g; #tabs become 5 spaces $text =~ s/\*([^\*]*)\*/$1<\/b>/g; #*x* bold $text =~ s/\+([^\+]*)\+/$1<\/i>/g; #+x+ italics $text =~ s/\[([^\s\|]*)\]/$1<\/a>/g; #[x] $text =~ s/\[([^\s\|]*)\|([^\n\|]*)\]/$2<\/a>/g; #[x|y] $text =~ s/\{([^\s\|]*)\}/$1<\/a>/g; #{x} $text =~ s/\{([^\s\|]*)\|([^\n\|]*)\}/$2<\/a>/g; #{x|y} #***do custom wrapping*** my $remaining=$width; my $intag=0; my $lastspace=-1; for (my $pos=0;$pos<=length($text);$pos++) { my $char = substr($text, $pos, 1); if ($char eq "<") {$intag=1;} if ($intag==0) {$remaining--;} if ($char eq ">") {$intag=0;} if ($char eq " " && $intag==0) {$lastspace=$pos;} if ($remaining==0) { if ($lastspace==-1) {$lastspace=$pos;} $text = substr($text, 0, $lastspace+1)."\n".substr($text,$lastspace+1); $pos=$lastspace+2; $lastspace=-1; $remaining=$width; } if ($char eq "\\") { my $nextchar = substr($text,$pos+1,1); if (($nextchar eq "n" && $remaining != $width-1) || ($nextchar eq "p" && $remaining==$width-1)) { $text = substr($text, 0, $pos)."\n".substr($text,$pos); $pos+=2; $remaining=$width; } if ($nextchar eq "p" && $remaining!=$width-1) { $text = substr($text, 0, $pos)."\n\n".substr($text,$pos); $pos+=3; $remaining=$width; } } } #$text = $text."\n"; my $textlines = $text =~ tr/\n/\n/; $textlines++; # print "Content-type: text/html\n\n"; open(MARGE, "$pageroot.marge") or die("Can't open $pageroot.marge for reading"); flock(MARGE,2); seek(MARGE,0,0); my @page = ; close(MARGE); #***Annotation*** my $line; if ($mode eq "add") {my $max=0; foreach $line (@page) { chomp($line); if ($line =~ /\*\*\*$pageroot$pagepath-(\d+)\*\*\*/) {if ($1<$noteline && $1>$max) {$max=$1;} } } open(MARGE, ">$pageroot.marge") or die("Can't open $pageroot.marge for writing"); flock(MARGE,2); seek(MARGE,0,0); my $count = 0; my $watch=0; my $location; foreach $line (@page) { $count++; chomp($line); if ($watch==1 && $line =~ /\*\*\*$pageroot$pagepath-(\d+)\*\*\*/) {$location = $count-1; $watch=0;} if (($max != 0 && $line eq "***$pageroot$pagepath-$max***") || ($max == 0 && $line eq "***$pageroot$pagepath***")) {$watch=1;} if ($line eq "***end***" && $location == 0) {$location = $count-1;} } my $firstline=1; my $weirdify=0; $count=0; foreach $line (@page) { $count++; chomp($line); if ($firstline == 1) {$line=($line+4+$textlines); $firstline=0;} if ($weirdify == 1) {$line++; $weirdify = 0;} if ($weirdify == 2) {$weirdify = 1;} if ($line eq "***$pageroot$pagepath***") {$weirdify = 2;} print MARGE "$line\n"; if ($count==$location) {print MARGE "***$pageroot$pagepath-$noteline***"; print MARGE "\n$textlines"; print MARGE "\n0"; print MARGE "\n$text\n";} } close(MARGE); # print "click here"; print "Location: margemain.pl?pageroot=$pageroot&pagepath=$pagepath&message=Your annotation has been successfully made. Huzzah, eh?\n\n"; } #***Note deletion*** #(Can only prune from the outside in - #i.e., only unannotated annotations may be deleted. if ($mode eq "delete" && $user eq "$god"){ open(MARGE, ">$pageroot.marge") or die("Can't open $pageroot.marge for writing"); flock(MARGE,2); seek(MARGE,0,0); my $weirdify =0; my $writeoff =0; my $upnote = $pagepath; my $line; while (chop($upnote) ne "-") {;} foreach $line (@page){ chomp($line); if ($weirdify == 1) {$line--; $weirdify = 0;} if ($weirdify == 2) {$weirdify--;} if ($line eq "***$pageroot$upnote***") {$weirdify = 2;} if ($writeoff == 1 && "$line" =~ /^\*\*\*/) {$writeoff = 0;} if ($line eq "***$pageroot$pagepath***") {$writeoff = 1;} if ($writeoff == 0) {print MARGE "$line\n";} } close(MARGE); # print "click here"; print "Location: margemain.pl?pageroot=$pageroot&pagepath=$upnote&message=You just lopped off a bit of this site. Hope you know what you're doing, and mean well.\n\n"; } #***Editing a page or note*** if ($mode eq "edit" && $user eq "$god") { open(MARGE, ">$pageroot.marge") or die("Can't open $pageroot.marge for writing"); flock(MARGE,2); seek(MARGE,0,0); my $countdown = -1; my $writeoff=0; my $line; foreach $line (@page){ chomp($line); if ($countdown == 2) {$line = $textlines;} if ($countdown == 0) {$writeoff=1; print MARGE "$text\n";} if ($countdown >= 0) {$countdown--;} if ($line eq "***$pageroot$pagepath***") {$countdown=2;} if ($writeoff==1 && "$line" =~ /^\*\*\*/) {$writeoff=0;} if ($writeoff==0) {print MARGE "$line\n";} } close(MARGE); print "Location: margemain.pl?pageroot=$pageroot&pagepath=$pagepath&message=I am not what I used to be\n\n"; } #***Creating a new page*** if ($mode eq "create" && "$user" eq "$god") { my $filelines=$textlines+11; #9 needed for stuff, and a couple extra just in case my $filename = param('createfilename'); my $notewidth = param('createnotewidth'); my $title = param('createtitle'); my $formatting = param('createformatting'); open(MARGE, ">$filename.marge") or die("Can't open $pageroot.marge for writing"); print MARGE "$filelines\n"; print MARGE "$notewidth\n"; print MARGE "$title\n"; print MARGE "$formatting\n"; print MARGE "$width\n"; print MARGE "***$filename***\n"; print MARGE "$textlines\n"; print MARGE "0\n"; print MARGE "$text\n"; print MARGE "***end***\n"; close(MARGE); # print "Content-type: text/html\n\n"; #print "The margification was a success."; print "Location: margemain.pl?pageroot=$filename&message=You made this\n\n"; } --------------------------------------------------------- #! /usr/pkg/bin/perl -w #margeedit.pl - provides a form for editing a page/note use strict; #***CONFIGURATION*** my $god = "god"; #As in margemain.pl #textarea dimensions my $width=60; my $height=40; use CGI qw/:standard/; my $pageroot = param('pageroot'); my $pagepath = param('pagepath'); my $user = cookie(-name=>'user'); print "Content-type: text/html\n\n"; if ("$user" ne "$god") {print "Sorry, only gods have editorial powers.\n";} else { my $physwidth; if ($pagepath eq "") {$physwidth = param('mainwidth');} else {$physwidth = param('notewidth');} print <marge - editing $pageroot$pagepath

Editing $pageroot$pagepath

Annotations will remain on the same physical lines they did before the edit, so any significant editing could make things go poo-poo (as they say). You have been warned.
EOF open(MARGE, "$pageroot.marge") or die("Couldn't open $pageroot.marge, which is bad."); flock(MARGE,2); seek(MARGE,0,0); my @page = ; close(MARGE); #$countdown=0; #foreach $line (@page) { # chomp($line); # if ($countdown==1) {$countdown=0; $height=$line*2;} # if ("$line" eq "***$pageroot$pagepath***") {$countdown=1;} #} print " EOF } --------------------------------------------------------- #! /usr/pkg/bin/perl -w #margificator.pl - Outputs a form for page creation use strict; #***CONFIGURATION*** my $god = "god"; #As in margemain.pl #textarea size - purely aesthetic my $height=40; my $width=60; use CGI qw/:standard/; my $user = cookie(-name=>'user'); print "Content-type: text/html\n\n"; if ($user ne "$god") { print "Sorry, but mortals such as yourself may not be trusted with the secrets of margification.";} else { print <margificator

Margificator V1.0

Please read the instructions before using this.

Page title -

Filename - .marge

Page width -

Note width -

Formatting - <BODY >

Page text -

Convert HTML?

EOF } ---------------------------------------------------------