# -*- mode: Perl -*-
# /=====================================================================\ #
# |  elsart_support_core                                                | #
# | 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.     | #
# |---------------------------------------------------------------------| #
# | Thanks to the arXMLiv group for initial implementation              | #
# |    http://arxmliv.kwarc.info/                                       | #
# | Released to the Public Domain                                       | #
# | enhancements by Lee Worden                                          | #
# |---------------------------------------------------------------------| #
# | 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;

#======================================================================
# Support for both elsart.cls and elsarticle.cls
#======================================================================
# 4. Frontmatter

DefEnvironment('{frontmatter}', '#body');

# \title{}
# single author!
DefMacro('\author[]{}',
  '\@add@frontmatter{ltx:creator}[role=author]{\@personname{#2}}');

DefMacro('\address[]{}', '\lx@contact{address}{#2}');
# Redefine to account for the label, which we ignore for now!
DefConstructor('\thanks[]{}', "<ltx:note role='thanks'>#2</ltx:note>");
DefConstructor('\person@thanks[]{}', "^ <ltx:contact role='thanks'>#2</ltx:contact>",
  alias => '\thanks', mode => 'text');
DefMacro('\thanksref{}',  Tokens());
DefMacro('\corauth[]{}',  '\lx@contact{correspondent}{#2}');
DefMacro('\corref{}',     Tokens());
DefMacro('\corauthref{}', Tokens());
DefMacro('\cortext[]{}',  Tokens());

# Is this significantly different?
DefMacro('\collab OptionalMatch:* {}', '\author{#1}');
Let('\collaboration', '\collab');

DefMacro('\tnotetext[]{}', '\lx@notetext[#1]{footnote}{#2}');
DefMacro('\fntext[]{}',    '\lx@notetext[#1]{footnote}{#2}');
DefMacro('\lx@elsart@noteref{}', sub {
    return map { (T_CS('\lx@notemark'),
        T_OTHER('['), T_OTHER($_),         T_OTHER(']'),
        T_BEGIN,      T_OTHER('footnote'), T_END); } split(/,/, ToString($_[1])); });

DefMacro('\tnoteref{}', '\lx@elsart@noteref{#1}');
DefMacro('\fnref{}',    '\lx@elsart@noteref{#1}');

DefMacro('\runauthor{}', Tokens());
DefMacro('\runtitle{}',  Tokens());

DefMacro('\subtitle{}', '\@add@frontmatter{ltx:subtitle}{#1}');

DefMacro('\ead Semiverbatim', '\@add@to@frontmatter{ltx:creator}{\@@@email{#1}}');
DefConstructor('\@@@email{}', "^ <ltx:contact role='email'>#1</ltx:contact>");

DefMacro('\sep', '\unskip,\space');

DefMacro('\received{}',     '\@add@frontmatter{ltx:date}[role=received]{#1}');
DefMacro('\revised{}',      '\@add@frontmatter{ltx:date}[role=revised]{#1}');
DefMacro('\accepted{}',     '\@add@frontmatter{ltx:date}[role=accepted]{#1}');
DefMacro('\communicated{}', '\@add@frontmatter{ltx:date}[role=communicated]{#1}');
DefMacro('\dedicated{}',    '\@add@frontmatter{ltx:note}[role=dedicated]{#1}');
DefMacro('\presented{}',    '\@add@frontmatter{ltx:date}[role=presented]{#1}');
DefMacro('\articletype{}',  '\@add@frontmatter{ltx:note}[role=articletype]{#1}');
DefMacro('\issue{}',        '\@add@frontmatter{ltx:note}[role=issue]{#1}');
DefMacro('\journal{}',      '\@add@frontmatter{ltx:note}[role=journal]{#1}');
DefMacro('\volume{}',       '\@add@frontmatter{ltx:note}[role=volume]{#1}');
DefMacro('\pubyear{}',      '\@add@frontmatter{ltx:date}[role=publication]{#1}');

DefMacro('\FullCopyrightText', Tokens());
DefMacro('\copyear{}',         '\@add@frontmatter{ltx:date}[role=copyright]{#1}');
DefMacro('\copyrightholder{}', '\@add@frontmatter{ltx:note}[role=copyrightholder]{#1}');
Let('\copyrightyear', '\copyear');

DefMacro('\RUNART',    Tokens());
DefMacro('\RUNDATE',   Tokens());
DefMacro('\RUNJNL',    Tokens());
DefMacro('\company{}', Tokens());

