Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
Import to git
Browse files Browse the repository at this point in the history
  • Loading branch information
drahosp committed Oct 1, 2010
0 parents commit 252aea1
Show file tree
Hide file tree
Showing 9 changed files with 718 additions and 0 deletions.
17 changes: 17 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,17 @@
# Copyright (C) 2007-2009 LuaDist.
# Created by Peter Drahoš
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# For details see the COPYRIGHT file distributed with LuaDist.
# Please note that the package source code is licensed under its own license.

PROJECT(mixlua C)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
INCLUDE(dist.cmake)

# Build
ADD_LUA_MODULE(mixlua mixlua.c mixlua.def)

# Install all files and documentation
INSTALL (TARGETS mixlua DESTINATION ${INSTALL_CMOD})
INSTALL (FILES README VERSION DESTINATION ${INSTALL_DATA})
INSTALL (FILES test.lua DESTINATION ${INSTALL_TEST})
18 changes: 18 additions & 0 deletions Makefile
@@ -0,0 +1,18 @@
CC=cc
CFLAGS=-W -Wall -ansi -O2 -fpic -shared
INC=-I/usr/include/lua5.1

mixlua.so: mixlua.c
@echo "CC mixlua.so"
@${CC} ${CFLAGS} ${INC} -o mixlua.so mixlua.c

install:
@echo "CP mixlua.so"
@cp mixlua.so /usr/lib/lua/5.1

clean:
@echo "RM mixlua.so"
@rm -f mixlua.so

.PHONY: install clean

90 changes: 90 additions & 0 deletions README
@@ -0,0 +1,90 @@
README for Mixlua

* What is Mixlua?
---------------
Mixlua is a library for Lua 5.1 who provide an onload preprocessor for lua
files and allow mixing of Lua code with other data.
It provide loadstring and loadfile function similar to Lua ones but with
aditional argument for specifing how the data element are recognized and
handled, and produce a regular lua compiled chunk with additional material
for handleing the data.

* Example of use:
---------------
Imagine an application who have a table like this one :
infos = {
{key = "foo", value = "dummy 1"},
{key = "bar", value = "dummy 2"},
...
}

If you want to export it in various formats, you just have to build some
templates like this one for an xml output :
<?xml version="1.0" />
<infos>
$[ for _, item in ipairs(infos) do ]$
<item key="$[=item.key]$" value="$[=item.value]$" />
$[ end $]
</infos>

If you load this file with :
mix.loadfile("template.xml", "$[", "$]")

The xml file with all data will be printed on stdout. If you prefer to save
it elsewhere, there is no problems, just give an output function to mixlua
and it will be ok :
function output(str) io.stderr:write(str) end)
mix.loadfile("template.xml", "$[", "]$", nil, "ouput")

You can see Mixlua as something like the PHP preprocessor but in more
powerfull.

* Usage:
------
Mixlua expose only two function loadstring and loadfile. They work exactly
the same way, except for the first parameter who is a filename for loadfile
and a string for loadfile.

The next two parameters are two string who represent the tag used to surround
block of lua code embeded in the data, they must be given and must not be
empty.

The parameter four is a string who mark an expression statement instead of a
code statement. More on this a below. The default value is "=".

And the last parameter is the name of the output function, by default
"io.write". (this must be the name of the function, not the function itself
cause Mixlua is just a preprocessor who do texte transformation)

When processed throught Mixlua, all data section are transformed into string
and given as the argument to the output function. Lua code are kept untouched
and lua expression are also given to the output function but untouched.
So, for example the example given before will be translated to :
io.write('<?xml version="1.0" />\
<infos>\
') for _, item in ipairs(infos) do io.write('\
<item key="')io.write(item.key)io.write('" value="')io.write(item.value)io.write('" />\
') end io.write('\
</infos>')

And this is passed to the Lua compiler to produce a chunk of compiled code.
The execution of this chunk will produce the output file.

In the preprocess, Mixlua escape data in a way that the line numbers are
kept unchanged so syntax error are reported correctly. And in the Lua
blocks, all string and comment for are skipped correctly so they can embed
the delimiter without any risk to corrupt the output.

* Real case example:
------------------
For example the luapage loading of Cgilua by the Kepler Project can be donne
easily using :
mix.loadfile("filename", "<?lua", "?>", "=")
With the advantage that the "?>" closing tag can now appear in a lua string or
comment.

Another advantage is that after loading the page is now a complied lua chunk
that can be executed in different environement and so producing different
pages depending on the request.
This allow to implement easily a caching of the templates.

1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
0.2.7
130 changes: 130 additions & 0 deletions dist.cmake
@@ -0,0 +1,130 @@
# LuaDist CMake utility library.
# Provides variables and utility functions common to LuaDist CMake builds.
#
# Copyright (C) 2007-2010 LuaDist.
# by David Manura, Peter Drahos
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# For details see the COPYRIGHT file distributed with LuaDist.
# Please note that the package source code is licensed under its own license.

# Few convinence settings
SET (CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
SET (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_MODULE_PATH})

