\NeedsTeXFormat{LaTeX2e}%
\ProvidesPackage{jumplines}[2015/01/05 v0.1 -- Teaser/Continued articles with hyperlinks]
%%%
%% License: LaTeX Project Public License version 1.3 
%% Copyright (2015) Dr. Christian Hupfer 
%% Author: Christian Hupfer christian.hupfer@yahoo.de
%%
%%%%

\RequirePackage{etex}%
\RequirePackage{etoolbox}[2011/01/03 2.2]%
\RequirePackage{xparse}%
\RequirePackage{xkeyval}[2012/10/14 v2.6b]%
\RequirePackage[svgnames]{xcolor}
\RequirePackage[breakable]{tcolorbox}%

\RequirePackage{tocloft}
\RequirePackage{ifluatex}

\ifluatex
\RequirePackage{luacolor}%  Recommended
\fi


%%%% Key - Value definitions

\NewDocumentCommand{\listofarticlesname}{}{List of Articles}%
\NewDocumentCommand{\listofcontinuedarticlesname}{}{List of continued Articles}%
\newlistof{article}{art}{\listofarticlesname}%
\newlistof{contarticle}{cont}{\listofcontinuedarticlesname}%




\define@key{jumpline}{ArticleHeadline}{%
  \def\JLKVMacroArticleHeadline{#1}%
}%

\define@key{jumpline}{ArticleAuthor}{%
  \def\JLKVMacroArticleAuthor{#1}%
}%

% Length related keys

\define@key{jumpline}{ArticleFullHeight}{%
  \def\JLKVMacroArticleFullHeight{#1}%
}%

\define@key{jumpline}{TeaserHeight}[2in]{%
  \def\JLKVMacroTeaserHeight{#1}%
}%

\define@key{jumpline}{ContinuedArticleHeight}{%
  \def\JLKVMacroContinuedArticleHeight{#1}%
}%

\define@key{jumpline}{ContinuedFromTopskip}{%
  \def\JLKVMacroContinuedFromTopskip{#1}%
}%
\define@key{jumpline}{ContinuedFromBottomskip}{%
  \def\JLKVMacroContinuedFromBottomskip{#1}%
}%

\define@key{jumpline}{ContinuedOnTopskip}{%
  \def\JLKVMacroContinuedOnTopskip{#1}%
}%

\define@key{jumpline}{ContinuedOnBottomskip}{%
  \def\JLKVMacroContinuedOnBottomskip{#1}%
}%

\define@key{jumpline}{ContinuedArticleBottomskip}{%
  \def\JLKVMacroContinuedArticleBottomskip{#1}%
}%

% Keys for optional teaser/continued article content and options

\define@key{jumpline}{TeaserHeaderContent}{%
  \def\JLKVMacroTeaserHeaderContent{#1}%
}%

\define@key{jumpline}{TeaserHeaderOptions}{%
  \def\JLKVMacroTeaserHeaderOptions{#1}%
}%

\define@key{jumpline}{ContinuedArticleHeaderContent}{%
  \def\JLKVMacroContinuedArticleHeaderContent{#1}%
}%


\define@key{jumpline}{ContinuedArticleHeaderOptions}{%
  \def\JLKVMacroContinuedArticleHeaderOptions{#1}%
}%

\presetkeys{jumpline}{TeaserHeight=2in,
                      ContinuedOnTopskip={0.4\baselineskip},
                      ContinuedOnBottomskip={0pt},
                      ContinuedFromTopskip={0.4\baselineskip},
                      ContinuedFromBottomskip={0pt},%
                      ContinuedArticleBottomskip={20pt},%
                      TeaserHeaderContent={},
                      TeaserHeaderOptions={breakable,leftlower=0pt,rightlower=0pt,boxrule=0pt,left=0pt,right=0pt, arc=0pt,auto outer arc,colbacktitle=black,coltitle=white,toptitle=2mm,bottomtitle=2mm},
                      ContinuedArticleHeaderOptions={breakable,leftlower=0pt,rightlower=0pt,boxsep=0pt,boxrule=0pt,left=0pt,right=0pt,arc=0pt,auto outer arc,colbacktitle=black,coltitle=white,toptitle=2mm,bottomtitle=2mm,righttitle=1mm}
                    }{}%
                    



\newcounter{@@jumplines@@internaldocounter}%

\newlength{\@@jumplines@@articleheight}%
\newlength{\@@jumplines@@teaserboxheight}%


\xdef\ContinuedArticleList{}%
\xdef\TeaserBoxList{}%
\xdef\JumplineOptionsList{}%

\listcsadd{ContinuedArticleList}{}%
\listcsadd{TeaserBoxList}{}%
\listcsxadd{JumplineOptionsList}{}%



\NewDocumentCommand\JumplineArticle{+o+m}{%    Provide later on for a starred version
  \refstepcounter{article}%
  \newbox\articlebox%
  \newbox\teaserbox%
  \begingroup%
  \IfValueTF{#1}{%
    \setkeys{jumpline}{#1}%
  }{%
    \typeout{Nothing!}%
  }%
  \setlength{\@@jumplines@@teaserboxheight}{\JLKVMacroTeaserHeight}%
  \global\setbox\articlebox=\vbox{\noindent#2}%
  \setlength{\@@jumplines@@articleheight}{\the\ht\articlebox}%
  \global\setbox\teaserbox=\vsplit\articlebox to \@@jumplines@@teaserboxheight% Split the input to teaser box and a continued box 
  \listcsxadd{TeaserBoxList}{\number\teaserbox}%
  \listcsxadd{ContinuedArticleList}{\number\articlebox}%
  % Transfer options to the child box handlers
  \listcsxadd{JumplineOptionsList}{\unexpanded{#1},ContinuedArticleHeight={\the\ht\articlebox},ArticleFullHeight={\the\@@jumplines@@articleheight}}%
  \endgroup%
}%


\NewDocumentCommand{\DisplayJumplineTeaser}{+m+m}{%
  \begingroup%
  \setkeys{jumpline}{#1}%
  \begin{tcolorbox}[title={Article \thearticle~\ifdef{\JLKVMacroArticleHeadline}{\JLKVMacroArticleHeadline}{}},
    code={\pgfkeysalsofrom\JLKVMacroTeaserHeaderOptions}]
    \ifdef{\JLKVMacroTeaserHeaderContent}{%
      \JLKVMacroTeaserHeaderContent}{}%
  \end{tcolorbox}%
  \phantomsection%
  \label{jlarticle::teaser::\number\value{article}}%
  \ifdef{\JLKVMacroArticleHeadline}{%
    \addcontentsline{art}{section}{\thearticle~\JLKVMacroArticleHeadline}
  }{%
    \addcontentsline{art}{section}{\thearticle}%
  }%
  % Now unbox it
%  \colorlet{saved}{.}
  \begingroup
  \noindent\unvbox#2%
  \endgroup
%  \color{saved}
  \vskip\JLKVMacroContinuedOnTopskip%
  \ifdef{\JLKVMacroArticleAuthor}{\raggedleft By \JLKVMacroArticleAuthor\par}{}%
  \ifdimless{\JLKVMacroArticleFullHeight}{\JLKVMacroTeaserHeight}{%
  }{% 
    \raggedleft\textit{\bfseries Continued on Page \pageref{jlarticle::\number\value{article}}} % Change later on to be more configurable!
  }%
  \vskip\JLKVMacroContinuedOnBottomskip%
  \endgroup%
}%



\NewDocumentCommand{\DisplayContinuedArticle}{+m+m}{%
  \begingroup%
  \setkeys{jumpline}{#1}%
  \ifdimgreater{\JLKVMacroArticleFullHeight}{\JLKVMacroTeaserHeight}{%
    \begin{tcolorbox}[title={\raggedleft Continued from Article \ref{jlarticle::teaser::\number\value{article}} on page \pageref{jlarticle::teaser::\number\value{article}}},code={\pgfkeysalsofrom\JLKVMacroContinuedArticleHeaderOptions}]%
      \ifdef{\JLKVMacroContinuedArticleHeaderContent}{%
        \JLKVMacroContinuedArticleHeaderContent}{%
      }%
    \end{tcolorbox}%
    \phantomsection\label{jlarticle::\number\value{article}}%
    \ifdef{\JLKVMacroArticleHeadline}{%
      \addcontentsline{cont}{section}{\thearticle~\JLKVMacroArticleHeadline}%
    }{%
      \addcontentsline{cont}{section}{\thearticle}%
    }%
    \noindent\unvbox#2%
  }{}%
  \endgroup%
}%

\NewDocumentCommand{\@@jumplines@@showcontinuedarticle}{+m}{%
  \refstepcounter{article}%
  \begingroup%
  \setcounter{@@jumplines@@internaldocounter}{0}%
  \renewcommand{\do}[1]{%
    \stepcounter{@@jumplines@@internaldocounter}%
    \ifnumequal{\value{@@jumplines@@internaldocounter}}{\value{article}}{%
      \DisplayContinuedArticle{##1}{#1}%
      \listbreak%
    }{}}%
  \dolistcsloop{JumplineOptionsList}%
  \endgroup%
}%

\NewDocumentCommand{\@@jumplines@@showteaser}{+m}{%
  \refstepcounter{article}%
  \begingroup%
  \setcounter{@@jumplines@@internaldocounter}{0}%
  \renewcommand{\do}[1]{%
    \stepcounter{@@jumplines@@internaldocounter}%
    \ifnumequal{\value{@@jumplines@@internaldocounter}}{\value{article}}{%
      \DisplayJumplineTeaser{##1}{#1}%  
      \listbreak%
    }{%
      % Do nothing in this case
    }%
  }% End of \do definition
  \dolistcsloop{JumplineOptionsList}%
  \endgroup%
}%



\NewDocumentCommand{\ShipoutArticleTeasers}{}{%
  % Do it for safety reasons%
  \setcounter{article}{0}%
  \forlistcsloop{\@@jumplines@@showteaser}{TeaserBoxList}%
}%

\NewDocumentCommand{\ShipoutArticleHangingArticles}{}{%
  \setcounter{article}{0}%
  \forlistcsloop{\@@jumplines@@showcontinuedarticle}{ContinuedArticleList}%
}%





\endinput