%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Module loading                                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usemodule[cyrillicnumbers]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Letterspacing / emphases                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usemodule [letterspace]

\defineletterspace [largecaps]
\setupletterspace  [largecaps] [
  factor=.5,
  spaceskip=.4em,
  suppresskern=yes,
]

\def\pompoustitle#1{%
  \setuplocalinterlinespace[line=33pt]%
  \color[darkmagenta]{%
    \tfd\bold\WORD\largecaps{#1}%
  }%
}

\defineletterspace [mediumcaps]
\setupletterspace  [mediumcaps] [
  factor=.25,
  spaceskip=.5em,
  suppresskern=yes,
]

\definefontfeature[smallcaps][smcp=yes,mode=node,script=latn]

\def\mediumtitle#1{%
  \setuplocalinterlinespace[line=33pt]%
  \color[darkmagenta]{%
    \tfa\sc\mediumcaps{#1}%
  }%
}

\defineletterspace [smallcaps]
\setupletterspace  [smallcaps] [
  factor=.05,
  spaceskip=.25em,
  suppresskern=yes,
]

\defineletterspace [textemph]
\setupletterspace  [textemph] [
  factor=.125,
  spaceskip=.33em,
  suppresskern=no,
]

\defineletterspace [slightly]
\setupletterspace  [slightly] [
  factor=.075,
  spaceskip=.33em,
  suppresskern=no,
]

\let\te\textemph
\unexpanded\def\name#1{\smallcaps{\sc#1}}

\defineletterspace [ugly]
\setupletterspace  [ugly] [
  factor=.4,
  spaceskip=1em,
  suppresskern=no,
]


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Font Setups                                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\definefontfeature [default] [default] [
  protrusion=quality,
  expansion=quality,
  mode=node,
  script=latn,
  onum=yes,
  dlig=yes,
  liga=yes,
  kern=yes,
]

\definefontfeature [kerning] [kern=yes]%

\usetypescript [computer-modern-unicode]
\def\russian#1{\begingroup\language[ru]\setupbodyfont[computer-modern-unicode]#1\endgroup}

\starttypescript [serif] [bukyvede]
  % \setups [font:fallback:serif]
  \definefontsynonym [Serif]        [name:Bukyvede]        [features=default]
  \definefontsynonym [SerifItalic]  [name:Bukyvede-Italic] [features=default]
\stoptypescript
\usetypescript  [bukyvede]
\definetypeface [vintagecyrillic]       [rm] [serif]  [bukyvede]     [default] [encoding=ec]

\def\oldrussian#1{\begingroup\language[ru]\setupbodyfont[vintagecyrillic]#1\endgroup}

\usetypescript [modern]
\setupbodyfont [modern]
% \usetypescript [antykwa-poltawskiego]
% \setupbodyfont [antykwa-poltawskiego,10pt]

\usetypescript [serif] [hz] [highquality]
\setupalign    [hanging,hz]

\setupbodyfontenvironment [default] [12pt] [interlinespace=15pt]
\setupbodyfontenvironment [default] [11pt] [interlinespace=13pt]
\setupbodyfontenvironment [default] [10pt] [interlinespace=12pt]
\setupbodyfontenvironment [default] [9pt]  [interlinespace=11pt]
\setupbodyfontenvironment [default] [8pt]  [interlinespace=10pt]

\setupbodyfontenvironment [default] [em=italic]

\def\quote#1{\bgroup\italic#1\egroup}
\def\uprightslash{\bgroup\tf/\egroup}
\def\uprightomiss{\bgroup\tf[\dots]\egroup}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Presenting the Interface                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usemodule  [int-load]
\loadsetups [t-cyrillicnumbers.xml]

\define\beautifyshowsetups{%
  \unexpanded\def\setupnumfont  {\rm}%
  \unexpanded\def\setuptxtfont  {\rm}%
  \unexpanded\def\setupintfont  {\rm\sc\Word}%
  \unexpanded\def\setupvarfont  {\rm\it}%
  \unexpanded\def\setupoptfont  {\rm\it}%
  \unexpanded\def\setupalwcolor {darkmagenta}%
  \unexpanded\def\setupoptcolor {darkmagenta}%
  \defineframedtext [setuptext] [
    frame=off,
    background=color,
    backgroundcolor=gray:2,
    width=\hsize,
    height=fit,
    align=right,
    offset=0.75em,
  ]%
}

