Snowcrash parser harness
Drafter is complex builder of API Blueprint. Internally it uses Snowcrash library, reference API Blueprint parser.
API Blueprint is Web API documentation language. You can find API Blueprint documentation on the API Blueprint site.
Additionally Drafter provide set of Wrappers for serialization, of parsing result, via SOS library into JSON and YAML format.
Drafter also provides the user ability to select the type of the output. There are two possible values:
- API Elements Parse Result: Parse Result is defined in API Elements according to Parse Result Namespace.
- Normal AST Parse Result: Parse Result defined by the API Blueprint AST Parse Result. The AST is deprecated and only available in the Drafter command line tool.
By default, Drafter assumes the Refract Parse Result.
Both the types of Parse Results are available in two different serialization formats, YAML and JSON. YAML is the default for the CLI.
- Format 1A9 fully implemented
OS X using Homebrew:
$ brew install drafter
AUR package for Arch Linux.
Other systems refer to installation notes.
Drafter is both a library and a command line tool.
The command line tool allows you to parse a blueprint and/or check the validity of a blueprint.
$ cat << 'EOF' > blueprint.apib
# My API
## GET /message
+ Response 200 (text/plain)
Hello World!
EOF
$ drafter blueprint.apib
element: "parseResult"
content:
-
element: "category"
meta:
classes:
- "api"
title: "My API"
...
See parse feature for the details on using the drafter
command line tool.
Please refer to
drafter.h
for the full API documentation. See Drafter bindings for using the
library in other languages.
The drafter_parse_blueprint_to
takes a source blueprint and returns the given
blueprint parsed and serialized as API
Elements in YAML or JSON.
int drafter_parse_blueprint_to(const char* source, char ** out, const drafter_options options);
#include <drafter/drafter.h>
const char* blueprint =
"# My API\n"
"## GET /message\n"
"+ Response 200 (text/plain)\n"
"\n"
" Hello World!\n";
drafter_options options;
options.format = DRAFTER_SERIALIZE_JSON;
options.sourcemap = true;
char* result = NULL;
if (drafter_parse_blueprint_to(blueprint, &result, options) == 0) {
printf("%s\n", result);
free(result);
}
The drafter_check_blueprint
function allows checking the validity of a
blueprint. This function will return a drafter_result
when the blueprint
produces warnings and/or errors. With a drafter_result
, the
drafter_serialize
function can be used to serialized the result as API
Elements in YAML or JSON.
drafter_result* drafter_check_blueprint(const char* source);
#include <drafter/drafter.h>
const char* blueprint =
"# My API\n"
"## GET /message\n"
"+ Response 200 (text/plain)\n"
"\n"
" Hello World!\n";
drafter_result* result = drafter_check_blueprint(blueprint);
if (result) {
// Serialize the result to print the warnings/errors
drafter_options options;
options.format = DRAFTER_SERIALIZE_JSON;
options.sourcemap = true;
char* out = drafter_serialize(result, options);
printf("The blueprint produces warnings or errors:\n\n%s\n", out);
free(out);
drafter_free_result(result);
} else {
printf("The given blueprint was valid.\n");
}
Building Drafter will require a modern C++ compiler and CMake. The following compilers are tested and known to work:
Compiler | Minimum Version |
---|---|
Clang | 4.0 |
GCC | 5.3 |
MSVC++ | 2015 |
The following steps can be used to build and install Drafter:
-
Download a stable release of Drafter (release tarballs can be found in GitHub Releases):
$ curl -OL <url to drafter release from GitHub releases> $ tar xvf drafter.tar.gz $ cd drafter
Alternatively, you can clone the source repository, for example:
$ git clone --recursive https://github.com/apiaryio/drafter.git $ cd drafter
-
Build & Install Drafter:
POSIX (macOS/Linux):
$ mkdir build $ cd build $ cmake .. $ make $ [sudo] make install
NOTE: You can use
cmake -DCMAKE_INSTALL_PREFIX="$HOME/.local ..
if you don't want a system wide install.Windows:
> mkdir build > cd build > cmake .. > cmake --build . --target drafter --config Release
On Windows,
drafter.exe
can be found insidesrc\Release
-
You can now use Drafter CLI and library:
$ drafter --help
Drafter bindings in other languages:
- drafter-npm (Node.js)
- drafter.js (Pure JavaScript)
- RedSnow (Ruby)
- DrafterPy (Python)
- fury-cli (Node.js)
- Drafter-php (PHP)
Fork & Pull Request
If you want to create a binding for Drafter please refer to the Writing a Binding article.
MIT License. See the LICENSE file.