@@ -2,22 +2,21 @@ use std::path::Path;
22use std:: collections:: { VecDeque , BTreeMap } ;
33
44use 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 } ;
76
87
98// Handlebars helper for navigation
109
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 > {
1211 debug ! ( "[fn]: previous (handlebars helper)" ) ;
1312
1413 debug ! ( "[*]: Get data from context" ) ;
1514 // get value from context data
1615 // rc.get_path() is current json parent path, you should always use it like this
1716 // 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 ( ) ;
1918
20- let current = c . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "path" )
19+ let current = rc . context ( ) . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "path" )
2120 . to_string ( )
2221 . replace ( "\" " , "" ) ;
2322
@@ -50,7 +49,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
5049 match previous. get ( "name" ) {
5150 Some ( n) => {
5251 debug ! ( "[*]: Inserting title: {}" , n) ;
53- previous_chapter. insert ( "title" . to_owned ( ) , n . to_json ( ) )
52+ previous_chapter. insert ( "title" . to_owned ( ) , json ! ( n ) )
5453 } ,
5554 None => {
5655 debug ! ( "[*]: No title found for chapter" ) ;
@@ -68,7 +67,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
6867
6968 match path. to_str ( ) {
7069 Some ( p) => {
71- previous_chapter. insert ( "link" . to_owned ( ) , p. replace ( "\\ " , "/" ) . to_json ( ) ) ;
70+ previous_chapter. insert ( "link" . to_owned ( ) , json ! ( p. replace( "\\ " , "/" ) ) ) ;
7271 } ,
7372 None => return Err ( RenderError :: new ( "Link could not be converted to str" ) ) ,
7473 }
@@ -78,13 +77,14 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
7877
7978 debug ! ( "[*]: Inject in context" ) ;
8079 // Inject in current context
81- let updated_context = c . extend ( & previous_chapter) ;
80+ let updated_context = rc . context ( ) . extend ( & previous_chapter) ;
8281
8382 debug ! ( "[*]: Render template" ) ;
8483 // Render template
8584 match _h. template ( ) {
8685 Some ( t) => {
87- try!( t. render ( & updated_context, r, rc) ) ;
86+ * rc. context_mut ( ) = updated_context;
87+ try!( t. render ( r, rc) ) ;
8888 } ,
8989 None => return Err ( RenderError :: new ( "Error with the handlebars template" ) ) ,
9090 }
@@ -108,16 +108,16 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
108108
109109
110110
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 > {
112112 debug ! ( "[fn]: next (handlebars helper)" ) ;
113113
114114 debug ! ( "[*]: Get data from context" ) ;
115115 // get value from context data
116116 // rc.get_path() is current json parent path, you should always use it like this
117117 // 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 ( ) ;
119119
120- let current = c . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "path" )
120+ let current = rc . context ( ) . navigate ( rc. get_path ( ) , & VecDeque :: new ( ) , "path" )
121121 . to_string ( )
122122 . replace ( "\" " , "" ) ;
123123
@@ -154,7 +154,7 @@ pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) ->
154154 match item. get ( "name" ) {
155155 Some ( n) => {
156156 debug ! ( "[*]: Inserting title: {}" , n) ;
157- next_chapter. insert ( "title" . to_owned ( ) , n . to_json ( ) ) ;
157+ next_chapter. insert ( "title" . to_owned ( ) , json ! ( n ) ) ;
158158 } ,
159159 None => return Err ( RenderError :: new ( "No title found for chapter in JSON data" ) ) ,
160160 }
@@ -166,21 +166,22 @@ pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) ->
166166 match link. to_str ( ) {
167167 Some ( l) => {
168168 // 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( "\\ " , "/" ) ) ) ;
170170 } ,
171171 None => return Err ( RenderError :: new ( "Link could not converted to str" ) ) ,
172172 }
173173
174174 debug ! ( "[*]: Inject in context" ) ;
175175 // Inject in current context
176- let updated_context = c . extend ( & next_chapter) ;
176+ let updated_context = rc . context ( ) . extend ( & next_chapter) ;
177177
178178 debug ! ( "[*]: Render template" ) ;
179179
180180 // Render template
181181 match _h. template ( ) {
182182 Some ( t) => {
183- try!( t. render ( & updated_context, r, rc) ) ;
183+ * rc. context_mut ( ) = updated_context;
184+ try!( t. render ( r, rc) ) ;
184185 } ,
185186 None => return Err ( RenderError :: new ( "Error with the handlebars template" ) ) ,
186187 }
0 commit comments