\let\Oldshowsetup\showsetup

\define[1]\showsetup{% hurray for diversity
  \bgroup\beautifyshowsetups%
    \Oldshowsetup{#1}%
  \egroup%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Paper                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\definepapersize[LHS][
  width=160mm,
  height=239mm,
]

\setuppapersize[LHS][LHS]

%\showframe
\setuplayout [
    width=122mm,
    %textheight=199mm, % ca. 47 rows * 12pt
    height=224mm, % text height should end up at ca. 47 rows * 12pt
    %height=fit,
    %
    topspace=14mm,
    header=12pt,
    headerdistance=4mm,
    top=00mm,
    %
    bottomspace=0mm,
    footer=23mm,
    bottom=0mm,
    footerdistance=0mm,
    %
    backspace=16mm,
    leftedge=0mm,
    leftedgedistance=0mm,
    leftmargin=16mm,
    leftmargindistance=1em,
    %
    rightmargin=20mm,
    rightmargindistance=2mm,
    rightedge=0mm,
    rightedgedistance=0mm,
]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Interaction                                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setupinteraction[%
  state=start,
  color=darkmagenta,
  contrastcolor=darkmagenta,
  %color=,
  %contrastcolor=,
  style=,
  focus=standard,
  title={Cyrillic Numbers Module},
  subtitle={Yet Another Number Converter},
  author={Philipp Gesang},
  keyword={ConTeXt, LuaTeX, cyrillic numerals},
]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Headings                                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\defineletterspace [LSchapter]
\defineletterspace [LSsection]
\defineletterspace [LSsubsection]
\setupletterspace  [LSchapter]    [factor=.1,  spaceskip=.33em,suppresskern=yes]
\setupletterspace  [LSsection]    [factor=.15, spaceskip=.40em,suppresskern=yes]
\setupletterspace  [LSsubsection] [factor=.125,spaceskip=.33em]

\def   \fontchapter#1{\setupbodyfont[10pt]\bold\WORD\LSchapter{#1}}
\def   \fontsection#1{\setupbodyfont[10pt]\word\sc\LSsection{#1}}
\def\fontsubsection#1{\setupbodyfont[10pt]\LSsubsection{#1}}

\def   \Chapterheadfontcmd{\fontchapter}
\def   \Sectionheadfontcmd{\fontsection}
\def\Subsectionheadfontcmd{\fontsubsection}

\def\empholdrussian#1{\russian{\italic#1}}

\definecyrnum[cyrnumone][
  command=\oldrussian,
  titlo=mp,
  titlomode=2,
  dots=no,
]

\definecyrnum[cyrnumtwo][
  command=\empholdrussian,
  titlo=mp,
  titlomode=7,
  dots=no,
]

\defineconversion[cyrnumone][\cyrnumone]
\defineconversion[cyrnumtwo][\cyrnumtwo]

\definestructureconversionset[regularstructure] [numbers,cyrnumone,cyrnumtwo] [cyrnumone]
\definestructureconversionset[regularstructure] [numbers,cyrnumone,cyrnumtwo] [cyrnumone]
% \definestructureconversionset[regularstructure] [numbers,romannumerals,romannumerals] [cyrnumone]

\setuphead [chapter] [
  align=middle,
  footer=text,
  grid=yes,
  header=empty,
  number=yes,
  sectionconversionset=regularstructure,
  % numberconversion=cyrnumone,
  page=yes,
  style=,
  textcommand=\Chapterheadfontcmd,
  before={\startlinecorrection\blank[3*line,force]},
  after={\stoplinecorrection\blank[line,force]},
]

\definetext [text] [footer] [pagenumber]

\setuphead [section] [
  align=middle,
  number=yes,
  page=no,
  style=,
  sectionconversionset=regularstructure,
  textcommand=\Sectionheadfontcmd,
  before={\blank[line]},
  after={\blank[line]},
]

% \setuphead [subsection] [
%   align=middle,
%   number=no,
%   page=no,
%   style=,
%   textcommand=\Subsectionheadfontcmd,
%   before={\blank[line]},
%   after={\blank[line]},
% ]


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% header and footers                                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setupfooter[text][% handy for occasional pagenumbers in footnotes at chapters
  style=\tfx,
]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ToC                                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def   \tocfontchapter#1{\tfx\WORD\LSchapter{#1}}
%\def   \tocfontsection#1{\word\sc\LSsection{#1}}
\def   \tocfontsection#1{\tfx\slightly{#1}}
\def\tocfontsubsection#1{\LSsubsection{#1}}

\setuplist [chapter] [
  alternative=c,
  interaction=text,
  textcommand=\tocfontchapter,
]

\setuplist [section] [
  alternative=b,
  interaction=text,
  margin=2em,
  numberstyle=,
  textcommand=\tocfontsection,
  textstyle=,
  numberstyle=\tfx,
]

% \setuplist [subsection] [
%   %after=2.5em,    % from the hack
%   alternative=d,
%   interaction=text,
%   margin=3em,     % hanging
%   textcommand=\tocfontsubsection,
% ]

\setuplistalternative[c] [
  %distance=0em,
  %width=0pt,
  stretch=.5em,
  command=\hskip.5em\phglistdots\hskip.5em\relax,
]

\def\phglistdots{\gleaders\hbox to 1em{\hss.\hss}\hfill}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Captions                                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% \setupcaptions[
%   location=bottom,
%   headstyle=\tfx\italic,
%   way=bytext,
%   prefixsegments=none,
%   style={\setupinterlinespace[9pt]\tfx},
% ]
% 
% \setupcaption [figure] [way=bytext]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Bibliography                                                  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Bib: Setups                                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setuppublications [
  alternative=ssa,
  refcommand=authoryear,
  %sorttype=bbl,
  sort=author,
  numbering=yes,
  autohang=yes,
]

\setuppublicationlist [
  artauthor=\invertedauthor,
]

\setupcite [authoryear] [compress=no]

%%% Used in bibliography formatting.
\definestartstop [bibindent] [
  before={\startnarrower[left]%
          \setupindenting[-\leftskip,yes,first]%
          \clubpenalty-9000%
          \widowpenalty-9000},
  after=\stopnarrower,
]

\unexpanded\def\ctay#1{\cite[authoryear][#1]}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Bib: Entries                                                  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\startpublication [
  k=trunte,
  t=inproceedings, % bibtex go to hell
  a={Trunte},
  y=2005,
  n=1,
  s={Altkirchenslavisch},
]
  \author[]{Nikolaos H.}[]{}{Trunte}
  \pubyear{2005}
  \arttitle{Altkirchenslavisch}
  \title{\oldrussian{Словѣньскъи ѩꙁъікъ}.  Ein praktisches Lehrbuch des
         Kirchenslavischen in 30 Lektionen. Zugleich eine
         Einführung in die slavische Philologie}
  \edition{3}
  \city{München}
\stoppublication

\startpublication [
  k=zolobov,
  t=inproceedings,
  a={Žolobov},
  y=2006,
  n=2,
  s={Čislitelʹnye},
]
  \author[]{O. F.}[]{}{Žolobov}
  \pubyear{2006}
  \arttitle{Čislitelʹnye}
  \title{Istoričeskaâ grammatika drevnerusskogo âzyka}
  \pages{58--63}
  \editor[]{S.I.}[]{}{Iordanidi}
  \editor[]{V.B.}[]{}{Krysʹko}
\stoppublication

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Misc                                                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\clubpenalty  -7000
\widowpenalty -7000

\def\etc{{\italic\letterampersand}c}

\setupindenting [yes,next,medium]

\useURL [leibnizausgabe] [http://www.leibniz-edition.de/Baende/] [] [\name{Leibniz}-Edition]
\useURL [soulpackage] [http://ctan.larsko.net/macros/latex/contrib/soul/] [] [soul]

\pdfcompresslevel9

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Makeup for Front Matter                                       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\definemakeup [FM] [standard]
\setupmakeup [FM] [
  width=125mm,
  height=172.506mm,
  location=middle,
]

\setupheadertexts [] [] [] []
\setuppagenumbering [state=stop,location=]

\starttext

\startfrontmatter
\setuplayout [width=middle]
\startFMmakeup
  \raggedcenter
  \vfill
  {\tfc\italic Typesetting}\par
  \vfill
  % {\tfd\WORD\largecaps{Cyrillic Numerals}}\par
  \pompoustitle{Cyrillic Numerals}\par
  \vfill
  {\tfc\italic with {\CONTEXT} MkIV}\par
  \vfill
  % {\tfa\sc \mediumcaps{A Module}}\par
  \mediumtitle{A Module}\par
  \vfill
\stopFMmakeup

\page

\useURL [phg-mail] [mailto:gesang@stud.uni-heidelberg.de] [] [\hyphenatedurl{gesang@stud.uni-heidelberg.de}]
\useURL [phg-bibu] [https://bitbucket.org/phg/]    [] []

% \def\cyrillicalphabet{%
%   А,Б,В,Г,Д,Е,Ж,Ѕ,ʐ,И,І,К,Л,М,Н,О,П,Р,С,Т,ОУ,Ф,%
%   Х,Ѡ,Ц,Ч,Ш,Щ,Ъ,Ы,Ь,Ѣ,Ю,Ι,Ѥ,Ѧ,Ѫ,Ѩ,Ѭ,Ѯ,Ѱ,Ѳ,Ѵ,%
% }

\def\showoldrussian#1{\begingroup\language[ru]\setupbodyfont[vintagecyrillic]\tfb#1\endgroup}
\def\numbershowcase{%
  \bgroup
  \setupindenting[no]
  \definecyrnum[showcasecyrnum][
    dots=yes,
    titlo=mp,
    titlomode=\the\thistitlomode,
    command=\showoldrussian,
    titlocolor=magenta,
  ]
  \newdimen\skipincrement \skipincrement=0.047619047619048\hsize
  \newdimen\showskip      \showskip=\zeropoint
  \newconditional\checker \setfalse\checker
  \newcount\thistitlomode \thistitlomode=1
  \startlines
    \dorecurse{20}{%
      \getrandomcount\thistitlomode{1}{9}%
      \dontleavehmode\hbox to\showskip{}%
      \ifconditional\checker
        \setfalse\checker
        \colored[darkmagenta]{\showcasecyrnum{\recurselevel}}%
      \else
        \settrue\checker
        \showcasecyrnum{\recurselevel}%
      \fi%
      \advance\showskip by \skipincrement
      \crlf%
    }
  \stoplines
  \egroup
}

\startstandardmakeup
  \numbershowcase
  \vfill\raggedright\tfx
  © 2012-01-29 {\italic Philipp Gesang}, Heidelberg\par
  The latest Version can be found at \from [phg-bibu].\par
  Mail bugs and fixes or complaints and suggestions to \from
  [phg-mail].\par
\stopstandardmakeup
\stopfrontmatter

\startbodymatter
\page [odd]

\setuppagenumber [number=1]
\setuppagenumbering [
  state=start,
  alternative=doublesided,
  location={right,header},
]

\title{Content}

\placelist[chapter,section][criterium=all]

\def\headertextformat{\tfx\word\sc}

\setupheadertexts
  [{\headertextformat\getmarking[chapter]}] [{\headertextformat\pagenumber}]
  [{\headertextformat\pagenumber}]          [{\headertextformat\getmarking[chapter]}]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Introduction}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Functionality}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Default Command}
\showsetup{cyrnum}
\showsetup{cyrnumdrawtitlo}

% \cyrnumdrawtitlo{foobar}

\section{Customized Commands}
\showsetup{setupcyrnum}
\showsetup{definecyrnum}

\section{Counters}

\stopbodymatter

\startbackmatter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{License}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Copyright 2012 \te{Philipp Gesang}. All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

\startitemize[n]
  \item Redistributions of source code must retain the above
    copyright notice, this list of conditions and the following
    disclaimer.
  \item Redistributions in binary form must reproduce the
    above copyright notice, this list of conditions and the
    following disclaimer in the documentation and/or other
    materials provided with the distribution.
\stopitemize

\begingroup
\setuptolerance [horizontal,strict]
\startalignment [right,nothyphenated]
\noindentation\sc\startsmallcaps
  this software is provided by the copyright
  holder “as is” and any express or implied warranties,
  including, but not limited to, the implied warranties of
  merchantability and fitness for a particular purpose are
  disclaimed. in no event shall the copyright holder or
  contributors be liable for any direct, indirect, incidental,
  special, exemplary, or consequential damages (including, but
  not limited to, procurement of substitute goods or services;
  loss of use, data, or profits; or business interruption)
  however caused and on any theory of liability, whether in
  contract, strict liability, or tort (including negligence or
  otherwise) arising in any way out of the use of this software,
  even if advised of the possibility of such damage.
\stopsmallcaps\endgraf
\stopalignment
\endgroup


\chapter{References}

\startbibindent
  \placepublications[criterium=all]
\stopbibindent

\stopbackmatter

\stoptext
