;;; 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*$//;
	    while ($nextctn =~ s/^\s*{(.*)}$/$1/) {}

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

	    $nextctn =~ s/\\\`(.)/&${1}grave;/g; 
            $nextctn =~ s/\\\'(.)/&${1}acute;/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>
   <perl>{
    <perl:assign $file><get-var file/></perl:assign>
    @file=@{bibparse($file)};

    <perl:print><table border=0></perl:print>
    my(%MONTH) = (
       jan => "January",
       feb => "February",
       mar => "March",
       apr => "April",
       may => "May",
       jun => "June",
       jul => "July",
       aug => "August",
       sep => "September",
       oct => "October",
       nov => "November",
       dec => "December",
    );
    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'};

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

      <perl:print>
       <tr><td valign="top">[<a href=\"#$entry{'name'}\">$entry{'name'}</a>] </td>
       <td>$entry{'author'}. <b>$entry{'title'}.</b> $where, $when.</td></tr>
       <tr><td></td></tr>
      </perl:print>
    }
    <perl:print></table></perl:print>
   }</perl>
  <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>;

      <perl:print>
        <a name="$entry{'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>$entry{'title'}.</b>
          </td>
          <td class="bibAttr" align=right>
      </perl:print>

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

      if ($entry{'where'} ne "") {
        <perl:print><small>  $entry{'where'}.</small></perl:print>
      }
      if ($entry{'year'} ne "") {
        <perl:print><small>  $entry{'year'}.</small></perl:print>
      }      

      <perl:print>;;; End of title line
     </td>
    </tr>
    <tr>
     <td class="bibCtn" colspan="3">
      by <i>$entry{'author'}</i>.<br>
      <table align=left border=0 cellspacing=5>
       <tr>
        <td>  </td>
        <td align=left>
       </perl:print>;;; end of title line

       ;;; Abstract
       if ("$entry{'abstract'}" ne "") {
         <perl:print>
 	   <p gettext=no align=justify><i>$entry{'abstract'}</i></p><br>\n
         </perl:print>
       }

       ;;; Resume
       if ("$entry{'resume'}" ne "") {
         <perl:print>
 	   <p gettext=no align=justify><i>$entry{'resume'}</i></p><br>\n
         </perl:print>
       }

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

       ;;; End of entry
       <perl:print> 
        $bibtex
        </td>
       </tr>
      </table>
     </td>
    </tr>
   </table>
  </td>
 </tr>
</table><br><br>
</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 name="<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>
        </td>
       </tr>
      </table>
     </td>
    </tr>
   </table>
  </td>
 </tr>
</table><br><br>
<restore title authors ps pdf ppt date note where name>
</define-tag>