-
Notifications
You must be signed in to change notification settings - Fork 6
/
html_template.cpp
1880 lines (1499 loc) · 61.3 KB
/
html_template.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
* Copyright (C) 2009 Andrei Taranchenko
* Contact: http://www.nulidex.com/contact
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
**********************************************************************/
#ifndef html_template_c
#define html_template_c
#include "html_template.h"
#endif
using namespace std;
namespace tmpl {
//----------------------------------------------------------------------------
// table cell
//----------------------------------------------------------------------------
row_cell_s::row_cell_s() {
// we are not creating a table object yet, just because we may not need it in
// the first place.
p_table = 0;
}
//----------------------------------------------------------------------------
row_cell_s::row_cell_s(const row_cell_s & arg) {
str_val = arg.str_val;
p_table = 0; // IMPORTANT, if not zero, the code will assume memory
// allocated for table, while this cell actually may
// contain a string value only
if ( arg.p_table ) // this is why zero-init above is important, but it's
// for the object being assigned
{
p_table = new loop_s;
*p_table = *arg.p_table;
}
}
//----------------------------------------------------------------------------
row_cell_s::~row_cell_s() {
if ( p_table ) {
delete p_table;
}
}
//----------------------------------------------------------------------------
row_cell_s & row_cell_s::operator= (const loop_s & arg) {
p_table = new loop_s;
*p_table = arg;
return *this;
}
//----------------------------------------------------------------------------
// table row
//----------------------------------------------------------------------------
row_cell_s & row_s::operator() (const std::string & str_name) {
string str_name_uc = str_name;
// uppercase
str_name_uc = str_name;
uc( str_name_uc );
// create new table cell
row_cell_s cell;
// insert cell into row container
cells_c.insert(std::make_pair(str_name_uc, cell));
// return cell so that we can assign a value to it
return cells_c[str_name_uc];
}
//----------------------------------------------------------------------------
// tag type object
//----------------------------------------------------------------------------
tag_type_s::tag_type_s() {
str_tag_class = "";
str_tag_type = "";
b_block_tag = false;
}
//----------------------------------------------------------------------------
tag_type_s::tag_type_s(const std::string & arg_type,
const std::string & arg_class,
bool arg_block_tag /*= false*/) {
str_tag_class = arg_class;
str_tag_type = arg_type;
b_block_tag = arg_block_tag;
}
//----------------------------------------------------------------------------
bool tag_type_s::operator== (const tag_type_s & rhs) const {
// two tags are equal if they are of the same CLASS, so ELSE is equal to IF
return (str_tag_class == rhs.str_tag_class);
}
//----------------------------------------------------------------------------
bool tag_type_s::operator!= (const tag_type_s & rhs) const {
return !(*this == rhs);
}
//----------------------------------------------------------------------------
// tag object
//----------------------------------------------------------------------------
tag_s::tag_s() {
Reset();
}
//----------------------------------------------------------------------------
void tag_s::Reset() {
str_name = "";
begin = string::npos;
end = string::npos;
b_termination = false;
b_splitter = false;
escape_mode = ESC_NONE;
}
//----------------------------------------------------------------------------
tag_s::tag_s(const tag_type_s & arg_type) {
tag_type = arg_type;
}
//----------------------------------------------------------------------------
void tag_s::Shift(const ptrdiff_t i_offset) {
// do not shit an empty tag
if ( begin == string::npos ) return;
begin = static_cast<size_t>(begin + i_offset);
end = static_cast<size_t>(end + i_offset);
}
//----------------------------------------------------------------------------
const bool tag_s::Empty() const {
// tag is empty if none of its properties have been defined
return (
str_name.empty() == true
&& b_termination == false
&& b_splitter == false
);
}
//----------------------------------------------------------------------------
const size_t tag_s::Length() const {
return (end - begin) + 1;
}
//----------------------------------------------------------------------------
bool tag_s::operator< (const tag_s & rhs) const {
// compare start positions of two tags to find which is further up
return begin < rhs.begin;
}
//----------------------------------------------------------------------------
bool tag_s::operator> (const tag_s & rhs) const {
return begin > rhs.begin;
}
//----------------------------------------------------------------------------
bool tag_s::operator== (const tag_s & rhs) const {
return (rhs.begin == begin);
}
//----------------------------------------------------------------------------
un_predicate_count_tag_type::un_predicate_count_tag_type(const tag_type_s & arg) {
tag_type = arg;
}
//----------------------------------------------------------------------------
bool un_predicate_count_tag_type::operator()(const tag_s & arg_tag) {
return (arg_tag.Get_Tag_Type() == tag_type);
}
//----------------------------------------------------------------------------
// block object
//----------------------------------------------------------------------------
block_s::block_s(const tag_s & arg_open) {
b_deleted = false;
Set_Open_Tag( arg_open );
}
//----------------------------------------------------------------------------
void block_s::Set_Open_Tag(const tag_s & arg) {
tag_open = arg;
block_type = arg.Get_Tag_Type();
}
//----------------------------------------------------------------------------
block_s::block_s() {
b_deleted = false;
}
//----------------------------------------------------------------------------
void block_s::Shift(const ptrdiff_t i_offset, const size_t ui_start,
const size_t ui_end) {
// do nothing if the block is no longer in the set
if ( Deleted() == true ) return;
// store all tags in a container, for easy iteration
vector <tag_s*> tags_c;
tags_c.push_back(&tag_open);
tags_c.push_back(&tag_split);
tags_c.push_back(&tag_close);
for ( size_t n=0; n < tags_c.size(); n++ ) {
/*not const*/ tag_s* itr_tag = tags_c[n];
// do nothing if the tag is above the start position or below the end
// posisiton
if ( itr_tag->Start() < ui_start || itr_tag->Stop() > ui_end ) {
continue;
}
itr_tag->Shift(i_offset);
}
}
//----------------------------------------------------------------------------
bool block_s::Contains(const block_s & rhs) const {
// if this is a block with no terminating tag, it can't contain anything
if ( this->Get_Type().b_block_tag == false ) {
return false;
}
return (rhs.Get_Open_Tag() > this->Get_Open_Tag() &&
rhs.Get_Close_Tag() < this->Get_Close_Tag());
}
//----------------------------------------------------------------------------
bool block_s::operator== (const tag_s & rhs) const {
return (rhs.Start() == tag_open.Start()
|| rhs.Start() == tag_close.Start()
|| rhs.Start() == tag_split.Start());
}
//----------------------------------------------------------------------------
bool block_s::operator!= (const tag_s & rhs) const {
return !(*this == rhs);
}
//----------------------------------------------------------------------------
bool block_s::operator== (const block_s & rhs) const {
return (rhs.tag_open == tag_open);
}
//----------------------------------------------------------------------------
bool block_s::operator!= (const block_s & rhs) const {
return !(*this == rhs);
}
//----------------------------------------------------------------------------
bool block_s::operator> (const block_s & rhs) const {
// block is greater than another block if the start of the block is after the
// close of another block
return (tag_open > rhs.tag_close);
}
//----------------------------------------------------------------------------
un_predicate_count_tag::un_predicate_count_tag(const tag_s & arg) {
tag = arg;
}
//----------------------------------------------------------------------------
bool un_predicate_count_tag::operator()(const block_s & arg_block) {
return (arg_block == tag);
}
//----------------------------------------------------------------------------
// template class public
//----------------------------------------------------------------------------
html_template::html_template() {
tag_type_prefix = "TMPL_";
init();
}
//----------------------------------------------------------------------------
html_template::html_template(const std::string & arg_file_name) {
tag_type_prefix = "TMPL_";
init();
Set_Template_File(arg_file_name);
}
//----------------------------------------------------------------------------
void html_template::Set_Template_File(const std::string & arg_file_name) {
str_tmpl_file_name = arg_file_name;
if (arg_file_name.empty()) {
throw("Template file name not specified");
}
#ifdef DEBUG
cout << "\n!!!!!!!!!!!!!!!\ntemplate " << str_tmpl_file_name << " loaded" << endl;
#endif
}
//----------------------------------------------------------------------------
const std::string & html_template::Process() {
std::ifstream in_stream;
in_stream.open( str_tmpl_file_name.c_str(), ios::binary );
if( !in_stream.is_open() ) {
runtime_ex ex("Could not open template file");
throw_exception(ex);
}
// read in the file
std::stringstream oss;
oss << in_stream.rdbuf();
str_tmpl_txt = oss.str();
in_stream.close();
// this will expand the include files, and build the map of all the variables -
// once if there are no includes or (1 + # of includes)
expand_includes();
str_tmpl_txt_cpy = str_tmpl_txt;
#ifdef DEBUG
print_block_map(block_map);
#endif
// tag iterator
tag_map_t::const_iterator itr_tag;
// check that every tag is part of a block
for ( itr_tag = tag_map.begin(); itr_tag != tag_map.end(); ++itr_tag ) {
block_map_t::const_iterator itr_block
= find(block_map.begin(), block_map.end(), *itr_tag);
if ( itr_block == block_map.end() ) {
syntax_ex error("Unmatched tag");
error.line = get_line_from_pos(itr_tag->Start());
error.detail = "Unmatched '"
+ itr_tag->Get_Tag_Type().str_tag_type
+ "' tag";
throw_exception(error);
}
}
#ifdef DEBUG
//
// assert that every tag appears only once in each block
//
for ( itr_tag = tag_map.begin(); itr_tag != tag_map.end(); ++itr_tag ) {
un_predicate_count_tag predicate(*itr_tag);
const size_t ui_occurances
= std::count_if(block_map.begin(), block_map.end(), predicate);
assert(ui_occurances == 1 && "Each tag must appear in a block only ONCE");
}
#endif
//**************************************************************************
//**************************************************************************
//
// start variable substitution
//
// process IF blocks first to take out unwanted data right away
process_conditionals(block_map, str_tmpl_txt_cpy, variables_c);
// process loops
process_loops(block_map, str_tmpl_txt_cpy, variables_c);
//
// process simple variables
//
process_simple_vars(block_map, str_tmpl_txt_cpy, variables_c);
//
// end variable substitution
//
//**************************************************************************
//**************************************************************************
return str_tmpl_txt_cpy;
}
//----------------------------------------------------------------------------
void html_template::print_block_map(block_map_t & r_block_map) {
// block iterator
block_map_t::const_iterator itr_block = r_block_map.begin();
cout << "\n*************************************************"
<< "\nBLOCK MAP"
<< "\n*************************************************\n";
for ( ; itr_block != r_block_map.end(); ++itr_block) {
cout << itr_block->Get_Type().str_tag_class
<< "\t"<< itr_block->Get_Name();
if ( itr_block->Deleted() )
cout << " [DELETED]";
cout << "\nOpen\tL " << get_line_from_pos(itr_block->Get_Open_Tag().Start())
<< " (" << itr_block->Get_Open_Tag().Start()
<< ", " << itr_block->Get_Open_Tag().Stop() << ")";
if ( itr_block->Get_Split_Tag().Start() != string::npos )
cout << "\nSplit\tL "
<< get_line_from_pos(itr_block->Get_Split_Tag().Start())
<< " (" << itr_block->Get_Split_Tag().Start()
<< ", " << itr_block->Get_Split_Tag().Stop() << ")";
if ( itr_block->Get_Close_Tag().Start() != string::npos )
cout << "\nClose\tL "
<< get_line_from_pos(itr_block->Get_Close_Tag().Start())
<< " (" << itr_block->Get_Close_Tag().Start()
<< ", " << itr_block->Get_Close_Tag().Stop() << ")";
cout << "\n-------------\n";
}
}
//----------------------------------------------------------------------------
void html_template::print_tag_map() {
//!
//! NOTE - the tag map is used to create the block map. After the block map
//! is created, the tag map is no longer used for anything - you can't debug
//! tag shifting with it. Use the block map for that.
//!
// tag iterator
tag_map_t::const_iterator itr_tag;
cout << "\n*************************************************"
<< "\nTAG MAP"
<< "\n*************************************************\n";
for ( itr_tag = tag_map.begin(); itr_tag != tag_map.end(); ++itr_tag ) {
cout << "L " << get_line_from_pos(itr_tag->Start()) << "\t";
if ( itr_tag->Is_Termination() )
cout << "</";
else
cout << "<";
cout << itr_tag->Get_Tag_Type().str_tag_type << ">";
if ( itr_tag->Is_Named() )
cout << "\t= ";
cout << itr_tag->Get_Name()
<< " (" << itr_tag->Start() << ", " << itr_tag->Stop() << ")"
<< endl;
}
}
//----------------------------------------------------------------------------
html_template::~html_template() {
#ifdef DEBUG
cout << "template " << str_tmpl_file_name << " destroyed" << endl;
#endif
}
//----------------------------------------------------------------------------
cls_variable & html_template::operator() (const std::string & arg_var_name) {
string arg_var_name_uc = arg_var_name;
// uppercase
uc(arg_var_name_uc);
// create variable
cls_variable var(arg_var_name_uc);
// save it
variables_c[arg_var_name_uc] = var;
#ifdef DEBUG3
cout << "creating variable '" << arg_var_name_uc << "'" << endl;
#endif
// return the variable so a value can be assigned to it
return variables_c[arg_var_name_uc];
}
//----------------------------------------------------------------------------
const size_t
html_template::Get_Tag_Type_Count(const tag_type_s & tag_type) const {
un_predicate_count_tag_type predicate(tag_type);
const size_t ui_count = std::count_if(
tag_map.begin(), tag_map.end(), predicate
);
return ui_count;
}
//----------------------------------------------------------------------------
// template class private
//----------------------------------------------------------------------------
void html_template::init() {
//
// declare all known tag types
//
// the last boolean flag defines if the tag is a block (has open and close
// tags)
tag_types_c["SIMPLE"] = tag_type_s("VAR", "VAR");
tag_types_c["LOOP"] = tag_type_s("LOOP", "LOOP", true);
tag_types_c["IF"] = tag_type_s("IF", "IF", true);
tag_types_c["ELSE"] = tag_type_s("ELSE", "IF");
tag_types_c["UNLESS"] = tag_type_s("UNLESS", "IF", true);
tag_types_c["INCLUDE"] = tag_type_s("INCLUDE", "INCLUDE");
// create the tag strings we expect to see in a document, based on the tag
// types
tag_types_t::const_iterator itr_tag_type = tag_types_c.begin();
for ( ; itr_tag_type != tag_types_c.end(); ++itr_tag_type ) {
tag_type_s tag_type = itr_tag_type->second;
string str_tag_type_ref = tag_type_prefix + tag_type.str_tag_type;
// save
tag_strings_c[ str_tag_type_ref ] = 1;
}
// populate reserved words container (helps us during parsing)
reserved_words_c[ "ESCAPE" ] = 1;
reserved_words_c[ "ESC" ] = 1;
reserved_words_c[ "HTML" ] = 1;
reserved_words_c[ "JS" ] = 1;
reserved_words_c[ "JAVASCRIPT" ] = 1;
reserved_words_c[ "URL" ] = 1;
reserved_words_c[ "XML" ] = 1;
}
//----------------------------------------------------------------------------
void html_template::expand_includes() {
build_block_map();
// block iterator
block_map_t::const_iterator itr_block;
itr_block = block_map.begin();
unsigned short ush_processed_includes = 0;
for (; itr_block != block_map.end(); ++itr_block) {
if ( itr_block->Get_Type() != tag_types_c["INCLUDE"] ) continue;
if ( itr_block->Deleted() ) continue;
// increment count
++ush_processed_includes;
// read in the file contents
string str_file_name = itr_block->Get_Name();
// the file may have either absolute or relative path. If relative, combine
// it with the path of the parent file
if ( str_file_name.find_first_of("\\/", 0, 1) == string::npos ) {
// this file is relative to main template location
// get the main template dir
const string str_parent_tmpl_dir = file_directory( str_tmpl_file_name );
str_file_name = str_parent_tmpl_dir + str_file_name;
}
std::ifstream in_stream;
in_stream.open( str_file_name.c_str() );
if( !in_stream.is_open() ) {
runtime_ex ex("Could not open file for reading");
throw_exception(ex);
}
// read in the file
std::stringstream oss;
oss << in_stream.rdbuf();
const string str_replace_with = oss.str();
in_stream.close();
const size_t block_len = itr_block->Get_Open_Tag().Stop()
- itr_block->Get_Open_Tag().Start()
+ 1;
const ptrdiff_t i_offset = get_offset(block_len, str_replace_with);
// shift tags in rest of document
shift_tags(
str_tmpl_txt, block_map, i_offset,
itr_block->Get_Open_Tag().Stop()
);
str_tmpl_txt.replace(
itr_block->Get_Open_Tag().Start(),
block_len,
str_replace_with
);
}
// recurse if any tags were processed
if ( ush_processed_includes ) {
expand_includes(); // RECURSION
}
// no need to build the map again - if there was at least one include, the
// map would be rebuilt by the recursive call
}
//----------------------------------------------------------------------------
void html_template::process_conditionals(block_map_t & r_block_map,
std::string & str_text,
variables_t & r_variables_c) {
// block iterator
block_map_t::iterator itr_block = r_block_map.begin();
for (; itr_block != r_block_map.end(); ++itr_block) {
if ( itr_block->Get_Type() != tag_types_c["IF"] ) continue;
if ( itr_block->Deleted() ) continue;
// ignore this conditional if it's within a loop scope (defined inside the
// given block map
block_map_t::const_iterator itr_block_2 = r_block_map.begin();
bool b_this_scope = true;
for (; itr_block_2 != r_block_map.end(); ++itr_block_2) {
if ( itr_block_2->Get_Type() == tag_types_c["LOOP"] ) {
if ( itr_block_2->Contains(*itr_block) ) {
b_this_scope = false;
break;;
}
}
}
if ( !b_this_scope ) continue;
#ifdef DEBUG2
cout << "processing conditional " << itr_block->Get_Name() << endl;
#endif
size_t replace_len = 0;
size_t replace_with_begin = 0;
size_t replace_with_end = 0;
size_t block_len = 0;
string str_replace_with = "";
bool b_eval_as_true = false;
// part of an IF block may be deleted. In that case, there are two tags
// between each other tags will need to be deleted as well
tag_s delete_btwn_tag_begin;
tag_s delete_btwn_tag_end;
// space between these tags will be the offset to shift other tags by
tag_s shift_offset_tag_begin;
tag_s shift_offset_tag_end;
// these define the block where tag shifting will occur
tag_s shift_tag_from;
tag_s shift_tag_until;
// if variable for this IF found, evaluate
variables_t::iterator itr_var;
itr_var = r_variables_c.find( itr_block->Get_Name() );
if ( itr_var != r_variables_c.end() ) {
cls_variable & r_var = itr_var->second;
// evaluate the IF
b_eval_as_true = evaluate(r_var);
// if this is an UNLESS, flip the evaluation result
if ( itr_block->Get_Type().str_tag_type == "UNLESS" ) {
b_eval_as_true = (b_eval_as_true == false) ? true : false;
}
} else {
// if this is an unless, the absent variable should eval to TRUE
if ( itr_block->Get_Type().str_tag_type == "UNLESS" ) {
b_eval_as_true = true;
}
}
// determine what we are replacing the IF with
// if this is a two part block
if ( itr_block->Has_Split_Tag() ) {
// replace with first part of statement if true
if ( b_eval_as_true) {
// replace the whole if with what's between the IF and the ELSE
replace_with_begin = itr_block->Get_Open_Tag().Stop();
replace_with_end = itr_block->Get_Split_Tag().Start();
// delete between the ELSE and the /IF
delete_btwn_tag_begin = itr_block->Get_Split_Tag();
delete_btwn_tag_end = itr_block->Get_Close_Tag();
shift_offset_tag_begin = itr_block->Get_Open_Tag();
shift_offset_tag_end = itr_block->Get_Open_Tag();
shift_tag_from = itr_block->Get_Open_Tag();
shift_tag_until = itr_block->Get_Split_Tag();
} else { // replace with second part if false
// replace the whole IF with what's between the ELSE and the /IF
replace_with_begin = itr_block->Get_Split_Tag().Stop();
replace_with_end = itr_block->Get_Close_Tag().Start();
// delete between the IF and the ELSE
delete_btwn_tag_begin = itr_block->Get_Open_Tag();
delete_btwn_tag_end = itr_block->Get_Split_Tag();
shift_offset_tag_begin = itr_block->Get_Open_Tag();
shift_offset_tag_end = itr_block->Get_Split_Tag();
shift_tag_from = itr_block->Get_Split_Tag();
shift_tag_until = itr_block->Get_Close_Tag();
}
} else { //one part IF block - it either stays or goes
// replacing with whatever's in the whole IF, when the statements
// evaluates to TRUE
replace_with_begin = itr_block->Get_Open_Tag().Stop();
replace_with_end = itr_block->Get_Close_Tag().Start();
shift_offset_tag_begin = itr_block->Get_Open_Tag();
shift_offset_tag_end = itr_block->Get_Open_Tag();
shift_tag_from = itr_block->Get_Open_Tag();
shift_tag_until = itr_block->Get_Close_Tag();
// if this evaluates to FALSE, define range in which all other tags
// will be nuked, and no longer expanded by future operations
if (!b_eval_as_true) {
delete_btwn_tag_begin = itr_block->Get_Open_Tag();
delete_btwn_tag_end = itr_block->Get_Close_Tag();
}
}
// length of test that will replace the IF
replace_len = replace_with_end - replace_with_begin - 1;
// get the actual text to replace the IF with
str_replace_with = str_text.substr(replace_with_begin + 1,
replace_len);
// length of the IF block
block_len = itr_block->Get_Close_Tag().Stop()
- itr_block->Get_Open_Tag().Start()
+ 1;
// delete all blocks in IF part that will go away, if that part is defined
if ( delete_btwn_tag_begin.Empty() == false ) {
delete_blocks(delete_btwn_tag_begin, delete_btwn_tag_end, r_block_map);
}
// shift tags inside the IF
shift_tags(
// subject text
str_text,
// the block map
r_block_map,
// shift size
(shift_offset_tag_end.Stop() - shift_offset_tag_begin.Start() + 1) * -1,
// shift range start
shift_tag_from.Stop(),
// shift range end
shift_tag_until.Start()
);
if ( itr_block->Has_Split_Tag() ) {
// shift tags in rest of document
const ptrdiff_t i_offset = get_offset(block_len, str_replace_with);
shift_tags(
str_text,
r_block_map,
i_offset,
shift_tag_until.Stop()
);
str_text.replace(
itr_block->Get_Open_Tag().Start(),
block_len,
str_replace_with
);
} else { // process the whole IF
if (!b_eval_as_true) {
str_replace_with.clear();
}
const ptrdiff_t i_offset = get_offset(block_len, str_replace_with);
// shift tags in rest of document
shift_tags(
str_text,
r_block_map,
i_offset,
itr_block->Get_Close_Tag().Start()
);
str_text.replace(
itr_block->Get_Open_Tag().Start(),
block_len,
str_replace_with
);
}
itr_block->Delete();
}
}
//----------------------------------------------------------------------------
void html_template::process_simple_vars(block_map_t & r_block_map,
std::string & str_text,
html_template::variables_t & r_variables_c) {
block_map_t::iterator itr_block = r_block_map.begin();
for (; itr_block != r_block_map.end(); ++itr_block) {
if ( itr_block->Get_Type() != tag_types_c["SIMPLE"] ) continue;
if ( itr_block->Deleted() ) continue;
#ifdef DEBUG2
cout << "processing variable " << itr_block->Get_Name() << endl;
#endif
string str_replace_with = "";
// find the variable of this name
variables_t::iterator pos_var;
pos_var = r_variables_c.find(itr_block->Get_Name());
cls_variable* p_var = 0;
pos_var = r_variables_c.find(itr_block->Get_Name());
if ( pos_var != r_variables_c.end() ) {
p_var = &(pos_var->second);
str_replace_with = p_var->Get_Val_String();
escape_var( str_replace_with, itr_block->Get_Escape_Mode() );
}
const size_t block_len = itr_block->Get_Open_Tag().Stop()
- itr_block->Get_Open_Tag().Start() + 1;
const ptrdiff_t i_offset = get_offset(block_len, str_replace_with);
shift_tags(
str_text,
r_block_map,
i_offset,
itr_block->Get_Open_Tag().Stop()
);
str_text.replace(
itr_block->Get_Open_Tag().Start(),
block_len,
str_replace_with
);
itr_block->Delete();
}
}
//----------------------------------------------------------------------------
void html_template::process_loops(block_map_t & r_block_map,
std::string & str_text,
variables_t & r_variables_c) {
block_map_t::iterator itr_block = r_block_map.begin();
for (; itr_block != r_block_map.end(); ++itr_block) {
if ( itr_block->Get_Type() != tag_types_c["LOOP"] ) continue;
if ( itr_block->Deleted() ) continue;
process_loop( *itr_block, str_text, r_block_map, r_variables_c );
itr_block->Delete();
}
}
//----------------------------------------------------------------------------
void html_template::process_loop(const block_s & block,
std::string & str_text,
block_map_t & r_block_map,
variables_t & r_variables_c) {
#ifdef DEBUG
cout << "processing loop: "<< block.Get_Name() << endl;
#endif
// Get the table variable for this block.
variables_t::iterator itr_var = r_variables_c.find( block.Get_Name() );
loop_s local_table;
itr_var = r_variables_c.find(block.Get_Name());
if ( itr_var != r_variables_c.end() ) {
cls_variable & r_var = itr_var->second;
local_table = r_var.Get_Val_Table();
}
const size_t block_start = block.Get_Open_Tag().Start();
const size_t block_end = block.Get_Close_Tag().Stop();
const size_t block_len = block_end - block_start + 1;
const size_t content_start = block.Get_Open_Tag().Stop() + 1;
const size_t content_end = block.Get_Close_Tag().Start() - 1;
const size_t content_len = content_end - content_start + 1;
// get loop text content
string str_row_content = str_text.substr(content_start, content_len);
//
// go through all variables and expand the ones within this loop.
//
// create a map of variables that we will re-use for each row
block_map_t loop_block_map;
block_map_t::iterator itr_block =
find(r_block_map.begin(), r_block_map.end(), block);
// move to next block inside this one
advance(itr_block, 1);
for (; itr_block != r_block_map.end(); ++itr_block) {
// skip processed
if ( itr_block->Deleted() ) continue;
// get out if we are past the end of this loop
if (*itr_block > block) break;
block_s loop_block = *itr_block;
// change offset relative to beginning of content
const ptrdiff_t & i_offset = content_start * -1;
loop_block.Shift(i_offset, block_start, block_end);
loop_block_map.push_back(loop_block);
#ifdef DEBUG2
cout << "adding " << loop_block.Get_Name() << " to loop map" << endl;
cout << "block start: " << loop_block.Get_Open_Tag().Start() << endl;
cout << "block end: " << loop_block.Get_Open_Tag().Stop() << endl;
#endif
}
// get row iterator
loop_s::rows_t rows = local_table.Get_Rows();
loop_s::rows_t::const_iterator itr_row = rows.begin();
// convenience variables for conext variable math
const unsigned int ui_total_rows = local_table.Get_Rows().size();
unsigned int ui_current_row = 0;
// our final table content
string str_replace_with;
// for each table row
for (; itr_row != rows.end(); ++itr_row) {
++ui_current_row;
// get cell iterator
const row_s::cells_t & row_cells = itr_row->cells_c;
row_s::cells_t::const_iterator itr_cell = row_cells.begin();
// local variables for this scope - copy from scope one level up
variables_t lcl_variables_c = r_variables_c;
// add variables specific to this scope, overriding any globals
for (; itr_cell != row_cells.end(); ++itr_cell) {
const string str_name = itr_cell->first;
const string str_val = itr_cell->second.str_val;
loop_s *p_nested_loop = 0;
if ( itr_cell->second.p_table ) {
p_nested_loop = itr_cell->second.p_table;
}
// delete this key of a global variable of the same name trespasses
// private scope
if ( lcl_variables_c.find(str_name) != lcl_variables_c.end() ) {
lcl_variables_c.erase(str_name);
}
if ( str_val.empty() && p_nested_loop != 0 ) {
cls_variable var(str_name);
var = *p_nested_loop;
lcl_variables_c[str_name] = var;
} else {
lcl_variables_c[str_name] = str_val;
}
}
//