@@ -2,22 +2,21 @@ use std::path::Path;
2
2
use std:: collections:: { VecDeque , BTreeMap } ;
3
3
4
4
use serde_json;
5
- use serde_json:: value:: ToJson ;
6
- use handlebars:: { Handlebars , RenderError , RenderContext , Helper , Context , Renderable } ;
5
+ use handlebars:: { Handlebars , RenderError , RenderContext , Helper , Renderable } ;
7
6
8
7
9
8
// Handlebars helper for navigation
10
9
11
- pub fn previous ( c : & Context , _h : & Helper , r : & Handlebars , rc : & mut RenderContext ) -> Result < ( ) , RenderError > {
10
+ pub fn previous ( _h : & Helper , r : & Handlebars , rc : & mut RenderContext ) -> Result < ( ) , RenderError > {
12
11
debug ! ( "[fn]: previous (handlebars helper)" ) ;
13
12
14
13
debug ! ( "[*]: Get data from context" ) ;
15
14
// get value from context data
16
15
// rc.get_path() is current json parent path, you should always use it like this
17
16
// param is the key of value you want to display
18
- let chapters = c . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "chapters" ) ;
17
+ let chapters = rc . context ( ) . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "chapters" ) . to_owned ( ) ;
19
18
20
- let current = c . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "path" )
19
+ let current = rc . context ( ) . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "path" )
21
20
. to_string ( )
22
21
. replace ( "\" " , "" ) ;
23
22
@@ -50,7 +49,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
50
49
match previous. get ( "name" ) {
51
50
Some ( n) => {
52
51
debug ! ( "[*]: Inserting title: {}" , n) ;
53
- previous_chapter. insert ( "title" . to_owned ( ) , n . to_json ( ) )
52
+ previous_chapter. insert ( "title" . to_owned ( ) , json ! ( n ) )
54
53
} ,
55
54
None => {
56
55
debug ! ( "[*]: No title found for chapter" ) ;
@@ -68,7 +67,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
68
67
69
68
match path. to_str ( ) {
70
69
Some ( p) => {
71
- previous_chapter. insert ( "link" . to_owned ( ) , p. replace ( "\\ " , "/" ) . to_json ( ) ) ;
70
+ previous_chapter. insert ( "link" . to_owned ( ) , json ! ( p. replace( "\\ " , "/" ) ) ) ;
72
71
} ,
73
72
None => return Err ( RenderError :: new ( "Link could not be converted to str" ) ) ,
74
73
}
@@ -78,13 +77,14 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
78
77
79
78
debug ! ( "[*]: Inject in context" ) ;
80
79
// Inject in current context
81
- let updated_context = c . extend ( & previous_chapter) ;
80
+ let updated_context = rc . context ( ) . extend ( & previous_chapter) ;
82
81
83
82
debug ! ( "[*]: Render template" ) ;
84
83
// Render template
85
84
match _h. template ( ) {
86
85
Some ( t) => {
87
- try!( t. render ( & updated_context, r, rc) ) ;
86
+ * rc. context_mut ( ) = updated_context;
87
+ try!( t. render ( r, rc) ) ;
88
88
} ,
89
89
None => return Err ( RenderError :: new ( "Error with the handlebars template" ) ) ,
90
90
}
@@ -108,16 +108,16 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
108
108
109
109
110
110
111
- pub fn next ( c : & Context , _h : & Helper , r : & Handlebars , rc : & mut RenderContext ) -> Result < ( ) , RenderError > {
111
+ pub fn next ( _h : & Helper , r : & Handlebars , rc : & mut RenderContext ) -> Result < ( ) , RenderError > {
112
112
debug ! ( "[fn]: next (handlebars helper)" ) ;
113
113
114
114
debug ! ( "[*]: Get data from context" ) ;
115
115
// get value from context data
116
116
// rc.get_path() is current json parent path, you should always use it like this
117
117
// param is the key of value you want to display
118
- let chapters = c . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "chapters" ) ;
118
+ let chapters = rc . context ( ) . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "chapters" ) . to_owned ( ) ;
119
119
120
- let current = c . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "path" )
120
+ let current = rc . context ( ) . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "path" )
121
121
. to_string ( )
122
122
. replace ( "\" " , "" ) ;
123
123
@@ -154,7 +154,7 @@ pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) ->
154
154
match item. get ( "name" ) {
155
155
Some ( n) => {
156
156
debug ! ( "[*]: Inserting title: {}" , n) ;
157
- next_chapter. insert ( "title" . to_owned ( ) , n . to_json ( ) ) ;
157
+ next_chapter. insert ( "title" . to_owned ( ) , json ! ( n ) ) ;
158
158
} ,
159
159
None => return Err ( RenderError :: new ( "No title found for chapter in JSON data" ) ) ,
160
160
}
@@ -166,21 +166,22 @@ pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) ->
166
166
match link. to_str ( ) {
167
167
Some ( l) => {
168
168
// Hack for windows who tends to use `\` as separator instead of `/`
169
- next_chapter. insert ( "link" . to_owned ( ) , l. replace ( "\\ " , "/" ) . to_json ( ) ) ;
169
+ next_chapter. insert ( "link" . to_owned ( ) , json ! ( l. replace( "\\ " , "/" ) ) ) ;
170
170
} ,
171
171
None => return Err ( RenderError :: new ( "Link could not converted to str" ) ) ,
172
172
}
173
173
174
174
debug ! ( "[*]: Inject in context" ) ;
175
175
// Inject in current context
176
- let updated_context = c . extend ( & next_chapter) ;
176
+ let updated_context = rc . context ( ) . extend ( & next_chapter) ;
177
177
178
178
debug ! ( "[*]: Render template" ) ;
179
179
180
180
// Render template
181
181
match _h. template ( ) {
182
182
Some ( t) => {
183
- try!( t. render ( & updated_context, r, rc) ) ;
183
+ * rc. context_mut ( ) = updated_context;
184
+ try!( t. render ( r, rc) ) ;
184
185
} ,
185
186
None => return Err ( RenderError :: new ( "Error with the handlebars template" ) ) ,
186
187
}
0 commit comments