-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsquarespace-template.js
More file actions
1542 lines (1261 loc) · 44 KB
/
squarespace-template.js
File metadata and controls
1542 lines (1261 loc) · 44 KB
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
/*!
*
* Squarespace template.
*
*/
var path = require( "path" ),
fs = require( "fs" ),
less = require( "less" ),
mustache = require( "mustache" ),
rIndexFolder = /^folder|^index/,
rMetaLeft = /\{\.meta-left\}/g,
rMetaRight = /\{\.meta-right\}/g,
rSlash = /^\/|\/$/g,
rJsonT = /^\{.*?\}$/,
rScripts = /<script.*?\>(.*?)<\/script\>/g,
rAt = /@/,
rBlockIncs = /\{[@a-zA-Z0-9.-]+?\|apply\s(.*?)\}/g,
rBlockRepl = /^\{(.*?)\|apply\s|\}$/g,
rBlockCtx = /^\{|\|apply\s(.*)|\}$/g,
rTplFiles = /\.item$|\.list$|\.region$/,
rItemOrList = /\.item$|\.list$/,
rRegions = /\.region$/,
rItem = /\.item$/,
rList = /\.list$/,
rLess = /\.less$/,
rCss = /\.css$/,
rJs = /\.js/,
rBlock = /\.block$/,
rPage = /\.page$/,
rJson = /\{@\|json.*?\}/,
rBodyTag = /<body.*?\>/,
rSQSQuery = /(<squarespace:query.*?\>)(.*?)(<\/squarespace:query\>)/,
rSQSQueryG = /(<squarespace:query.*?\>)(.*?)(<\/squarespace:query\>)/g,
rSQSNavis = /<squarespace:navigation(.*?)\/\>/g,
rSQSFolderNavis = /<squarespace:folder-navigation(.*?)\/\>/g,
rSQSBlockFields = /<squarespace:block-field(.*?)\/\>/g,
rSQSScripts = /<squarespace:script(.*?)\/\>/g,
rSQSFootersFull = /<script type="text\/javascript" data-sqs-type="imageloader-bootstraper"\>(.*?)(Squarespace\.afterBodyLoad\(Y\);)<\/script\>/,
rSQSHeadersFull = /<\!-- This is Squarespace\. -->(.*?)<\!-- End of Squarespace Headers -->/,
SQS_HEADERS = "{squarespace-headers}",
SQS_FOOTERS = "{squarespace-footers}",
SQS_MAIN_CONTENT = "{squarespace.main-content}",
SQS_PAGE_CLASSES = "{squarespace.page-classes}",
SQS_POST_ENTRY = "{squarespace-post-entry}",
SQS_PAGE_ID = /\{squarespace\.page-id\}|squarespace\.page-id/g,
SQS_TEMP_REV = /\{squarespace\.template-revision\}/g,
SQS_PAGE_TITLE = /\{squarespace\.page-title\}/g,
sqsHeaders = [],
sqsFooters = [],
sqsUser = null,
directories = {},
config = {},
scripts = [],
siteCss = "",
templates = {
regions: {},
collections: {},
blocks: {},
pages: {}
},
layoutHTML = "",
updating = {},
nsl = null,
sqsWare = null,
sqsUtil = require( "./squarespace-util" ),
sqsBlocktypes = require( "./squarespace-blocktypes" ),
sqsCollectiontypes = require( "./squarespace-collectiontypes" ),
sqsJsonTemplate = require( "node-squarespace-jsont" ),
sqsCache = require( "./squarespace-cache" ),
/******************************************************************************
* @Public
*******************************************************************************/
/**
*
* @method preload
* @public
*
*/
preload = function () {
sqsUtil.readFile( path.join( __dirname, "tpl/layout.html" ), function ( data ) {
layoutHTML = sqsUtil.packStr( data );
});
},
/**
*
* @method compile
* @param {function} cb The callback when loaded
* @public
*
*/
compile = function ( cb ) {
var compiled = 0,
methods = [
compileBlocks,
compileRegions,
compileCollections,
compileStylesheets
];
// Maybe a template won't have static pages ?
if ( fs.existsSync( directories.pages ) ) {
methods.push( compilePages );
}
function done() {
compiled++;
if ( compiled === methods.length ) {
replaceAll();
cb();
}
}
for ( var i = 0; i < methods.length; i++ ) {
methods[ i ].call(
null,
done
);
}
},
/**
*
* @method watch
* @public
*
*/
watch = function ( cb ) {
function doneWatch( filename ) {
nsl.log( "template", "Reloaded local template" );
replaceAll();
cb();
setTimeout( function () {
delete updating[ filename ];
}, 500 );
}
function onWatch( event, filename ) {
if ( !updating[ filename ] ) {
if ( rItemOrList.test( filename ) || rPage.test( filename ) || rRegions.test( filename ) || rBlock.test( filename ) || rLess.test( filename ) || rCss.test( filename ) || rJs.test( filename ) ) {
updating[ filename ] = true;
nsl.log( "template", ("Updated template file " + filename) );
compile(function () {
doneWatch( filename );
});
}
}
}
fs.watch( process.cwd(), onWatch );
fs.watch( directories.blocks, onWatch );
fs.watch( directories.collections, onWatch );
fs.watch( directories.styles, onWatch );
fs.watch( directories.scripts, onWatch );
// Maybe a template won't have static pages ?
if ( fs.existsSync( directories.pages ) ) {
fs.watch( directories.pages, onWatch );
}
},
/**
*
* @method replaceAll
* @public
*
*/
replaceAll = function () {
replaceBlocks();
replaceScripts();
replaceSQSScripts();
},
/**
*
* @method watch
* @public
*
*/
refresh = function () {
//scripts = [];
sqsHeaders = [];
sqsFooters = [];
},
/**
*
* @method setLogger
* @param {object} logger The log module
* @public
*
*/
setLogger = function ( logger ) {
nsl = logger;
},
/**
*
* @method setMiddleware
* @param {object} middleware The middleware instance
* @public
*
*/
setMiddleware = function ( middleware ) {
sqsWare = middleware;
},
/**
*
* @method setConfig
* @param {object} conf The server configuration
* @public
*
*/
setConfig = function ( key, conf ) {
config[ key ] = conf;
},
/**
*
* @method setDirs
* @param {object} conf The directories
* @public
*
*/
setDirs = function ( dirs ) {
directories = dirs;
},
/**
*
* @method setUser
* @param {object} conf The directories
* @public
*
*/
setUser = function ( user ) {
sqsUser = user;
},
/**
*
* @method getSiteCss
* @returns {string} compiled css
* @public
*
*/
getSiteCss = function () {
return siteCss;
},
/**
*
* @method replaceBlocks
* @public
*
*/
replaceBlocks = function () {
var filesByType = [
"regions",
"collections",
"pages",
"blocks"
],
replace,
matched,
block,
i,
j;
filesByType.forEach(function ( fileOfType ) {
for ( i in templates[ fileOfType ] ) {
while ( matched = templates[ fileOfType ][ i ].match( rBlockIncs ) ) {
j = matched.length;
for ( j; j--; ) {
block = matched[ j ].replace( rBlockRepl, "" );
block = templates.blocks[ block ];
// Simple replace for @|apply is easy
// Otherwise we need a context wrapper
// foo.bar|apply => {.section foo.bar}...{.end}
if ( !rAt.test( matched[ j ] ) ) {
block = ("{.section " + matched[ j ].replace( rBlockCtx, "" ) + "}" + block + "{.end}");
}
templates[ fileOfType ][ i ] = templates[ fileOfType ][ i ].replace( matched[ j ], block );
}
}
}
});
},
/**
*
* @method replaceScripts
* @public
*
*/
replaceScripts = function () {
var matched,
token;
for ( var i in templates.regions ) {
matched = templates.regions[ i ].match( rScripts );
if ( matched ) {
for ( var j = 0, len = matched.length; j < len; j++ ) {
if ( !rJson.test( matched[ j ] ) ) {
token = sqsUtil.getToken();
scripts.push({
token: token,
script: matched[ j ]
});
templates.regions[ i ] = templates.regions[ i ].replace( matched[ j ], token );
}
}
}
}
for ( var i in templates.collections ) {
matched = templates.collections[ i ].match( rScripts );
if ( matched ) {
for ( var j = 0, len = matched.length; j < len; j++ ) {
if ( !rJson.test( matched[ j ] ) ) {
token = sqsUtil.getToken();
scripts.push({
token: token,
script: matched[ j ]
});
templates.collections[ i ] = templates.collections[ i ].replace( matched[ j ], token );
}
}
}
}
for ( var i in templates.pages ) {
matched = templates.pages[ i ].match( rScripts );
if ( matched ) {
for ( var j = 0, len = matched.length; j < len; j++ ) {
if ( !rJson.test( matched[ j ] ) ) {
token = sqsUtil.getToken();
scripts.push({
token: token,
script: matched[ j ]
});
templates.pages[ i ] = templates.pages[ i ].replace( matched[ j ], token );
}
}
}
}
},
/**
*
* @method replaceSQSScripts
* @public
*
*/
replaceSQSScripts = function () {
var matched,
attrs,
block,
filed;
for ( var i in templates.regions ) {
matched = templates.regions[ i ].match( rSQSScripts );
if ( matched ) {
for ( var j = 0, len = matched.length; j < len; j++ ) {
attrs = sqsUtil.getAttrObj( matched[ j ] );
block = ( "/scripts/" + attrs.src );
filed = '<script src="' + block + '"></script>';
templates.regions[ i ] = templates.regions[ i ].replace( matched[ j ], filed );
}
}
}
for ( var i in templates.collections ) {
matched = templates.collections[ i ].match( rSQSScripts );
if ( matched ) {
for ( var j = 0, len = matched.length; j < len; j++ ) {
attrs = sqsUtil.getAttrObj( matched[ j ] );
block = ( "/scripts/" + attrs.src );
filed = '<script src="' + block + '"></script>';
templates.collections[ i ] = templates.collections[ i ].replace( matched[ j ], filed );
}
}
}
for ( var i in templates.pages ) {
matched = templates.pages[ i ].match( rSQSScripts );
if ( matched ) {
for ( var j = 0, len = matched.length; j < len; j++ ) {
attrs = sqsUtil.getAttrObj( matched[ j ] );
block = ( "/scripts/" + attrs.src );
filed = '<script src="' + block + '"></script>';
templates.pages[ i ] = templates.pages[ i ].replace( matched[ j ], filed );
}
}
}
},
/**
*
* @method renderTemplate
* @param {object} qrs Querystring mapping
* @param {object} pageJson JSON data for page
* @param {string} pageHtml HTML for page
* @param {function} callback Fired when done
* @public
*
*/
renderTemplate = function ( qrs, pageJson, pageHtml, callback ) {
var queries = {
raw: [],
processed: []
},
processedQueries = 0,
matched = null,
templateKey = getTemplateKey( pageJson ),
rendered = "",
regionKey,
len,
i;
// Unique page JSON property for sandbox dev mode
pageJson.nodeServer = true;
// Remove authenticatedAccount JSON key from rendered template during CLI auth arugment.
if ( !config.server.auth ) {
delete pageJson.authenticatedAccount;
}
// Create {squarespace-headers} / {squarespace-footers}
setHeaderFooterTokens( pageJson, pageHtml );
// 0.1 => Template is a list or item for a collection, NOT a region
// Injection point for {squarespace.main-content} AND add the site layout
if ( rItemOrList.test( templateKey ) ) {
regionKey = ((pageJson.collection.regionName || "default") + ".region");
// Possibly the regionName isn't really what we want ?
// It would seem that when using galleries, the regionKey is not what we're looking for.
// In this case, just assume the collection template and we don't have to replace mainContent.
if ( !templates.regions[ regionKey ] ) {
rendered += templates.collections[ templateKey ];
} else {
// This wraps the matched collection template with the default or set region
rendered += templates.regions[ regionKey ].replace( SQS_MAIN_CONTENT, templates.collections[ templateKey ] );
}
// 0.2 => Template is a static page
} else if ( rPage.test( templateKey ) ) {
regionKey = ((pageJson.collection.regionName || "default") + ".region");
// In case there was a regionName but no `layout` to match in `template.conf`
regionKey = templates.regions[ regionKey ] ? regionKey : "default.region";
// This wraps the matched page template with the default or set region
rendered += templates.regions[ regionKey ].replace( SQS_MAIN_CONTENT, templates.pages[ templateKey ] );
// 0.3 => Template is a region
} else {
rendered += templates.regions[ templateKey ];
}
// Pre-process Queries
matched = rendered.match( rSQSQueryG );
if ( matched ) {
for ( i = 0, len = matched.length; i < len; i++ ) {
var match = matched[ i ].match( rSQSQuery ),
token = sqsUtil.getToken();
rendered = rendered.replace( match[ 2 ], token );
queries.raw.push({
token: token,
template: match[ 2 ]
});
}
}
// Render squarespace tags like {squarespace.page-id} etc...
rendered = replaceSQSTags( rendered, pageJson, pageHtml );
// Render {squarespace-headers} to the best of our ability
rendered = rendered.replace( SQS_HEADERS, sqsHeaders.join( "" ) );
// Render {squarespace-footers} to the best of our ability
rendered = rendered.replace( SQS_FOOTERS, sqsFooters.join( "" ) );
// Render Navigations from pageJson
rendered = replaceNavigations( rendered, pageJson );
// Render Folder Navigations from pageJson
rendered = replaceFolderNavigations( rendered, pageJson );
// Render page in full w/jsontemplate
rendered = sqsJsonTemplate.render( rendered, pageJson );
// Post-process Queries
matched = rendered.match( rSQSQueryG );
if ( matched ) {
for ( i = 0, len = matched.length; i < len; i++ ) {
var match = matched[ i ].match( rSQSQuery ),
token = match[ 2 ];
for ( var j = queries.raw.length; j--; ) {
if ( queries.raw[ j ].token === token ) {
queries.processed.push({
template: queries.raw[ j ].template,
queryData: sqsUtil.getAttrObj( match[ 1 ] ),
queryProcessed: match[ 0 ]
});
}
}
}
}
function handleDone() {
// Add token scripts back into the template
for ( i = scripts.length; i--; ) {
// Allow {.meta-left} and {.meta-right}
scripts[ i ].script = scripts[ i ].script.replace( rMetaLeft, "{" ).replace( rMetaRight, "}" );
rendered = rendered.replace( scripts[ i ].token, scripts[ i ].script );
}
// Render squarespace tags like {squarespace.page-id} etc...
rendered = replaceSQSTags( rendered, pageJson, pageHtml );
// Render Block Fields
replaceBlockFields( rendered, qrs, function ( finalRender ) {
callback( finalRender );
});
}
function handleProcessed() {
processedQueries++;
if ( queries.processed[ processedQueries ] ) {
handleQueried( queries.processed[ processedQueries ] );
} else {
handleDone();
}
}
function handleQueried( query ) {
var skips = ["format", "password", "nocache", "nodata", "tag", "category"],
cache = null,
key = "",
tpl = null;
key += ("query-" + query.queryData.collection);
for ( i in qrs ) {
if ( skips.indexOf( i ) === -1 ) {
key += ("-" + i + "--" + qrs[ i ]);
}
}
// Tag?
if ( query.queryData.tag ) {
key += "-tag--" + query.queryData.tag;
}
// Category?
if ( query.queryData.category ) {
key += "-category--" + query.queryData.category;
}
key = (key + ".json");
cache = sqsCache.get( key );
// Cached?
if ( cache && qrs.nocache === undefined ) {
cache.nodeServer = true;
tpl = sqsJsonTemplate.render( query.template, cache );
rendered = rendered.replace( query.queryProcessed, tpl );
handleProcessed();
} else {
// Nocache? is the only field that should be passed along in the query string
sqsWare.getQuery( query.queryData, (qrs.nocache ? {nocache: true} : null), function ( error, json ) {
if ( !error ) {
sqsCache.set( key, json );
json.nodeServer = true;
tpl = sqsJsonTemplate.render( query.template, json );
rendered = rendered.replace( query.queryProcessed, tpl );
handleProcessed();
} else {
// Handle errors
nsl.log( "error", ("Squarespace:query request error => " + error) );
}
});
}
}
if ( queries.processed.length ) {
handleQueried( queries.processed[ processedQueries ] );
} else {
handleDone();
}
},
/******************************************************************************
* @Private
*******************************************************************************/
/**
*
* @method compileRegions
* @param {function} cb The callback when done
* @private
*
*/
compileRegions = function ( cb ) {
var regions = [];
for ( var i in config.template.layouts ) {
regions.push({
files: sqsUtil.copyArr( config.template.layouts[ i ].regions ),
link: (i + ".region")
});
}
function read() {
if ( !regions.length ) {
cb();
} else {
var region = regions.pop(),
cont = "";
function _read() {
var file = path.join( config.server.webroot, (region.files.shift() + ".region") );
sqsUtil.readFile( file, function ( data ) {
cont += sqsUtil.packStr( data );
if ( !region.files.length ) {
templates.regions[ region.link ] = cont;
read();
} else {
_read();
}
});
}
_read();
}
}
read();
},
/**
*
* @method compileBlocks
* @param {function} cb The callback when done
* @private
*
*/
compileBlocks = function ( cb ) {
sqsUtil.readDir( directories.blocks, function ( files ) {
function read() {
if ( !files.length ) {
cb();
} else {
var block = files.pop(),
file = path.join( directories.blocks, block );
if ( !rBlock.test( block ) ) {
read();
return;
}
sqsUtil.readFile( file, function ( data ) {
templates.blocks[ block ] = sqsUtil.packStr( data );
read();
});
}
}
read();
});
},
/**
*
* @method compileCollections
* @param {function} cb The callback when done
* @private
*
*/
compileCollections = function ( cb ) {
sqsUtil.readDir( directories.collections, function ( files ) {
function read() {
if ( !files.length ) {
cb();
} else {
var collection = files.pop(),
file = path.join( directories.collections, collection );
if ( !rItemOrList.test( collection ) ) {
read();
return;
}
sqsUtil.readFile( file, function ( data ) {
templates.collections[ collection ] = sqsUtil.packStr( data );
read();
});
}
}
read();
});
},
/**
*
* @method compilePages
* @param {function} cb The callback when done
* @private
*
*/
compilePages = function ( cb ) {
sqsUtil.readDir( directories.pages, function ( files ) {
function read() {
if ( !files.length ) {
cb();
} else {
var page = files.pop(),
file = path.join( directories.pages, page );
if ( !rPage.test( page ) ) {
read();
return;
}
sqsUtil.readFile( file, function ( data ) {
templates.pages[ page ] = sqsUtil.packStr( data );
read();
});
}
}
read();
});
},
/**
*
* @method compileStylesheets
* @param {function} cb The callback when done
* @private
*
*/
compileStylesheets = function ( cb ) {
siteCss = "";
var styles = [{
name: "reset.css",
path: path.join( directories.styles, "reset.css" )
}];
for ( var i = 0, len = config.template.stylesheets.length; i < len; i++ ) {
styles.push({
name: config.template.stylesheets[ i ],
path: path.join( directories.styles, config.template.stylesheets[ i ] )
});
}
function read() {
if ( !styles.length ) {
cb();
} else {
var style = styles.shift();
sqsUtil.isFile( style.path, function ( exists ) {
if ( !exists ) {
read();
} else {
sqsUtil.readFile( style.path, function ( data ) {
// Process through less compiler no matter what
// This will account for Sass -> Less interpolation issues
// Like this note, https://github.com/kitajchuk/templar#sass-vs-less
less.render( data, function ( error, css ) {
if ( error === null ) {
siteCss += css;
} else {
nsl.log( "warn", ("Issue compiling less file `" + style.name + "` => " + error.message) );
}
read();
});
});
}
});
}
}
read();
},
/**
*
* @method replaceSQSTags
* @param {string} rendered The template rendering
* @param {object} pageJson JSON data for page
* @returns {string}
* @private
*
*/
replaceSQSTags = function ( rendered, pageJson, pageHtml ) {
var pageType = pageJson.item ? "item" : "collection",
pageId = pageJson.item ? pageJson.item.id : pageJson.collection.id,
bodyElem = pageHtml.match( rBodyTag ),
bodyAttr = sqsUtil.getAttrObj( bodyElem[ 0 ] );
rendered = rendered.replace( SQS_MAIN_CONTENT, (pageJson.mainContent || "") );
rendered = rendered.replace( SQS_POST_ENTRY, "" );
rendered = rendered.replace( SQS_PAGE_CLASSES, bodyAttr.class );
rendered = rendered.replace( SQS_PAGE_ID, (pageType + "-" + pageId) );
rendered = rendered.replace( SQS_TEMP_REV, Date.now() );
if ( pageJson.item ) {
rendered = rendered.replace( SQS_PAGE_TITLE, pageJson.websiteSettings.itemTitleFormat.replace( "%i", pageJson.item.title ).replace( "%c", pageJson.collection.title ).replace( "%s", pageJson.website.siteTitle ) );
} else {
rendered = rendered.replace( SQS_PAGE_TITLE, (pageJson.collection.homepage ? pageJson.websiteSettings.homepageTitleFormat : pageJson.websiteSettings.collectionTitleFormat).replace( "%c", pageJson.collection.title ).replace( "%s", pageJson.website.siteTitle ) );
}
return rendered;
},
/**
*
* @method getTemplateKey
* @param {object} pageJson JSON data for page
* @private
*
*/
getTemplateKey = function ( pageJson ) {
var template = "",
typeName = null,
regionName = null;
// This could happen, I suppose...
if ( !pageJson ) {
return template;
}
// Grab the collection typeName
typeName = pageJson.collection.typeName;
// Grab the regionName
regionName = (pageJson.collection.regionName || "default");
// Handle collection item
if ( pageJson.item ) {
template = (typeName + ".item");
// Handle collection list
} else if ( pageJson.items ) {
template = (typeName + ".list");
// Handle collection root
} else {
// Handle static page
if ( pageJson.collection.type === sqsCollectiontypes.TEMPLATE_PAGE ) {
template = (typeName + ".page");
// Handle collection without children
// This is a fallback in case the conditions above don't capture the collection...
} else if ( pageJson.collection.type === sqsCollectiontypes.COLLECTION_TYPE_GENERIC ) {
template = (typeName + ".list");
} else {
template = (regionName + ".region");
}
}
return template;
},
/**
*
* @method setHeaderFooterTokens
* @param {object} pageJson JSON data for page
* @param {string} pageHtml HTML for page
* @returns {string}
* @private
*
*/
setHeaderFooterTokens = function ( pageJson, pageHtml ) {
var tokenHeadersFull = sqsUtil.getToken(),
tokenFootersFull = sqsUtil.getToken(),
tokenStyleTag = sqsUtil.getToken(),
sHeadersFull = pageHtml.match( rSQSHeadersFull ),
sFootersFull = pageHtml.match( rSQSFootersFull ),
siteStyleTag = null;
// Headers?
if ( sHeadersFull ) {
sHeadersFull = sHeadersFull[ 0 ];
// Override isWrappedForDamask to ensure public site loads
if ( config.server.sandbox ) {
sHeadersFull = sHeadersFull.replace(
'Squarespace.load(window);',
'Squarespace.isWrappedForDamask=function(){return true;};Squarespace.load(window);'
);
}
sqsHeaders.push( tokenHeadersFull );
scripts.push({
token: tokenHeadersFull,
script: sHeadersFull
});
sqsHeaders.push( tokenStyleTag );
scripts.push({
token: tokenStyleTag,
script: '<link href="/site.css" rel="stylesheet" />'
});
}
// Footers?