Git Product home page Git Product logo

restdown's Introduction

Pretty REST API docs authored in Markdown

  1. Write a Markdown file that describes your REST API -- with some light conventions (see "Conventions" below) to structure to your doc file. E.g.:

     $ cat api.restdown    # or api.md or index.markdown whatever
     ---
     title: My Awesome REST API
     ---
    
     # My Awesome REST API
    
     Some introduction...
    
     # Wuzzers
    
     ## GET /wuzzers
    
     ...
    
     ## POST /wuzzers
    
     ...
    
  2. Run it through restdown and out pops (a) "api.html", fairly light semantic HTML5 for your API; and (b) "api.json", a JSON representation of your API.

     $ restdown -m STATICDIR api.restdown
    

    where "STATICDIR" is a path to your static content served dir.

You should now have pretty decent looking REST API docs. Read on for details.

Installation

cd /opt             # or whereever you like install apps
git clone https://github.com/trentm/restdown.git
cd restdown
# Optionally checkout a particular release tag. E.g.:
#   LATEST_RELEASE_TAG=`git tag -l | grep '[0-9]\+\.[0-9]\+\.[0-9]\+' | tail -1`
#   git checkout $LATEST_RELEASE_TAG
export PATH=`pwd`/bin:$PATH

Now you should be able to run restdown:

restdown --version
restdown --help

Conventions

Expected conventions to follow in your restdown document to get nice REST API docs.

  • The first h1 is the API title, and its body is a preface to the API. This first section is exluded from the table of contents (TOC).

  • Subsequent h1's are API section titles. (If your whole API is one logical grouping then you might need that second h1 anyway. Please log an issue if that is the case for you so I can gauge popularity.)

  • h2's are API methods. The text of the h2 should be one of the following forms:

      1. "NAME (VERB PATH)" if you name your api methods other than just
         with the HTTP verb and path. E.g. "ListMachines (GET /:login/machines)".
    
      2. "VERB PATH", E.g. "DELETE /widgets/:uuid"
    
      3. "NAME", E.g. "flickr.photos.recentlyUpdated".
    

    Note that while the more structured names aren't required, they will help get good docs (including: HTML anchors, table of contents entries, JSON API summary content, etc.).

  • h3's are just normal subsection headers within endpoints, if needed for longer documentation for a particular endpoint.

  • h4's are typically for showing example request and response output for an endpoint. A pre-block inside an h4 will get a CSS class.

Brands

A "brand" is a directory with all of the styling (CSS, JS, images) for a restdown-created .html file. The default brand is called "ohthejoy". It was originally derived from the styling of https://api.no.de, though has diverged quite a bit by now. I (or you?) should add more.

The idea is that you can start with the brand here and tweak it to create your own style. You can use your own brand files (for your own HTML/CSS/image tweaks). Start by copying one of the brands in the restdown/brands directory and then use the "-b|--brand" option to restdown. However, if you are happy with the existing brand, then just keep using that. :)

Document Metadata

A restdown document should start with a metadata block like this:

---
key: value
...
---

At the least, you should provide the "title". Supported metadata keys depend on the brand (the metadata is interpolated into the 'header.html.in' and 'footer.html.in' files), but typical keys are:

  • title: The text for the HTML <title>.

  • mediaroot: The base URL from which to load the brand media (images, css). If not provided, the default is "media" (i.e. a relative path).

  • apisections: By default all h1 sections (except the leading preface section) are presumed to define API methods, i.e. all h2's in them are methods. If this isn't the case for you (perhaps you have some expository sections), then you can explicitly list the sections that include API methods. This is a comma-separated list of h1 section titles. E.g.:

      apisections: Accounts, Data Centers, Widgets
    
  • markdown2extras: A list of "extras" to be used for processing the markdown in the document. Valid values are the Extra supported by python-markdown2 (the Markdown processor used by restdown). Note that the "toc", "header-ids" and "markdown-in-html" extras are always turned on. E.g.:

      markdown2extras: wiki-tables, cuddled-lists
    
  • logo-color (brands: spartan): A CSS color string (e.g. '#ff5533', 'blue') to be used for the #logo element.

  • logo-font-family (brands: spartan): A CSS font-family list of font faces for the #logo element. This also supports a font from Google Web Fonts with a "google:" prefix. E.g.:

      logo-font-family: google:Aldrich, Verdana, sans-serif
    
  • header-font-family (brands: spartan): A CSS font-family list of font faces for the h1 - h6 elements. Supports "google:" prefix as above.

Metadata can also be provided on the command-line with the -d|--define option. For example:

restdown --define mediaroot=/ index.restdown

JSON API Summary

A by-product of building the HTML file from the input Restdown is a JSON API summary, that looks something like this:

{
  "endpoints": [
    "GET    /wuzzers",
    "POST   /wuzzers",
    "DELETE /wuzzers",
    ...
  ]
}

This might or might not be useful to you. Really it isn't that useful but can make for a nice endpoints summary for someone curl'ing your API.

