add online examples

This commit is contained in:
Niels 2015-06-29 23:02:41 +02:00
parent 540c58964d
commit 5ae2babf61
81 changed files with 239 additions and 1 deletions

View file

@ -2,6 +2,7 @@
[![Build Status](https://travis-ci.org/nlohmann/json.svg?branch=master)](https://travis-ci.org/nlohmann/json) [![Build Status](https://travis-ci.org/nlohmann/json.svg?branch=master)](https://travis-ci.org/nlohmann/json)
[![Coverage Status](https://img.shields.io/coveralls/nlohmann/json.svg)](https://coveralls.io/r/nlohmann/json) [![Coverage Status](https://img.shields.io/coveralls/nlohmann/json.svg)](https://coveralls.io/r/nlohmann/json)
[![Try online](https://img.shields.io/badge/try-online-blue.svg)](http://melpon.org/wandbox/permlink/GnGKwji06WeVonlI)
[![Documentation Status](https://img.shields.io/badge/documentation-doxygen-blue.svg)](http://nlohmann.github.io/json) [![Documentation Status](https://img.shields.io/badge/documentation-doxygen-blue.svg)](http://nlohmann.github.io/json)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nlohmann/json/master/LICENSE.MIT) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nlohmann/json/master/LICENSE.MIT)
[![Github Issues](https://img.shields.io/github/issues/nlohmann/json.svg)](http://github.com/nlohmann/json/issues) [![Github Issues](https://img.shields.io/github/issues/nlohmann/json.svg)](http://github.com/nlohmann/json/issues)

View file

@ -28,7 +28,7 @@ INHERIT_DOCS = YES
SEPARATE_MEMBER_PAGES = YES SEPARATE_MEMBER_PAGES = YES
TAB_SIZE = 4 TAB_SIZE = 4
ALIASES = "complexity=@par Complexity\n" ALIASES = "complexity=@par Complexity\n"
ALIASES += liveexample{2}="@par Example\n \1 \n @includelineno \2.cpp \n Output:\n @verbinclude \2.output \n The example code above can be translated with @verbatim g++ -std=c++11 -Isrc doc/examples/\2.cpp -o \2 @endverbatim." ALIASES += liveexample{2}="@par Example\n \1 \n @includelineno \2.cpp \n Output (play with this example @htmlinclude \2.link):\n @verbinclude \2.output \n The example code above can be translated with @verbatim g++ -std=c++11 -Isrc doc/examples/\2.cpp -o \2 @endverbatim"
ALIASES += requirement="@par Requirements\n" ALIASES += requirement="@par Requirements\n"
TCL_SUBST = TCL_SUBST =
OPTIMIZE_OUTPUT_FOR_C = NO OPTIMIZE_OUTPUT_FOR_C = NO

View file

@ -24,9 +24,20 @@ EXAMPLES = $(wildcard examples/*.cpp)
diff $@ $(<:.cpp=.output) diff $@ $(<:.cpp=.output)
rm $(<:.cpp=) $@ rm $(<:.cpp=) $@
# create links to try the code online
%.link: %.cpp
rm -fr tmp
mkdir tmp
cp $(SRCDIR)/json.hpp tmp
./send_to_wandbox.py tmp $< > $@.tmp
/bin/echo -n "<a target=\"_blank\" href=\"`cat $@.tmp`\"><b>online</b></a>" > $@
rm -fr tmp $@.tmp
# create output from all stand-alone example files # create output from all stand-alone example files
create_output: $(EXAMPLES:.cpp=.output) create_output: $(EXAMPLES:.cpp=.output)
create_links: $(EXAMPLES:.cpp=.link)
# check output of all stand-alone example files # check output of all stand-alone example files
check_output: $(EXAMPLES:.cpp=.test) check_output: $(EXAMPLES:.cpp=.test)

31
doc/examples/README.cpp Normal file
View file

@ -0,0 +1,31 @@
#include <json.hpp>
using namespace nlohmann;
int main()
{
// create a JSON object
json j = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
// add new values
j["new"]["key"]["value"] = {"another", "list"};
// count elements
j["size"] = j.size();
// pretty print with indent of 4 spaces
std::cout << std::setw(4) << j << '\n';
}

1
doc/examples/README.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/GnGKwji06WeVonlI"><b>online</b></a>

1
doc/examples/array.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/NeN8wmHsQGxFXC6L"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/syN4hQrhPvlUy5AG"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/zwOKkXrKstDlMPjr"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/wKBBW3ORmTHPlgJV"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/Ti47m4DY2lV4SWNV"><b>online</b></a>

1
doc/examples/back.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/zB9tSkSVYIthfgj6"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/Hrajl95QUa1Lplou"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/vyrjruntW4ASDBKH"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/cyithYO7sPbXxK16"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/yO6SWuP9bEYvNEvX"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/FCiYtNwovQ84DWxl"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/h8c1hfORMnVrJ1Ky"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/z2P1DpA8uSSlwgTl"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/vwVgsv7w6YZUgctJ"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/wwzBU7cK8Xr2yiKJ"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/RzWnqhbfuvbHXCBJ"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/BPk4vUc4kJPWX0rz"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/P8QWpucrWhov8JRR"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/IWIZSNthuHrT3AS6"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/dvTtz7DIo8TDhYE6"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/hcERmrPzjmXeSzB8"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/47cqX7AduSWn24lA"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/RUSNdk5YEAr2vL4E"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/4IF0ofRq0nZcV8pI"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/dkznQmTMqulurCqD"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/S9jwK28ThNIGkdUI"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/xNtouqvB2OJBOwkv"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/ncOG8fdxNrISUNFj"><b>online</b></a>

1
doc/examples/begin.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/Nwb94PEGlvnjrqj1"><b>online</b></a>

1
doc/examples/cbegin.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/mWrMi5IrJjRILgir"><b>online</b></a>

1
doc/examples/cend.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/0OHrFwFGvRuDKdfx"><b>online</b></a>

1
doc/examples/clear.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/Ddggfbg18WXOIBkW"><b>online</b></a>

1
doc/examples/count.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/mPQHwiNeVa872Aky"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/XA8jRF4u0UBKMVKT"><b>online</b></a>

1
doc/examples/crend.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/XHuFmdmwqZ7Tdw2U"><b>online</b></a>

1
doc/examples/dump.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/tkZFGPekIFMEQYY0"><b>online</b></a>

1
doc/examples/empty.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/8vK2Qdo2tJk3loVq"><b>online</b></a>

1
doc/examples/end.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/PTRnmRsywWUoknqU"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/iqtEFeReXatgjqHy"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/4ZSaQAFaqyN8Vhop"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/vQ4ipI1dlW8IZhNh"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/foNekpKoDDTEfKuy"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/q5oRles9a6IgEOkD"><b>online</b></a>

1
doc/examples/front.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/OvihNTNV4XPwoSw8"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/fdsMCtY67vLJrlzv"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/4OkGAXhiCsa1E5Jx"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/Ka7Efl5Jo2FTDy69"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/pX9pR42isGha24Up"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/yBDoHvFIsRqoo7Ca"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/CwckRKEAALOpqiq7"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/xVKMzHSvo4XHFyxV"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/SbPvCBbOwWkIH5RX"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/CVMYqyeIFOKcBZZU"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/uXT6peoqwN6DIFAr"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/f5AMxY70IoQ4o0wb"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/ILh5L9NsOSQ29Cg4"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/D9WgQUo2eieU7kqs"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/RLNomTjexFIzyUx4"><b>online</b></a>

1
doc/examples/object.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/kQKYwNmQRJAJhCTe"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/PK8Jc2HcZ1MdHOFH"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/OTJgTuHDjzQlrONB"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/q0DrCAa87X0xR7vu"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/t8kFPBs2j2mN6VBc"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/NjX5TeaQdGHer7Xu"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/9X5Q8p7DlWA6v0Ey"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/x5eACUyqALnEmtJa"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/eyDuHRe4RnWdGv2o"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/2ZxOGwFs5YLdSOlG"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/XD9GkEmVZI9ecPEL"><b>online</b></a>

1
doc/examples/rbegin.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/qVpSdd1SQJeMkVQQ"><b>online</b></a>

1
doc/examples/rend.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/7oaTSGkGGPAiGxSU"><b>online</b></a>

1
doc/examples/size.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/GozebkNWdjby4a8N"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/qHbCdOUwZK6INFH8"><b>online</b></a>

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/TKgXGJF9Cr5TXabd"><b>online</b></a>

1
doc/examples/type.link Normal file
View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/bYygSYwOFyTzN2Py"><b>online</b></a>

119
doc/send_to_wandbox.py Executable file
View file

@ -0,0 +1,119 @@
#! /usr/bin/env python
# This script uploads a directory to Wandbox (http://melpon.org/wandbox),
# which is an online compiler environment, and prints a permalink to the
# uploaded code. We use this to provide a "Try it online" version of the
# library to make the barrier to entry as low as possible.
#
# This script was adapted from the script proposed in
# https://github.com/melpon/wandbox/issues/153.
#
# To know how to use this script: ./wandbox.py --help
#
# Copyright Louis Dionne 2015
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
import argparse
import fnmatch
import json
import os
import re
import urllib2
# Strips C and C++ comments from the given string.
#
# Copied from http://stackoverflow.com/a/241506/627587.
def strip_comments(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " " # note: a space and not an empty string
else:
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
# Post the given JSON data to Wandbox's API, and return the result
# as a JSON object.
def upload(options):
request = urllib2.Request('http://melpon.org/wandbox/api/compile.json')
request.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(request, json.dumps(options))
return json.loads(response.read())
# Returns a list of the '.hpp' headers in the given directory and in
# subdirectories.
#
# The path must be absolute, and the returned paths are all absolute too.
def headers(path):
return [
os.path.join(dir, file)
for (dir, _, files) in os.walk(path)
for file in fnmatch.filter(files, "*.hpp")
]
def main():
parser = argparse.ArgumentParser(description=
"""Upload a directory to Wandbox (http://melpon.org/wandbox).
On success, the program prints a permalink to the uploaded
directory on Wandbox and returns 0. On error, it prints the
response from the Wandbox API and returns 1.
Note that the comments are stripped from all the headers in the
uploaded directory.
"""
)
parser.add_argument('directory', type=str, help=
"""A directory to upload to Wandbox.
The path may be either absolute or relative to the current directory.
However, the names of the files uploaded to Wandbox will all be
relative to this directory. This way, one can easily specify the
directory to be '/some/project/include', and the uploaded files
will be uploaded as-if they were rooted at '/some/project/include'
""")
parser.add_argument('main', type=str, help=
"""The main source file.
The path may be either absolute or relative to the current directory.
"""
)
args = parser.parse_args()
directory = os.path.abspath(args.directory)
if not os.path.exists(directory):
raise Exception("'%s' is not a valid directory" % args.directory)
cpp = os.path.abspath(args.main)
if not os.path.exists(cpp):
raise Exception("'%s' is not a valid file name" % args.main)
response = upload({
'code': open(cpp).read(),
'codes': [{
'file': os.path.relpath(header, directory),
'code': strip_comments(open(header).read())
} for header in headers(directory)],
'options': 'boost-nothing,c++11',
'compiler': 'gcc-4.9.2',
'save': True,
'compiler-option-raw': '-I.'
})
if 'status' in response and response['status'] == '0':
print response['url']
return 0
else:
print response
return 1
exit(main())