Skip to content
This repository has been archived by the owner on Jan 31, 2018. It is now read-only.

Commit

Permalink
[t1492] server-side popcorn string compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
secretrobotron authored and jbuck committed Jun 13, 2012
1 parent 8a971d5 commit 7668ef3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
80 changes: 72 additions & 8 deletions cornfield/app.js
Expand Up @@ -10,7 +10,9 @@ const express = require('express'),
TEMPLATES_DIR = CONFIG.dirs.templates,
PUBLISH_DIR = CONFIG.dirs.publish,
PUBLISH_PREFIX = CONFIG.dirs.publishPrefix,
WWW_ROOT = path.resolve(CONFIG.dirs.wwwRoot || __dirname + "/..");
WWW_ROOT = path.resolve(CONFIG.dirs.wwwRoot || __dirname + "/.."),
VALID_TEMPLATES = CONFIG.templates,
EXPORT_ASSETS = CONFIG.exportAssets;

var canStoreData = true;

Expand Down Expand Up @@ -74,6 +76,7 @@ function publishRoute( req, res ){
}

UserModel.findOne( { email: email }, function( err, doc ) {
var i;
if ( err ) {
res.json({ error: 'internal db error' }, 500);
return;
Expand All @@ -84,20 +87,81 @@ function publishRoute( req, res ){
return;
}

for ( var i=0; i<doc.projects.length; ++i ) {
var project;
for ( i=0; i<doc.projects.length; ++i ) {
if ( String( doc.projects[ i ]._id ) === id ) {
project = doc.projects[ i ];
break;
}
}

if ( project ) {
var template = project.template;
if ( template && VALID_TEMPLATES[ template ] ) {
var projectPath = PUBLISH_DIR + "/" + id + ".html",
url = PUBLISH_PREFIX + "/" + id + ".html",
data = doc.projects[ i ].html;
projectData = JSON.parse( project.data );

fs.readFile( VALID_TEMPLATES[ template ], 'utf8', function(err, data){
var headEndTagIndex,
bodyEndTagIndex,
externalAssetsString = '',
popcornString = '',
currentMedia,
currentTrack,
currentTrackEvent,
mediaPopcornOptions,
j, k;

data = data.replace( /\s*<script[\.\/='":_-\w\s]*data-butter-exclude[\.\/='":_-\w\s]*><\/script>/g, '' );

headEndTagIndex = data.indexOf( '</head>' );
bodyEndTagIndex = data.indexOf( '</body>' );

for ( i = 0; i < EXPORT_ASSETS.length; ++i ) {
externalAssetsString += '\n<script src="' + EXPORT_ASSETS[ i ] + '"></script>';
}

fs.writeFile( projectPath, data, function(){
if( err ){
res.json({ error: 'internal file error' }, 500);
return;
popcornString += '<script>'

for ( i = 0; i < projectData.media.length; ++i ) {
currentMedia = projectData.media[ i ];
mediaPopcornOptions = currentMedia.popcornOptions || {};
popcornString += '\n(function(){';
popcornString += '\nvar popcorn = Popcorn.smart("#' + currentMedia.target + '", "' + currentMedia.url + '", ' + JSON.stringify( mediaPopcornOptions ) + ');';
for ( j = 0; j < currentMedia.tracks.length; ++ j ) {
currentTrack = currentMedia.tracks[ j ];
for ( k = 0; k < currentTrack.trackEvents.length; ++k ) {
currentTrackEvent = currentTrack.trackEvents[ k ];
popcornString += '\npopcorn.' + currentTrackEvent.type + '(';
popcornString += JSON.stringify( currentTrackEvent.popcornOptions, null, 2 );
popcornString += ');';
}
}
popcornString += '}());';
}
res.json({ error: 'okay', url: url });
popcornString += '</script>';

data = data.substring( 0, headEndTagIndex ) + externalAssetsString + data.substring( headEndTagIndex, bodyEndTagIndex ) + popcornString + data.substring( bodyEndTagIndex );

fs.writeFile( projectPath, data, function(){
if( err ){
res.json({ error: 'internal file error' }, 500);
return;
}
res.json({ error: 'okay', url: url });
});

});
}
else {
res.json({ error: 'template not found' }, 500);
return;
}
}
else {
res.json({ error: 'project not found' }, 500);
return;
}
});
}
Expand Down
14 changes: 13 additions & 1 deletion cornfield/config/default.json
Expand Up @@ -15,5 +15,17 @@
"templates": "../templates",
"publish": "view",
"publishPrefix": "http://localhost:8888/cornfield/view"
}
},
"templates": {
"test": "../templates/test.html",
"robots": "../templates/supported/robots/index.html",
"report": "../templates/supported/report/index.html",
"popup": "../templates/supported/popup/index.html",
"musicvideo": "../templates/supported/musicvideo/index.html",
"context": "../templates/supported/context/index.html",
"robots": "../templates/supported/newscaster/index.html"
},
"exportAssets": [
"../../dist/buttered-popcorn.min.js"
]
}
1 change: 1 addition & 0 deletions templates/test-config.json
@@ -1,4 +1,5 @@
{
"name": "test",
"savedDataUrl": "saved-data.json",
"baseDir": "../"
}

0 comments on commit 7668ef3

Please sign in to comment.