# -*- mode: Perl -*-
# /=====================================================================\ #
# |  deluxetable.sty.ltxml                                              | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Thanks to the arXMLiv group for initial implementation              | #
# |    http://arxmliv.kwarc.info/                                       | #
# | Released to the Public Domain                                       | #
# |---------------------------------------------------------------------| #
# | 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;

#======================================================================
# 2.15.1 The deluxetable Environment
# \begin{deluxetable}{cols}
#    preamble
#    \startdata
#    data
#    \enddata
#    \tablenotetext,\tablecomments,\tablerefs
# \end{deluxetable}

DefMacro('\dummytable', '\refstepcounter{table}');

DefMacroI('\deluxetable', '{}',
  '\set@deluxetable@template{#1}\def\@deluxetable@header{}\begin{table}');
DefMacro('\enddeluxetable', '\spew@tblnotes\end{table}');
DefMacroI('\csname deluxetable*\endcsname', '{}',
  '\set@deluxetable@template{#1}\def\@deluxetable@header{}\begin{table}');
DefMacro('\csname enddeluxetable*\endcsname', '\spew@tblnotes\end{table}');

DefMacro('\set@deluxetable@template AlignmentTemplate', sub {
    AssignValue('@deluxetable@template', $_[1]); });

DefMacro('\startdata', '\bgroup\@deluxetable@bindings\@@deluxetabular\@start@alignment\hline\hline\@deluxetable@header');
DefMacro('\enddata', '\\\\\hline\@finish@alignment\@end@deluxetabular\egroup');

DefPrimitive('\@deluxetable@bindings', sub {
    tabularBindings(LookupValue('@deluxetable@template')); });

DefConstructor('\@@deluxetabular DigestedBody',
  '#1',
  reversion    => '\begin{tabular}[#1]{#2}#3\end{tabular}',
  beforeDigest => sub { $_[0]->bgroup; },
  mode         => 'text');
DefPrimitive('\@end@deluxetabular', sub { $_[0]->egroup; });

#======================================================================
# 2.15.2 Preamble to the deluxetable

# add the internal registers, just in case, ignored for now.
DefRegisterI(T_CS('\pt@width'),  undef, Dimension(6));
DefRegisterI(T_CS('\pt@line'),   undef, Dimension(0));
DefRegisterI(T_CS('\pt@column'), undef, Dimension(0));
DefRegisterI(T_CS('\pt@nlines'), undef, Dimension(0));
DefRegisterI(T_CS('\pt@ncol'),   undef, Dimension(0));
DefRegisterI(T_CS('\pt@page'),   undef, Dimension(0));

DefMacro('\tabletypesize{}',       '');                      # Ignorable
DefMacro('\rotate',                '');                      # Ignorable ?
DefMacro('\tabletail{}',           '');                      # analog to head? but its ignored!!
DefMacro('\tablewidth{Dimension}', '\pt@width=#1\relax');    # Ignorable?
DefMacro('\tableheadfrac{}',       '');                      # Ignorable
DefMacro('\tablenum{}',            '\def\thetable{#1}');
# Note: This needs an UnRefStepCounter('table');

DefMacro('\tablecolumns{Number}', '');                       # Ignorable ???

Let('\tablecaption', '\caption');

DefMacro('\tablehead{}',
  '\def\@deluxetable@header{\@tabular@begin@heading#1\\\\\hline\@tabular@end@heading}');
DefMacro('\colhead{}',    '\multicolumn{1}{c}{#1}');
DefMacro('\twocolhead{}', '\multicolumn{2}{c}{\hss #1 \hss}');
DefMacro('\nocolhead{}',  '\multicolumn{1}{h}{#1}');
DefMacro('\dcolhead{}',   '\multicolumn{1}{c}{$\relax#1$}');

DefMacro('\nl',            '\\\\[0pt]');                     # Obsolete form
DefMacro('\nextline',      '\\\\[0pt]');                     # Obsolete form
DefMacro('\tablevspace{}', '\noalign{\vskip#1}');            # Obsolete form
DefMacro('\tablebreak',    '\\\\[0pt]');                     # Obsolete form

#======================================================================
# 2.15.3 Content of deluxetable

DefMacro('\tablebreak', '');    # Ignorable; we're not splitting tables.
DefMacro('\nodata',     '');    # Ignorable

DefMacro('\cutinhead{}', '\hline\multicolumn{\@alignment@ncolumns}{c}{#1}\\\\\hline');
DefMacro('\sidehead{}',  '\hline\multicolumn{\@alignment@ncolumns}{l}{#1}\\\\\hline');

DefMacro('\tableline', '\hline');

# A good article to test these macros on is arXiv:astro-ph/0001334
DefConstructor('\tablenotemark{}',
  "<ltx:note role='footnotemark' mark='#1'></ltx:note>",
  mode => 'text');
DefConstructor('\tablenotetext{}{}',
  "<ltx:note role='footnotetext' mark='#1'>#2</ltx:note>",
  mode => 'text');

# hardcoding \textwidth instead of doing the \pt@width arithmetic, which would need more work.
# the important bit is timing \spew@tblnotes correctly, so that the notes are
# flushed at the end of the figure, after the table is closed.
DefMacro('\tablerefs{}', sub {
    AddToMacro(T_CS('\tblnote@list'),
      Invocation(T_CS('\@tableref'), $_[1])->unlist(),
      TokenizeInternal('\let\email\@@email')->unlist()); });
DefMacro('\@tableref{}', '\par
 \vspace*{3ex}%
 {\parbox{\textwidth}{\hskip1em\rmfamily References. --- #1}\par}}');
DefMacro('\tablecomments{}', sub {
    AddToMacro(T_CS('\tblnote@list'),
      Invocation(T_CS('\@tablecom'), $_[1])->unlist());
});

DefMacro('\@tablecom{}', '\par 
 \vspace*{3ex}% 
 {\parbox{\textwidth}{\hskip1em\rmfamily Note. --- #1}\par}');
DefMacro('\spew@tblnotes',
  '\@tablenotes{\tblnote@list}\global\let\tblnote@list\@empty');
DefMacro('\@tablenotes{}', '\par 
 \vspace{4.5ex}\footnoterule\vspace{.5ex}%
 {\footnotesize #1}');

#======================================================================
# Other esoterica

# Not sure here, probably needs some css magic?
DefMacro('\ulap{}', '#1');
DefMacro('\dlap{}', '#1');

# MISSING, but any usage here would fail, anyway...
# \appdef, \appgdef, \prepdef

AtBeginDocument(<<'EoTeX');
\let\tblnote@list\@empty
\let\pt@caption\@empty 
\let\pt@head\@empty 
\let\pt@tail\@empty 
\pt@width\textwidth 
\def\pt@headfrac{.1}
EoTeX
#======================================================================
1;