;;; This file contains the main canevas of each pages.
;;; License GPL.
;;; Authors: Arnaud Legrand, Martin Quinson

;;; You should define the tag <my-email> somewhere, because it's used here.
;;; For example, I've put the following in my customization.wml:
;;;  <define-tag my-email>Martin.Quinson@ens-lyon.fr</define-tag>

;;; You don't even have to load this file using a #include line, because
;;; banner do it for you.

;;;;;;;;;;;;;;;;;
;;; Misc tags ;;;
;;;;;;;;;;;;;;;;;

;;; <under_construction>: to mark a page as being under construction. be
;;;   carefull, it does not look that good, so use that only when actually
;;;   working on pages, for a few hours. 

;;; <date when="YYYY-MM-DD">: beautifull date output 

;;; directory where html source pages are generated
<define-tag sources-dir whitespace=delete>
<ifneq "$(SOURCES_DIR)" "." "$(SOURCES_DIR)/">
</define-tag>

;;;;;;;;;;;;;;;;;;;;
;;; Framing tags ;;;
;;;;;;;;;;;;;;;;;;;;

;;; This file also defines two tags to make beautiful frames:
;;;  <frame> make a major frame on the page
;;;  <subframe> have other colors.

;;; Both take the following arguments:
;;;  title: 
;;;  note:  crappy text added to the right of the title
;;;  name:  adds an internal html ancor. So, you can build links directly to 
;;;         this frame (using the "#ancor" form).
;;;  toc="no": Don't register this frame to the TOC (see below)
;;;  expend="no": don't put spaces between letters in title

;;;;;;;;;;;;;;;;
;;; TOC tags ;;; frames and subframes may be used to build a TOC. 
;;;;;;;;;;;;;;;;

;;;  The compilation mecanism is quite the same than for LaTeX. You compile
;;;     your code one time, it builds a "aux" file containing the needed
;;;     info, and you recompile it to get the TOC right. 

;;;  Needed compilation args: -oAUX:.$(FILE).aux -oUNDEF:$(FILE).html

;;; <add-toc-entry ...>: add an entry to the toc. Args:
;;;    title: text to display
;;;    name: of the ancor (which will be placed automatically). If ommited,
;;;          a default value is computed
;;;    lvl: level. 1=>frame, 2=>subframe.

;;; <display-toc> 
;;;    file: basename of this wml file
;;;    maxlevel: maximal deep displayed

;;;  How does it works: man slice(1)

;;;;;;;;;;;;
;;; TODO ;;;
;;;;;;;;;;;;
;;; There is a lot of common code in the frame, subframe, and bib tags.
;;;       we should make an 'ancestor' to share it.

#use wml::std::tags

<set-var toc:_cnt=0 /> ;;; for automatic ancors
;;;;;;;;;;;;;;;;;;;;;
;;; tag add-toc-entry
;;;;;;;;;;;;;;;;;;;;;
<define-tag add-toc-entry>
  <preserve name title lvl /><set-var %attributes>
     <ifeq <get-var name /> "" 
       <group <increment toc:_cnt />
              <set-var name="l<get-var lvl />.<get-var toc:_cnt />" />
        />
     />
     <a name="<get-var name />"></a>
     [AUX:TOC##<get-var lvl />##<get-var name />##<get-var title />
:]
  <restore name title lvl />
</define-tag>

;;;;;;;;;;;;;;;;;;;
;;; tag display-toc
;;;;;;;;;;;;;;;;;;;
<define-tag display-toc>
  <preserve file maxlevel toc line parts>
  <set-var %attributes>

  ;;; test the args
  <ifeq <get-var file /> "" 
    <compound>
      <warning "Argument 'file' mandatory in tag 'display-toc'">
      <exit 1>
    </compound>
  />
  <ifeq <get-var maxlevel /> "" 
    <compound>
      <warning "Argument 'maxlevel' mandatory in tag 'display-toc'">
      <exit 1>
    </compound>
  />
  ;;; increment maxlevel because builtin gt is strict comparaison.
  <increment maxlevel>

  ;;; reads the aux file
  <if <file-exists ".<get-var file />.aux" />
      <set-var toc="<include ".<get-var file />.aux" />" />
   />

  ;;; foreach line of the aux file
  <foreach line toc>
     <set-var parts="<subst-in-string <get-var line /> "##" "\n" />" />
     <ifeq <get-var parts[0] /> "TOC"
      <compound> 
       ;;;maxlevel=<get-var maxlevel /> ; curlevel=<get-var parts[1] /> <br>
       <if <gt <get-var maxlevel /> <get-var parts[1] /> />
        <compound>
         ;;; put some spaces
	 <set-var i=<get-var parts[1] /> />
         <while <gt <get-var i /> 1 />>   <decrement i /></while>
         ;;; put the entry
         <a href="#<get-var parts[2] />"><get-var parts[3] /></a><br>;;;
        </compound>
       />
      </compound>
     />
  </foreach>
  <restore file maxlevel toc line parts>
</define-tag>
;;;;;;;;;;;;;
;;; tag frame
;;;;;;;;;;;;;

<define-tag frame endtag=required>
    <table width="100%" border=0 cellspacing=0 cellpadding=0 class="outline">
      <tr><td>
       <preserve title note name toc expend><set-var %attributes>

;;; Take care of the TOC and ancors
       <ifneq <get-var toc /> "no"  
         ;;; then
	 <add-toc-entry title="<get-var title />"  
                        name="<get-var name />" lvl="1">
         ;;; else
         <if <get-var name /> 
            <group <a name="<get-var name />"></a> />
         />
       />


	  <table width="100%" border="0" cellspacing="1" cellpadding="3">
     <perl>{
       my $tit;<perl:assign $tit><_><get-var title /></_></perl:assign>;
       my $not;<perl:assign $not><get-var note /></perl:assign>;
       my $expend;<perl:assign $expend><get-var expend /></perl:assign>
       $expend = $expend || "yes";
       if (  (defined($tit) && $tit =~ /\S/)
           ||(defined($not) && $not =~ /\S/)) {
       $string = "
	    <tr>
	      <td class=\"leftTitle\" nowrap>
		<b>
		  <font class=\"leftTitle\">".
       ($expend == "no" ? $tit : isotitlestring ($tit))."
		  </font>
		</b>
	     </td>
	     <td class=\"rightTitle\" width=\"100%\" align=right><span>".($not||" ").
	"</span></td>
	    </tr>";
       <perl:print>$string</perl:print>
       }
     }</perl>
     <restore title note name toc expend>
	    <tr class="content">
	      <td align="left" valign="top" colspan="2" class="content">
                <perl>{<perl:assign>%body</perl:assign>}</perl>
	      </td>
	    </tr>
	  </table>
	</td>
      </tr>
    </table><br>
</define-tag>

;;;;;;;;;;;;;;;;
;;; tag subframe
;;;;;;;;;;;;;;;;

<define-tag subframe endtag=required>
  <preserve title note name toc expend><set-var %attributes>
    <table width="100%" border=0 cellspacing=0 cellpadding=0 class="content">
    <tr>
      <td class="content" width="1">  </td>
      <td>

;;; Take care of the TOC and ancors
       <ifneq <get-var toc /> "no"  
         ;;; then
	 <add-toc-entry title="<get-var title />"  
                        name="<get-var name />" lvl="2">
         ;;; else
         <if <get-var name /> 
            <group <a name="<get-var name />"></a> />
         />
       />
	
      <frame title="<get-var title />" 
             note="<get-var note />" toc="no">%body</td>
     </tr>
    </table>
  <restore title note name toc expend>  
</define-tag>

<perl>
sub isotitlestring {
  my ($str) = @_;

  $str =~ s/ /_/g;
  $str =~ s/(\S)/$1~/g;
  $str =~ s/_/ /g;
  $str =~ s/ / /g;
  $str =~ s/~/ /g;
  return $str;
}

sub isotime {
  my ($time) = @_;

  my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($time);
  my ($str) = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
                      $year+1900, $mon+1, $mday, $hour, $min, $sec);
  return $str;
}


</perl>

;;;
;;; Tag under_construction 
;;;
<define-tag under_construction endtag=none>
 <subframe title="UNDER CONSTRUCTION">
   <font color=red size=+4>I'm working on this page !! Come back soon !</font>
 </subframe>
</define-tag>

;;;
;;; Tag date
;;;
<define-tag date endtag=none>
  <preserve when>
  <set-var %attributes>
  
  <perl>
    my $str;<perl:assign $str><get-var when /></perl:assign>;
    if ($str =~ m/([0-9]{4})-([0-9]{2})-([0-9]{2})/) {
	$str = "$3-$2-$1";
	$str =~ s|-|/|g;
       <perl:print>$str</perl:print>
    }
  </perl>
  <restore when>
</define-tag>