# Where to install module parts:
set(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
set(INSTALL_LIB lib CACHE PATH "Where to install libraries to.")
set(INSTALL_INC include CACHE PATH "Where to install headers to.")
set(INSTALL_ETC etc CACHE PATH "Where to store configuration files")
set(INSTALL_LMOD share/lua/lmod CACHE PATH "Directory to install Lua modules.")
set(INSTALL_CMOD share/lua/cmod CACHE PATH "Directory to install Lua binary modules.")
set(INSTALL_DATA share/${PROJECT_NAME} CACHE PATH "Directory the package can store documentation, tests or other data in.")
set(INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH "Recommended directory to install documentation into.")
set(INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH "Recommended directory to install examples into.")
set(INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH "Recommended directory to install tests into.")
set(INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH "Where to install additional files")


# In MSVC, prevent warnings that can occur when using standard libraries.
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif(MSVC)

# Adds Lua shared library module target `_target`.
# Additional sources to build the module are listed after `_target`.
macro(add_lua_module _target)
find_package(Lua51 REQUIRED)
include_directories(${LUA_INCLUDE_DIR}) #2DO: somehow apply only to _target?

add_library(${_target} MODULE ${ARGN})
set_target_properties(${_target} PROPERTIES PREFIX "")
target_link_libraries(${_target} ${LUA_LIBRARY})

IF(WIN32)
set_target_properties(${_target} PROPERTIES LINK_FLAGS "-Wl,--enable-auto-import")
ENDIF()

endmacro(add_lua_module)

# Runs Lua script `_testfile` under CTest tester.
# Optional argument `_testcurrentdir` is current working directory to run test under
# (defaults to ${CMAKE_CURRENT_BINARY_DIR}).
# Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}.
# Under LuaDist, set test=true in config.lua to enable testing.
macro(add_lua_test _testfile)
include(CTest)
if(BUILD_TESTING)
find_program(LUA NAMES lua lua.bat)
get_filename_component(TESTFILEABS ${_testfile} ABSOLUTE)
get_filename_component(TESTFILENAME ${_testfile} NAME)
get_filename_component(TESTFILEBASE ${_testfile} NAME_WE)

# Write wrapper script.
set(TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME})
set(TESTWRAPPERSOURCE
"package.path = '${CMAKE_CURRENT_BINARY_DIR}/?.lua\;${CMAKE_CURRENT_SOURCE_DIR}/?.lua\;' .. package.path
package.cpath = '${CMAKE_CURRENT_BINARY_DIR}/?.so\;${CMAKE_CURRENT_BINARY_DIR}/?.dll\;' .. package.cpath
return dofile '${TESTFILEABS}'
" )
if(${ARGC} GREATER 1)
set(_testcurrentdir ${ARGV1})
get_filename_component(TESTCURRENTDIRABS ${_testcurrentdir} ABSOLUTE)
set(TESTWRAPPERSOURCE
"require 'lfs'
lfs.chdir('${TESTCURRENTDIRABS}')
${TESTWRAPPERSOURCE}")
endif()
FILE(WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE})

add_test(${TESTFILEBASE} ${LUA} ${TESTWRAPPER})
endif(BUILD_TESTING)

# see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake
endmacro(add_lua_test)

# Converts Lua source file `_source` to binary string embedded in C source
# file `_target`. Optionally compiles Lua source to byte code (not available
# under LuaJIT2, which doesn't have a bytecode loader). Additionally, Lua
# versions of bin2c [1] and luac [2] may be passed respectively as additional
# arguments.
#
# [1] http://lua-users.org/wiki/BinToCee
# [2] http://lua-users.org/wiki/LuaCompilerInLua
function(add_lua_bin2c _target _source)
find_program(LUA NAMES lua lua.bat)
execute_process(COMMAND ${LUA} -e "string.dump(function()end)" RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET)
if (NOT ${_LUA_DUMP_RESULT})
SET(HAVE_LUA_DUMP true)
endif()
message("-- string.dump=${HAVE_LUA_DUMP}")

if (ARGV2)
get_filename_component(BIN2C ${ARGV2} ABSOLUTE)
set(BIN2C ${LUA} ${BIN2C})
else()
find_program(BIN2C NAMES bin2c bin2c.bat)
endif()
if (HAVE_LUA_DUMP)
if (ARGV3)
get_filename_component(LUAC ${ARGV3} ABSOLUTE)
set(LUAC ${LUA} ${LUAC})
else()
find_program(LUAC NAMES luac luac.bat)
endif()
endif (HAVE_LUA_DUMP)
message("-- bin2c=${BIN2C}")
message("-- luac=${LUAC}")

get_filename_component(SOURCEABS ${_source} ABSOLUTE)
if (HAVE_LUA_DUMP)
get_filename_component(SOURCEBASE ${_source} NAME_WE)
add_custom_command(
OUTPUT ${_target} DEPENDS ${_source}
COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ${SOURCEABS}
COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ">${_target}" )
else()
add_custom_command(
OUTPUT ${_target} DEPENDS ${SOURCEABS}
COMMAND ${BIN2C} ${_source} ">${_target}" )
endif()
endfunction(add_lua_bin2c)
14 changes: 14 additions & 0 deletions dist.info
@@ -0,0 +1,14 @@
--- This file is part of LuaDist project

name = "mixlua"
version = "0.2.7"

desc = "A preprocessor for files including embedded Lua code"
author = "Thomas Lavergne"
license = "MIT/X11"
url = "http://oniros.org/projects.html"
maintainer = "Peter Drahoš"

depends = {
"lua ~> 5.1"
}

0 comments on commit 252aea1

Please sign in to comment.