;;; This file contains the needed macro to make a bibliography page. 
;;; Licence: GPL 
;;; Authors: Arnaud Legrand (idea, preliminary coding)
;;; Authors: Martin Quinson (proper, clean coding ;)

;;; This file contains two interface levels.

;;;;;;;;;;;;;;;;
;;; Higher level
;;;;;;;;;;;;;;;;

;;; <bibfile::summary file="????.bib">
;;;     Makes a sort of table of content, with clickable links to entries
;;; <bibfile::details file="????.bib" />
;;;     Actually make the entries.

;;; About the bibfile

;;; Be careful, my parser is really not robust. Please be kind and put one
;;; entry per line, no empty line inside the entry, exactly one empty line
;;; between entries, and so on.

;;; Each entry should be valid (ie, "@InProceding" should have "author",
;;;  "title", "booktitle" and "year"...). Make sure to give the key, too.

;;; In addition to the standard fields, you can/should add the following ones:
;;;  - "abstract": abstract in english
;;;  - "resume": abstract in french
;;;  - "ps", "pdf", "ppt": url where this doc can be downloaded
;;;  - "where": where it was submitted

;;;;;;;;;;;;;;;
;;; Lower level (now kinda deprecated)
;;;;;;;;;;;;;;;

;;; The squeleton of such an entry is:
;;; <bib (args)>
;;;   <bib::abstract>english abstract</bib::abstract>
;;;   <bib::resume>french abstract</bib::resume>
;;;   <bib::tex>BibTeX entry</bib::tex>
;;; </bib>

;;; The arguments of the bib tag are used to build a cute box for this entry.
;;; Here is the list:
;;;  title:   title of the paper
;;;  authors: authors of the paper
;;;  where:   where it was submitted
;;;  date:    when it was submitted
;;;  note:    any crappy text you want to put on the left of the title.
;;;           I use it to put a 'back to the top' link.
;;;  ps: pdf: ppt: the url of the document in the specified format
;;;  name:    Build an internal ancor with this name

