Skip to content

Commit

Permalink
Merge pull request #1 from jerily/import-js-components
Browse files Browse the repository at this point in the history
Import js components
  • Loading branch information
jerily authored Aug 28, 2024
2 parents c14e957 + d1c3205 commit 8cb16c9
Show file tree
Hide file tree
Showing 32 changed files with 1,666 additions and 618 deletions.
92 changes: 92 additions & 0 deletions .github/workflows/linux-build-9.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: Linux (with Tcl 9.0)
on: [push]
permissions:
contents: read
defaults:
run:
shell: bash
jobs:
build:
runs-on: ubuntu-24.04
timeout-minutes: 7
steps:

- name: Checkout Tcl
uses: actions/checkout@v4
with:
repository: tcltk/tcl
ref: core-9-0-b3-rc
path: tcl
- name: Configure Tcl
working-directory: tcl/unix
run: |
./configure --prefix=$HOME/tcl_install || {
cat config.log
echo "::error::Failure during Configure Tcl"
exit 1
}
- name: Build Tcl
working-directory: tcl/unix
run: |
make -j || {
echo "::error::Failure during Build Tcl"
exit 1
}
- name: Install Tcl
working-directory: tcl/unix
run: |
make install || {
echo "::error::Failure during Install Tcl"
exit 1
}
- name: Build tdom
shell: bash
run: |
curl -L -O http://tdom.org/downloads/tdom-0.9.4-src.tar.gz
tar -xzvf tdom-0.9.4-src.tar.gz
cd tdom-0.9.4-src/unix
../configure --with-tcl=$HOME/tcl_install/lib --prefix=$HOME/tcl_install || {
cat config.log
echo "::error::Failure during Configure tdom"
exit 1
}
make -j || {
echo "::error::Failure during Build tdom"
exit 1
}
make install || {
echo "::error::Failure during Install tdom"
exit 1
}
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Configure
run: |
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/thtml_install -DCMAKE_PREFIX_PATH=$HOME/thtml_install -DTCL_INCLUDE_DIR=$HOME/tcl_install/include -DTCL_LIBRARY_DIR=$HOME/tcl_install/lib
- name: Build
working-directory: build
run: |
make -j || {
echo "::error::Failure during Build"
exit 1
}
- name: Run Tests
working-directory: build
run: |
PATH="$HOME/tcl_install/bin:$PATH" LD_LIBRARY_PATH="$HOME/tcl_install/lib" make test || {
echo "::error::Failure during Test"
exit 1
}
- name: Test-Drive Installation
working-directory: build
run: |
make install || {
echo "::error::Failure during Install"
exit 1
}
52 changes: 46 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,54 @@ set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED true)
set(THREADS_PREFER_PTHREAD_FLAG ON)

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_C_FLAGS "-g -DTCL_THREADS -DPROJECT_VERSION=${PROJECT_VERSION} ${CMAKE_C_FLAGS}")
#set(CMAKE_VERBOSE_MAKEFILE ON)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(TCL 8.6.13 REQUIRED) # TCL_INCLUDE_PATH TCL_LIBRARY
find_program(TCL_TCLSH
NAMES
tclsh90 tclsh9.0
tclsh86 tclsh8.6
tclsh
PATHS
${TCL_INCLUDE_PATH}/../bin
${TCL_INCLUDE_PATH}/../../bin
NO_DEFAULT_PATH
)

message(STATUS "TCL_INCLUDE_PATH: ${TCL_INCLUDE_PATH}")
message(STATUS "TCL_LIBRARY: ${TCL_LIBRARY}")

add_compile_options(-Wall -Wextra -Wpedantic)
add_compile_definitions(TCL_THREADS PROJECT_VERSION=${PROJECT_VERSION})

if ("${ADDRESS_SANITIZER}" STREQUAL "ON")
add_compile_options(-fPIC -g -fsanitize=undefined -fsanitize=address)
add_link_options(-fsanitize=undefined -fsanitize=address)
endif ()

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(CMAKE_C_FLAGS "-g -DDEBUG ${CMAKE_C_FLAGS}")
else()
# NDEBUG to not generate code for assert
add_compile_definitions(NDEBUG)
endif ()

