From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/39284 Path: news.gmane.org!not-for-mail From: Peter =?utf-8?Q?M=C3=BCnster?= Newsgroups: gmane.comp.tex.context Subject: t-inifile, my first experience with lua Date: Mon, 25 Feb 2008 23:01:12 +0100 Message-ID: <20080225220112.GA1981@gaston.couberia.bzh> Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="/9DWx/yDrRhgMJTb" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1203976965 3020 80.91.229.12 (25 Feb 2008 22:02:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 25 Feb 2008 22:02:45 +0000 (UTC) To: ConTeXt list Original-X-From: ntg-context-bounces@ntg.nl Mon Feb 25 23:03:09 2008 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from ronja.vet.uu.nl ([131.211.172.88] helo=ronja.ntg.nl) by lo.gmane.org with esmtp (Exim 4.50) id 1JTlPe-0000Tx-B7 for gctc-ntg-context-518@m.gmane.org; Mon, 25 Feb 2008 23:03:06 +0100 Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 15EA41FB4E; Mon, 25 Feb 2008 23:02:33 +0100 (CET) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 26347-01-11; Mon, 25 Feb 2008 23:01:54 +0100 (CET) Original-Received: from ronja.vet.uu.nl (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 5C8B51FADB; Mon, 25 Feb 2008 23:01:53 +0100 (CET) Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 57CA01FA4C for ; Mon, 25 Feb 2008 23:01:50 +0100 (CET) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 24220-03-5 for ; Mon, 25 Feb 2008 23:01:14 +0100 (CET) Original-Received: from smtp2b.orange.fr (smtp2b.orange.fr [80.12.242.146]) by ronja.ntg.nl (Postfix) with ESMTP id 3AEFF1FB30 for ; Mon, 25 Feb 2008 23:01:13 +0100 (CET) Original-Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf2b24.orange.fr (SMTP Server) with ESMTP id 6EC2370000A1 for ; Mon, 25 Feb 2008 23:01:13 +0100 (CET) Original-Received: from gaston.couberia.bzh (ARennes-358-1-82-166.w90-54.abo.wanadoo.fr [90.54.17.166]) by mwinf2b24.orange.fr (SMTP Server) with ESMTP id 233677000097 for ; Mon, 25 Feb 2008 23:01:13 +0100 (CET) X-ME-UUID: 20080225220113144.233677000097@mwinf2b24.orange.fr Original-Received: by gaston.couberia.bzh (Postfix, from userid 1000) id 9FE7B30670; Mon, 25 Feb 2008 23:01:12 +0100 (CET) Mail-Followup-To: ConTeXt list Content-Disposition: inline User-Agent: Mutt/1.5.17 (2007-11-01) X-Virus-Scanned: amavisd-new at ntg.nl X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.9 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: ntg-context-bounces@ntg.nl Errors-To: ntg-context-bounces@ntg.nl X-Virus-Scanned: amavisd-new at ntg.nl Xref: news.gmane.org gmane.comp.tex.context:39284 Archived-At: --/9DWx/yDrRhgMJTb Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Hello, Hey, it's really fun to code in lua: pattern matching, easy table manipulation and so on, I should have learned it earlier! Since it's my first experience with lua, I would like to ask you for comments on the t-inifile.tex module, attached to this message. I tried to implement a demo-mode, but there are two small problems: - I don't know how to use \savebuffer, all line-endings disappear in the saved file - the utf-thinspace is ignored Cheers and thanks a lot to the luatex and MKIV developers! Peter -- http://pmrb.free.fr/contact/ --/9DWx/yDrRhgMJTb Content-Type: text/x-tex; charset=utf-8 Content-Disposition: attachment; filename="t-inifile.tex" Content-Transfer-Encoding: quoted-printable %D \module %D [ file=3Dt-inifile, %D version=3D2008.02.25, %D title=3D\CONTEXT\ User Module, %D subtitle=3DFormatting of ini-files, %D author=3DPeter M=C3=BCnster, %D date=3D\currentdate, %D copyright=3D{Peter M=C3=BCnster}] %C This module is copyrighted by Peter M=C3=BCnster. %C Please send any comments to pmrb at free.fr. \writestatus{loading}{Formatting of ini-files} \unprotect \startluacode -- namespace inifile =3D {} -- just for debugging: print the table to stdout local function print_table() for i =3D 1,#inifile.t do print("") print(i, inifile.t[i].key) for k, v in pairs(inifile.t[i]) do print("", k, v) end print("") end end -- sort the table -- s1: primary key -- s2: secondary key local function sort_table(s1, s2) local function cmp(a, b) if s2 ~=3D "" and a[s1] =3D=3D b[s1] then return a[s2] < b[s2] else return a[s1] < b[s1] end end if s1 ~=3D "" then table.sort(inifile.t, cmp) end end -- write default values to table entry -- d: the default values separated by commas -- i: the index of the entry local function write_defaults(d, i) for k, v in string.gmatch(d, "[,%s]*(.-)=3D([^,]*)") do inifile.t[i][k] =3D v end end -- generate table from ini-file -- d: default values for all entries -- s1: primary sort-key -- s1: secondary sort-key function inifile.make_table(d, s1, s2) inifile.t =3D {} local i =3D 0 while true do local l =3D io.read() if not l then break end while true do -- check for new entry: key =3D string.match(l, "^%[(.+)%]$") if key then i =3D i + 1 inifile.t[i] =3D {} inifile.t[i]["key"] =3D key write_defaults(d, i) break -- continue ! end -- treat continued lines: while string.match(l, "\\$") do local c =3D io.read() l =3D string.match(l, "^(.*)\\$") .. c end -- get a new key value pair: key, val =3D string.match(l, "^([%w_]+)%s*=3D%s(.*)$") if key then inifile.t[i][key] =3D val end break end end sort_table(s1, s2) print_table() end -- let ConTeXt print the sorted table with user defined formatting -- c: the user supplied command to print one entry function inifile.print(c) for i =3D 1,#inifile.t do tex.print(string.format("%s\\def\\IF@index{%d}%s\\%s", "\\unprotect", i, "\\protect", c)) end end -- initialise the new entry, in general to be called in the beginning -- of the user supplied formatting command -- i: the index of the new entry function inifile.newentry(i) for k, v in pairs(inifile.t[i]) do tex.print(string.format("\\def\\IF%s{%s}", k, v)) end end -- filter applied to values of a key -- k: the key -- s: the search pattern -- r: the replace string function inifile.filter(k, s, r) for i =3D 1,#inifile.t do inifile.t[i][k] =3D string.gsub(inifile.t[i][k], s, r) end end \stopluacode \def\setupIniFile[#1]{ \getparameters[IF@][defaults=3D,sortA=3D,sortB=3D,#1] \ctxlua{io.input("\IF@file")} \ctxlua{inifile.make_table("\IF@defaults", "\IF@sortA", "\IF@sortB")} } \def\IniFilePrint{\ctxlua{inifile.print("\IF@command")}} \def\IniFileNewEntry{\ctxlua{inifile.newentry(\IF@index)}} \def\IniFileFilter[#1][#2][#3]{\ctxlua{inifile.filter("#1", "#2", "#3")}} \protect \doifnotmode{demo}{\endinput} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %D Usage example: \usemodule[inifile] \setupIniFile[defaults=3D{birthyear=3D1944,comment=3D}, sortA=3Dbirthyear,sortB=3Dkey,command=3DFormatMember,file=3Dthewho.txt] \IniFileFilter[comment][\%\% ][\\letterpercent\\space ] \IniFileFilter[comment][\%\%][\\letterpercent ] \setupTABLE[frame=3Doff,width=3D0.5\textwidth] \nonknuthmode \def\IFbirthyear{} \def\FormatMember{ \edef\LastBirthyear{\IFbirthyear} \IniFileNewEntry \doifnot\IFbirthyear\LastBirthyear{\section{\IFbirthyear}} \subsection{\WORD{\IFkey}} \bTABLE\bTR \bTD Given name: \IFgivenname\eTD\bTD Surname: \IFsurname\eTD \eTR\eTABLE \doifsomething\IFcomment{Comment to show the treatment of the percent sign: \IFcomment}} \starttext \title{The Who} \IniFilePrint \stoptext %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Put this into the file thewho.txt for the usage example: [p_t] givenname =3D Peter surname =3D Townshend birthyear =3D 1945 comment =3D 100=C2=A0% with nobreakspace [r_d] givenname =3D Roger surname =3D Daltrey comment =3D 100=E2=80=89% with thinspace [j_e] givenname =3D John surname =3D Entwistle [k_m] givenname =3D Keith surname =3D Moon birthyear =3D 1946 [k_j] givenname =3D Kenney surname =3D Jones birthyear =3D 1948 %%% Local Variables:=20 %%% default-tab-width: 4 %%% End:=20 --/9DWx/yDrRhgMJTb Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : https://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________ --/9DWx/yDrRhgMJTb--