;;;;;;;;;;;;;;;;;
;;; Perl function bibparse: parse a .bib file
;;;;;;;;;;;;;;;;;
<:
sub bibparse {
    my $file = shift;
    @entries=();
    $entrynb=0;
    open BIBHANDLE,"$file";
    
    foreach $entry ( split(/\n\n/,join("",<*BIBHANDLE*>)) ) {
      $entry =~ s/\n/§§/g;
      $entry =~ /\@(.*?){(.*)}/m;
      my $type=$1;
      my $entry=$2; $entry =~ s/§§/\n/g;
      ;;; Parse line
      $state=1;  ;;; 0 : wait for next block ; 1 : in block
      $nextname="name";
      $nextctn="";
      foreach $line (split(/\n/,$entry)) {
;;;        print "LINE:$line<br />\n";
	if ($state==0) { ;;; new block
	    $line =~ /^\s*(\S*)\s*=\s*(.*)$/;
	    $nextname = $1;
	    $nextctn = $2;
	    $nextname = lc($nextname);
	} else {
	    $nextctn .= $line;
	}
;;;	print "CTN=$nextctn=CTN<br />\n";
	$state=scalar (split(/\{/ , " $nextctn ")) - 
	       scalar (split(/\}/ , " $nextctn "));
	;;; Still in block if some '{' are open

;;;	print "STATE:$state<br />\n";
	if ($state==0) { ;;;done with this block
	    $nextctn =~ s/,\s*$//;
	    $nextctn =~ s/^\s*//;

	    $entries[$entrynb]{"bib$nextname"}="$nextctn";

	    while ($nextctn =~ s/^\s*{(.*)}$/$1/) {}
	    $nextctn =~ s/\\\`(.)/&${1}grave;/g; 
            $nextctn =~ s/\\\'(.)/&${1}acute;/g;
            $nextctn =~ s/\\,(.)/&${1}cedil;/g;
            $nextctn =~ s/\\\^(.)/&${1}circ;/g;
            $nextctn =~ s/{//g;
            $nextctn =~ s/}//g;
            $nextctn =~ s/[~]/ /g;

	    $entries[$entrynb]{"$nextname"}="$nextctn";
	}
      }
      $entries[$entrynb]{'author'} =~ s/ and /, /g; 
      $entries[$entrynb]{'author'} =~ s/, ([^,]*)$/ and $1/;
      $entries[$entrynb ++]{'type'}=$type;
    }
    close BIBHANDLE;
    return \@entries;
}
:>

;;;;;;;;;;;;;;;;;;;;;;;;;
;;; TAG: bibfile::summary
;;;;;;;;;;;;;;;;;;;;;;;;;

<define-tag bibfile::summary whitespace=delete>
  <preserve file /><set-var %attributes />
   <dl class="bibsummary">
   <perl>{
    <perl:assign $file><get-var file/></perl:assign>
    @file=@{bibparse($file)};

    my(%MONTH) = (
       jan => "Jan.",
       feb => "Febr.",
       mar => "March",
       apr => "April",
       may => "May",
       jun => "June",
       jul => "Jul.",
       aug => "Aug.",
       sep => "Sep.",
       oct => "Oct.",
       nov => "Nov.",
       dec => "Dec.",
    );
    foreach $ref (@file) {
      %entry = %{$ref};
      $where = $when = "";
      $where = $entry{'journal'}   if $entry{'journal'};
      $where = $entry{'booktitle'} if $entry{'booktitle'};
      $where = "Master thesis, $entry{'school'}" if $entry{'school'};
      $where = $entry{'note'} if (not $where and $entry{'type'} eq "Misc") ;

      $when = $entry{'month'};
      $when = lc $when; 
      $when = $MONTH{$when} or $entry{'month'};
      $when = "$when $entry{'year'}";

      <perl:print>
       <dt>[<a href=\"#$entry{'name'}\">$entry{'name'}</a>]</dt>
       <dd>$entry{'author'}. <b>$entry{'title'}.</b> $where, $when.</dd>
      </perl:print>
    }
   }</perl>
  </dl>
  <restore file />
</define-tag>

;;;;;;;;;;;;;;;;;;;;;;;;;
;;; TAG: bibfile::details
;;;;;;;;;;;;;;;;;;;;;;;;;
<define-tag bibfile::details whitespace=delete>
  <preserve file /><set-var %attributes />
    <perl>{
    <perl:assign $file><get-var file/></perl:assign>
    @file=@{bibparse($file)};

    foreach $ref (@file) {
      %entry = %{$ref};
;;;      foreach (keys %entry) {
;;;	  <perl:print>$_=DEBUT($entry{"$_"})FIN<br />\n</perl:print>
;;;      }
;;;      <perl:print><br /></perl:print>;

      my($formats)="";
      my($where)="";
      my($year)="";
      my($abstract)="";
      my($resume)="";

      # Output the existing format
;;;       my $first = 1;
       #my $debug = 1;
       foreach my $format (qw(ps pdf ppt)) {
         if (defined($entry{"$format"}) && $entry{"$format"} =~ m/\S/) {
;;;           if ($first) {
;;;             $first=0;
;;;             $formats = "(";
;;;           } else {
;;;             $formats .= ", ";
;;;           }
	   my($url)=$entry{"bib$format"};
	   $url=~ s/^{(.*)}$/\1/;
	   $url=~ s,\\~,~,g;
;;;           $formats .= "<a href=\"".$url."\">$format</a>";
           $formats .= "<a href=\"".$url."\">".
	   	"<img src=\"Icons/$format.gif\" ".
			"width=32 height=32 border=0 />".
		"</a>";
;;;           $res .= "  <a href=\"".$entry{"bib$format"}."\"><img src=\"Utils/Icons/$format.gif\" alt=\"$format\" width=32 height=32 border=0></a>";
         }
	 if ($debug) {
           $formats.="[format=$format=".$entry{"bib$format"}."]";
	 }
       }
;;;       if (!$first) {
;;;         $formats .= ")";
;;;       }

      if ($entry{'where'} ne "") {
        $where="<small>  ".$entry{'where'}.".</small>";
      }
      if ($entry{'year'} ne "") {
        $year="<small>  ".$entry{'year'}.".</small>";
      }      

       ;;; Abstract
       if ("$entry{'abstract'}" ne "") {
 	   $abstract='<p gettext="no" class="abstract">'.
	              $entry{'abstract'}.'</p>'."\n";
       } elsif ("$entry{'infoabstract'}" ne "") {
 	   $abstract='<p gettext="no" class="abstract">'.
	              $entry{'infoabstract'}.'</p>'."\n";
       }

       ;;; Resume
       if ("$entry{'resume'}" ne "") {
 	   $resume='<p gettext="no" class="abstract">'.
	            $entry{'resume'}.'</p>'."\n";
       } elsif ("$entry{'inforesume'}" ne "") {
 	   $resume='<p gettext="no" class="abstract">'.
	            $entry{'inforesume'}.'</p>'."\n";
       }

       ;;; BiBTeX
       my $fbegin='<font face=fixed>';
       my $fend='</font>';
       $bibtex="\@".$entry{'type'}."\{".$entry{'name'}.",\n";
       $bibtex .= "\n<dl class=\"bibline\">\n"; 
       @fields = qw(author institution organisation organization title school 
                    journal booktitle chapter pages 
                    crossref key edition editor publisher 
                    address note annote howpublished series number volume 
                    url month year);
#       print "XXXXXX ".$entry{'name'}."<br />\n";
       for ($i=0 ; $i < scalar @fields ; $i++) {
          $field=$fields[$i];
          if (defined ($entry{"$field"})){
#	    print "<dt>$field =</dt>\n";
            $bibtex.="<dt>$field</dt>";
            $bibtex.="<dd>= ".$entry{"bib$field"};
            $bibtex.=',' unless $field eq "year";
            $bibtex.="</dd>\n";
          }
#	  else { print "pas $field<br />\n";}
       }
       $bibtex .= "</dl>\}\n";

      <perl:print>
        <subframe title="$entry{'title'}" 
                  gettext="no" 
                  name="$entry{'name'}"
	          note="$formats $where $year">
          <div class="bibentry">
             <p gettext="no" class="author">$entry{'author'}</p>
	     $abstract
	     $resume
	     <div class="bibitem">
             $bibtex
             </div>
          </div>
        </subframe>
</perl:print>
    }

   }</perl>
  <restore file />
</define-tag>


;;;;;;;;;;;;;;;;;;;;
;;; TAG: bib::resume
;;;;;;;;;;;;;;;;;;;;

<define-tag bib::resume endtag=required whitespace=delete>
  <i>%body</i><br /><br />
</define-tag>

;;;;;;;;;;;;;;;;;;;;;;
;;; TAG: bib::abstract
;;;;;;;;;;;;;;;;;;;;;;

<define-tag bib::abstract endtag=required whitespace=delete>
  <i>%body</i><br /><br />
</define-tag>

;;;;;;;;;;;;;;;;;
;;; TAG: bib::tex
;;;;;;;;;;;;;;;;;

<define-tag bib::tex endtag=required whitespace=delete>
  <pre>
    <protect pass=3-9>%body</protect>
  </pre>
</define-tag>

;;;;;;;;;;;;
;;; TAG: bib
;;;;;;;;;;;;

<define-tag bib endtag=required whitespace=delete>
<preserve title authors ps pdf ppt date note where name />
<set-var %attributes />
   
<if <get-var name/> <a id="<get-var name />"></a> />
<table width="100%" border=0 cellspacing=0 cellpadding=0 class="navOutline">
 <tr>
  <td>
   <table width="100%" border="0" cellspacing="1" cellpadding="0">
    <tr class="content">
     <td class="bibTitle" valign=middle>
      <b><get-var title />.</b>
     </td>
     <td class="bibAttr" align=right>
<perl>
  # Output the existing format
  my $ps; <perl:assign $ctn{'ps'}> <get-var ps  /></perl:assign>
  my $pdf;<perl:assign $ctn{'pdf'}><get-var pdf /></perl:assign>
  my $ppt;<perl:assign $ctn{'ppt'}><get-var ppt /></perl:assign>

  my $first = 1;
  my $res;
  foreach my $format (qw(ps pdf ppt)) {
    if (defined($ctn{"$format"}) && $ctn{"$format"} =~ m/\S/) {
      if ($first) {
        $first=0;
        $res = "  (";
      } else {
        $res .= ", ";
      }
      $res .= "<a href=\"".$ctn{"$format"}."\">$format</a>";
    }
    print "[format=$format=".$ctn{"$format"}."]" if $debug;
  }
  if (!$first) {
    $res .= ")";
  }
  <perl:print>$res \n</perl:print>
</perl>

<if <get-var where />
 <small>  <get-var where />.</small>
/>
<if <get-var date />
 <small>  <get-var date />.</small>
/>

     </td>
    </tr>
    <tr>
     <td class="bibCtn" colspan="3">
      by <i><get-var authors /></i><br />
      <get-var note />
      <table align=left border=0 cellspacing=5>
       <tr>
        <td>  </td>
        <td align=left>
         <p gettext=no align=justify>
          <perl>{<perl:assign>%body</perl:assign>}</perl>
         </p>
        </td>
       </tr>
      </table>
     </td>
    </tr>
   </table>
  </td>
 </tr>
</table><br /><br />
<restore title authors ps pdf ppt date note where name />
</define-tag>