set(CMAKE_CTEST_ARGUMENTS "--verbose")
set(CMAKE_CTEST_ARGUMENTS "--output-on-failure")
enable_testing()
add_test(NAME AllUnitTests
COMMAND ${TCL_TCLSH} ${CMAKE_CURRENT_SOURCE_DIR}/tests/all.tcl
)
set_tests_properties(AllUnitTests
PROPERTIES ENVIRONMENT TCLLIBPATH=${CMAKE_CURRENT_BINARY_DIR}
)

add_custom_target(shell ${CMAKE_COMMAND} -E env TCLLIBPATH=${CMAKE_CURRENT_BINARY_DIR} ${TCL_TCLSH}
USES_TERMINAL
DEPENDS ${TARGET})


add_library(${PROJECT_NAME} SHARED src/library.c src/compiler_tcl.c src/compiler_c.c src/md5.c
src/common.c)
set_target_properties(${PROJECT_NAME}
Expand All @@ -33,6 +72,11 @@ install(TARGETS ${TARGET}

configure_file(pkgIndex.tcl.in pkgIndex.tcl @ONLY)

# following is needed for tests to work properly
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tcl" DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/cmake" DESTINATION ${CMAKE_CURRENT_BINARY_DIR})


install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pkgIndex.tcl
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${TARGET}${PROJECT_VERSION}
)
Expand All @@ -44,7 +88,3 @@ install(DIRECTORY ${CMAKE_SOURCE_DIR}/tcl
install(DIRECTORY ${CMAKE_SOURCE_DIR}/cmake
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${TARGET}${PROJECT_VERSION}
)

install(DIRECTORY ${CMAKE_SOURCE_DIR}/include
DESTINATION ${CMAKE_INSTALL_PREFIX}
)
21 changes: 0 additions & 21 deletions bin/compiledir.tcl

This file was deleted.

23 changes: 23 additions & 0 deletions bin/thtml-compiledir.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package require thtml

if { [llength $argv] != 3 } {
puts "Usage: [file tail [info script]] <targetlang> <rootdir> <dir>"
exit 1
}

set target_lang [lindex $argv 0]

if { $target_lang ni {c tcl}} {
puts "Invalid target language: must be either 'c' or 'tcl'"
exit 1
}

set rootdir [lindex $argv 1]
set dir [lindex $argv 2]

::thtml::init [dict create build 1 cache 1 rootdir $rootdir target_lang $target_lang debug 1]
set cachedir [::thtml::get_cachedir]
file delete -force $cachedir
file mkdir $cachedir

puts [::thtml::build::compiledir $dir $target_lang]
4 changes: 2 additions & 2 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(CMAKE_C_STANDARD_REQUIRED true)
set(THREADS_PREFER_PTHREAD_FLAG ON)

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_C_FLAGS "-g -DTCL_THREADS -DPROJECT_VERSION=${PROJECT_VERSION} ${CMAKE_C_FLAGS}")
set(CMAKE_C_FLAGS "-g -DDEBUG -DTCL_THREADS -DPROJECT_VERSION=${PROJECT_VERSION} ${CMAKE_C_FLAGS}")

list(APPEND CMAKE_MODULE_PATH "${THTML_CMAKE_DIR}")
find_package(TCL 8.6.13 REQUIRED) # TCL_INCLUDE_PATH TCL_LIBRARY
Expand All @@ -21,7 +21,7 @@ set_target_properties(${PROJECT_NAME}
INSTALL_RPATH_USE_LINK_PATH ON
)

include_directories(${TCL_INCLUDE_PATH})
include_directories(${TCL_INCLUDE_PATH} ${THTML_CMAKE_DIR}/include)
target_link_directories(${PROJECT_NAME} PRIVATE ${TCL_LIBRARY_PATH})
target_link_libraries(${PROJECT_NAME} PRIVATE ${TCL_LIBRARY})
get_filename_component(TCL_LIBRARY_PATH "${TCL_LIBRARY}" PATH)
Expand Down
32 changes: 32 additions & 0 deletions include/thtml.h → cmake/include/thtml.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ typedef int Tcl_Size;
#define SetResult(str) Tcl_ResetResult(__interp__); \
Tcl_SetStringObj(Tcl_GetObjResult(__interp__), (str), -1)