If you swing with the expressjs crowd, here is how you can wire this into your project:

// Show JSON API summary by default, but show the API docs if accepts
// HTML (e.g. in a browser).
app.get('/', function(req, res) {
  var accept = req.header("Accept");
  if (accept && (accept.search("application/xhtml+xml") != -1
                 || accept.search("text/html") != -1)) {
    res.sendfile(__dirname + "/docs/api.html");
  } else {
    res.header("Content-Type", "application/json");
    res.sendfile(__dirname + "/docs/api.json");
  }
});

restdown's People

Contributors

trentm 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

restdown's Issues

TOC - cyrillic support?

Cyrillic chars in headers become empty while defining headers ids.

"Мир", "Труд", "Май" -----> "", "-1", "-2" . The first header with empty id is ignored in TOC

I've made some edits for myself:
yuriykashin@e0a712d, js in ohthejoy: yuriykashin@3936a81
Not sure whether to pull request, maybe the problem is a little wider than only cyrillic stuff

spartan brand tweaks

  • not pink for logo color (change default and add metavar for picking this)
  • perhaps a metavar for logo and header font? (from google fonts)
  • slightly wider for markc's use case (ldapjs)
  • the cli pre-block styling from "ohthejoy"

fenced-code-blocks extra with pygments returns an error

I'm trying to use the fenced-code-blocks extra as instructed here but when parsing a block as in the example:

```python
if True:
    print "hi"
```

restdown returns this error

restdown: error: _color_with_pygments() argument after ** must be a mapping, not bool (~/Development/restdown/externals/lib/markdown2.py:1534 in _code_block_sub)

Am I missing something?
Thanx

TOC anchor on non unique headings

If 2 headings start with the same words, the anchor tags always jump to the first heading. If a name collision is detected, it would be nice if some unique value would be generated to avoid this.

CRLF line endings cause error

  1. Create a markdown document api.restdown with CRLF line endings.
  2. $ restdown api.restdown
  3. restdown returns restdown: error: need more than 1 value to unpack (/opt/restdown/bin/restdown:205 in restdown_path)
  4. Convert CRLF to LF using $ dos2unix api.restdown
  5. $ restdown api.restdown
  6. HTML and JSON are successfully generated.

I know the easy way to fix this is to never use CRLF, but it would be helpful to either handle this case automatically, or at least return a more specific error message to the user so they know to reconfigure their editor or run dos2unix.

I have since forced my editor to use LF for all new files.

restdown blows up on document with no TOC elements (i.e. no second h1)

$ python  deps/restdown/bin/restdown -v -m build/docs/public docs/workflowapi.restdown
restdown: error: expected string or buffer (/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/re.py:151 in sub)
()
Traceback (most recent call last):
  File "deps/restdown/bin/restdown", line 691, in <module>
    retval = main(sys.argv)
  File "deps/restdown/bin/restdown", line 686, in main
    defines=defines)
  File "deps/restdown/bin/restdown", line 206, in restdown_path
    html, data = restdown(metadata, markdown, brand_dir)
  File "deps/restdown/bin/restdown", line 248, in restdown
    html.toc_html)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/re.py", line 151, in sub
    return _compile(pattern, 0).sub(repl, string, count)
TypeError: expected string or buffer

Font-awesome support

Hey a greate feature must be font-awesome support.

For the moment i use:

## <i class="fa -fa-check"></i> GET /users

idea is to do something like that:

## fa_fa-check GET /users

Any idea if it's possible?

See ya!

restdown/ohthejoy runs away under Chrome, Firefox

Here's the restdown file:

---
title: Title
markdown2extras: wiki-tables, code-friendly
apisections: Job API
---

# Title

Summary goes here.

# Job API

# Job API examples

Something here

## Create a job

    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d

## Checking job state while it's running

    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d
    d

Built with:

$ restdown -m media/ foo.restdown 
restdown: info: wrote foo.html
restdown: info: wrote foo.json

(where media/ is the media from brands/ohthejoy)

When I scroll down (or up, for that matter) to the "Checking job state..." section, Chrome 17.0.963.83 hangs. Ditto Firefox 3 (I know), though Safari 5.1.4 doesn't.

Escaping underscores producing md5 hash

When writing the following:

## GET /posts/:post\_id/comments/:map\_id

It's producing an md5 hash instead of an underscore:

GET /posts/:postmd5-e60453642b5f53da16f4e11348bc05c1id/comments/:mapmd5-e60453642b5f53da16f4e11348bc05c1id

We need to escape /posts/:post_id/comments/:map_id otherwise it places emphasis tags around the middle of the URL.

wiki table parsing expects each row in one line

Wiki table parsing works well when a row is all in one line:

|| cell || cell || cell ||

But in cases where you have a lot of text in one cell, that can make the text hard to write. Maybe something like this?

|| one || two || a very very long line \
                 with a line continuation character? ||

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.