diff options
author | Karl Berry <karl@freefriends.org> | 2018-03-22 21:56:45 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2018-03-22 21:56:45 +0000 |
commit | b8c3630840af3e208cb60e260adc4c9622a0bd6a (patch) | |
tree | 314b2d94f484fadb53c7cb50843d2e5570e2116d /Master/texmf-dist/tex/context/third | |
parent | 6a779f2a3a5171811c5a3b6ed99f3d0affb1d371 (diff) |
context-handlecsv (22mar18)
git-svn-id: svn://tug.org/texlive/trunk@47080 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/context/third')
-rw-r--r-- | Master/texmf-dist/tex/context/third/handlecsv/t-handlecsv-extra.lua | 86 | ||||
-rw-r--r-- | Master/texmf-dist/tex/context/third/handlecsv/t-handlecsv.lua | 114 |
2 files changed, 160 insertions, 40 deletions
diff --git a/Master/texmf-dist/tex/context/third/handlecsv/t-handlecsv-extra.lua b/Master/texmf-dist/tex/context/third/handlecsv/t-handlecsv-extra.lua new file mode 100644 index 00000000000..6afcf07b068 --- /dev/null +++ b/Master/texmf-dist/tex/context/third/handlecsv/t-handlecsv-extra.lua @@ -0,0 +1,86 @@ +-- %D \module +-- %D [ file=t-handlecsv-extra.lua, +-- %D version=2018.02.26, +-- %D title=HandleCSV extra, +-- %D subtitle=CSV file analysis - extended functions and macros, +-- %D author=Jaroslav Hajtmar, +-- %D date=2018-02-26, +-- %D copyright=Jaroslav Hajtmar, +-- %D license=GNU General Public License] +-- +-- %C Copyright (C) 2018 Jaroslav Hajtmar +-- %C +-- %C This program is free software: you can redistribute it and/or modify +-- %C it under the terms of the GNU General Public License as published by +-- %C the Free Software Foundation, either version 3 of the License, or +-- %C (at your option) any later version. +-- %C +-- %C This program is distributed in the hope that it will be useful, +-- %C but WITHOUT ANY WARRANTY; without even the implied warranty of +-- %C MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- %C GNU General Public License for more details. +-- %C +-- %C You should have received a copy of the GNU General Public License +-- %C along with this program. If not, see <http://www.gnu.org/licenses/>. + + +thirddata = thirddata or { } + +thirddata = thirddata or { } + +thirddata.handlecsv = thirddata.handlecsv or { -- next global variables + +} + + +-- Initialize global variables etc. + + +-- Utility and documentation function and macros + + + +function thirddata.handlecsv.addleadingcharacters(character, tonumberortext, width) +-- Add leading characters to number to align with the width + local strcharacter=tostring(character) + local strnumberortext=tostring(tonumberortext) + strnumberortext = string.rep(strcharacter, width-#strnumberortext)..strnumberortext + return strnumberortext -- It returns a strange result unless the leading character is just one. +end + +function thirddata.handlecsv.addleadingzeros(tonumberortext, width) +-- Add leading zeros to number to align with the width + return thirddata.handlecsv.addleadingcharacters(0, tonumberortext, width) +end + +function thirddata.handlecsv.addzeros(tonumber) +-- Add leading zeroes depending on the number of rows + local width=string.len(tostring(thirddata.handlecsv.numrows())) + return thirddata.handlecsv.addleadingzeros(tonumber, width) +end + + + + +-- ConTeXt source: +local string2print=[[% + +\def\addleading#1#2#3{\ctxlua{context(thirddata.handlecsv.addleadingcharacters('#1','#2','#3'))}} +\def\addzeros#1#2{\ctxlua{context(thirddata.handlecsv.addleadingzeros('#1','#2'))}} +\def\zeroed#1{\ctxlua{context(thirddata.handlecsv.addzeros('#1'))}} +\def\zeroedlineno{\ctxlua{context(string.rep( "0",(tostring(thirddata.handlecsv.numrows())):len() - (tostring(thirddata.handlecsv.linepointer())):len()) .. thirddata.handlecsv.linepointer())}}% from Pablo + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Complete listing macros and commands that can be used (to keep track of all defined macros): +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% \addleading{}{}{}, \addzeros{}{}, \zeroed{}, \zeroedlineno (from Pablo) +% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +]] + +-- write definitions into ConTeXt: +thirddata.handlecsv.string2context(string2print) + diff --git a/Master/texmf-dist/tex/context/third/handlecsv/t-handlecsv.lua b/Master/texmf-dist/tex/context/third/handlecsv/t-handlecsv.lua index ed06591bf25..9d6e265d2e4 100644 --- a/Master/texmf-dist/tex/context/third/handlecsv/t-handlecsv.lua +++ b/Master/texmf-dist/tex/context/third/handlecsv/t-handlecsv.lua @@ -1,6 +1,6 @@ -- %D \module -- %D [ file=t-handlecsv.lua, --- %D version=2018.02.14, +-- %D version=2018.02.26, -- %D title=HandleCSV module, -- %D subtitle=CSV file handling, -- %D author=Jaroslav Hajtmar, @@ -9,7 +9,7 @@ -- %D email=hajtmar@gyza.cz, -- %D license=GNU General Public License] -- --- %C Copyright (C) 2018 Jaroslav Hajtmar +-- %C Copyright (C) 2018 Jaroslav Hajtmar -- %C -- %C This program is free software: you can redistribute it and/or modify -- %C it under the terms of the GNU General Public License as published by @@ -83,9 +83,8 @@ function thirddata.handlecsv.texmacroisdefined(macroname) -- check whether macro return token.get_cmdname(token.create(macroname)) ~= "undefined_cs" end - --- tool function ParseCSVLine is defined for compatibility. Parsing string (or line). function thirddata.handlecsv.ParseCSVLine(line,sep) +-- tool function ParseCSVLine is defined for compatibility. Parsing string (or line). local mycsvsplitter = utilities.parsers.rfc4180splitter{ separator = sep, quote = '"', @@ -168,10 +167,30 @@ function thirddata.handlecsv.ar2colnum(arnum) -- According to the settings glob. end end +function thirddata.handlecsv.substitutecontentofcellof(csvfile,column,row,whattoreplace,substitution) +-- Substitute text in cell content of specified CSV file with other text + local csvfile=thirddata.handlecsv.handlecsvfile(csvfile) + local column=thirddata.handlecsv.gColNames[csvfile][column] + local whattoreplace=tostring(whattoreplace) + local substitution=tostring(substitution) + return thirddata.handlecsv.getcellcontentof(csvfile,column,row):gsub(whattoreplace,substitution) +end +function thirddata.handlecsv.substitutecontentofcell(column,row,whattoreplace,substitution) +-- Substitute text in cell content of current CSV file with other text + local csvfile=thirddata.handlecsv.getcurrentcsvfilename() + local column=thirddata.handlecsv.gColNames[csvfile][column] + return thirddata.handlecsv.substitutecontentofcellof(csvfile,column,row,whattoreplace,substitution) +end +function thirddata.handlecsv.substitutecontentofcellofcurrentrow(column,whattoreplace,substitution) +-- Substitute text in cell content of current row of current CSV file with other text + local row=thirddata.handlecsv.linepointer() + return thirddata.handlecsv.substitutecontentofcell(column,row,whattoreplace,substitution) +end -function thirddata.handlecsv.processinputvalue(inputparameter,replacingnumber) -- when inputparameter is not correct, then return replacingnumber +function thirddata.handlecsv.processinputvalue(inputparameter,replacingnumber) +-- when inputparameter is not correct, then return replacingnumber local returnparameter=inputparameter if type(inputparameter)~= 'number' then returnparameter=replacingnumber @@ -304,11 +323,11 @@ function thirddata.handlecsv.emptylineevaluation(lineindex) end +function thirddata.handlecsv.removeemptylines() -- This function remove empty rows only from field of variables thirddata.handlecsv.gTableRows! -- The field is only re-indexed and function does not affect onto the physical input CSV file! -- When the physical CSV file is reopened by using \open macro, the global field variable -- thirddata.handlecsv.gTableRows[csvfile] is reset into original state! -function thirddata.handlecsv.removeemptylines() thirddata.handlecsv.markemptylines() local csvfilename=thirddata.handlecsv.getcurrentcsvfilename() @@ -341,7 +360,8 @@ function thirddata.handlecsv.hooksevaluation() end -function thirddata.handlecsv.setgetcurrentcsvfile(filename) -- In the absence of the file name to use the global variable +function thirddata.handlecsv.setgetcurrentcsvfile(filename) +-- In the absence of the file name to use the global variable thirddata.handlecsv.gCurrentlyProcessedCSVFile = (filename ~= nil) and filename or thirddata.handlecsv.gCurrentlyProcessedCSVFile thirddata.handlecsv.gCurrentlyProcessedCSVFile = (thirddata.handlecsv.gCurrentlyProcessedCSVFile == nil) and filename or thirddata.handlecsv.gCurrentlyProcessedCSVFile local filename = filename ~= nil and filename or thirddata.handlecsv.gCurrentlyProcessedCSVFile @@ -349,7 +369,8 @@ function thirddata.handlecsv.setgetcurrentcsvfile(filename) -- In the absence of return tostring(filename) end -function thirddata.handlecsv.handlecsvfile(filename) -- not used yet +function thirddata.handlecsv.handlecsvfile(filename) +-- not used yet local filename = tostring(filename) filename = string.gsub(filename, '"', '') filename = string.gsub(filename, "'", "") @@ -362,22 +383,26 @@ end return filename end -function thirddata.handlecsv.getcurrentcsvfilename() -- return current (actual) CSV file +function thirddata.handlecsv.getcurrentcsvfilename() +-- return current (actual) CSV file return tostring(thirddata.handlecsv.gCurrentlyProcessedCSVFile) end -function thirddata.handlecsv.isopenfile(csvfilename) -- testing of opening CSV files +function thirddata.handlecsv.isopenfile(csvfilename) +-- testing of opening CSV files local retval=(thirddata.handlecsv.gOpenFiles[csvfilename] ~= nil) return retval end -function thirddata.handlecsv.closecsvfile(csvfilename) -- manual closing of CSV files +function thirddata.handlecsv.closecsvfile(csvfilename) +-- manual closing of CSV files thirddata.handlecsv.gOpenFiles[csvfilename] = nil end -function thirddata.handlecsv.getnumberofopencsvfiles() -- get the number of open files +function thirddata.handlecsv.getnumberofopencsvfiles() +-- get the number of open files local count = 0 for k, v in pairs(thirddata.handlecsv.gOpenFiles) do count = count + 1 @@ -397,7 +422,8 @@ function thirddata.handlecsv.setpointersofopeningcsvfile(inpcsvfile) thirddata.handlecsv.resetmarkemptylines() end -function thirddata.handlecsv.opencsvfile(filetoscan) -- Open CSV tabule, inicialize variables +function thirddata.handlecsv.opencsvfile(filetoscan) +-- Open CSV tabule, inicialize variables -- open the table and load it into the global variable thirddata.handlecsv.gTableRows[filetoscan] -- if the option thirddata.handlecsv.gCSVHeader==true is enabled, then into glob variable thirddata.handlecsv.gColumnNames[filetoscan] -- sets the column names from the title, if not then sets XLS notation, ie. cA, cB, cC, ... @@ -478,12 +504,8 @@ return end -- of thirddata.handlecsv.opencsvfile(file) - - - - --- Main function. Read data from specific line of specific file, parse them etc. function thirddata.handlecsv.readlineof(inpcsvfile,numberofline) -- +-- Main function. Read data from specific line of specific file, parse them etc. local inpcsvfile=thirddata.handlecsv.handlecsvfile(inpcsvfile) local numberofline=numberofline local returnpar=false @@ -511,8 +533,8 @@ function thirddata.handlecsv.readlineof(inpcsvfile,numberofline) -- end -- function thirddata.handlecsv.readlineof(inpcsvfile,numberofline) -- --- Main function. Read data from specific line of specific file, parse them etc. function thirddata.handlecsv.readline(numberofline) -- +-- Main function. Read data from specific line of specific file, parse them etc. local csvfile=thirddata.handlecsv.getcurrentcsvfilename() if type(numberofline) == 'number' then thirddata.handlecsv.readlineof(csvfile,numberofline) -- @@ -522,8 +544,6 @@ function thirddata.handlecsv.readline(numberofline) -- end - - function thirddata.handlecsv.createxlscommandof(xlsname,csvfile) local inpcsvfile=thirddata.handlecsv.handlecsvfile(csvfile) local cxlsname=tostring('col'..xlsname) @@ -567,9 +587,8 @@ thirddata.handlecsv.createxlscommandof(xlsname,inpcsvfile) end - --- after read of line this function put content of columns into specific TeX macros... function thirddata.handlecsv.assigncontentsof(inpcsvfile,line) -- put data into columns macros +-- after read of line this function put content of columns into specific TeX macros... --if tex.modes['XXL'] then context("XXL mode") else context("not XXL mode") end local inpcsvfile=thirddata.handlecsv.handlecsvfile(inpcsvfile) local cutoffinpcsvfile=thirddata.handlecsv.ParseCSVLine(inpcsvfile,".")[1] -- cut filename extension @@ -633,8 +652,8 @@ thirddata.handlecsv.assigncontentsof(thirddata.handlecsv.getcurrentcsvfilename() end --- Read data from specific cell of specific the csv table function thirddata.handlecsv.getcellcontentof(csvfile,column,row) +-- Read data from specific cell of specific the csv table -- local returnparametr='nil' -- 1.10.2015 local csvfile=thirddata.handlecsv.handlecsvfile(csvfile) local returnparametr='' -- 9.1.2016 @@ -647,7 +666,16 @@ function thirddata.handlecsv.getcellcontentof(csvfile,column,row) else column=testcolumn end + else + testcolumn=tonumber(column) + if testcolumn==nil then + column=0 + else + column=testcolumn + end end + if column<=0 then column=1 end + if column>thirddata.handlecsv.gNumCols[csvfile] then column=thirddata.handlecsv.gNumCols[csvfile] end if type(row)=='string' then local testrow=tonumber(row) if testrow==nil then @@ -667,16 +695,16 @@ function thirddata.handlecsv.getcellcontentof(csvfile,column,row) end --- Read data from specific cell of current open csv table function thirddata.handlecsv.getcellcontent(column,row) +-- Read data from specific cell of current open csv table local csvfile=thirddata.handlecsv.getcurrentcsvfilename() local returnparametr=thirddata.handlecsv.getcellcontentof(csvfile,column,row) return returnparametr end --- Move line pointer to next line. function thirddata.handlecsv.nextlineof(csvfile) +-- Move line pointer to next line. local csvfile=thirddata.handlecsv.handlecsvfile(csvfile) if thirddata.handlecsv.gCurrentLinePointer[csvfile] > thirddata.handlecsv.gNumRows[csvfile] then thirddata.handlecsv.gCurrentLinePointer[csvfile]=thirddata.handlecsv.gNumRows[csvfile] @@ -689,14 +717,16 @@ function thirddata.handlecsv.nextlineof(csvfile) end end --- Move line pointer to next line. + function thirddata.handlecsv.nextline() +-- Move line pointer to next line. local csvfile=thirddata.handlecsv.getcurrentcsvfilename() thirddata.handlecsv.nextlineof(csvfile) end --- Move line pointer to previous line. + function thirddata.handlecsv.previouslineof(csvfile) +-- Move line pointer to previous line. local csvfile=thirddata.handlecsv.handlecsvfile(csvfile) if thirddata.handlecsv.gCurrentLinePointer[csvfile] < 2 then thirddata.handlecsv.gCurrentLinePointer[csvfile] = 1 @@ -707,8 +737,9 @@ function thirddata.handlecsv.previouslineof(csvfile) context([[\global\notEOFtrue%]]) end --- Move line pointer to previous line. + function thirddata.handlecsv.previousline() +-- Move line pointer to previous line. local csvfile=thirddata.handlecsv.getcurrentcsvfilename() thirddata.handlecsv.previouslineof(csvfile) end @@ -725,30 +756,33 @@ function thirddata.handlecsv.setlinepointerof(csvfile,numberofline) thirddata.handlecsv.readlineof(csvfile,numberofline) end + function thirddata.handlecsv.setlinepointer(numberofline) local csvfile=thirddata.handlecsv.getcurrentcsvfilename() thirddata.handlecsv.setlinepointerof(csvfile,numberofline) end + function thirddata.handlecsv.savelinepointer() local csvfile=thirddata.handlecsv.getcurrentcsvfilename() thirddata.handlecsv.gSavedLinePointerNo = thirddata.handlecsv.gCurrentLinePointer[csvfile] end + function thirddata.handlecsv.setsavedlinepointer() thirddata.handlecsv.setlinepointer(thirddata.handlecsv.gSavedLinePointerNo) end --- Take pointer to first row of table function thirddata.handlecsv.resetlinepointerof(csvfile) +-- Take pointer to first row of table local csvfile=thirddata.handlecsv.handlecsvfile(csvfile) thirddata.handlecsv.setlinepointerof(csvfile,1) end --- Take pointer to first row of table function thirddata.handlecsv.resetlinepointer() +-- Take pointer to first row of table local csvfile=thirddata.handlecsv.getcurrentcsvfilename() thirddata.handlecsv.setlinepointerof(csvfile,1) end @@ -765,16 +799,17 @@ function thirddata.handlecsv.linepointer() return thirddata.handlecsv.gCurrentLinePointer[csvfile] end + function thirddata.handlecsv.getcurrentlinepointer() -- for compatibility return thirddata.handlecsv.linepointer() end + function thirddata.handlecsv.getlinepointer() -- for compatibility return thirddata.handlecsv.linepointer() end - function thirddata.handlecsv.setnumlineof(csvfile,numline) local csvfile=thirddata.handlecsv.handlecsvfile(csvfile) thirddata.handlecsv.gNumLine[csvfile]=numline @@ -819,15 +854,15 @@ local csvfile=thirddata.handlecsv.getcurrentcsvfilename() end --- Move numline pointer to next number. function thirddata.handlecsv.nextnumlineof(csvfile) +-- Move numline pointer to next number. local csvfile=thirddata.handlecsv.handlecsvfile(csvfile) thirddata.handlecsv.gNumLine[csvfile]=thirddata.handlecsv.gNumLine[csvfile]+1 end --- Move numline pointer to next number. function thirddata.handlecsv.nextnumline() +-- Move numline pointer to next number. local csvfile=thirddata.handlecsv.getcurrentcsvfilename() thirddata.handlecsv.gNumLine[csvfile]=thirddata.handlecsv.gNumLine[csvfile]+1 end @@ -856,7 +891,6 @@ return thirddata.handlecsv.gNumRows[thirddata.handlecsv.getcurrentcsvfilename()] end - function thirddata.handlecsv.numcolsof(csvfile) local csvfile=thirddata.handlecsv.handlecsvfile(csvfile) context(thirddata.handlecsv.gNumCols[csvfile]) @@ -869,10 +903,8 @@ local csvfile=thirddata.handlecsv.getcurrentcsvfilename() end - - --- initialize ConTeXt hooks function thirddata.handlecsv.resethooks() +-- initialize ConTeXt hooks context([[% \letvalue{blinehook}=\relax% \letvalue{elinehook}=\relax% @@ -884,9 +916,8 @@ function thirddata.handlecsv.resethooks() end - --- for safety writen function thirddata.handlecsv.string2context(str2ctx) +-- for safety writen local s=str2ctx s=string.gsub(s, "%%(.-)\n", "\n") -- remove TeX comments from string. From % character to the end of line -- s=string.gsub(s, '\n', "") @@ -1066,6 +1097,8 @@ local string2print=[[% %\ctxlua{context(tostring(thirddata.handlecsv.getcellcontent(#1,8)))} }% +% Substitution of text #2 in cell content by text #3. Substitution is done in the current column of column #1 (number, XLS name or cX name) +\def\replacecontentin#1#2#3{\ctxlua{context(thirddata.handlecsv.substitutecontentofcellofcurrentrow('#1','#2','#3'))}}% % Get number from XLS column name (ie n-th column) \def\numberxlscolname[#1]{\ctxlua{context(thirddata.handlecsv.xls2ar(#1))}}% @@ -1345,6 +1378,7 @@ local string2print=[[% % \opencsvfile, \opencsvfile{<filename>}, \closecsvfile{<filename>} % \readline, \readline{<numberofline>} % \readandprocessparameters#1#2#3#4 -- for internal use only +% \replacecontentin{<colname/colnumber>}{<substitutefrom>}{<substituteto>} % % Module predefined cycles for processing of lines CSV table: % \doloopfromto{<fromnumblerline>}{<tonumblerline}{<\actionmacro>} |