#ifdef DEBUG
# define DBG(x) x
#ifndef __FUNCTION_NAME__
#ifdef _WIN32 // WINDOWS
#define __FUNCTION_NAME__ __FUNCTION__
#else // GCC
#define __FUNCTION_NAME__ __func__
#endif
#endif
# define DBG2(x) {printf("%s: line %d: ", __FUNCTION_NAME__, __LINE__); printf(x); printf("\n"); fflush(stdout);}
#else
# define DBG(x)
# define DBG2(x)
#endif


#define UWIDE_MAX ((Tcl_WideUInt)-1)
#define WIDE_MAX ((Tcl_WideInt)(UWIDE_MAX >> 1))
#define WIDE_MIN ((Tcl_WideInt)((Tcl_WideUInt)WIDE_MAX+1))
Expand Down Expand Up @@ -1162,4 +1178,20 @@ Tcl_Obj *__thtml_ternary__(Tcl_Interp *interp, Tcl_Obj *a, Tcl_Obj *b, Tcl_Obj *
return a_val ? b : c;
}

int __thtml_dict_merge__(Tcl_Interp *interp, Tcl_Obj *target_obj, Tcl_Obj *source_obj) {
Tcl_Obj *key_obj = NULL, *value_obj = NULL;
int i, done;
Tcl_DictSearch search;
if (Tcl_DictObjFirst(interp, source_obj, &search, &key_obj, &value_obj,
&done) != TCL_OK) {
return TCL_ERROR;
}
while (!done) {
Tcl_DictObjPut(interp, target_obj, key_obj, value_obj);
Tcl_DictObjNext(&search, &key_obj, &value_obj, &done);
}
Tcl_DictObjDone(&search);
return TCL_OK;
}

#endif // THTML_H
45 changes: 44 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ HTML Templating Engine for TCL and [twebserver](https://github.com/jerily/twebse
* [tcl](https://www.tcl.tk/) (version 9.0) - TCL
* [tdom](http://www.tdom.org/) (version [0.9.4](http://tdom.org/downloads/tdom-0.9.4-src.tar.gz)) - Tcl XML parser

* If you are planning to use ```import_node_module``` and ```js``` tags, you will need:
* [npm](https://www.npmjs.com/) - Node.js package manager
* rollup bundler:
```bash
sudo npm -g i rollup
```

## Installation

```bash
Expand Down Expand Up @@ -153,4 +160,40 @@ set template {
TCL:
```tcl
::thtml::render $template {title "Hello World!"}
```
```
## Working with JavaScript
### Plain old script tags
Template:
```html
set template {
<script>
console.log("Hello World!");
</script>
}
```
### Node modules
Template:
```html
set template {
<import_node_module name="lodash" src="lodash" />
<import_node_module src="purecss/build/pure-min.css" />
<html>
<head>
<bundle_css url_prefix="/bundle/" />
</head>
<body>
<div id="version"></div>
<js>
var el = document.getElementById("version");
el.innerText = "lodash version: " + lodash.VERSION;
</js>
<bundle_js url_prefix="/bundle/" />
</body>
</html>
}
11 changes: 11 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@ typedef int Tcl_Size;
#define XSTR(s) STR(s)
#define STR(s) #s

#define UNUSED(expr) do { (void)(expr); } while (0)

#ifdef DEBUG
# define DBG(x) x
#ifndef __FUNCTION_NAME__
#ifdef _WIN32 // WINDOWS
#define __FUNCTION_NAME__ __FUNCTION__
#else // GCC
#define __FUNCTION_NAME__ __func__
#endif
#endif
# define DBG2(x) {printf("%s: ", __FUNCTION_NAME__); x; printf("\n"); fflush(stdout);}
#else
# define DBG(x)
# define DBG2(x)
#endif

#define ObjCmdProc(x) int (x)(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Expand Down
Loading

0 comments on commit 8cb16c9

Please sign in to comment.