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

AssignValue(BASE_URL => '');

# Ignorable stuff, since we're not doing linebreaks.
DefMacro('\UrlBreaks',    '');
DefMacro('\UrlBigBreaks', '');
DefMacro('\UrlNoBreaks',  '');
DefMacro('\UrlOrds',      '');
DefMacro('\UrlSpecials',  '');

# Font style definitions.
DefMacro('\urlstyle{}',    '\expandafter\protect\csname url@#1style\endcsname');
DefMacro('\url@ttstyle',   '\def\UrlFont{\ttfamily}');
DefMacro('\url@rmstyle',   '\def\UrlFont{\rmfamily}');
DefMacro('\url@sfstyle',   '\def\UrlFont{\sffamily}');
DefMacro('\url@samestyle', '');
DefMacro('\UrlFont',       '\ttfamily');

# Bracketting.
Let('\UrlLeft',  '\@empty');
Let('\UrlRight', '\@empty');

# \DeclareUrlCommand\cmd{settings}
# Have this expand into \@Url w/ the declared cmd as arg, so it gets reflected in XML.
DefMacro('\DeclareUrlCommand{}{}', '\def#1{\begingroup #2\@Url#1}');

# This is an extended version of \Url that takes an extra token as 1st arg.
# That token is the cs that invoked it, so that it can be reflected in the generated XML,
# as well as used to generate the reversion.
# In any case, we read the verbatim arg, and build a Whatsit for @@Url
DefMacro('\@Url Token', sub {
    my ($gullet, $cmd) = @_;
    my ($open, $close, $url);
    StartSemiverbatim('%');
    $open = $gullet->readToken;
    if ($open->equals(T_BEGIN)) {
      $open = T_OTHER('{'); $close = T_OTHER('}');
      $url  = $gullet->readBalanced; }
    else {
      $close = $open = T_OTHER($open->getString);
      $url   = $gullet->readUntil($close); }
    EndSemiverbatim();

    my @toks = grep { $_->getCatcode != CC_SPACE; } $url->unlist;
    @toks = map { T_OTHER(ToString($_)) } @toks;
    (Invocation(T_CS('\@@Url'),
        T_OTHER(ToString($cmd)), Tokens($open), Tokens($close),
        Tokens(@toks),
        Tokens(T_CS('\UrlFont'), T_CS('\UrlLeft'), @toks, T_CS('\UrlRight')))->unlist,
      T_CS('\endgroup')); });

# Define \Url, in case its used; won't be represented as nicely
DefMacro('\Url', sub {
    my ($gullet) = @_;
    $gullet->unread(T_OTHER('\Url'));
    (T_CS('\@Url')); });

# \@@Url cmd {open}{close}{url}{formattedurl}
DefConstructor('\@@Url Undigested {}{} Semiverbatim {}',
  "?#isMath(<ltx:XMWrap href='#href'>#5</ltx:XMWrap>)"    # Allow this to work in Math!
    . " (<ltx:ref href='#href' class='#class'>#5</ltx:ref>)",
  properties => sub { (href => ComposeURL(LookupValue('BASE_URL'), $_[4]),
      class => sub { my $c = ToString($_[1]); $c =~ s/^\\//; 'ltx_' . $c; }); },
  sizer     => '#5',
  reversion => '#1#2#4#3');

# These are the expansions of \DeclareUrlCommand
DefMacro('\path', '\begingroup\urlstyle{tt}\@Url\path');
DefMacro('\url', '\begingroup\@Url\url', locked => 1);

# \urldef{newcmd}\cmd{arg}
# Kinda tricky, since we need to get the expansion of \cmd as the value of \newcmd
# Along with the annoying \endgroup that must balance the one always preceding \Url!
DefPrimitive('\urldef{}{}', sub {
    my ($stomach, $cmd, $start) = @_;
    my $gullet = $stomach->getGullet;
    # important to have a {} for $start in the macro signature,
    # so that arg braces can be unwrapped, if provided
    $gullet->unread($start);
    my @expansion = $stomach->digestNextBody(T_CS('\endgroup'));
    DefPrimitiveI($cmd, undef, sub { @expansion; });
    (); });

1;
