%D \module
%D   [       file=m-database,
%D        version=2010.08.04,
%D          title=\CONTEXT\ Modules,
%D       subtitle=Database Thingies,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]

%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D For Mojca.

\registerctxluafile{m-database}{1.001}

\unprotect

\definenamespace
  [db]
  [type=module,
   comment=database module,
   version=1,
   name=database,
   parent=db,
   setup=yes,
   command=yes]

\setupdatabase
  [\c!separator={,},
   \c!quotechar=,
   \c!commentchar=,
   \c!strip=\v!no,
   \c!before=,
   \c!after=,
   \c!first=,
   \c!last=,
   \c!left=,
   \c!right=]

% \let\currentdatabase\empty \the\everypresetdatabase % or just:

\setupdatabase
  [\c!separator={,}]

\def\module_database_process#1#2#3%
  {\edef\currentdatabasetype{#1}%
   \edef\currentdatabase    {#2}%
   \edef\currentdatabasename{#3}%
   \ifx\currentdatabasename\empty
     \let\currentdatabasename\currentdatabase
     \let\currentdatabase\empty
   \fi
   \ctxlua{buffers.database.process {
     name        = "\currentdatabase",
     type        = "\currentdatabasetype",
     database    = "\currentdatabasename",
     strip       = "\databaseparameter\c!strip",
     separator   = \!!bs\databaseparameter\c!separator  \!!es,
     quotechar   = \!!bs\databaseparameter\c!quotechar  \!!es,
     commentchar = \!!bs\databaseparameter\c!commentchar\!!es,
     setups      = \!!bs\databaseparameter\c!setups     \!!es,
     before      = \!!bs\databaseparameter\c!before     \!!es,
     after       = \!!bs\databaseparameter\c!after      \!!es,
     first       = \!!bs\databaseparameter\c!first      \!!es,
     last        = \!!bs\databaseparameter\c!last       \!!es,
     left        = \!!bs\databaseparameter\c!left       \!!es,
     right       = \!!bs\databaseparameter\c!right      \!!es,
     command     = \!!bs\databaseparameter\c!command    \!!es,
  }}}

\unexpanded\def\processdatabasebuffer{\dodoubleempty\module_database_process_buffer}
\unexpanded\def\processdatabasefile  {\dodoubleempty\module_database_process_file}

\def\module_database_process_buffer[#1][#2]{\module_database_process{buffer}{#1}{#2}}
\def\module_database_process_file  [#1][#2]{\module_database_process{file}  {#1}{#2}}

% for old times sake:

\unexpanded\def\defineseparatedlist {\dodoubleempty\module_database_separated_list_define}
\unexpanded\def\processseparatedfile{\dodoubleempty\module_database_separated_list_process}

\def\module_database_separated_list_define[#1][#2]%
  {\definedatabase[#1][#2]%
   \setuvalue{\e!start#1}{\grabbufferdatadirect{#1}{\e!start#1}{\e!stop#1}}%
   \setuvalue{\e!stop#1}{\processdatabasebuffer[#1][#1]}}

\def\module_database_separated_list_process[#1][#2]%
  {\processdatabasefile[#1][#2]}

\unexpanded\def\startseparatedlist[#1]% to be interfaced
  {\unexpanded\def\stopseparatedlist{\processdatabasebuffer[#1][#1]}%
   \grabbufferdatadirect{#1}{startseparatedlist}{stopseparatedlist}}

\let\setupseparatedlist\setupdatabase

\protect

\continueifinputfile{m-database.mkiv}

\starttext

% m-database.txt
%
% 1,2,3,4,5
% 6,7,8,"9,x",0
% A,B,C,D
% E,,F
% G

\definedatabase[test]

\setupdatabase
  [test]
  [separator={,},
   quotechar={"},
   before={<},
   after={>},
   first={\endgraf[},
   last={]\endgraf},
   left={ (},
   right={) },
   command=\ruledhbox]

\startbuffer[testbuffer]
1,2,3,4,5
6,7,8,"9,x",0
A,B,C,D
E,,F
G
\stopbuffer

\processdatabasebuffer[test][testbuffer]

\processdatabasefile[test][m-database.txt]

\defineseparatedlist
  [CSV]
  [separator={,},
   before=\bTABLE,after=\eTABLE,
   first=\bTR,last=\eTR,
   left=\bTD,right=\eTD]

% \startseparatedlist[CSV]
% a,b,c
% d,e,f
% \stopseparatedlist

\startCSV
a,b,c
d,e,f
\stopCSV

\defineseparatedlist
  [CSV]
  [separator={,+},quotechar={"'},commentchar=\letterhash,
   before={\starttabulate[|l|l|l|]},after=\stoptabulate,
   first=\NC,last=\NR,
   left=,right=\NC]

\startCSV
#a,b,"c,d"
a,b,"c,d"
a,'b,c',d
"a,b"+c+d
\stopCSV

\defineseparatedlist
  [CSV]
  [separator=space,
   first=\NC,last=\NR,
   left=,right=\NC,
   before={\starttabulate[|l|l|l|]},after=\stoptabulate]

\startCSV
a b c
d e f
\stopCSV

\startsetups csv:unix
  \catcode\hashasciicode\commentcatcode
\stopsetups

\defineseparatedlist
  [CSV]
  [setups=csv:unix,
   separator={,},
   first=\NC,last=\NR,
   left=,right=\NC,
   before={\starttabulate[|l|l|l|]},after=\stoptabulate]

\processseparatedfile[CSV][m-database.txt]

\defineseparatedlist[CSV]
  [separator=comma,
   before=\bTABLE, after=\eTABLE,
   first=\bTR, last=\eTR,
   left=\bTD, right=\eTD]

\startCSV
a,b,c,č
d,e,f,š
\stopCSV

\startseparatedlist[CSV]
a,b,c,č
d,e,f,š
\stopseparatedlist

\stoptext
