% \iffalse meta-comment
%
% Copyright (C) 2012 by Raphaël Pinson <raphink@gmail.com>
% ---------------------------------------------------------------------------
% 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'.
%
% The Current Maintainer of this work is Raphaël Pinson.
%
% This work consists of the files impnattypo.dtx and impnattypo.ins
% and the derived filebase impnattypo.sty.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{scrjrnl.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesClass{scrjrnl}
%<*package>
    [2012/09/24 0.1 A class to typeset diaries or journals]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage[english]{babel}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\usepackage{metalogo}
\usepackage[all]{nowidow}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{scrjrnl.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{355}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{0.1}{2012/09/16}{First version}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \providecommand*{\url}{\texttt}
% \GetFileInfo{scrjrnl.dtx}
% \title{The \textsf{scrjrnl} class}
% \author{Raphaël Pinson \\ \url{raphink@gmail.com}}
% \date{\fileversion~from \filedate}
%
% \maketitle
%
% \section{Introduction}
%
% The \textsf{scrjrnl} class is based of the \textsf{scrbook} class,
% and inherits all of its commands and settings.
% Please refer to the Koma-Script documentation for the detail of
% all of these. Note that the \textsf{scrjrnl} class is not an
% official Koma-Script class.
%
% \section{Usage}
%
% \begin{verbatim}
%    \documentclass[<options>]{scrjrnl}
% \end{verbatim}
%
% \subsection{Options}
%
% The class options are described below.
%
% \DescribeMacro{monthpages}
%
% By default, the month pages are skiped and only the days are typeset.
% If you wish to display a page for each starting month, use this setting.
%
% \DescribeMacro{fancytabs}
%
% This class can automatically make use of the \textsf{fancytabs} package
% to typeset tabs on the side of every odd page, with the name of the current month.
%
% \subsection{The \texttt{journal} environment}
%
% The \texttt{journal} environment is the main addition of this class.
% It provides an environment in which to typeset journal entries.
%
% The \texttt{journal} environment takes the following options:
%
% \DescribeMacro{startmonth}
%
% The month in which to start the journal. Defaults to 1.
%
% \DescribeMacro{startday}
%
% The day in which to start the journal. Defaults to 1.
%
% \subsection{Typesetting months}
%
% Once the \texttt{journal} environment in initiated, you can use
% the \texttt{jrnlmonth} and \texttt{jrnlday} commands
% to typeset journal entries.
%
% \DescribeMacro{\jrnlmonth}
% 
% The \verb!\jrnlmonth! command starts a new month. It takes the following options:
%
% \DescribeMacro{month}
%
% Forces the current month instead of simply incrementing from the previous one. This parameter is an integer between 1 and 12.
%
% \DescribeMacro{startday}
%
% Start month at a given day, instead of 1.
%
% \subsection{Typesetting a journal entry}
%
% \DescribeMacro{\jrnlday}
%
% In order to typeset a single journal entry, use the \verb!\jrnlday! command. This command takes a mandatory argument which is the journal entry's title, as well as the following options:
%
% \DescribeMacro{day}
%
% Forces the current day instead of simply incrementing from the preivous one. This parameter is an integer.
%
% \DescribeMacro{ante}
%
% Add a text before the date.
%
% \DescribeMacro{post}
%
% Add a text after the date.
%
% \subsection{Index}
%
% When typesetting a journal, you might want to rely only on day entries
% to relate to pages and turn off completely page numbers by using
% an \texttt{empty} page style.
%
% In this situation, if you have to create an index, you might want index entries
% to link to dates in the journal rather than page numbers.
%
% The \textsf{scrjrnl} class records each typeset day in macros so they can be used to generate an index. The macros are named \verb!jrnldaydate\thejrnlday!, with the page day corresponding to the entry, numbered from the first typeset day in the journal.
%
% Below is an example of an index generated using the \textsf{bibleref} package, and linking to journal entries instead of pages:
%
% \begin{verbatim}
%% Index by day
%\renewcommand*{\bvidxpgformat}{transform}
%
%% Get the date for the recorded day
%\newcommand\getdaydate[1]{\csname jrnldaydate#1\endcsname}
%
%% Go through index items
%\newcommand{\transform}[1]{%
%  \def\secondparam{0}%
%  \forcsvlist\decodesec{#1}}
%\newcommand{\decodesec}[1]{%
%  \ifthenelse{\secondparam=1}{, }{}%
%  \mbox{\textsc{\getdaydate{#1}}}
%  \def\secondparam{1}}
%
%% Patch imakeidx to map journal entry instead of page
%\makeatletter
%\patchcmd{\@index}{\@wrindex}{\@Wrindex}{}{}
%\let\@Wrindex\@wrindex
%\patchcmd{\@Wrindex}{\thepage}{\thejrnltotalday}{}{}
%\let\protected@iwrite\protected@write
%\patchcmd{\protected@iwrite}{\write}{\immediate\write}{}{}
%\patchcmd{\@Wrindex}{\protected@write}{\protected@iwrite}{}{}
%\makeatother
% \end{verbatim}
%
% \StopEventually{}
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
%    \begin{macrocode}
\ProvidesClass{scrjrnl}
\DeclareOption{scrartcl}{\def\jrnl@class{scrartcl}}
\DeclareOption{scrreprt}{\def\jrnl@class{scrreprt}}
\DeclareOption{scrbook}{\def\jrnl@class{scrbook}}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{\jrnl@class}}
\ExecuteOptions{scrbook}
\newif\if@jrnl@monthpages
\DeclareOption{monthpages}{\@jrnl@monthpagestrue}
\newif\if@jrnl@fancytabs
\DeclareOption{fancytabs}{\@jrnl@fancytabstrue}
\newif\if@jrnl@cleardaypages
\DeclareOption{cleardaypages}{\@jrnl@cleardaypagestrue}
\ProcessOptions\relax
\LoadClass{\jrnl@class}
\RequirePackage{titlesec}
%    \end{macrocode}
%
% \marginpar{Load datetime after babel}
%
%    \begin{macrocode}
\AfterPackage!{babel}{\RequirePackage{datetime}}
\newcommand\jrnlformatdate\formatdateny
\newcommand\jrnlformatindexdate\formatdateny
%    \end{macrocode}
%
% \marginpar{More datetime}
%
%    \begin{macrocode}
% formatdate without year
\def\formatdateny{\csname noyear\languagename\expandafter\endcsname\formatdate}

\def\noyearenglish#1, \the\@year{#1}
\let\noyearamerican\noyearenglish
\let\noyearbritish\noyearenglish
\def\noyearfrench#1\space\number\@year{#1}
\let\noyeargerman\noyearfrench
\let\noyearaustrian\noyeargerman
\let\noyearswedish\noyearfrench
\let\noyearbreton\noyearfrench
\def\noyearrussian#1\ \number\@year~\cyrg.{#1}
\def\noyearspanish#1\ de~\number\@year{#1}
\let\noyearcatalan\noyearspanish
\def\noyearbasque#1\number\@year.eko\space{#1}
%    \end{macrocode}
%
% \marginpar{Set default lengths}
%
%    \begin{macrocode}
\newlength{\jrnldaysepskip}
\setlength{\jrnldaysepskip}{0pt}
\newlength{\jrnldayafterskip}
\setlength{\jrnldayafterskip}{2\baselineskip}
%    \end{macrocode}
%
% \marginpar{Load fancytabs}
%
%    \begin{macrocode}
%%\if@jrnl@fancytabs
\RequirePackage{fancytabs}[2012/09/24]
%%\fi
%    \end{macrocode}
%
% \marginpar{Month names}
%
%    \begin{macrocode}
\newcommand{\jrnl@monthname}[1]{\monthname[#1]}
\newcommand{\jrnl@curmonth}{\jrnl@monthname{\thejrnlmonth}}
%    \end{macrocode}
%
% \marginpar{Setup counters}
%
%    \begin{macrocode}
\newcounter{jrnlstartchapter}
\newcounter{jrnlday}
\newcounter{jrnlmonth}
\newcounter{jrnlyear}
\newcounter{jrnltotalday}
%    \end{macrocode}
%
% \marginpar{HTML hooks for TeX4HT}
%
%    \begin{macrocode}
\newcommand{\HTMLchapHook}[1]{}
\newcommand{\HTMLsecHook}[2]{}
%    \end{macrocode}
%
% \marginpar{Define jrnlmonth}
%
%    \begin{macrocode}
\newcounter{jrnlmonthstartday}
\define@key{jrnlmonth}{month}{\setcounter{jrnlmonth}{#1}\addtocounter{jrnlmonth}{-1}}
\define@key{jrnlmonth}{startday}{\setcounter{jrnlmonthstartday}{#1}\addtocounter{jrnlmonthstartday}{-1}}

\newcommand{\jrnlmonth}[1][]{%
  % Use \thejrnlstatday if first month of journal
  \setcounter{jrnlmonthstartday}{\thejrnlstartday}%
  \setcounter{jrnlstartday}{0}%
  \setkeys{jrnlmonth}{#1}%
  \stepcounter{jrnlmonth}%
  \HTMLchapHook{\jrnl@curmonth}%
  \if@jrnl@monthpages
    \chapter{\jrnl@curmonth}%
    \cleardoublepage%
  \else
    \refstepcounter{chapter}%
    \@maybeautodot\thechapter
    \addchaptertocentry{\thechapter}{\jrnl@curmonth}%
  \fi
  \setcounter{jrnlday}{\thejrnlmonthstartday}%
}
%    \end{macrocode}
%
%
% \marginpar{Define jrnlday}
%
%    \begin{macrocode}
\newcommand{\jrnl@pagedate}{%
  \jrnlformatdate{\thejrnlday}{\thejrnlmonth}{\thejrnlyear}%
}
\define@key{jrnlday}{day}{\setcounter{jrnlday}{#1}\addtocounter{jrnlday}{-1}}
\define@key{jrnlday}{ante}{\def\jrnlday@ante{#1}}
\define@key{jrnlday}{post}{\def\jrnlday@post{#1}}

\newcommand{\jrnlday}[2][]{%
  \def\jrnlday@ante{}%
  \def\jrnlday@post{}%
  \setkeys{jrnlday}{#1}%
  \stepcounter{jrnlday}%
  \stepcounter{jrnltotalday}%
  \if@jrnl@cleardaypages
  \cleardoublepage
  \fi
  \newpage\section{#2}%
  % TODO: provide index macros to be used with jrnlpagedate\thepage
  \expandafter\xdef\csname jrnldaydate\thejrnltotalday\endcsname{%
    \noexpand\jrnlformatindexdate{\thejrnlday}{\thejrnlmonth}{\thejrnlyear}%
}%
  \HTMLsecHook{\jrnlday@ante\jrnlformatdate{\thejrnlday}{\thejrnlmonth}{\thejrnlyear}\jrnlday@post}{#2}%
}
%    \end{macrocode}
%
%
% \marginpar{Define fonts}
%
%    \begin{macrocode}
\newcommand\dayheadfont{}
\newcommand\monthheadfont{}
%    \end{macrocode}
%
%
% \marginpar{Chapter/section styles}
%
%    \begin{macrocode}
\newcommand\jrnl@titlestrut{\vrule height 25pt width0pt\relax}
\newcommand{\jrnl@secstyle}{\filcenter\dayheadfont\huge}
\newcommand{\jrnl@seclblstyle}{\raggedright\normalfont\scshape\Large}
\newcommand{\jrnl@seclbl}{\jrnl@seclblstyle\jrnlday@ante\jrnl@pagedate\jrnlday@post\\}

\newcommand{\jrnl@chapstyle}{\filcenter\monthheadfont\Huge\textsc}
%    \end{macrocode}
%
%
% \marginpar{Define journal environment}
%
%    \begin{macrocode}
\newcounter{jrnlstartmonth}
\newcounter{jrnlstartday}
\define@key{jrnl}{startmonth}{\setcounter{jrnlstartmonth}{#1}\addtocounter{jrnlstartmonth}{-1}}
\define@key{jrnl}{startday}{\setcounter{jrnlstartday}{#1}\addtocounter{jrnlstartday}{-1}}
\newlength{\jrnl@bleed}
\define@key{jrnl}{bleed}{\setlength{\jrnl@bleed}{#1}}

\newenvironment{journal}[1][]%
{%
\setcounter{jrnlstartmonth}{0}%
\setcounter{jrnlstartday}{0}%
\setkeys{jrnl}{#1}%
\setcounter{jrnlstartchapter}{\thechapter}
\addtocounter{jrnlmonth}{\thejrnlstartmonth}

\titleformat{\chapter}[block]
  {\vfill}
  {}{0pt}
  {\jrnl@chapstyle}
  [\vfill\vfill]

\if@jrnl@fancytabs
  % We know \paperheight and bleed
  % Calculation base:
  % 6*fancytabsHeight+2*fancytabsTop+5*fancytabsGap = paperheight
  % fancytabsHeight = 10*fancytabsGap
  % fancytabsTop = 5*fancytabsGap + 2*bleed

  % Calculate \calcfancytabsGap as (\the\paperheight-2*bleed)/75
  \newlength{\calcfancytabsGap}
  \pgfmathsetlength{\calcfancytabsGap}{\the\paperheight-2*\the\jrnl@bleed)/75}
  \fancytabsGap{\the\calcfancytabsGap}

  % Calculate \calcfancytabsHeight as 10*\calcfancytabsGap
  \newlength{\calcfancytabsHeight}
  \pgfmathsetlength{\calcfancytabsHeight}{10*\calcfancytabsGap}
  \fancytabsHeight{\the\calcfancytabsHeight}

  % Calculate \calcfancytabsTop as (5*\calcfancytabsGap+2\jrnl@bleed)
  \newlength{\calcfancytabsTop}
  \pgfmathsetlength{\calcfancytabsTop}{5*\calcfancytabsGap+2*\the\jrnl@bleed}
  \fancytabsTop{\the\calcfancytabsTop}

  % Calculate \fancytabsWidth, without bleed
  \newlength{\calcfancytabsWidth}
  \pgfmathsetlength{\calcfancytabsWidth}{\the\paperwidth/12}
  
  % Calculate \fancytabsWidthBleed, with bleed
  \newlength{\calcfancytabsWidthBleed}
  \pgfmathsetlength{\calcfancytabsWidthBleed}{\the\calcfancytabsWidth+\the\jrnl@bleed}
  \fancytabsWidth{\the\calcfancytabsWidthBleed}

  \fancytabsTextVPos{0.5}
  % Put text more inside
  % \fancytabsTextHPos = (\fancytabsWidth/2)/\fancytabsWidthBleed
  \pgfmathsetmacro{\calcfancytabsTextHPos}{(\the\calcfancytabsWidth/2)/(\the\calcfancytabsWidthBleed)}
  \fancytabsTextHPos{\calcfancytabsTextHPos}

  \newcommand{\jrnl@fancytab}{%
    \ifthenelse{\isodd{\thepage}}%
      {\fancytab{\jrnl@curmonth}{\thejrnlmonth}}%
      {}%
  }
\else
  \newcommand{\jrnl@fancytab}{}% 
\fi

\titleformat{\section}[display]
  {\jrnl@secstyle} % format
  {\jrnl@seclbl} % label
  {\jrnldaysepskip} %sep
  {\jrnl@fancytab%
   \jrnl@titlestrut\filcenter} %before
  [\vspace{\jrnldayafterskip}] %after

\titlespacing{\section}{0pt}{*}{-3pt}

} % end of \begin{devotional}
{} % end of \end{devotional}
%    \end{macrocode}
%
% \iffalse
%</package>
% \fi
%
% \Finale
\endinput
