# -*- CPERL -*-
#======================================================================
# Collected separately so I can override for LaTeXML
#======================================================================
package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;

AssignValue(DOCUMENTID => 'manual');
Let('\orig@maketitle', '\maketitle');    # Save!!!

UnshiftValue(SEARCHPATHS => 'sty');
# Load the raw style file, to get most of the definitions, as is.
InputDefinitions('latexmlman', type => 'sty', noltxml => 1);

Let('\maketitle', '\orig@maketitle');    # Restore!!!

# Redefine title page & front matter stuff
DefMacro('\subtitle{}', '\@add@frontmatter{ltx:subtitle}{#1}');
DefMacro('\toctitle{}', '\@add@frontmatter{ltx:toctitle}{#1}');
DefMacro('\BibTeX',     'BibTeX');

DefConstructor('\ltxcmd{}',
  "<ltx:ref labelref='#label'>#1</ltx:ref>",
  properties => sub { (label => CleanLabel($_[1])); });
#DefConstructor('\ltxpod{}', "<ltx:ref labelref='#ref'>#1</ltx:ref>",
#  afterDigest => sub {
#    my $name = $_[1]->getArg(1)->toString;
#    $_[1]->setProperty(ref => CleanLabel("LaTeXML::" . $name)); });

DefConstructor('\ltxpod@ref{}{}', "<ltx:ref labelref='#ref'>#1</ltx:ref>",
  afterDigest => sub {
    my $name = $_[1]->getArg(2)->toString;
    $_[1]->setProperty(ref => CleanLabel('LaTeXML::' . $name)); });

DefConstructor('\pod[] Semiverbatim', "<ltx:ref href='#href' labelref='#ref'>#text</ltx:ref>",
  afterDigest => sub {
    my $text = $_[1]->getArg(1);
    my $name = $_[1]->getArg(2)->toString;
    if (($name =~ /^LaTeXML/) || ($name =~ /^latexml/)) {
      my $label = $name;
      # reformat the label
      if ($label =~ /^(.*?)\/(.*)$/) {    # a reference to a POD/SECTION
        $label = $1;
        my $sec = $2;
        if ($sec =~ /^\"(.*?)\"$/) {      # Strip quotes
          $sec = $1; }
        $text = $sec unless $text;
        $sec =~ s/\s+/_/g;                # convert spaces
        $label .= '_' . $sec; }
      $_[1]->setProperty(ref => CleanLabel($label));
      $_[1]->setProperty(text => $text || $name); }
    else {
      $name =~ s/::/%3A%3A/g;
      my $url = "https://search.cpan.org/search?query=$name&mode=module";
      $_[1]->setProperty(href => $url); }
    return; });

#======================================================================
DefEnvironment('{advanced}', "<ltx:paragraph class='ltx_advanced'>#body</ltx:paragraph>");

#======================================================================
DefEnvironment('{moduledescription}',
  "<ltx:description  xml:id='#id' class='ltx_moduledescription'>#body</ltx:description>",
  properties => sub { beginItemize('description'); });

DefEnvironment('{elementdescription}',
  "<ltx:description  xml:id='#id' class='ltx_elementdescription'>#body</ltx:description>",
  properties => sub { beginItemize('description'); });

DefEnvironment('{patterndescription}',
  "<ltx:description  xml:id='#id' class='ltx_patterndescription'>#body</ltx:description>",
  properties => sub { beginItemize('description'); });

#======================================================================
# Section heading styles...
DefMacroI('\chaptername',       undef, 'Chapter');
DefMacroI('\sectionname',       undef, '\S');
DefMacroI('\subsectionname',    undef, '\S');
DefMacroI('\subsubsectionname', undef, '\S');
DefMacroI('\paragraphname',     undef, '\P');
DefMacroI('\subparagraphname',  undef, '\P');
DefMacroI('\appendixname',      undef, 'Appendix');

DefMacroI('\appendix', undef,
  '\@appendix'
    . '\def\sectionname{}'
    . '\def\subsectionname{}'
    . '\def\subsubsectionname{}');

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