Giter Club home page Giter Club logo

Comments (1)

jbboehr avatar jbboehr commented on July 19, 2024

I'm not really familiar with Python or Heroku unfortunately.

What sort of undefined references are you getting? Can you go into a bit more detail on what you've tried?

Depending on which flags you compile it with, libhandlebars.so does depend on several other shared libraries (libyaml, json-c, talloc - use readelf -d /path/to/libhandlebars.so to see which are required) in the environment.

Assuming you want the full execution pipeline, you can see my other project here for implementing this in PHP, although it's a bit more in-depth than the bare-minimum required functionality:
https://github.com/jbboehr/php-handlebars

If the data is JSON-like and can be converted to the types internally used by handlebars.c, the implementation can be fairly simple:

static int do_execute(void)
{
struct handlebars_context * ctx;
struct handlebars_parser * parser;
struct handlebars_compiler * compiler;
struct handlebars_string * tmpl;
struct handlebars_ast_node * ast;
struct handlebars_program * program;
HANDLEBARS_VALUE_DECL(partials);
jmp_buf jmp;
ctx = handlebars_context_ctor_ex(root);
// Save jump buffer
if( handlebars_setjmp_ex(ctx, &jmp) ) {
fprintf(stderr, "ERROR: %s\n", handlebars_error_message(ctx));
handlebars_context_dtor(ctx);
return 1;
}
parser = handlebars_parser_ctor(ctx);
compiler = handlebars_compiler_ctor(ctx);
if (enable_partial_loader) {
struct handlebars_string *partial_path_str = NULL;
struct handlebars_string *partial_extension_str = NULL;
partial_path_str = handlebars_string_ctor(ctx, partial_path, strlen(partial_path));
partial_extension_str = handlebars_string_ctor(ctx, partial_extension, strlen(partial_extension));
(void) handlebars_value_partial_loader_init(ctx, partial_path_str, partial_extension_str, partials);
}
handlebars_compiler_set_flags(compiler, compiler_flags);
// Read
readInput();
tmpl = handlebars_string_ctor(HBSCTX(parser), input_buf, strlen(input_buf));
// Preprocess
if( compiler_flags & handlebars_compiler_flag_compat ) {
tmpl = handlebars_preprocess_delimiters(ctx, tmpl, NULL, NULL);
}
// Read context
HANDLEBARS_VALUE_DECL(input);
if( input_data_name ) {
size_t input_data_name_len = strlen(input_data_name);
char * input_str = file_get_contents(input_data_name);
if (input_str && strlen(input_str)) {
if (handlebars_value_is_empty(input) && input_data_name_len > 5 && (0 == strcmp(input_data_name + input_data_name_len - 5, ".yaml") ||
0 == strcmp(input_data_name + input_data_name_len - 4, ".yml"))) {
#ifdef HANDLEBARS_HAVE_YAML
handlebars_value_init_yaml_string(ctx, input, input_str);
#else
fprintf(stderr, "Failed to process input data: YAML support is disabled");
exit(1);
#endif
}
if (handlebars_value_is_empty(input)) {
#ifdef HANDLEBARS_HAVE_JSON
// assume json
handlebars_value_init_json_string(ctx, input, input_str);
if (convert_input) {
handlebars_value_convert(input);
}
#else
fprintf(stderr, "Failed to process input data: JSON support is disabled");
exit(1);
#endif
}
}
}
// Parse
ast = handlebars_parse_ex(parser, tmpl, compiler_flags);
// Compile
program = handlebars_compiler_compile_ex(compiler, ast);
// Serialize
struct handlebars_module * module = handlebars_program_serialize(ctx, program);
// Execute
struct handlebars_string * buffer = NULL;
do {
if (buffer) {
handlebars_talloc_free(buffer);
buffer = NULL;
}
struct handlebars_vm * vm;
vm = handlebars_vm_ctor(ctx);
handlebars_vm_set_flags(vm, compiler_flags);
handlebars_vm_set_partials(vm, partials);
buffer = handlebars_vm_execute(vm, module, input);
buffer = talloc_steal(ctx, buffer);
handlebars_vm_dtor(vm);
} while(--run_count > 0);
if (buffer) {
fwrite(hbs_str_val(buffer), sizeof(char), hbs_str_len(buffer), stdout);
}
if (newline_at_eof) {
fwrite("\n", sizeof(char), 1, stdout);
}
HANDLEBARS_VALUE_UNDECL(input);
HANDLEBARS_VALUE_UNDECL(partials);
handlebars_context_dtor(ctx);
return 0;
}

The data would have to be converted to a JSON string first, or the equivalent of handlebars_value_init_json_string for python types would have to be implemented.

from handlebars.c.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.