@@ -79,6 +79,7 @@ isTruthy(dom::Value const& arg)
79
79
return !arg.getString ().empty ();
80
80
case dom::Kind::Array:
81
81
case dom::Kind::Object:
82
+ case dom::Kind::Function:
82
83
return true ;
83
84
case dom::Kind::Null:
84
85
return false ;
@@ -1206,7 +1207,6 @@ render_to(
1206
1207
if (options.data .isNull ())
1207
1208
{
1208
1209
state.data .set (" root" , context);
1209
- state.data .set (" level" , " warn" );
1210
1210
}
1211
1211
else if (options.data .isObject ())
1212
1212
{
@@ -1348,6 +1348,8 @@ JSON_stringify(
1348
1348
s += " }" ;
1349
1349
return s;
1350
1350
}
1351
+ case dom::Kind::Function:
1352
+ return " [Function]" ;
1351
1353
default :
1352
1354
MRDOX_UNREACHABLE ();
1353
1355
}
@@ -2927,8 +2929,13 @@ if_fn(
2927
2929
return ;
2928
2930
}
2929
2931
2930
- dom::Value const & conditional = args[0 ];
2931
- if (conditional.kind () == dom::Kind::Integer) {
2932
+ dom::Value conditional = args[0 ];
2933
+ if (conditional.isFunction ())
2934
+ {
2935
+ conditional = conditional.getFunction ()(options.context ());
2936
+ }
2937
+
2938
+ if (conditional.isInteger ()) {
2932
2939
// Treat the zero path separately
2933
2940
std::int64_t v = conditional.getInteger ();
2934
2941
if (v == 0 ) {
@@ -2947,7 +2954,7 @@ if_fn(
2947
2954
return ;
2948
2955
}
2949
2956
}
2950
- if (isTruthy (conditional)) {
2957
+ if (! isEmpty (conditional)) {
2951
2958
options.fn (out);
2952
2959
return ;
2953
2960
}
@@ -2958,38 +2965,9 @@ void
2958
2965
unless_fn (
2959
2966
dom::Array const & args,
2960
2967
HandlebarsCallback const & options) {
2961
- OutputRef out = options.output ();
2962
- auto r = validateArgs (options.name (), 1 , args);
2963
- if (!r.empty ()) {
2964
- out << r;
2965
- return ;
2966
- }
2967
-
2968
- dom::Value const & conditional = args[0 ];
2969
- if (conditional.kind () == dom::Kind::Integer) {
2970
- // Treat the zero path separately
2971
- std::int64_t v = conditional.getInteger ();
2972
- if (v == 0 ) {
2973
- bool includeZero = false ;
2974
- if (options.hashes ().exists (" includeZero" )) {
2975
- auto zeroV = options.hashes ().find (" includeZero" );
2976
- if (zeroV.isBoolean ()) {
2977
- includeZero = zeroV.getBool ();
2978
- }
2979
- }
2980
- if (includeZero) {
2981
- options.inverse (out);
2982
- } else {
2983
- options.fn (out);
2984
- }
2985
- return ;
2986
- }
2987
- }
2988
- if (isTruthy (conditional)) {
2989
- options.inverse (out);
2990
- return ;
2991
- }
2992
- options.fn (out);
2968
+ HandlebarsCallback options2 = options;
2969
+ std::swap (options2.fn_ , options2.inverse_ );
2970
+ return if_fn (args, options2);
2993
2971
}
2994
2972
2995
2973
void
@@ -3003,6 +2981,10 @@ with_fn(
3003
2981
return ;
3004
2982
}
3005
2983
dom::Value newContext = args[0 ];
2984
+ if (newContext.isFunction ()) {
2985
+ newContext = newContext.getFunction ()(options.context ());
2986
+ }
2987
+
3006
2988
if (!isEmpty (newContext)) {
3007
2989
dom::Object data = createFrame (options.data ());
3008
2990
std::string contextPath = appendContextPath (
@@ -3023,13 +3005,11 @@ void
3023
3005
each_fn (
3024
3006
dom::Array const & args,
3025
3007
HandlebarsCallback const & options) {
3026
- OutputRef out = options.output ();
3027
- auto r = validateArgs (options.name (), 1 , args);
3028
- if (!r.empty ()) {
3029
- out << r;
3030
- return ;
3008
+ if (args.empty ()) {
3009
+ throw HandlebarsError (" Must pass iterator to #each" );
3031
3010
}
3032
3011
3012
+ OutputRef out = options.output ();
3033
3013
MRDOX_ASSERT (!options.ids ().empty ());
3034
3014
std::string contextPath = appendContextPath (
3035
3015
options.data ().find (" contextPath" ), options.ids ()[0 ]) + ' .' ;
@@ -3038,6 +3018,10 @@ each_fn(
3038
3018
dom::Object blockValues;
3039
3019
3040
3020
dom::Value context = args[0 ];
3021
+ if (context.isFunction ()) {
3022
+ context = context.getFunction ()(options.context ());
3023
+ }
3024
+
3041
3025
std::size_t index = 0 ;
3042
3026
if (context.isArray ()) {
3043
3027
dom::Array const & items = context.getArray ();
@@ -3101,9 +3085,12 @@ log_fn(
3101
3085
dom::Array const & args,
3102
3086
HandlebarsCallback const & options) {
3103
3087
dom::Value level = 1 ;
3104
- if (auto hl = options.hashes ().find (" level" ); !hl.isNull ()) {
3088
+ if (auto hl = options.hashes ().find (" level" ); !hl.isNull ())
3089
+ {
3105
3090
level = hl;
3106
- } else if (auto ol = options.hashes ().find (" level" ); !ol.isNull ()) {
3091
+ }
3092
+ else if (auto ol = options.data ().find (" level" ); !ol.isNull ())
3093
+ {
3107
3094
level = ol;
3108
3095
}
3109
3096
options.log (level, args);
0 commit comments