# -*- CPERL -*-
# /=====================================================================\ #
# |  amsrefs                                                            | #
# | Implementation for LaTeXML                                          | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #
package LaTeXML::Package::Pool;
use strict;
use LaTeXML::Package;
use LaTeXML::Util::KeyVal;

#======================================================================
# Leverage the BibTeX implementation
#======================================================================
RequirePackage('BibTeX',type=>'pool');

# \bib{key}{type}{keyval pairs}

DefParameterType('BibURL',sub {
  my($gullet)=@_;
  StartSemiverbatim();
  my @tokens = $gullet->readArg->unlist;
  EndSemiverbatim();
  my @cleaned = ();
  while(my $t = shift(@tokens)){
    if($t->equals(T_OTHER('%')) && @tokens && $tokens[0]->equals(Token("\n",CC_SPACE))){
      shift(@tokens); }
    else {
      push(@cleaned,$t); }}
  Tokens(T_BEGIN,@cleaned,T_END); },
  semiverbatim=>1);

DefKeyVal('amsrefs','url','BibURL');

# \bib{key}{type}{keyval-pairs}
DefMacro('\bib{}{} RequiredKeyVals:amsrefs', sub{
  my($gullet,$key,$type,$keyvals)=@_;
  $key = CleanBibKey(ToString($key));
  my @rawpairs = $keyvals->getPairs;
  my @fields;
  while(@rawpairs){
    push(@fields, [lc(shift(@rawpairs)),ToString(shift(@rawpairs))]); }
  AssignValue('BIBENTRY@'.$key=>LaTeXML::Bib::BibEntry->new(ToString($type),$key,@fields));
  Invocation(T_CS('\ProcessBibTeXEntry'),T_OTHER($key)); });

#======================================================================
# Someday may want to try to decipher, but ...
DefMacro('\BibSpec{}{}',Tokens());

# ?
Let(T_CS('\cites'),T_CS('\cite'));
#======================================================================
DefEnvironment('{bibdiv}',
	       "<ltx:bibliography xml:id='#id'>"
	       . "<ltx:title>#refname</ltx:title>"
	       . "#body"
	       ."</ltx:bibliography>",
	       afterDigestBegin=>sub { 
		 my $docid = ToString(Expand(T_CS('\thedocument@ID')));
		 ResetCounter('enumiv');
		 DefMacroI(T_CS('\thebibliography@ID'),undef,($docid ? "$docid.bib" : 'bib'));
		 $_[1]->setProperty(id=>ToString(Expand(T_CS('\thebibliography@ID'))));
		 $_[1]->setProperty(refname=>Digest(T_CS('\refname')));  
	       });

DefEnvironment('{biblist}',"<ltx:biblist>#body</ltx:biblist>");

#======================================================================
# Other Defns (presumably from textcmds?)

DefConstructor('\MR{}',
	       "<ltx:ref href='#href' class='mr'>MathReviews</ltx:ref>",
	       properties=>sub{
		 my $mr = ToString($_[1]);
		 # Patch up old-style MathReviews numbers
		 $mr =~ s/\s+\\\#\s*/:/;
		 (mr=>$mr, href=>"http://www.ams.org/mathscinet-getitem?mr=".$mr); });

DefConstructor('\ndash',"\x{2013}"); # EN DASH
DefConstructor('\mdash',"\x{2014}"); # EM DASH
#**********************************************************************
1;
