• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2001-2004 Brandon Long
3  * All Rights Reserved.
4  *
5  * ClearSilver Templating System
6  *
7  * This code is made available under the terms of the ClearSilver License.
8  * http://www.clearsilver.net/license.hdf
9  *
10  */
11 
12 #include <ruby.h>
13 #include "ClearSilver.h"
14 #include "neo_ruby.h"
15 
16 static VALUE cCs;
17 extern VALUE mNeotonic;
18 extern VALUE eHdfError;
19 
20 VALUE r_neo_error(NEOERR *err);
21 
22 #define Srb_raise(val) rb_raise(eHdfError, "%s/%d %s",__FILE__,__LINE__,RSTRING(val)->ptr)
23 
c_free(CSPARSE * csd)24 static void c_free (CSPARSE *csd) {
25   if (csd) {
26     cs_destroy (&csd);
27   }
28 }
29 
c_init(VALUE self)30 static VALUE c_init (VALUE self) {
31   return self;
32 }
33 
c_new(VALUE class,VALUE oHdf)34 VALUE c_new (VALUE class, VALUE oHdf) {
35   CSPARSE *cs = NULL;
36   NEOERR *err;
37   t_hdfh *hdfh;
38   VALUE r_cs;
39 
40   Data_Get_Struct(oHdf, t_hdfh, hdfh);
41 
42   if (hdfh == NULL) rb_raise(eHdfError, "must include an Hdf object");
43 
44   err = cs_init (&cs, hdfh->hdf);
45   if (err) Srb_raise(r_neo_error(err));
46   err = cgi_register_strfuncs(cs);
47   if (err) Srb_raise(r_neo_error(err));
48 
49   r_cs = Data_Wrap_Struct(class, 0, c_free, cs);
50   rb_obj_call_init(r_cs, 0, NULL);
51   return r_cs;
52 }
53 
c_parse_file(VALUE self,VALUE oPath)54 static VALUE c_parse_file (VALUE self, VALUE oPath) {
55   CSPARSE *cs = NULL;
56   NEOERR *err;
57   char *path;
58 
59   Data_Get_Struct(self, CSPARSE, cs);
60   path = STR2CSTR(oPath);
61 
62   err = cs_parse_file (cs, path);
63   if (err) Srb_raise(r_neo_error(err));
64 
65   return self;
66 }
67 
c_parse_str(VALUE self,VALUE oString)68 static VALUE c_parse_str (VALUE self, VALUE oString)
69 {
70   CSPARSE *cs = NULL;
71   NEOERR *err;
72   char *s, *ms;
73   long l;
74 
75   Data_Get_Struct(self, CSPARSE, cs);
76   s = rb_str2cstr(oString, &l);
77 
78   /* This should be changed to use memory from the gc */
79   ms = strdup(s);
80   if (ms == NULL) rb_raise(rb_eNoMemError, "out of memory");
81 
82   err = cs_parse_string (cs, ms, (size_t)l);
83 
84   if (err) Srb_raise(r_neo_error(err));
85 
86   return self;
87 }
88 
render_cb(void * ctx,char * buf)89 static NEOERR *render_cb (void *ctx, char *buf)
90 {
91   STRING *str= (STRING *)ctx;
92 
93   return nerr_pass(string_append(str, buf));
94 }
95 
c_render(VALUE self)96 static VALUE c_render (VALUE self)
97 {
98   CSPARSE *cs = NULL;
99   NEOERR *err;
100   STRING str;
101   VALUE rv;
102 
103   Data_Get_Struct(self, CSPARSE, cs);
104 
105   string_init(&str);
106   err = cs_render (cs, &str, render_cb);
107   if (err) Srb_raise(r_neo_error(err));
108 
109   rv = rb_str_new2(str.buf);
110   string_clear (&str);
111   return rv;
112 }
113 
Init_cs()114 void Init_cs() {
115   cCs = rb_define_class_under(mNeotonic, "Cs", rb_cObject);
116   rb_define_singleton_method(cCs, "new", c_new, 1);
117 
118   rb_define_method(cCs, "initialize", c_init, 0);
119   rb_define_method(cCs, "parse_file", c_parse_file, 1);
120   rb_define_method(cCs, "parse_string", c_parse_str, 1);
121   rb_define_method(cCs, "render", c_render, 0);
122 }
123