\NeedsTeXFormat{LaTeX2e}%
\ProvidesPackage{jumplines}[2015/06/02 v0.2 -- Teaser/Continued articles with hyperlinks]
%%%
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2015 by Dr. Christian Hupfer <christian dot hupfer at yahoo dot de>
%% -------------------------------------------------------------------------------------------
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% This work consists of all files listed in README
%%

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

\DeclareOptionX{languages}[english]{%
  \def\JLPackageMacroLanguages{#1}%
}%

\DeclareOptionX*{\PackageWarning{jumplines}{Option '\CurrentOption ' ignored}}%

\ExecuteOptionsX{languages={spanish,french,ngerman,english}}

\ProcessOptionsX%


\RequirePackage[strings,ngerman,english,\JLPackageMacroLanguages]{babel}

\RequirePackage{tocloft}

\RequirePackage{ifluatex}
\RequirePackage[bookmarksopen=true]{hyperref}
\RequirePackage{bookmark}

\ifluatex
\RequirePackage{luacolor}%  Recommended
\fi

%%%% Provide dummy macros for unknown languages
\newcommand{\listofarticlesname}{}
\newcommand{\listofcontinuedarticlesname}{}
\newcommand{\articlesname}{}
\newcommand{\JLArticleName}{}
\newcommand{\ContinuedOn}{}
\newcommand{\ContinuedFrom}{}
\newcommand{\OnPage}{}
\newcommand{\Pagename}{}
\newcommand{\byauthor}{}


\StartBabelCommands{english}{extras}
\SetString\JLArticleName{Article\xspace}
\SetString\articlesname{Articles} % plural
\SetString\Pagename{Page\xspace}
\SetString\ContinuedOn{Continued on\xspace}
\SetString\ContinuedFrom{Continued from\xspace}
\SetString\OnPage{on \Pagename\xspace}
\SetString\listofarticlesname{List of Articles}%
\SetString\listofcontinuedarticlesname{List of continued \articlesname}%
\SetString\byauthor{By\xspace}
\EndBabelCommands

\StartBabelCommands{ngerman}{extras}
\SetString\JLArticleName{Artikel\xspace}
\SetString\articlesname{Artikel\xspace}
\SetString\Pagename{Seite\xspace}
\SetString\ContinuedOn{Weiterlesen auf\xspace}
\SetString\ContinuedFrom{Fortsetzung von\xspace}
\SetString\OnPage{auf \Pagename\xspace}
\SetString\listofarticlesname{Artikelverzeichnis}%
\SetString\listofcontinuedarticlesname{Fortgesetzte \articlesname}
\SetString\byauthor{Von\xspace}
\EndBabelCommands

\StartBabelCommands{french}{extras}
\SetString\JLArticleName{Article\xspace}
\SetString\articlesname{Articles} % plural
\SetString\Pagename{Page\xspace}
\SetString\ContinuedOn{Suite \`{a} la page\xspace}
\SetString\ContinuedFrom{Continu\'ee de\xspace}
\SetString\OnPage{\`a la \Pagename\xspace}
\SetString\listofarticlesname{Liste des articles}%
\SetString\listofcontinuedarticlesname{Liste des articles continu\'ee}%
\SetString\byauthor{Ecrit par\xspace}
\EndBabelCommands

\StartBabelCommands{spanish}{extras}
\SetString\JLArticleName{Art\'iculo\xspace}
\SetString\articlesname{Art\'iculos} % plural
\SetString\Pagename{P\'agina\xspace}
\SetString\ContinuedOn{Continuo en la p\'agina\xspace}
\SetString\ContinuedFrom{Continuado de\xspace}
\SetString\OnPage{la \Pagename\xspace}
\SetString\listofarticlesname{Lista de los art\'iculos}%
\SetString\listofcontinuedarticlesname{Lista des art\'iculos continuado}%
\SetString\byauthor{By\xspace}
\EndBabelCommands


\NewDocumentCommand{\DefaultTeaserTocExt}{}{%
teaser%
}%

\NewDocumentCommand{\DefaultContinuedArticleTocExt}{}{%
continued%
}%


\newlistof{article}{\DefaultTeaserTocExt}{\listofarticlesname}%
\newlistof{contarticle}{\DefaultContinuedArticleTocExt}{\listofcontinuedarticlesname}%


\@ifpackageloaded{bookmark}{%
  \NewDocumentCommand{\@@jumpline@@bookmark}{O{}+mO{}}{%
    \bookmark[#1]{#2}%
  }%
}{%
  \@ifpackageloaded{hyperref}{%
    \NewDocumentCommand{\@@jumpline@@bookmark}{O{}+mO{}}{%
      \pdfbookmark[#1]{#2}{#3}%
    }%
  }{%
    \NewDocumentCommand{\@@jumpline@@bookmark}{O{}+mO{}}{%
      % \pdfbookmark[#1]{#2}{#3}%
    }%
  }%
}


%%%% Key - Value definitions



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

\define@key{jumpline}{toc}{%
  \def\JLKVMacroToc{#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}{TeaserHeaderColor}{%
  \def\JLKVMacroTeaserHeaderColor{#1}%
}%

\define@key{jumpline}{ContinuedHeaderColor}{%
  \def\JLKVMacroContinuedHeaderColor{#1}%
}%


\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}%
}%

\define@boolkey{jumpline}{RightHere}[false]{%
}%

\define@boolkey{jumpline}{autobookmark}[true]{%
}%

\define@key{jumpline}{BookmarkEntry}{%
  \def\JLKVMacroBookmarkEntry{#1}%
}%


\define@key{jumpline}{genericbookmarklevel}{%
  \def\JLKVMacroGenericBookmarkLevel{#1}%
}%

\define@key{jumpline}{genericbookmarkdest}{%
  \def\JLKVMacroGenericBookmarkDest{#1}%
}%


\define@boolkey{jumpline}{emptyarticleintoc}[false]{%
}%

\define@boolkey{jumpline}{emptyarticleinbookmarks}[false]{%
}%


\define@key{jumpline}{teasertoclevel}[section]{%
\def\JLKVMacroTeaserTocLevel{#1}%
}%

\define@key{jumpline}{continuedtoclevel}[section]{%
\def\JLKVMacroContinuedTocLevel{#1}%
}%


\define@key{jumpline}{teaserbookmarklevel}[section]{%
\def\JLKVMacroTeaserBookmarkLevel{#1}%
}%

\define@key{jumpline}{continuedbookmarklevel}[section]{%
\def\JLKVMacroContinuedBookmarkLevel{#1}%
}%


\define@key{jumpline}{teasertocext}{%
\def\JLKVMacroTeaserTocExt{#1}%
}%

\define@key{jumpline}{continuedtocext}{%
\def\JLKVMacroContinuedTocExt{#1}%
}%

\define@key{jumpline}{internalmode}{%
\def\JLKVMacroInternalMode{#1}%
}%



\presetkeys{jumpline}{TeaserHeight=2in,
                      ContinuedOnTopskip={0.4\baselineskip},
                      ContinuedOnBottomskip={0pt},
                      ContinuedFromTopskip={0.4\baselineskip},
                      ContinuedFromBottomskip={0pt},%
                      ContinuedArticleBottomskip={20pt},%
                      TeaserHeaderColor={white},
                      ContinuedHeaderColor={white},
                      TeaserHeaderContent={},
                      ContinuedArticleHeaderContent={},
                      TeaserHeaderOptions={leftlower=0pt,rightlower=0pt,boxrule=0pt,left=0pt,right=0pt, arc=0pt,auto outer arc,colbacktitle=black,coltitle=white,toptitle=2mm,bottomtitle=2mm},
                      ContinuedArticleHeaderOptions={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,colback=black},
                      emptyarticleintoc=false,
                      emptyarticleinbookmarks=false,
                      autobookmark=true,
                      teasertoclevel=subsection,
                      continuedtoclevel=subsection,
                      teaserbookmarklevel=subsection,
                      continuedbookmarklevel=subsection,
                      teasertocext=\DefaultTeaserTocExt,
                      continuedtocext=\DefaultContinuedArticleTocExt
                    }{}%
                    



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


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

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


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




\NewDocumentCommand{\@@jumpline@@bookmarkwrapper}{+O{}}{%
  \begingroup
  \setkeys{jumpline}{#1}%
  \gdef\JLBookmarkEntry{}% Empty macro
  \ifdef{\JLKVMacroBookmarkEntry}{%
    \def\JLBookmarkEntry{\JLKVMacroBookmarkEntry}%
  }{% Nope, use (if defined !) the article headline, otherwise empty
    \ifdef{\JLKVMacroArticleHeadline}{%
      \gdef\JLBookmarkEntry{\JLKVMacroArticleHeadline}%
    }{%
    }%
  }%
  \ifdefempty{\JLBookmarkEntry}{%
    \ifKV@jumpline@autobookmark % Check if autobookmark is on
    \ifKV@jumpline@emptyarticleinbookmarks% Check if empty bookmark entry is allowed
    \@@jumpline@@bookmark[level=\JLKVMacroGenericBookmarkLevel,dest=\JLKVMacroGenericBookmarkDest]{\thearticle}%
    \fi%
    \fi%
  }{%
    \ifKV@jumpline@autobookmark%
    \@@jumpline@@bookmark[level=\JLKVMacroGenericBookmarkLevel,dest=\JLKVMacroGenericBookmarkDest]{\thearticle~\JLBookmarkEntry}%
    \fi%
  }%
  \undef\JLBookmarkEntry%
\endgroup%
}%



\NewDocumentCommand{\@@jumpline@@addcontentsline}{+O{}}{%
  \begingroup
  \setkeys{jumpline}{#1}%
  \gdef\JLTocEntry{}% Empty macro
  \ifdef{\JLKVMacroToc}{%
    \gdef\JLTocEntry{\JLKVMacroToc}%
  }{%
    \ifdef{\JLKVMacroArticleHeadline}{%
      \gdef\JLTocEntry{\JLKVMacroArticleHeadline}%
    }{%
    }%
  }%
  \ifdefempty{\JLTocEntry}{%
    \ifKV@jumpline@emptyarticleintoc%
    \addcontentsline{\csuse{JLKVMacro\JLKVMacroInternalMode TocExt}}{\csuse{JLKVMacro\JLKVMacroInternalMode TocLevel}}{\thearticle}%
    \fi%
    \@@jumpline@@bookmarkwrapper[genericbookmarklevel=\csuse{JLKVMacro\JLKVMacroInternalMode BookmarkLevel},genericbookmarkdest=jlarticle::\JLKVMacroInternalMode::\number\value{article},#1]%
    }{%
      \addcontentsline{\csuse{JLKVMacro\JLKVMacroInternalMode TocExt}}{\csuse{JLKVMacro\JLKVMacroInternalMode TocLevel}}{\thearticle~\JLTocEntry}%
      \@@jumpline@@bookmarkwrapper[genericbookmarklevel=\csuse{JLKVMacro\JLKVMacroInternalMode BookmarkLevel},genericbookmarkdest={jlarticle::\JLKVMacroInternalMode::\number\value{article}},#1]
    }%
    \endgroup%
    \undef{\JLTocEntry}%
}%


\NewDocumentCommand{\@@jumpline@@addcontentslineold}{+O{}}{%
  \begingroup
  \setkeys{jumpline}{#1}%
  \gdef\JLTocEntry{}% Empty macro
  \ifdef{\JLKVMacroToc}{%
    \gdef\JLTocEntry{\JLKVMacroToc}%
  }{%
    \ifdef{\JLKVMacroArticleHeadline}{%
      \gdef\JLTocEntry{\JLKVMacroArticleHeadline}%
    }{%
    }%
  }%

  \ifdefempty{\JLTocEntry}{%
    \ifKV@jumpline@emptyarticleintoc%
    \addcontentsline{\JLKVMacroTeaserTocExt}{\JLKVMacroTeaserTocLevel}{\thearticle}%
    \fi%
    \@@jumpline@@bookmarkwrapper[genericbookmarklevel=\JLKVMacroTeaserBookmarkLevel,genericbookmarkdest=jlarticle::teaser::\number\value{article},#1]%
    }{%
      \addcontentsline{\JLKVMacroTeaserTocExt}{\JLKVMacroTeaserTocLevel}{\thearticle~\JLTocEntry}%
      \@@jumpline@@bookmarkwrapper[genericbookmarklevel=\JLKVMacroTeaserBookmarkLevel,genericbookmarkdest=jlarticle::teaser::\number\value{article},#1]
    }%
    \endgroup%
    \undef{\JLTocEntry}%
}%



\NewDocumentCommand{\@@jumpline@@addcontentslinecontinuedarticle}{+O{}}{%
  \begingroup
  \setkeys{jumpline}{#1}%
  \gdef\JLTocEntry{}% Empty macro
  \ifdef{\JLKVMacroToc}{%
    \gdef\JLTocEntry{\JLKVMacroToc}%
  }{%
    \ifdef{\JLKVMacroArticleHeadline}{%
      \gdef\JLTocEntry{\JLKVMacroArticleHeadline}%
    }{%
    }%
  }%
  \ifdefempty{\JLTocEntry}{%
    \ifKV@jumpline@emptyarticleintoc%
    \addcontentsline{\JLKVMacroContinuedTocExt}{\JLKVMacroTeaserTocLevel}{\thearticle}%
    \fi%
    \@@jumpline@@bookmarkwrapper[genericbookmarklevel=\JLKVMacroContinuedBookmarkLevel,genericbookmarkdest=jlarticle::\number\value{article},#1]%
    }{%
      \addcontentsline{\JLKVMacroContinuedTocExt}{\JLKVMacroTeaserTocLevel}{\thearticle~\JLTocEntry}%
      \@@jumpline@@bookmarkwrapper[genericbookmarklevel=\JLKVMacroTeaserBookmarkLevel,genericbookmarkdest=jlarticle::\number\value{article},#1]
    }%
    \endgroup%
    \undef{\JLTocEntry}%
}%



\NewDocumentCommand{\DisplayJumplineTeaser}{+m+m}{%
  \begingroup%
  \setkeys{jumpline}{#1,internalmode={Teaser}}%
  \begin{tcolorbox}[colback=black,code={\pgfkeysalsofrom\JLKVMacroTeaserHeaderOptions}]
    \textcolor{\JLKVMacroTeaserHeaderColor}{\JLArticleName\thearticle~\ifdef{\JLKVMacroArticleHeadline}{\JLKVMacroArticleHeadline}{}}
  \end{tcolorbox}%
  % Typesetting possible additional header content 
  \ifdefempty{\JLKVMacroTeaserHeaderContent}{}{%
    \begin{tcolorbox}[colback=white,boxrule=0pt,sharp corners,nobeforeafter]%
      \JLKVMacroTeaserHeaderContent%
    \end{tcolorbox}%
  }%
  \phantomsection%
  \label{jlarticle::\JLKVMacroInternalMode::\number\value{article}}%
  \hypertarget{jlarticle::\JLKVMacroInternalMode::\number\value{article}}{}%
  \@@jumpline@@addcontentsline[#1]%
  % Now unbox it
  \begingroup%
  \noindent\unvbox#2%
  \endgroup%
  \vskip\JLKVMacroContinuedOnTopskip%
  \ifdef{\JLKVMacroArticleAuthor}{\raggedleft \byauthor\JLKVMacroArticleAuthor\par}{}%
  \ifdimless{\JLKVMacroArticleFullHeight}{\JLKVMacroTeaserHeight}{%
  }{% 
    \raggedleft\textit{\bfseries\ContinuedOn\Pagename~\pageref{jlarticle::\number\value{article}}} % Change later on to be more configurable!
  }%
  \vskip\JLKVMacroContinuedOnBottomskip%
  \endgroup%
}%



\NewDocumentCommand{\DisplayContinuedArticle}{+m+m}{%
  \begingroup%
  \setkeys{jumpline}{#1,internalmode=Continued}%
  \ifdimgreater{\JLKVMacroArticleFullHeight}{\JLKVMacroTeaserHeight}{%
    \begin{tcolorbox}[code={\pgfkeysalsofrom\JLKVMacroContinuedArticleHeaderOptions}]%
      \textcolor{\JLKVMacroContinuedHeaderColor}{{\raggedleft\ContinuedFrom\JLArticleName\ref{jlarticle::Teaser::\number\value{article}} \OnPage \pageref{jlarticle::Teaser::\number\value{article}}}}%
    \end{tcolorbox}
    \ifdefempty{\JLKVMacroContinuedArticleHeaderContent}{}{%
      \noindent\begin{tcolorbox}[boxsep=0pt,boxrule=0pt,colback=white,sharp corners,nobeforeafter]%
        \JLKVMacroContinuedArticleHeaderContent%
      \end{tcolorbox}%
    }%
    \phantomsection\label{jlarticle::\number\value{article}}%
    \hypertarget{jlarticle::\JLKVMacroInternalMode::\number\value{article}}{}%
    \@@jumpline@@addcontentsline[#1]%
    \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}%
  \phantomsection\hypertarget{teaserarticlesstart}{}%
  \@@jumpline@@bookmark[level=section,dest=teaserarticlesstart]{\articlesname}%
  \forlistcsloop{\@@jumplines@@showteaser}{TeaserBoxList}%
}%

\NewDocumentCommand{\ShipoutArticleHangingArticles}{}{%
  \setcounter{article}{0}%
  \phantomsection\hypertarget{continuedarticlesstart}{}%
  \@@jumpline@@bookmark[level=section,dest=continuedarticlesstart]{\listofcontinuedarticlesname}
  \forlistcsloop{\@@jumplines@@showcontinuedarticle}{ContinuedArticleList}%
}%





\endinput