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

\usemodule[cyrillicnumbers]
\definecyrnum[normaltextcyrnum][
  command=\oldrussian,
  dots=no,
  titlo=no,
]

\definecyrnum[rednum][
  command={\switchtocolor[red]},
]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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]%

\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]

\usetypescriptfile [type-gfsbodoni.mkiv]
\usetypescript     [gfs-bodoni]
\usetypescriptfile [type-imp-computer-modern-unicode.mkiv]
\usetypescript     [computer-modern-unicode]

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

\def\oldrussian#1{\begingroup\language[ru]\setupbodyfont[vintagecyrillic]#1\endgroup}
\def\greek     #1{\begingroup\language[agr]\setupbodyfont[computer-modern-unicode]#1\endgroup}

\usetypescript [modern]
\starttypescript [mymodern]
  \definetypeface [mymodern] [rm] [serif] [modern]                  [default]
  \definetypeface [mymodern] [tt] [mono]  [computer-modern-unicode] [default]
\stoptypescript %% this will do as lm and cmu are acceptably similar
\usetypescript [mymodern]
\setupbodyfont [mymodern]

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

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

\setupbodyfontenvironment [default] [em=italic]

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

% \setuptyping[style=\tfx\sans]

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

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

\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=gray92,
    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,
]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Margins                                                       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% loosely based on: http://archive.contextgarden.net/message/20110804.141422.6e52d0bc.en.html
\define[1]\marginhintbox{%
  \toplinebox{\rotate{\tfx\tt #1}}%
}

\definemargindata[marginhint][inmargin][%
  style={\ss\tfx},
  location=inner,
  align=outer,
  command=\marginhintbox,
  % stack=yes,
]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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,
]

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

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

\startpublication [
  k=fonts,
  t=book, % bibtex go to hell
  a={Hagen/Hoekwater},
  y=2011,
  n=1,
  s={Fonts},
]
  \author[]{Hans}[]{}{Hagen}
  \author[]{Taco}[]{}{Hoekwater}
  \pubyear{2011}
  \title{Fonts in \CONTEXT}
  \city{Hasselt}
\stoppublication

\startpublication [
  k=trunte,
  t=inproceedings, % bibtex go to hell
  a={Trunte},
  y=2005,
  n=2,
  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=3,
  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 [petr]         [http://www.paratype.ru/e-zine/issue04/peter1/peter1a.htm] [] [\hyphenatedurl{http://www.paratype.ru/e-zine/issue04/peter1/peter1a.htm}]
\useURL [reform1917]   [http://ru.wikipedia.org/wiki/Реформа_русской_орфографии_1918_года] [] [\hyphenatedurl{http://ru.wikipedia.org/wiki/Реформа_русской_орфографии_1918_года}]

\useURL [pttypescript] [http://archive.contextgarden.net/message/20110105.204326.d0228ca7.en.html]

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


\pdfcompresslevel9

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

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

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

\startbuffer[showcase1]
  local tab = { }
  tab[#tab+1] = [[\placetable[right,3*hang][numval]{Number values of the Cyrillic alphabet}{\starttabulate[|r|]]..string.rep("l|", 9).."]"
  tab[#tab+1] = [[\NR\NC $n$]]
  for i=1, 9 do tab[#tab+1] = [[\NC $]]..i.."$" end
  tab[#tab+1] = [[\NC\NR\HL\NC $n · 10^0$]]
  for i=1, 9 do tab[#tab+1] = [[\NC\normaltextcyrnum{]]..i.."}" end
  tab[#tab+1] = [[\NC\NR\NC $n · 10^1$]]
  for i=10, 90, 10 do  tab[#tab+1] = [[\NC\normaltextcyrnum{]]..i.."}" end
  tab[#tab+1] = [[\NC\NR\NC $n · 10^2$]]
  for i=100, 900, 100 do  tab[#tab+1] = [[\NC\normaltextcyrnum{]]..i.."}" end
  tab[#tab+1] = [[\stoptabulate\blank[force,2*big]}]]
  context(table.concat(tab))
\stopbuffer

\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

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\startchapter[title=Introduction]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The \CONTEXT\ format comes with a collection of conversion
routines for different number systems that are specified in the
files \type{core-con.mkiv} and \type{core-con.lua}.
\te{Cyrillic numerals}, however, are not part of this collection.
The aim of the module at hand is to provide means of handling
Cyrillic numbers and make them seamlessly integrate with the
existing interface for number conversion.

If you are familiar with Cyrillic numbers, you might choose to
skip the rest of this section and instead continue in medias res
with the description on the module’s usage in the section on
\about[functionality].

\ctxluabuffer[showcase1]
\indentation The Cyrillic system, like the alphabet
it is based on, originated from the Greek numerals and thus
continues many features of the latter.\footnote{%
  Thorough examinations of how Cyrillic numbers were used in
  praxi are hard to find.
  The best that the couple of bookshelfs dedicated to grammar in
  a local department of Slavonic languages has to offer appears
  to be \ctay{zolobov}.
  Another work, \ctay{trunte}, although it follows a less
  descriptive but rather instructional approach, deserves
  mentioning as well.
}
As with the Roman number system, there are no genuine glyphs
reserved for numerals, instead numbers are represented by strings
of letters from the ordinary alphabet, organized in a peculiar
way.
The first nine digits, in ascending order, are:
\dorecurse{9}{\ifnum\recurselevel=9 and \fi%
              “\normaltextcyrnum{\recurselevel}”%
              \ifnum\recurselevel<9, \fi}.
As you might have noticed, this series does not correspond to the
first nine glyphs of the Cyrillic alphabet
(\oldrussian{а б в г д е ж ѕ з}), but rather to the order of the
original Greek letters from which they were derived
(\greek{α β γ δ ε ϛ ζ η θ}, with the character \greek{ϛ} at
position №~6 representing “stigma”).
The inherited order of numerical values was kept, essentially
trading backward compatibility for simplicity.
The two other sets of letters that represent multiples of ten and
hundred are listed in \in{Table}[numval].
The digits are written in descending order, beginning with the
most significant one.
The numbers from 11 to 19 follow a different rule for their order
mimicks the spoken language, which means that the \te{less}
significant digit \te{precedes} the more significant one
(\dostepwiserecurse{11}{18}{1}{\normaltextcyrnum{\recurselevel}, }%
\normaltextcyrnum{19}).
There are no glyphs to represent zeros, so they are simply left
out.
For example, in the Cyrillic system the number 42 is written as
\normaltextcyrnum{42}; the lack of a distinct zero sign causes
402 to have \te{two} digits as well, but the character
representing the digit 4 gets chosen from the hundreds set:
\normaltextcyrnum{402}.

% above 10^3
The rules so far don’t allow for numbers above 999.
To compensate for the lack of additional letters, greater numbers
are represented by the same glyphs (their value being padded by
1000).
There are two ways to avoid confusion: Each digit may be prefixed
with a special character, the thousands sign \oldrussian{҂}.
That way, 42 is still written \normaltextcyrnum{42}, but 42~000
becomes \normaltextcyrnum{42000}, and their sum
\normaltextcyrnum{42042}.
But if numbers become bigger, the high digits can take
alternative ornate forms:
\space\normaltextcyrnum[preferhundredk=yes]{100000} -- instead of
\normaltextcyrnum{100000} -- for 100~000, and
\space\normaltextcyrnum{1000000} for 1~000~000.

% dots, titlo
In an environment where punctuation was at best minimal and
interword spacing a luxury, numbers of this kind tend to be
confused with text.
Therefore, a Cyrillic number can have additional markers.
\te{Dots} are used as delimiters before and after a number:
\normaltextcyrnum[dots=yes]{42}.
Additionally, a number may be indicated by the \te{titlo}, which
may span its whole length or just parts of it:
\normaltextcyrnum[titlo=mp]{42}.

The Cyrillic number module combines all the above mentioned
aspects into one handy command generator, relying on Lua for the
conversion routine and \METAPOST\ for the titlo placement.
With all options in one place, it is trivial to create and
maintain different conversion settings for different purposes.

\stopchapter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\startchapter[reference=functionality,title=Functionality]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\startsection[title=Setup]

The module is initialized as any other:

\starttyping
\usemodule[cyrillicnumbers]
\stoptyping

Once the module code is loaded, the \te{setup} command provides
means to configure all the functionality it offers.

\showsetup{setupcyrnum}

Let’s walk through the options one by one.\marginhint{dots}
As was hinted in the introduction a common praxis is to delimit
Cyrillic numbers with dots.
Dot placement is enabled or disabled by setting the \type{dots}
key to {\italic yes} or {\italic no} respectively.
The \type{dotsymbol} key allows the user to supply a delimiter of eir
own choice; it defaults to the character “·” (unicode
U+00B7).\marginhint{dotsymbol}
If a font doesn’t contain a glyph for this code point or for
whatever reason another symbol is required, the solution will
look like this: \type{\setupcyrnum[dots=yes,dotsymbol=\cdot]}.
The result of this modification looks as follows:
\normaltextcyrnum[dots=yes,dotsymbol=\cdot]{42}.
As the dot can be an arbitrary symbol, it could be replaced by
the asterisk: \normaltextcyrnum[dots=yes,dotsymbol=*]{42}, or --
even weirder -- the hash character (“Gartenzaun”):
\normaltextcyrnum[dots=yes,dotsymbol=\#]{42}.

In order to notate the sixth decimal digit (for multiples of one
hundred million) there are two mutually excluding
options\marginhint{preferhundredk}: either the hundred thousands
sign or the thousands prefix may be employed.
Thus, the setup key \type{preferhundredk} determines which one
will be chosen. If set to {\italic yes}, then it’s going to be
the houndred thousands sign, else the regular thousands sign:
\normaltextcyrnum[preferhundredk=yes]{900000} $=$
\normaltextcyrnum[preferhundredk=no] {200000} $+$
\normaltextcyrnum[preferhundredk=no] {700000}.
(As is obvious from this example, the visual quality of the
houndred thousands sign, which is a separate glyph, depends on
the font used.)

Not every font contains proper glyphs for the entire Cyrillic
unicode range, in fact every dedicated font for one Cyrillic
alphabet -- Russian, say -- might not contain all the characters
needed to represent every Cyrillic numeral.\marginhint{command}
This is the result of the historical development the respective
scripts went through.
This process usually lead to the elimination of several glyphs at
different stages of the development.
For example the Russian alphabet experienced one significant
reduction of letters at the hand of emperor Peter~I\footnote{%
  For an overview cf. \from[petr] or just google
  \quotation{\russian{гражданский шрифт}}.
}
and another one later in 1917 as a consequence of -- not only --
the revolution.\footnote{%
  Cf. \from[reform1917].
}
Thus, chances are that in order to represent Cyrillic numbers,
which rely on a superset of the modern Russian alphabet, another
font needs to be chosen.\footnote{%
  Font fallbacks are another option if the substitutes match the
  main font typographically; see \ctay{fonts}, pp. 97--99.
}
The Cyrillic Numbers module provides a hook for this kind of
customization: you may define your own font switching macro and
assign it to the \type{command} key of the setup.
Suppose you decide to typeset your numbers using the Paratype
Serif font.\footnote{%
  A typescript can be found on the \CONTEXT\ mailing list:
  \from[pttypescript]. Keep in mind that ParaType Serif itself is
  not a particularly suitable number font as it does not contain
  all required glyphs.
}

\starttyping

%%% 1. Load the module and the font.
\usemodule         [cyrillicnumbers]
\usetypescriptfile [type-paratype.mkiv]

%%% 2. Define the font and a command that switches to it.
\definetypeface [numberfont] [rm]      [serif]
                [paratype]   [default] []
\unexpanded\define[1]\numbercommand{%
  \begingroup
    \language[ru]%
    \setupbodyfont[numberfont]%
    #1%
  \endgroup%
}

%%% 3. Hook the command into our converter.
\setupcyrnum [dots=yes,command=\numbercommand]

%%% 4. Use as needed.
\starttext
Normal text \cyrnum{42} \dots
\stoptext

%%% 5. ????

%%% 6. Profit!!!!!

\stoptyping

Naturally, the \type{command} may do anything that fits inside a
one-argument macro, from coloring (\rednum{42}) to case
manipulation
(\normaltextcyrnum[command=\WORD\oldrussian]{42}).

\start\setuptolerance[horizontal,tolerant,stretch]
\placetable[left][]{Comparison of different values for the parameter \type{penwidth}.}{%
  \starttabulate[|r|r|l|r|r|l|]%% tabulate sucks hard, just try to use it with a macro that contains a numberconversion and you’ll know why
    \let\lp\letterpercent
    \startluacode
      local max      = 10
      local step     = .2
      local dimleft  = .4
      local f_row = [[\NC \lp d \NC \lp .3fpt \NC \cyrnum[penwidth=\lp dpt]{\lp d} \NC \lp d \NC \lp .3fpt \NC \cyrnum[penwidth=\lp dpt]{\lp d} \AR]]
      for i=1, max do
        dimleft = dimleft + step
        local dimright = dimleft + max * step
        context(string.format(f_row, i, dimleft, dimleft, i, i+4200, dimright, dimright, i+4200))
      end
    \stopluacode
  \stoptabulate
}
The \METAPOST\ drawing options have separate keys.
Of these, \type{penwidth} rather obviously determines the width
of the pen that is used when drawing a titlo.
Finding the optimal width can involve a lot of testing;
as a rule, the bigger the font, the wider the pen should be.

\stop

\stopsection

\startsection[title=Predefined Commands]

\showsetup{cyrnum}
\showsetup{cyrnumdrawtitlo}

% \cyrnumdrawtitlo{foobar}
\stopsection

\startsection[title=Command Derivation]
\showsetup{definecyrnum}

\stopsection

\stopchapter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\startchapter[reference=usage,title=Usage and Precautions]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\startsection[title=Counters]
\stopsection

\startsection[title=The Titlo]
\stopsection

\startsection[title=Font Issues]
\stopsection

\stopchapter

\stopbodymatter

\startbackmatter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\startchapter[title=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

\stopchapter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\startchapter[title=References]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\startbibindent
  \placepublications[criterium=all]
\stopbibindent

\stopchapter

\stopbackmatter

\stoptext
