Skip to content

Commit

Permalink
Use a more informative identifier for exec directive outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Apr 30, 2015
1 parent 2f7d0f7 commit dfc056c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cpp/stencila/map-context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MapContext : public Context {
}

std::string execute(const std::string& code, const std::string& id="", const std::string& format="", const std::string& width="", const std::string& height="", const std::string& units=""){
return "";
return id;
}

std::string interact(const std::string& code, const std::string& id=""){
Expand Down
21 changes: 20 additions & 1 deletion cpp/stencila/stencil-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <boost/algorithm/string.hpp>

#include <stencila/stencil.hpp>
#include <stencila/string.hpp>

namespace Stencila {

Expand Down Expand Up @@ -132,6 +133,7 @@ void Stencil::Execute::parse(const std::string& attribute){
for(auto& context : contexts) trim(context);
for(const auto& context : contexts){
if(not(
context=="map" or
context=="exec" or
context=="cila" or
context=="py" or
Expand Down Expand Up @@ -245,8 +247,25 @@ void Stencil::Execute::render(Stencil& stencil, Node node, Context* context){
units.value = "cm";
}

// Generate a unique id for this execute directive which, if possible,
// includes useful text as well as the unique-ifying hash
std::string id;
// Does the parent of this element have an id?
id += node.parent().attr("id");
// Does the parent of this element have a caption
Node caption = node.parent().select("caption,figcaption");
if(caption){
std::string slug = slugify(caption.text(),25);
if(id.length()) id += "-";
id += slug;
}
if(id.length()) id += "-";
id += stencil.hash_;

// Execute code
std::string result = context->execute(code,stencil.hash_,
std::string result = context->execute(
code,
id,
format.value,
width.value,
height.value,
Expand Down
14 changes: 14 additions & 0 deletions cpp/tests/stencil-render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ BOOST_AUTO_TEST_CASE(exec){
render(R"(<pre data-exec="map">a = 42</pre>Text after)");
}

BOOST_AUTO_TEST_CASE(exec_output){
render(R"(
<figure id="figure-a">
<pre data-exec="map format png">do</pre>
</figure>
<figure id="figure-b">
<pre data-exec="map format png">do</pre>
<figcaption>Hello world</figcaption>
</figure>
)");
dump();
}

BOOST_AUTO_TEST_CASE(when){
render(R"(
<div data-when="map">
Expand Down

0 comments on commit dfc056c

Please sign in to comment.