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

RawTeX(<<'EoTeX');
\newlength{\beforeepigraphskip}
  \setlength{\beforeepigraphskip}{.5\baselineskip}
\newlength{\afterepigraphskip}
  \setlength{\afterepigraphskip}{.5\baselineskip}
\newlength{\epigraphwidth}
  \setlength{\epigraphwidth}{.4\textwidth}
\newlength{\epigraphrule}
  \setlength{\epigraphrule}{.4\p@}
\newcommand{\epigraphsize}{\small}
\newcommand{\epigraphflush}{flushright}
\newcommand{\textflush}{flushleft}
\newcommand{\sourceflush}{flushright}
EoTeX

# \epigraph{text}{source}
DefConstructor('\epigraph{}{}',
  "<ltx:quote class='ltx_epigraph #epigraphflush' cssstyle='#qwidth #qalign'>"
    . "<ltx:block class='ltx_epigraph_text' cssstyle='#talign'>"
    . "#1"
    . "</ltx:block>"
    . "<ltx:block class='ltx_epigraph_source' cssstyle='#srule #salign'>"
    . "#2"
    . "</ltx:block>"
    . "</ltx:quote>",
  bounded      => 1,
  beforeDigest => sub { Digest(T_CS('\epigraphsize')); },
  afterDigest  => sub {
    my ($stomach, $whatsit) = @_;
    my $rule  = LookupValue('\epigraphrule')->ptValue . 'pt';
    my $width = LookupValue('\epigraphwidth')->ptValue . 'pt';
    my $qa    = ToString(Digest(T_CS('\epigraphflush')));
    my $ta    = ToString(Digest(T_CS('\textflush')));
    my $sa    = ToString(Digest(T_CS('\sourceflush')));
    $whatsit->setProperties(srule => "border-top:solid $rule;",
      qwidth => "width:$width;",
      qalign => ($qa eq 'center'
        ? "margin-right:auto; margin-left:auto;"
        : ($qa eq 'flushleft' ? "margin-right:auto;"
          : ($qa eq 'flushright' ? "margin-left:auto;"
            : ''))),
      talign => ($ta eq 'center'
        ? "text-align:center; "
        : ($ta eq 'flushleft'
          ? "text-align:left; "
          : ($ta eq 'flushright'
            ? "text-align:right; "
            : ''))),
      salign => ($sa eq 'center'
        ? "text-align:center; "
        : ($sa eq 'flushleft'
          ? "text-align:left; "
          : ($sa eq 'flushright'
            ? "text-align:right; "
            : ''))));
    return; });

# epigraph environment contains a set of epigraphs
DefEnvironment("{epigraphs}", "#body",
  beforeDigest => sub { Let('\qitem', '\epigraph'); });

# Each epigraph in an epigraph environment
# \qitem{text}{source}

#======================================================================
# Since we aren't managing pages,
# We'll just immediately output the material
DefMacro('\epigraphhead[]{}', '#1');
# Or do nothing for these
DefMacro('\dropchapter{}',   '');
DefMacro('\undodrop',        '');
DefMacro('\cleartoevenpage', '');
#======================================================================
1;

