# -*- CPERL -*-
# /=====================================================================\ #
# |  dcolumn                                                            | #
# | 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=/ #
use LaTeXML::Package;
use strict;

RequirePackage('array');

sub absorbedString {
  my($tokens)=@_;
  my $capdocument = LaTeXML::Document->new($STATE->getModel);
  my $capture = $capdocument->openElement('_Capture_', font=>LaTeXML::Font->new());
  $capdocument->absorb(Digest($tokens));
  my @nodes= $capdocument->findnodes("//ltx:XMath/*",$capture);
  $nodes[0]->textContent; }

DefMacro('\DC@{}{}{}',sub {
  my($gullet,$delim,$todelim,$ndec)=@_;
  $delim=ToString($delim);
  if($delim ne ToString($todelim)){
    AssignCatcode('math:$delim'=>1);
    DefMacroI(T_CS($delim),undef,'\ROLE{PERIOD}{'.join('',map(ToString($_),$todelim->revert)).'}');
  }
  if(LookupValue('IN_MATH') || $gullet->ifNext(T_MATH)){ # Not really good enough, but...
    (); }
  else {
    Let('\DC@end',T_MATH);
    (T_MATH); }});
# NOTE: We should be making arrangements for this funny thing to still
# be considered a number!

DefMacro('\DC@end','');

DefColumnType('D{}{}{}',sub { 
  my($gullet,$delim,$todelim,$ndec)=@_;
  my $alignment =absorbedString(Tokens(T_CS('\ensuremath'),T_BEGIN,$todelim,T_END));
  $LaTeXML::BUILD_TEMPLATE->addColumn(before=>Tokens(T_CS('\DC@'),
						     T_BEGIN,$delim->unlist,T_END,
						     T_BEGIN,$todelim->unlist,T_END,
						     T_BEGIN,$ndec->unlist,T_END),
				      align=>'char:'.$alignment,
				      after=>Tokens(T_CS('\DC@end')));
   return; });

1;
