This blog post is about to show a new way of blogging about C++.

Look at a typical blog post about C++: The post usually presents a couple of code snippets. As I see it, there are two pains with code snippets:

  1. they contain the input and the output but not the actual evaluation of the input
  2. it’s impossible for the reader to modify the output

The forgotten dream

A long time ago, all the developers had a common dream. The dream was about interactivity, liveness, evaluation…

But we put this dream aside - because the browser understands only javascript.

And after a while, we even forgot that we ever had this dream.

Still, there are some people that didn’t forget this dream, like Alan Kay:

Question: Well, look at Wikipedia — it’s a tremendous collaboration.

Alan Kay: It is, but go to the article on Logo, can you write and execute Logo programs? Are there examples? No. The Wikipedia people didn’t even imagine that, in spite of the fact that they’re on a computer.

Here is the full interview of Alan Kay. (Thanks @fasihsignal for bringing this quote to our awareness.)

dream

The klipse plugin

The klipse plugin is a small step toward Alan Kay’s vision: it is a javascript tag that transforms static C++ code snippets of an html page to live and interactive snippets:

  1. Live: The code is executed in your browser
  2. Interactive: You can modify the code and it is evaluated as you type

Klipse is written in clojurescript, and uses CodeMirror for text editing and JSCPP for the code evaluation.

Klipsify a C++ code snippet

Let’s have on this page a static code snippet with a Hello World program in C++:

#include <iostream>
using namespace std;
int main() {
  cout << "Hello World!" << endl;
  return 0;
}

(This blog is written with jekyll: the kramdown plugin helps a lot in beautifying the code snippets.)

And now, we are going to klipsify this code snippet:

#include <iostream>
using namespace std;
int main() {
  cout << "Hello World!" << endl;
  return 0;
}

Feel free to edit the code above: it’s interactive => it evaluates as you type.

All I had to do in order to klipsify my code snippet, was to set the language-klipse-cpp class (configurable) to the appropriate html element.

See it by yourself: here is the source of this page:

<p>And now, we are going to <strong>klipsify</strong> this code snippet:</p>

<pre><code class="language-klipse-cpp">#include &lt;iostream&gt;
using namespace std;
int main() {
  cout &lt;&lt; "Hello World!" &lt;&lt; endl;
  return 0;
}
</code></pre>

Live demo

Before dealing about integration of the klipse plugin on a web page, let’s enjoy another klipse snippet with a C++ program that prints the system time in seconds:

#include <ctime>
#include <iostream>
using namespace std;

int main()
{
  const double sysTime = time(0);
  cout << "System Time in seconds is: " << sysTime << "." << endl;
  return 0;
}

Go ahead! modify the klipse snippet above, and it will evaluate as you type…

Evaluating a gist

We can also evaluate code from a gist.

For instance, let’s klipsify this gist that prints the Fibonacci sequence using Matrix Exponentiation.

Again, enjoy the interactivity and modify the code…

Evaluating a snippet in a loop

We can evaluate a code snippet in a loop by setting the data-loop-msec attribute of the DOM element containing the code snippet.

As an example, let’s evaluate in a loop our previous code snippet that prints the system time with data-loop-msec=1000:


#include <ctime>
#include <iostream>
using namespace std;

int main()
{
  const double sysTime = time(0);
  cout <<  sysTime;
  return 0;
}

As you can see, the time is printed in a loop every 1000 msec.

Klipse plugin integration

All you need to do in order to integrate the klipse plugin to your blog (or any other web page), is to add this javascript tag to your web page:

<link rel="stylesheet" type="text/css" href="https://storage.googleapis.com/app.klipse.tech/css/codemirror.css">

<script>
    window.klipse_settings = {
        selector_eval_cpp: '.language-klipse-cpp', // css selector for the html elements you want to klipsify
    };
</script>
<script src="https://storage.googleapis.com/app.klipse.tech/plugin_prod/js/klipse_plugin.min.js"></script>

By the way, this is exactly what we did on the page that you are currently reading.

Limitations

For the moment, JSCPP provides a partial implementation of C++. But I’m sure that with the increase of the community interest, eventually C++ will be fully implemented.

Other languages

The klipse plugin is designed as a platform that could support any language that has a client-side evaluator, by writing modules to the klipse plugin. Currently, there are modules available for the following languages:

Have a look at the KLIPSE repository on github to learn more about the klipse plugin.

Don’t hesitate to contact me by email at viebel@gmail.com or on twitter at @viebel if you need help to write your first C++ blog with interactive code snippets.