Skip to content

Instantly share code, notes, and snippets.

@tristanlins
Last active December 8, 2015 11:08
Show Gist options
  • Save tristanlins/abe4b927f4bde7ef6000 to your computer and use it in GitHub Desktop.
Save tristanlins/abe4b927f4bde7ef6000 to your computer and use it in GitHub Desktop.
cmake_minimum_required(VERSION 3.2)
project(url-function-bug)
set(CMAKE_BUILD_TYPE Debug)
set(SOURCE_FILES url-function-bug.c empty.cpp)
include_directories("../libsass/include")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
message("search for libsass static library in: ${CMAKE_SOURCE_DIR}/../libsass/lib")
find_library(SASS_EXT_LIBRARY NAMES sass libsass PATHS "${CMAKE_SOURCE_DIR}/../libsass/lib" NO_DEFAULT_PATH)
message(STATUS "libsass library: ${SASS_EXT_LIBRARY}")
add_library(sass UNKNOWN IMPORTED)
set_property(TARGET sass PROPERTY IMPORTED_LOCATION "${SASS_EXT_LIBRARY}")
file(WRITE empty.cpp "")
if(APPLE)
add_executable(url-function-bug MACOSX_BUNDLE ${SOURCE_FILES})
elseif(WIN32)
add_executable(url-function-bug WIN32 ${SOURCE_FILES})
else()
add_executable(url-function-bug ${SOURCE_FILES})
endif()
set_property(TARGET url-function-bug PROPERTY C_STANDARD 99)
set(LINK_FLAGS ${LINK_FLAGS} "-Wl,--whole-archive")
if(APPLE)
target_link_libraries(url-function-bug sass)
else()
target_link_libraries(url-function-bug "-Wl,--whole-archive" sass "-Wl,--no-whole-archive")
endif()
#include <stdio.h>
#include <string.h>
#include <sass.h>
#include <sass2scss.h>
#include <stdlib.h>
union Sass_Value *custom_function(
const union Sass_Value *sass_arguments,
Sass_Function_Entry sass_function_entry,
struct Sass_Compiler *sass_compiler
) {
union Sass_Value *value = sass_list_get_value(sass_arguments, 0);
printf("call function with %s\n", sass_string_get_value(value));
return sass_make_qstring(sass_string_get_value(value));
}
int main() {
char* scss =
".item1 {\n"
" background: url(item1.jpg);\n"
"}\n"
".item2 {\n"
" background: url('item2.jpg');\n"
"}\n"
".item3 {\n"
" background: url(\"item3.jpg\");\n"
"}\n"
// This item fails with a parsing exception
// ".item4 {\n"
// " background: func(item4.jpg);\n"
// "}\n"
".item5 {\n"
" background: func('item5.jpg');\n"
"}\n"
".item6 {\n"
" background: func(\"item6.jpg\");\n"
"}\n";
struct Sass_Data_Context *sass_context = sass_make_data_context(scss);
struct Sass_Options *sass_options = sass_data_context_get_options(sass_context);
// Register the custom function
Sass_Function_List sass_function_list = sass_make_function_list(2);
{
printf("Register url($url) function\n");
Sass_Function_Entry sass_function_entry = sass_make_function(
"url($url)", (Sass_Function_Fn) custom_function, 0
);
sass_function_set_list_entry(sass_function_list, 0, sass_function_entry);
}
{
printf("Register func($value) function\n");
Sass_Function_Entry sass_function_entry = sass_make_function(
"func($value)", (Sass_Function_Fn) custom_function, 0
);
sass_function_set_list_entry(sass_function_list, 1, sass_function_entry);
}
sass_option_set_c_functions(sass_options, sass_function_list);
// Compile
sass_data_context_set_options(sass_context, sass_options);
struct Sass_Compiler *sass_compiler = sass_make_data_compiler(sass_context);
sass_compiler_parse(sass_compiler);
sass_compiler_execute(sass_compiler);
const char *css = sass_context_get_output_string((struct Sass_Context *) sass_context);
const char *error_json = sass_context_get_error_json((struct Sass_Context *) sass_context);
printf("Result: %s\n", css);
printf("Error: %s\n", error_json);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment