GithubHelp home page GithubHelp logo

snowcrash's Introduction

logo

Snow Crash Build Status Build status

API Blueprint Parser

Snow Crash is the reference API Blueprint parser built on top of the Sundown Markdown parser.

API Blueprint is Web API documentation language. You can find API Blueprint documentation on the API Blueprint site.

Status

Use

C++ library

#include "snowcrash.h"

mdp::ByteBuffer blueprint = R"(
# My API
## GET /message
+ Response 200 (text/plain)

        Hello World!
)";

snowcrash::ParseResult<snowcrash::Blueprint> ast;
snowcrash::parse(blueprint, 0, ast);

std::cout << "API Name: " << ast.node.name << std::endl;

Refer to Blueprint.h for the details about the Snow Crash AST and BlueprintSourcemap.h for details about Source Maps tree.

Command line tool

CLI was removed. It is replaced by utility named drafter

Build

  1. Clone the repo + fetch the submodules:

    $ git clone --recursive git://github.com/apiaryio/snowcrash.git
    $ cd snowcrash
  2. Build & test Snow Crash:

    $ ./configure
    $ make test

We love Windows too! Please refer to Building on Windows.

Contribute

Fork & Pull Request

License

MIT License. See the LICENSE file.

snowcrash's People

Contributors

abtris avatar alikh31 avatar almad avatar andreasrs avatar bensleveritt avatar danielgtaylor avatar fbidu avatar fosrias avatar goganchic avatar honzajavorek avatar klokane avatar ksmyth avatar kuangmarkeleven avatar kuba-kubula avatar kylef avatar lfaraone avatar lilymgoh avatar linusu avatar pksunkara avatar ryansb avatar samuelmarks avatar tjanc avatar tnewmandenso avatar w-vi avatar zdne avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

snowcrash's Issues

Un-escape HTML entities

HTML entities such as &mdash; should be escaped in non-description nodes (e.g. name).

In description nodes of AST it should be controlled by the rendering parser flag.

# A &mdash; Z API

Lorem &mdash; Ipsum

Object payload body isn't parsed

Body section in following snippet isn't parsed but instead it is considered to be part of Suite Object description

# Suite

## Suite [/suite]

+   Suite Object (application/json)

    This object represents a suite.

    +   Body

    {
      "name" : "My API",
    }

Snow Crash test fails to build with GCC 4.7

On gcc (Ubuntu/Linaro 4.7.3-2ubuntu1~12.04) 4.7.3:

flock out/Release/linker.lock g++ -pthread -rdynamic -m64 -stdlib=libstdc++  -o out/Release/test-snowcrash -Wl,--start-group out/Release/obj.target/test-snowcrash/test/test-AssetParser.o out/Release/obj.target/test-snowcrash/test/test-Blueprint.o out/Release/obj.target/test-snowcrash/test/test-BlueprintParser.o out/Release/obj.target/test-snowcrash/test/test-HeaderParser.o out/Release/obj.target/test-snowcrash/test/test-MarkdownBlock.o out/Release/obj.target/test-snowcrash/test/test-MarkdownParser.o out/Release/obj.target/test-snowcrash/test/test-MethodParser.o out/Release/obj.target/test-snowcrash/test/test-Parser.o out/Release/obj.target/test-snowcrash/test/test-PayloadParser.o out/Release/obj.target/test-snowcrash/test/test-RegexMatch.o out/Release/obj.target/test-snowcrash/test/test-ResouceGroupParser.o out/Release/obj.target/test-snowcrash/test/test-ResourceParser.o out/Release/obj.target/test-snowcrash/test/test-SymbolTable.o out/Release/obj.target/test-snowcrash/test/test-snowcrash.o out/Release/obj.target/libsnowcrash.a out/Release/obj.target/libsundown.a -Wl,--end-group

g++: error: unrecognized command line option '-stdlib=libstdc++'

Missing error position and typo

Hi,
there is missing character number locating error in the code in the first message below and there is also one tiny typo.
Adam

error: (3)  unexpected model definiton, a model can be only defined in the resource section
warning: (6)  empty response message-body :689:13
warning: (6)  empty request message-body :745:27

Unable to compile master

The compilation process stopped with error:

/usr/include/c++/4.6/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
make[1]: *** [out/Release/obj.target/snowcrash/src/snowcrash/snowcrash.o] Error 1
make: *** [snowcrash] Error 2

See here for complete output, https://drone.io/github.com/subosito/iglo/21. While using v.0.6.1, it's fine, https://drone.io/github.com/subosito/iglo/22.

Any ideas?

PS: I am using snowcrash to test my Iglo project. Iglo is a custom API blueprint formatter.

required/optional field in json output does not match

example:

FORMAT: X-1A

# GET /message{?param1,param2}

+ Parameters

    + param1 (required)
    + param2 (optional)

+ Response 200 (text/plain)

        Hello World!

json result:

"parameters": {
  "param1": {
    "description": "",
    "type": "",
    "required": false,
    "default": "",
    "example": "",
    "values": []
  },
  "param2": {
    "description": "",
    "type": "",
    "required": true,
    "default": "",
    "example": "",
    "values": []
  }
}

Snowcrash generate invalid json

Blueprint markdown:

### Save organizer [POST]

+ Request

    + Headers 

                Content-Type: application/json

    + Body

                { "name": "1. lékařská fakulta", "www": "http://www.lf1.cuni.cz/"}

Generated json:

          "responses": [
            {
              "name": "200",
              "description": "",
              "body": "{   "organizers": [{\n        id: "organizer-f8f4b1",\n        name: "1. lékařská fakulta",\n        www: "http://www.lf1.cuni.cz/"\n    }]\n}\n",
              "schema": "",
              "headers": [
                {
                  "Content-Type": "application/json"
                }
              ]
            }
          ]

is not valid, missing escape quotes. I try to parse with jsonlint.com and ruby, php parser.

Multiple request objects not displayed properly

The second request does not show up.

This code:


### Create Message [POST]
In API Blueprint requests can hold exactly the same kind of information and can be described by exactly the same structure as responses, only with different signature. 

+ Request Create Message (application/json)

  This is our first request named `Create Message`.

    + Headers

            X-My-Request-Header: 42

    + Body

            { "message" : "A message to be posted." }


+ Response 201 (text/plain)

        Created.

+ Request Duplicate Message (application/json)

  This is our second request named `Duplicate Message`.

    + Headers

            X-My-Request-Header2: 43

    + Body

            { "url" : "http://messageoftheday.org" }


+ Response 409 (text/plain)

        Duplicate.

Generates this response


Request
X-My-Request-Header: 42
Content-Type: application/json
X-Resource-Header: 42
{ "message" : "A message to be posted." }
Response
201 (Created)
    Content-Type: text/plain
    X-Resource-Header: 42

Created.
Response
409 (Conflict)
    Content-Type: text/plain
    X-Resource-Header: 42

Duplicate.


Report not allowed characters in identifiers

Currently only some characters are allowed in API Blueprint identifiers.

If illegal character is used the whole section is silently ignored.