# Apparently some sort of identifier?
DefMacro('\aid{}',    Tokens());
DefMacro('\ssdi{}{}', Tokens());

# Parses version control of the class file
DefMacro('\readRCS Until:$ Until:$', Tokens());
DefMacro('\RCSdate',                 Tokens());
DefMacro('\RCSfile',                 Tokens());
DefMacro('\RCSversion',              Tokens());

DefMacro('\firstpage{}', Tokens());
DefMacro('\lastpage{}',  Tokens());

DefMacro('\preface', Tokens());

DefMacro('\theHaddress', Tokens());
DefMacro('\theaddress',  Tokens());

Let('\ESpagenumber', '\arabic');

#======================================================================
# 5. Abstract
#  as an environment!

# Normally, we'd define the acknowledgements environment simply as:
# DefEnvironment('{ack}',
# However, people seem to insist on "misusing" it, by just saying
# \ack... which actually "works".
# So, we just open it, and let it autoclose.

DefConstructor('\ack',    "<ltx:acknowledgements>");
DefConstructor('\endack', "</ltx:acknowledgements>");
Tag("ltx:acknowledgements", autoClose => 1);

#======================================================================
# 6 Keywords

# \PACS & \MSC should appear inside a keyword environment;
# apparently, they implicitly get whatever there is until next thing or end of environment.
# elsart.sty apparently defines \JEL and \UK as well.
# But of course, Somebody has to use the non-environment form!
# Worse, apparently if you do that, you've got to end with an unbalanced }
#DefMacro('\keyword',   '\@keyword');
DefMacroI('\begin{keyword}', undef, '\begingroup\@keyword');
DefMacroI('\end{keyword}',   undef, '\@keyword@cut\endgroup');
DefMacro('\keyword', sub {
    my ($gullet) = @_;
    my $arg = $gullet->readBalanced;
    (T_CS('\@keyword'), $arg->unlist, T_CS('\@keyword@cut')); });
DefMacro('\endkeyword', '\@keyword@cut');
DefMacro('\PACS',       '\@keyword@cut\@PACS');
DefMacro('\MSC',        '\@keyword@cut\@MSC');
DefMacro('\JEL',        '\@keyword@cut\@JEL');
DefMacro('\UK',         '\@keyword@cut\@UK');

DefMacro('\@keyword XUntil:\@keyword@cut', '\@add@frontmatter{ltx:classification}[scheme=keywords]{#1}');
DefMacro('\@PACS    XUntil:\@keyword@cut', '\@add@frontmatter{ltx:classification}[scheme=PACS]{#1}');
DefMacro('\@MSC     XUntil:\@keyword@cut', '\@add@frontmatter{ltx:classification}[scheme=MSC]{#1}');
DefMacro('\@JEL     XUntil:\@keyword@cut', '\@add@frontmatter{ltx:classification}[scheme=JEL]{#1}');
DefMacro('\@UK      XUntil:\@keyword@cut', '\@add@frontmatter{ltx:classification}[scheme=UK]{#1}');
DefConstructor('\@keyword@cut', '');

#======================================================================
# Document Structure

DefMacro('\theparagraph',    '\thesubsubsection.\arabic{paragraph}');
DefMacro('\thesubparagraph', '\theparagraph.\arabic{subparagraph}');

if (LookupValue('@seceqn')) {
  RawTeX('\@addtoreset{equation}{section}');
  DefMacro('\theequation', '\thesection.\arabic{equation}'); }

#======================================================================
# Tables, Figures & Theorems

if (LookupValue('@seceqn')) {
  RawTeX('\newtheorem{thm}{Theorem}[section] \@addtoreset{thm}{section}'); }
else {
  RawTeX('\newtheorem{thm}{Theorem}'); }

# Section 7, https://www.elsevier.com/__data/assets/pdf_file/0008/56843/elsdoc-1.pdf
Let(T_CS('\newdefinition'), T_CS('\newtheorem'));
Let(T_CS('\newproof'),      T_CS('\newtheorem'));

#======================================================================
# Random spaces, dimensions

DefRegister('\eqnarraycolsep'  => Dimension('1pt'));
DefRegister('\eqnbaselineskip' => Glue('14pt'));
DefRegister('\eqnlineskip'     => Glue('2pt'));
DefRegister('\eqntopsep'       => Glue('12pt'));

#======================================================================
# random stuff
DefMacro('\mpfootnotemark', Tokens());

DefMacro('\note{}', "<ltx:note>#1</ltx:note>");    # ?

1;
