1616 Global functions in metaprograms:
1717 - copyTable
1818 - escapePattern
19- - getFileContents, fileExists
19+ - getIndentation
2020 - pack
2121 - pairsSorted
2222 - printf
23+ - readFile, writeFile, fileExists
2324 - run
2425 - sortNatural, compareNatural
2526 - tokenize, newToken, concatTokens, removeUselessTokens, eachToken, isToken, getNextUsefulToken
2627 - toLua, serialize, evaluate
2728 Only during processing:
29+ - getCurrentIndentation
2830 - getCurrentPathIn, getCurrentPathOut
2931 - getOutputSoFar, getOutputSoFarOnLine, getOutputSizeSoFar, getCurrentLineNumberInOutput
3032 - loadResource
@@ -220,6 +222,7 @@ local current_meta_maxLogLevel = "trace"
220222
221223local _concatTokens
222224local _loadResource
225+ local _readFile , _writeFile , fileExists
223226local _tokenize
224227local assertarg
225228local cleanError
@@ -229,7 +232,6 @@ local errorf, errorLine, errorfLine, errorOnLine, errorInFile, errorAtToken, err
229232local errorIfNotRunningMeta
230233local escapePattern
231234local F , tryToFormatError
232- local getFileContents , fileExists
233235local getLineNumber
234236local getNextUsableToken
235237local getRelativeLocationText
@@ -640,7 +642,7 @@ function _tokenize(s, path, allowPpTokens, allowBacktickStrings, allowJitSyntax)
640642 end
641643
642644 if tok .long then
643- -- Check for nesting of [[...]], which is depricated in Lua.
645+ -- Check for nesting of [[...]], which is deprecated in Lua.
644646 local chunk , err = loadLuaString (" --" .. tok .representation , " @" )
645647
646648 if not chunk then
@@ -722,7 +724,7 @@ function _tokenize(s, path, allowPpTokens, allowBacktickStrings, allowJitSyntax)
722724 end
723725 end
724726
725- -- Check for nesting of [[...]], which is depricated in Lua.
727+ -- Check for nesting of [[...]], which is deprecated in Lua.
726728 local valueChunk , err = loadLuaString (" return" .. tok .representation , " @" )
727729
728730 if not valueChunk then
865867
866868
867869
868- function getFileContents (path , isTextFile )
870+ function _readFile (path , isTextFile )
869871 assertarg (1 , path , " string" )
870872 assertarg (2 , isTextFile , " boolean" ," nil" )
871873
@@ -877,6 +879,25 @@ function getFileContents(path, isTextFile)
877879 return contents
878880end
879881
882+ -- success, error = _writeFile( path, [ isTextFile=false, ] contents )
883+ function _writeFile (path , isTextFile , contents )
884+ assertarg (1 , path , " string" )
885+
886+ if type (isTextFile ) == " boolean" then
887+ assertarg (3 , contents , " string" )
888+ else
889+ isTextFile , contents = false , isTextFile
890+ assertarg (2 , contents , " string" )
891+ end
892+
893+ local file , err = io.open (path , " w" .. (isTextFile and " " or " b" ))
894+ if not file then return false , err end
895+
896+ file :write (contents )
897+ file :close ()
898+ return true
899+ end
900+
880901function fileExists (path )
881902 assertarg (1 , path , " string" )
882903
@@ -1453,7 +1474,7 @@ function _loadResource(resourceName, isParsing, nameTokOrErrLevel, stats)
14531474
14541475 else
14551476 local err
1456- lua , err = getFileContents (resourceName , true )
1477+ lua , err = _readFile (resourceName , true )
14571478
14581479 if lua then
14591480 -- void
@@ -1506,10 +1527,17 @@ local metaFuncs = {}
15061527-- Print a formatted string to stdout.
15071528metaFuncs .printf = printf
15081529
1509- -- getFileContents ()
1510- -- contents = getFileContents ( path [, isTextFile=false ] )
1530+ -- readFile ()
1531+ -- contents = readFile ( path [, isTextFile=false ] )
15111532-- Get the entire contents of a binary file or text file. Returns nil and a message on error.
1512- metaFuncs .getFileContents = getFileContents
1533+ metaFuncs .readFile = _readFile
1534+ metaFuncs .getFileContents = _readFile -- @Deprecated
1535+
1536+ -- writeFile()
1537+ -- success, error = writeFile( path, contents ) -- Writes a binary file.
1538+ -- success, error = writeFile( path, isTextFile, contents )
1539+ -- Write an entire binary file or text file.
1540+ metaFuncs .writeFile = _writeFile
15131541
15141542-- fileExists()
15151543-- bool = fileExists( path )
@@ -1688,11 +1716,7 @@ function metaFuncs.getOutputSoFar(asTable)
16881716 return asTable and copyArray (current_meta_outputStack [1 ]) or table.concat (current_meta_outputStack [1 ])
16891717end
16901718
1691- -- getOutputSoFarOnLine()
1692- -- luaString = getOutputSoFarOnLine( )
1693- -- Get Lua code that's been outputted so far on the current line.
1694- -- Raises an error if no file or string is being processed.
1695- function metaFuncs .getOutputSoFarOnLine ()
1719+ local function _getOutputSoFarOnLine ()
16961720 errorIfNotRunningMeta (2 )
16971721
16981722 local lineFragments = {}
@@ -1711,6 +1735,12 @@ function metaFuncs.getOutputSoFarOnLine()
17111735 return table.concat (lineFragments )
17121736end
17131737
1738+ -- getOutputSoFarOnLine()
1739+ -- luaString = getOutputSoFarOnLine( )
1740+ -- Get Lua code that's been outputted so far on the current line.
1741+ -- Raises an error if no file or string is being processed.
1742+ metaFuncs .getOutputSoFarOnLine = _getOutputSoFarOnLine
1743+
17141744-- getOutputSizeSoFar()
17151745-- size = getOutputSizeSoFar( )
17161746-- Get the amount of bytes outputted so far.
@@ -1742,6 +1772,40 @@ function metaFuncs.getCurrentLineNumberInOutput()
17421772 return ln
17431773end
17441774
1775+ local function _getIndentation (line , tabWidth )
1776+ if not tabWidth then
1777+ return line :match " ^[ \t ]*"
1778+ end
1779+
1780+ local indent = 0
1781+
1782+ for i = 1 , # line do
1783+ if line :sub (i , i ) == " \t " then
1784+ indent = math.floor (indent / tabWidth )* tabWidth + tabWidth
1785+ elseif line :sub (i , i ) == " " then
1786+ indent = indent + 1
1787+ else
1788+ break
1789+ end
1790+ end
1791+
1792+ return indent
1793+ end
1794+
1795+ -- getIndentation()
1796+ -- string = getIndentation( line )
1797+ -- size = getIndentation( line, tabWidth )
1798+ -- Get indentation of a line, either as a string or as size in spaces.
1799+ metaFuncs .getIndentation = _getIndentation
1800+
1801+ -- getCurrentIndentation()
1802+ -- string = getCurrentIndentation( )
1803+ -- size = getCurrentIndentation( tabWidth )
1804+ -- Get the indentation of the current line, either as a string or as size in spaces.
1805+ function metaFuncs .getCurrentIndentation (tabWidth )
1806+ return (_getIndentation (_getOutputSoFarOnLine (), tabWidth ))
1807+ end
1808+
17451809-- getCurrentPathIn()
17461810-- path = getCurrentPathIn( )
17471811-- Get what file is currently being processed, if any.
@@ -2893,7 +2957,7 @@ local function _processFileOrString(params, isFile)
28932957 if virtualPathIn == " -" then
28942958 luaUnprocessed , err = io.stdin :read " *a"
28952959 else
2896- luaUnprocessed , err = getFileContents (virtualPathIn , true )
2960+ luaUnprocessed , err = _readFile (virtualPathIn , true )
28972961 end
28982962
28992963 if not luaUnprocessed then
0 commit comments