Following blueprint should at least produce a warning about illegal identifier (character ')

# Group Parcel 
## Parcel's sticker [/parcels/{parcel_id}/stickers.json]

Support for Windows

Hi,
I really like the API Blueprint idea and I want to use it to describe my own api.

To do this I have to rebuild the parser in C# (yay, .net... :neckbeard:)

Here you show a example output of snowcrash. Is that everything or is there something missing?

Cheers!

Btw: The Parser will be open source'd.

SIGTERM parsing blueprint

Snow Crash crashes while parsing following blueprint:

# A
# B
C

D

E

F

G

# /1
# GET
+ Request
+ Response 200
    + Body

            H

I
# J
> K

Report keywords in description headers

Following blueprint is parsed as with API Name POST and the rest of keywords as a API description. Parser should warn about this.

FORMAT: X-1A

## POST 

+ Request

+ Header 

``` 
lorem ipsum
``` 

+ Body 

``` 
{ 
lorem ipsum
}
```

C interface

As far as I can tell (and I could be wrong here) the snowcrash library is written in C++ without a C compatible interface.

My guess is based on the use of the namespace in snowcrash.h, the lack of extern "C" and the output of nm showing no compiler unmangled parse method. (After reading http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c)

 nm libsnowcrash.dylib | grep parse
0000000000001202 T __ZN9snowcrash14MarkdownParser5parseERKSsRNS_6ResultERSt6vectorINS_13MarkdownBlockESaIS6_EE
000000000002085e T __ZN9snowcrash5parseERKSsjRNS_6ResultERNS_9BlueprintE
00000000000030fe T __ZN9snowcrash6Parser5parseERKSsjRNS_6ResultERNS_9BlueprintE
00000000000221e8 t _parse_block
0000000000026224 t _parse_emph1
00000000000263c2 t _parse_emph2
0000000000023a32 t _parse_htmlblock
0000000000024a08 t _parse_inline
0000000000023e95 t _parse_list
000000000002670e t _parse_table_row

If I am wrong then please do correct me.

Otherwise ruby cannot bind to C++ libraries without this interface so it needs to be written before we can do anything.

Warning message referencing whole model in a message-body

Warning

warning: (3)  message-body content is expected to be a pre-formatted code block, separate it by an empty line and indent every of its line by at least 4 spaces or 1 tab :2619:9

Screenshot

screen shot 2013-10-08 at 11 49 55 pm

Blueprint

FORMAT: X-1A
HOST: https://alpha-api.app.net

# Real World API
This API Blueprint demonstrates a real world example documenting a portion of [App.net API](http://developers.app.net).

NOTE: This document is a **work in progress**.

# Group Posts
This section groups App.net post resources.

## Post [/stream/0/posts/{post_id}/{user_id}/{page_id}]

A Post is the other central object utilized by the *‘App.net Stream API’*. It has rich text and annotations which comprise all of the content a users sees in their feed. Posts are closely tied to the follow graph.

+ Parameters
    + post_id (string, `1`) ... The id of the Post.
    + user_id (string, `1`) ... The id of the Post.
    + page_id (string, `1`) ... The id of the Post.

+ Model (application/json)

    ```js
    {
        "data": {
            "id": "1", // note this is a string
            "user": {
                ...
            },
            "created_at": "2012-07-16T17:25:47Z",
            "text": "@berg FIRST post on this new site #newsocialnetwork",
            "html": "<span itemprop=\"mention\" data-mention-name=\"berg\" data-mention-id=\"2\">@berg</span> FIRST post on <a href=\"https://join.app.net\" rel=\"nofollow\">this new site</a> <span itemprop=\"hashtag\" data-hashtag-name=\"newsocialnetwork\">#newsocialnetwork</span>.",
            "source": {
                "client_id": "udxGzAVBdXwGtkHmvswR5MbMEeVnq6n4",
                "name": "Clientastic for iOS",
                "link": "http://app.net"
            },
            "machine_only": false,
            "reply_to": null,
            "thread_id": "1",
            "num_replies": 3,
            "num_reposts": 0,
            "num_stars": 0,
            "entities": {
                "mentions": [{
                    "name": "berg",
                    "id": "2",
                    "pos": 0,
                    "len": 5
                }],
                "hashtags": [{
                    "name": "newsocialnetwork",
                    "pos": 34,
                    "len": 17
                }],
                "links": [{
                    "text": "this new site",
                    "url": "https://join.app.net"
                    "pos": 20,
                    "len": 13
                }]
            },
            "you_reposted": false,
            "you_starred": false
        },
        "meta": {
            "code": 200,
        }
    }
    ```

### Retrieve a Post [GET]
Returns a specific Post.

+ Headers

    ```
    X-My-Header-1: 1
    ```

+ Response 200
    + Headers

        ```
        X-My-Header-2: 2
        ```

    + Body

        [Post][]


### Delete a Post [DELETE]
Delete a Post. The current user must be the same user who created the Post. It returns the deleted Post on success.

+ Response 204

## Posts Collection [/stream/0/posts]
A Collection of posts.

+ Model (application/json)

    ```js
    {
        ["data": {
            "id": "1", // note this is a string
            "user": {
                ...
            },
            "created_at": "2012-07-16T17:25:47Z",
            "text": "@berg FIRST post on this new site #newsocialnetwork",
            "html": "<span itemprop=\"mention\" data-mention-name=\"berg\" data-mention-id=\"2\">@berg</span> FIRST post on <a href=\"https://join.app.net\" rel=\"nofollow\">this new site</a> <span itemprop=\"hashtag\" data-hashtag-name=\"newsocialnetwork\">#newsocialnetwork</span>.",
            "source": {
                "client_id": "udxGzAVBdXwGtkHmvswR5MbMEeVnq6n4",
                "name": "Clientastic for iOS",
                "link": "http://app.net"
            },
            "machine_only": false,
            "reply_to": null,
            "thread_id": "1",
            "num_replies": 3,
            "num_reposts": 0,
            "num_stars": 0,
            "entities": {
                "mentions": [{
                    "name": "berg",
                    "id": "2",
                    "pos": 0,
                    "len": 5
                }],
                "hashtags": [{
                    "name": "newsocialnetwork",
                    "pos": 34,
                    "len": 17
                }],
                "links": [{
                    "text": "this new site",
                    "url": "https://join.app.net"
                    "pos": 20,
                    "len": 13
                }]
            },
            "you_reposted": false,
            "you_starred": false
        },
        "meta": {
            "code": 200,
        }],
        ...
    }
    ```

### Create a Post [POST]
Create a new Post object. Mentions and hashtags will be parsed out of the post text, as will bare URLs...

+ Request

    [Post][]

+ Response 201

    [Post][]

### Retrieve all Posts [GET]
Retrieves all posts.

+ Response 200

    [Posts Collection][]

## Stars [/stream/0/posts/{post_id}/star]
A User’s stars are visible to others, but they are not automatically added to your followers’ streams.

+ Parameters
    + post_id (string, `1`) ... The id of the Post.

### Star a Post [POST]
Save a given Post to the current User’s stars. This is just a “save” action, not a sharing action.

*Note: A repost cannot be starred. Please star the parent Post.*

+ Response 200

    [Post][]

### Unstar a Post [DELETE]
Remove a Star from a Post.

+ Response 200

    [Post][]


# Group Users

This section groups App.net post resources.

Incorrect warning about duplicate resources

Parsing following API Blueprint:

FORMAT: X-1A

# API Name

# Resource 1 [/1]
## Retrieve Resource 1 [GET]
+ Response 200

        ...

# Section Header
## Resource 2 [/2]

yells incorrect warning: warning: resource /2 is already defined :123:20

Parse adjacent asset blocks

Parser should be able to parse (but warn) asset blocks with wrong indentation:

# GET /1
+ response 200

asset

Unrecognized request section, if the request identifier contains an exclamation mark

Hi,

If the request identifier contains ‘!’, Snowcrash doesn't recognize the request section. Please see below.

Blueprint

FORMAT: X-1A

# Shopping Cart API

## Shopping Cart [/shopping-cart]

### Add an item to a shopping cart [POST]

+ Request I want JSON!

    + Headers

        ```
        Accept: application/json
        ```

    + Body

        ```
        (...)
        ```

+ Response 201 (application/json)

    + Body

        ```
        {
            "some": "value"
        }
        ```

AST

metadata:
- FORMAT: X-1A
name: Shopping Cart API
description:
resourceGroups:
- name:
  description:
  resources:
  - uriTemplate: /shopping-cart
    name: Shopping Cart
    description:
    object:
    methods:
    - method: POST
      name: Add an item to a shopping cart
      description: "+ Request I want JSON!\n+ Headers\n\n    ```\n    Accept: application/json\n    ```\n+ Body\n\n    ```\n    (...)\n    ```\n"
      responses:
      - name: 201
        description:
        body: "{\n    \"some\": \"value\"\n}\n"
        schema:
        headers:
        - Content-Type: application/json

Specification

(...) The "Request" can be followed by an arbitrary string representing the user identifier of this request. (...)

Defining a model after the reference, and using references in code blocks.

Right now, it seems that:

  • Models must come before they are referenced
  • Models cannot be referenced in some contexts, specifically code blocks.

The first just seems like a bug (maybe it's an issue with protagonist?), since the references seem based on MultiMarkdown's references, which I think can be defined after they are referenced.

The second is an enhancement (split this into two issues?). I'm not sure how much is handled by snowcrash specifically and how much is standard multimarkdown, but being able to reference Models in code blocks could save a ton of typing. Or more generally, a simple macro system would be very useful.

Provide instructions for dynamic libraries

Ruby FFI can only bind to dynamic libraries not static ar archives as the current snowcrash build goes to.

Therefore instructions on creating dynamic libraries is the first step for a ruby binding.

Ideally we use something like https://github.com/ffi/ffi-compiler so that we can compile at install time so the instructions hopefully need to be as generic and widely compatible as possible.

Worst case we ship pre-compiled libraries in the gem

Snowcrash CLI output is not a valid YAML

Hi,

Output from the following command is not a valid YAML:

curl --trace - http://httpbin.org/ip | curl-trace-parser --blueprint | snowcrash

As you can see the output is not parseable. Why are headers an array in the AST? In my opinion headers should be a [multi-line string] leaded by a | or an array of line-by-line strings.

Adam

Invalid ‘warning: empty body asset’ for certain status codes

Hi,

Snow Crash throws a warning ‘empty body asset’ for a response with 304 status code. There should not be a warning according the spec.

If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD respond with this status code. The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.

Blueprint

### Get a message [GET]

+ Request

...

+ Response 304

Output

warning: empty body asset

Wrong warning position & misleading warning

Following blueprint is invalid because superfluous colon in Values: however the warning is very unclear ("ignoring whole unrecognized list") and pointing to a wrong position.

FORMAT: X-1A
HOST: xxxxxxxxxxxxxxxxxxxxxxx

# A very long API name

# Resource [/1/{param}]

## GET

+ Parameters
    + param
        + Values:
            + `lorem`

+ Response 204

Unrecognized resource group

The group "Two" in following snippet is not recognized by the parser.

A:1
B:2
C:3

# AI
Lorem Ipsum

# Group One
Lorem Ipsum

## Resource [/1]
Lorem Ipsum

### Create [POST]

# Group Two
Lorem Ipsum

Improve model-referencing warnings

Warning for this blueprint should be improved:

# Resource [/1]

+ Model (plain/text)

        Hello World!

## Retrieve a resource [GET]

+ Response 200

    + Headers

            X-Header: A
    + Body

        [Resource][]


And perhaps also when a reference is inside a pre block like so:

# Resource [/1]

+ Model (plain/text)

        Hello World!

## Retrieve a resource [GET]

+ Response 200

    + Headers

            X-Header: A
    + Body

            [Resource][]

Refer to #37

Error ‘undefined symbol `X`’

Hi Zdeněk,

I'm getting the ‘undefined symbol Suite’ error. Please note I use 4 spaces to indent. What am I doing wrong?

Blueprint

FORMAT: X-1A

# Title

Description...

---

# Suite

## Suite [/suite]

    + Suite Object (application/json)

        This object represents a suite.

        + Body

            ```
            {
              "a": "b"
            }
            ```

### Get the current suite [GET]

+ Response 200 (application/json)

    [Suite][]

Error

{
  code: 3,
  message: 'undefined symbol `Suite`',
  location: [
    {
      index: 324,
      length: 10
    }
  ]
}

Fail to parse payload's body

In the following example's Discussion Object body is parsed as a description instead of body.

FORMAT: X-1A

# My API

# Group Discussion

## Discussion [/discussion]

+ Discussion Object (application/json)

    + List item

    End of description.

    + Body

        ```
        {
          ...
        }
        ```

### Get a discussion [GET]

+ Response 200

    [Discussion][]

Distinguish between a payload with description and dangling message-body

Following request:

+ request
   A request description.

is parsed as a message-body with a warning instead as a request without message-body but with a description.

NOTE: Original rationale for this behavior was to enable parsing (with error) of malformed asset (pre-formatted) code blocks.

Malformed:

+ request
   { "message" : "Hello World" }

Correct:

+ request

        { "message" : "Hello World" }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.