# -*- mode: Perl -*-
# /=====================================================================\ #
# |  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 warnings;
use LaTeXML::Package;

#======================================================================
# 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)), UnTeX(shift(@rawpairs))]); }
    AssignValue('BIBENTRY@' . NormalizeBibKey($key)
        => LaTeXML::Pre::BibTeX::Entry->new(ToString($type), $key, [@fields], [@fields]));
    Invocation(T_CS('\ProcessBibTeXEntry'), T_OTHER($key)); });

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

# ?
Let('\cites', '\cite');
#======================================================================
DefEnvironment('{bibdiv}',
  "<ltx:bibliography xml:id='#id' "
    . "bibstyle='#bibstyle' citestyle='#citestyle' sort='#sort'>"
    . "<ltx:title font='#titlefont' _force_font='true'>#title</ltx:title>"
    . "#body"
    . "</ltx:bibliography>",
  afterDigestBegin => sub { beginBibliography_clean($_[1]); Let('\par', '\relax'); });

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

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

DefConstructor('\MR{}',
  "<ltx:ref href='#href' class='ltx_mathreviews'>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;