# -*- mode: Perl -*-
# /=====================================================================\ #
# |  glossaries                                                         | #
# | 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;

InputDefinitions('glossaries', type => 'sty', noltxml => 1);
RequirePackage('xspace');

DefMacro('\glsnoidxstripaccents', '');    # Silence pointless warnings

#======================================================================
# The various \gls{label},... macros eventually call \@gls@link to typeset the term
# We'll override it to wrap the results in ltx:glossaryref
Let('\lx@orig@glossaries@gls@link', '\@gls@link');
DefMacro('\@gls@link[]{}{}',
  '\lx@glossaries@gls@link{\csname glo@#2@type\endcsname}{#2}{'
    . '\lx@orig@glossaries@gls@link[#1]{#2}{#3}}');
DefConstructor('\lx@glossaries@gls@link{}{}{}',
  "<ltx:glossaryref inlist='#list' key='#2'>#3</ltx:glossaryref>",
  properties => sub {
    my $list = ToString($_[1]);
    $list = 'main' unless $_[1];
    (list => $list); });

# Skip over hyperref links, since we'll be handling it.
DefMacro('\glsdohyperlink{}{}',   '#2');
DefMacro('\glsdonohyperlink{}{}', '#2');
RawTeX('\glsdisablehyper');

# This seems necessary, although it ought to be built in???
DefMacro('\glspostlinkhook', '\xspace');

#======================================================================
# The various glossary and acronym defining macros
#   \newglossaryentry { label }{ kv }
#   \longnewglossaryentry { label }{ kv }
#   \newacronym { short }{ long }
# ultimately call \gls@defglossaryentry, # which normalizes and completes the various bits
#of data and then calls \@newglossaryentryposthook
# We'll use that hook to record the data in the Document.
# Note that there's some redundancy of keywords,
# in particular w.r.t acronym vs glossary use cases....
#DefMacro('\@newglossaryentryprehook', '\let\SS\relax');
DefMacro('\@newglossaryentryposthook', <<'EoTeX');
\lx@glossaries@newentry{\@glo@type}{\glslabel}{
%label=\glslabel,
name=\@glo@name,
description=\@glo@desc,
%descriptionpl=\@glo@descplural,
%type=\@glo@type,
symbol=\@glo@symbol,
symbolplural=\@glo@symbolplural,
text=\@glo@text,
plural=\@glo@plural,
first=\@glo@first,
firstplural=\@glo@firstplural,
sort=\@glo@sort,
counter=\@glo@counter,
see=\@glo@see,
parent=\@glo@parent,
prefix=\@glo@prefix,
%\@gls@initnonumberlist,
%\@glo@useri,
%\@glo@userii,
%\@glo@useriii,
%\@glo@useriv,,
%\@glo@userv,
%\@glo@uservi,
short=\@glo@short,
shortplural=\@glo@shortpl,
long=\@glo@long,
longplural=\@glo@longpl
}
EoTeX

DefConstructor('\lx@glossaries@newentry{}{} RequiredKeyVals', sub {
    my ($document, $list, $key, $kv) = @_;
    $key = ToString($key);
    $document->openElement('ltx:glossarydefinition', key => $key, inlist => $list);
    my $hash = GetKeyVals($kv);
    foreach my $role (sort keys %$hash) {
      if (my $value = $kv->getValue($role)) {
        $document->insertElement('ltx:glossaryphrase', $value,
          key => $key, role => $role)
          if ToString($value); } }
    $document->closeElement('ltx:glossarydefinition'); },
  #             properties=>sub { %{ GetKeyVals($_[2]) }; }
);

#======================================================================
# Redefine \printglossary and friends to produce ltx:glossary
# (\printglossaries creates one of each declared glossary)
DefMacro('\printglossary',
  '\global\let\warn@noprintglossary\relax'
    . '\@ifnextchar[{\lx@printglossary}{\lx@printglossary[type=main]}');
Let('\printnoidxglossary', '\printglossary');

DefConstructor('\lx@printglossary OptionalKeyVals',
  "<ltx:glossary xml:id='#id' lists='#list'>"
    . "<ltx:title font='#titlefont' _force_font='true'>#title</ltx:title>"
    . "</ltx:glossary>",
  afterDigest => sub { noteBackmatterElement($_[1], 'ltx:glossary'); },
  properties  => sub {
    my ($stomach, $kv) = @_;
    my $type  = ToString($kv && $kv->getValue('type')) || 'main';
    my $title = Digest(T_CS('\@glotype@' . $type . '@title'));
    my $docid = ToString(Expand(T_CS('\thedocument@ID')));
    my $id    = ($docid ? "$docid.glo" : 'glo') . '.' . CleanID($type);
    (list => $type, id => $id, title => $title); },
  beforeConstruct => sub { adjustBackmatterElement($_[0], $_[1]); });

#======================================================================
1;