Skip to content

Commit

Permalink
support for audio and video
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Jan 23, 2013
1 parent 92c8449 commit cc293cb
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 17 deletions.
16 changes: 11 additions & 5 deletions src/cpp/session/modules/learning/SlideParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ struct CompareName

bool isCommandField(const std::string& name)
{
return !boost::iequals(name, "title");
return boost::iequals(name, "help-doc") ||
boost::iequals(name, "help-source") ||
boost::iequals(name, "source");
}

std::string normalizeFieldValue(const std::string& value)
Expand All @@ -60,10 +62,13 @@ std::string normalizeFieldValue(const std::string& value)

} // anonymous namespace


// default title to true unless this is a video slide
bool Slide::showTitle() const
{
return !boost::iequals(fieldValue("title"), "false");
if (video().empty())
return !boost::iequals(fieldValue("title", "true"), "false");
else
return boost::iequals(fieldValue("title", "false"), "true");
}

std::string Slide::commandsJsArray() const
Expand Down Expand Up @@ -100,14 +105,15 @@ std::vector<std::string> Slide::fields() const
return fields;
}

std::string Slide::fieldValue(const std::string& name) const
std::string Slide::fieldValue(const std::string& name,
const std::string& defaultValue) const
{
std::vector<Field>::const_iterator it =
std::find_if(fields_.begin(), fields_.end(), CompareName(name));
if (it != fields_.end())
return normalizeFieldValue(it->second);
else
return std::string();
return defaultValue;
}


Expand Down
10 changes: 7 additions & 3 deletions src/cpp/session/modules/learning/SlideParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ class Slide
{
}

const std::string& title() const { return title_; }

std::string title() const { return title_; }
bool showTitle() const;

std::string video() const { return fieldValue("video"); }
std::string audio() const { return fieldValue("audio"); }


std::string commandsJsArray() const;

std::vector<std::string> fields() const;
std::string fieldValue(const std::string& name) const;
std::string fieldValue(const std::string& name,
const std::string& defaultValue="") const;
std::vector<std::string> fieldValues(const std::string& name) const;

const std::string& content() const { return content_; }
Expand Down
69 changes: 61 additions & 8 deletions src/cpp/session/modules/learning/SlideRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <sstream>

#include <boost/foreach.hpp>
#include <boost/format.hpp>

#include <core/Error.hpp>
#include <core/FilePath.hpp>
Expand All @@ -37,20 +38,54 @@ namespace learning {
namespace {


void renderMedia(const std::string& type,
const std::string& format,
int slideNumber,
const std::string& fileName,
std::ostream& os,
std::vector<std::string>* pJsActions)
{
boost::format fmt("slide%1%%2%");
std::string mediaId = boost::str(fmt % slideNumber % type);
fmt = boost::format(
"<script type=\"text/javascript\">\n"
" function %2%Updated() {\n"
" \n"
" }\n"
" function %2%Ended() {\n"
" %2%.load();\n"
" }\n"
"</script>\n"

"<%1% id=\"%2%\" controls\n"
" ontimeupdate=\"%2%Updated();\"\n"
" onended=\"%2%Ended();\">\n"
" <source src=\"%3%\" type=\"%1%/%4%\">\n"
" Your browser does not support the %1% tag.\n"
"</%1%>\n");

os << boost::str(fmt % type % mediaId % fileName % format) << std::endl;

// add video autoplay action
fmt = boost::format("if (%1%.ended) %1%.load(); %1%.play();");
pJsActions->push_back(boost::str(fmt % mediaId));
}


} // anonymous namespace


Error renderSlides(const SlideDeck& slideDeck,
std::string* pSlides,
std::string* pSlideCommands,
std::string* pSlideActions,
std::string* pUserErrorMsg)
{
// setup markdown options
markdown::Extensions extensions;
markdown::HTMLOptions htmlOptions;

// render the slides to HTML and slide commands to case statements
std::ostringstream ostr, ostrCmds;
std::ostringstream ostr, ostrActions;
std::string cmdPad(8, ' ');
int slideNumber = 0;
for (std::vector<Slide>::const_iterator it = slideDeck.begin();
Expand All @@ -72,22 +107,40 @@ Error renderSlides(const SlideDeck& slideDeck,
return error;
}

// render content
ostr << htmlContent << std::endl;

ostr << "</section>" << std::endl;
// setup a vector of js actions to take when the slide loads
// (we always take the action of adding any embedded commands)
std::vector<std::string> jsActions;
jsActions.push_back("cmds = " + it->commandsJsArray());

// commands
ostrCmds << cmdPad << "case " << slideNumber << ":" << std::endl
<< cmdPad << " cmds = " << it->commandsJsArray() << ";"
<< std::endl << cmdPad << " break;" << std::endl;
// render video if specified
std::string video = it->video();
if (!video.empty())
renderMedia("video", "mp4", slideNumber, video, ostr, &jsActions);

// render audio if specified
std::string audio = it->audio();
if (!audio.empty())
renderMedia("audio", "mpeg", slideNumber, audio, ostr, &jsActions);

ostr << "</section>" << std::endl;

// javascript actions to take on slide load
ostrActions << cmdPad << "case " << slideNumber << ":" << std::endl;
BOOST_FOREACH(const std::string& jsAction, jsActions)
{
ostrActions << cmdPad << " " << jsAction << ";" << std::endl;
}
ostrActions << std::endl << cmdPad << " break;" << std::endl;

// increment slide number
slideNumber++;
}

*pSlides = ostr.str();
*pSlideCommands = ostrCmds.str();
*pSlideActions = ostrActions.str();
return Success();


Expand Down
2 changes: 1 addition & 1 deletion src/cpp/session/modules/learning/SlideRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SlideDeck;

core::Error renderSlides(const SlideDeck& slideDeck,
std::string* pSlides,
std::string* pSlideCommands,
std::string* pSlideActions,
std::string* pUserErrorMsg);


Expand Down
7 changes: 7 additions & 0 deletions src/cpp/session/resources/learning/slides.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@
.reveal .slides>section>section {
padding: 0px 0px;
}

.reveal video {
height: 75%;
width: 100%;
}


12 changes: 12 additions & 0 deletions src/cpp/session/resources/learning/slides.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,19 @@

<script>

function pausePlayers(players) {
for(var i = 0; i < players.length; i++) {
players[i].pause();
}
}

function notifySlideChanged(indexh) {

// pause all audio and video
pausePlayers(document.getElementsByTagName('video'));
pausePlayers(document.getElementsByTagName('audio'))

// execute slide-specific commants
var cmds = [];
switch(event.indexh) {
#!slide_commands#
Expand Down

0 comments on commit cc293cb

Please sign in to comment.