% http://cr.yp.to/bib.html
%
% The bibliography format is a strict line-by-line format,
% similar to James Alexander's TIB,
% but with prefixes based on the old AMS-TeX bibliography format.
%
% Bibliography entries go into LaTeX papers
% between \begin{thebibliography}{999} and \end{thebibliography}.
% A bibliography entry starts with \bib and ends with \endref.

\def\bib#1{\catcode`\^^M=13\def\refno{#1}
   \let\refeditors\relax
   \let\refbyone\relax\let\refbytwo\relax\let\refbythree\relax
   \let\refpaper\relax\let\refjour\relax\let\refpaperyr\relax
   \let\refvol\relax\let\refpages\relax
   \let\refpaperinfo\relax
   \let\refinbook\relax
   \let\refbook\relax\let\refbookyr\relax
   \let\refedition\relax\let\refprinting\relax
   \let\refthesis\relax
   \let\refseries\relax
   \let\refseriesvol\relax
   \let\refbookinfo\relax
   \let\refpubl\relax\let\refpubladdr\relax
   \let\refyr\relax
   \let\refurl\relax\let\refid\relax
   \let\refolder\relax
   \let\refnewer\relax
   \let\refalso\relax
   \let\refmr\relax\let\refisbn\relax\let\refissn\relax}
\catcode`\^^A=3
\catcode`\^^M=13\def^^M{}
\def\by#1^^M{\ifx\refbyone\relax
   \def\refbyone{#1}\else\ifx\refbytwo\relax
   \def\refbytwo{#1}\else\ifx\refbythree\relax
   \def\refbythree{#1}\else\let\refbyold\refbyone
   \edef\refbyone{\refbyold, \refbytwo}\let\refbytwo\refbythree
   \def\refbythree{#1}\fi\fi\fi}
\def\editor#1^^M{\ifx\refbyone\relax
   \def\refbyone{#1}\def\refeditors{ (editor)}\else\ifx\refbytwo\relax
   \def\refbytwo{#1}\def\refeditors{ (editors)}\else\ifx\refbythree\relax
   \def\refbythree{#1}\else\let\refbyold\refbyone
   \edef\refbyone{\refbyold, \refbytwo}\let\refbytwo\refbythree
   \def\refbythree{#1}\fi\fi\fi}
\def\book#1^^M{\def\refbook{, \it #1\rm}}
\def\paper#1^^M{\def\refpaper{, \it #1\rm}}
\def\yr#1^^M{\def\refpaperyr{ (#1)}\def\refbookyr{, #1}}
\def\edition#1^^M{\def\refedition{, #1 edition}}
\def\printing#1^^M{\def\refprinting{, #1 printing}}
\def\phdthesis^^M{\def\refthesis{, Ph.D. thesis}}
\def\mathesis^^M{\def\refthesis{, M.A. thesis}}
\def\series#1^^M{\def\refseries{, #1}}
\def\seriesvol#1^^M{\def\refseriesvol{, #1}}
\def\bookinfo#1^^M{\def\refbookinfo{, #1}}
\def\publ#1^^M{\def\refpubl{, #1}}
\def\publaddr#1^^M{\def\refpubladdr{, #1}}
\def\seeolder#1^^M{\def\refolder{; see also older version #1}}
\def\seenewer#1^^M{\def\refnewer{; see also newer version #1}}
\def\also#1^^M{\def\refalso{; #1}}
\def\inbook#1^^M{\def\refinbook{, in #1}}
\def\paperinfo#1^^M{\def\refpaperinfo{, #1}}
\def\jour#1^^M{\def\refjour{, #1}}
\def\vol#1^^M{\def\refvol{ \bf#1\rm}}
\def\mr#1^^M{\def\refmr{. MR #1}} % Math Reviews number
\def\isbn#1^^M{\def\refisbn{. ISBN #1}}
\def\issn#1^^M{\def\refissn{. ISSN #1}}
\def\url#1^^M{\def\refurl{. URL: \showurl{#1}}}
\def\id#1^^M{\def\refid{. ID \def\next{}\showid #1^^A^^A}}
\def\pages#1^^M{\def\refpages{, #1}}
\catcode`\^^M=5

\def\showid{\ifx\next^^A\else{\tt\next\penalty500}\afterassignment\showid\fi\let\next=}

\def\endref{\catcode`\^^M=5{}\bibitem{\refno}
   {\ifx\refbyone\relax--- (no editor)\else\refbyone\ifx\refbytwo\relax\else
   \ifx\refbythree\relax, \refbytwo\else, \refbytwo, \refbythree\fi\fi\fi
   \refeditors
   \ifx\refpaper\relax\else
     \refpaper\refinbook\refpaperinfo
     \refjour\refvol\refpaperyr\refpages
   \fi
   \ifx\refbook\relax\else
     \refbook\refedition\refprinting
     \refthesis\refseries\refseriesvol\refbookinfo
     \refpubl\refpubladdr\refbookyr
   \fi
   \refolder\refnewer\refalso
   \refisbn\refissn\refmr\refurl\refid.}\par\ignorespaces}

\def\showurl#1{{\tt\hyphenchar\font=-1\def\nextpenalty{}\def\next{}\dosplit#1^^A^^A}}
\def\dosplit{%
  \ifx\next^^A\else
    \ifx\next/{/\gdef\nextpenalty{\penalty0}}\else
    \ifx\next.{.\gdef\nextpenalty{\penalty500}}\else
    \ifx\next:{:\gdef\nextpenalty{\penalty50}}\else
    \ifx\next?{?\gdef\nextpenalty{\penalty50}}\else
    \ifx\next={=\gdef\nextpenalty{\penalty50}}\else
    \ifx\next##{\char'043\gdef\nextpenalty{\penalty50}}\else
    \ifx\next&{\char'046\gdef\nextpenalty{\penalty50}}\else
    \ifx\next_{\char'137\gdef\nextpenalty{\penalty50}}\else
    \ifx\next({\penalty50(\gdef\nextpenalty{}}\else
    \ifx\next~{\penalty50\char'176\gdef\nextpenalty{}}\else
    {\nextpenalty\next\gdef\nextpenalty{}}%
    \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\afterassignment\dosplit
  \fi\let\next=}

\catcode`#=11
