api_guides 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Rakefile +11 -0
- data/api_guides.gemspec +27 -0
- data/lib/api_guides.rb +22 -0
- data/lib/api_guides/document.rb +111 -0
- data/lib/api_guides/example.rb +38 -0
- data/lib/api_guides/generator.rb +161 -0
- data/lib/api_guides/markdown_helper.rb +59 -0
- data/lib/api_guides/reference.rb +42 -0
- data/lib/api_guides/resources/style.css +258 -0
- data/lib/api_guides/resources/syntax.css +64 -0
- data/lib/api_guides/section.rb +65 -0
- data/lib/api_guides/templates/page.mustache +80 -0
- data/lib/api_guides/version.rb +5 -0
- data/lib/api_guides/view_helper.rb +10 -0
- data/lib/api_guides/views/document.rb +25 -0
- data/lib/api_guides/views/example.rb +17 -0
- data/lib/api_guides/views/page.rb +10 -0
- data/lib/api_guides/views/reference.rb +26 -0
- data/lib/api_guides/views/section.rb +38 -0
- data/readme.md +343 -0
- data/spec/lib/document_spec.rb +53 -0
- data/spec/lib/example_spec.rb +25 -0
- data/spec/lib/generator_spec.rb +47 -0
- data/spec/lib/markdown_helper_spec.rb +57 -0
- data/spec/lib/reference_spec.rb +25 -0
- data/spec/lib/section_spec.rb +65 -0
- data/spec/lib/views/document_spec.rb +15 -0
- data/spec/lib/views/example_spec.rb +11 -0
- data/spec/lib/views/reference_spec.rb +15 -0
- data/spec/lib/views/section_spec.rb +33 -0
- data/spec/spec_helper.rb +36 -0
- data/spec/support/test_guide.xml +14 -0
- metadata +180 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module ApiGuides
|
4
|
+
module Views
|
5
|
+
class Document
|
6
|
+
include ViewHelper
|
7
|
+
|
8
|
+
def initialize(document)
|
9
|
+
@document = document
|
10
|
+
end
|
11
|
+
|
12
|
+
def id
|
13
|
+
anchorize "d-#{@document.title}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def title
|
17
|
+
@document.title
|
18
|
+
end
|
19
|
+
|
20
|
+
def sections
|
21
|
+
@document.sections.map { |s| Section.new(s) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module ApiGuides
|
4
|
+
# Represents a HTML page that can be generated using Mustache.
|
5
|
+
class Page < ::Mustache
|
6
|
+
self.template_file = "#{File.expand_path('../../', __FILE__)}/templates/page.mustache"
|
7
|
+
|
8
|
+
attr_accessor :title, :logo, :documents
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module ApiGuides
|
4
|
+
module Views
|
5
|
+
class Reference
|
6
|
+
include MarkdownHelper
|
7
|
+
include ViewHelper
|
8
|
+
|
9
|
+
def initialize(reference)
|
10
|
+
@reference = reference
|
11
|
+
end
|
12
|
+
|
13
|
+
def id
|
14
|
+
anchorize "r-#{@reference.title}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def title
|
18
|
+
@reference.title
|
19
|
+
end
|
20
|
+
|
21
|
+
def content
|
22
|
+
markdown @reference.content
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module ApiGuides
|
4
|
+
module Views
|
5
|
+
class Section
|
6
|
+
include MarkdownHelper
|
7
|
+
include ViewHelper
|
8
|
+
|
9
|
+
def initialize(section)
|
10
|
+
@section = section
|
11
|
+
end
|
12
|
+
|
13
|
+
def id
|
14
|
+
anchorize "s-#{@section.title}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def title
|
18
|
+
@section.title
|
19
|
+
end
|
20
|
+
|
21
|
+
def docs
|
22
|
+
markdown @section.docs if @section.docs
|
23
|
+
end
|
24
|
+
|
25
|
+
def reference
|
26
|
+
if @section.reference
|
27
|
+
Reference.new @section.reference
|
28
|
+
else
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def examples
|
34
|
+
(@section.examples || []).map {|ex| Example.new ex }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/readme.md
ADDED
@@ -0,0 +1,343 @@
|
|
1
|
+
# API Guides
|
2
|
+
|
3
|
+
**Example (and primary use case)**:
|
4
|
+
[http://developer.radiumcrm.com](http://developer.radiumcrm.com)
|
5
|
+
|
6
|
+
`ApiGuides` is a simple static generator based off Docco. It is
|
7
|
+
specifically designed to handle writing documentation for HTTP APIs,
|
8
|
+
however it should be flexible enough to work in many other situations as
|
9
|
+
well. The generator will create HTML, CSS, an optional logo image inside
|
10
|
+
a directory. You can deploy the site using any web server or Heroku if
|
11
|
+
you like.
|
12
|
+
|
13
|
+
You should use this generator if you need want to create documenation
|
14
|
+
with these things in mind:
|
15
|
+
|
16
|
+
1. You want to write in Markdown.
|
17
|
+
2. You want syntax highlighting for a variety of langauges.
|
18
|
+
3. You want an automatic table of contents
|
19
|
+
4. You want an index of all the technical information
|
20
|
+
5. You want to provide examples of using your code in one or more
|
21
|
+
different languages or frameworks.
|
22
|
+
6. You want to structure your documentation between different code
|
23
|
+
repos or separate files.
|
24
|
+
7. You want to link to specific sections of documentation from within
|
25
|
+
other sections of documentation.
|
26
|
+
8. You have **alot** of documentation to write. This generator all works
|
27
|
+
well with small about of documentation as well.
|
28
|
+
9. You want something that looks pretty, is easy to read, and don't want
|
29
|
+
to make any CSS decisions or worry presentational issues.
|
30
|
+
|
31
|
+
If you think most of those things sound good then read on.
|
32
|
+
|
33
|
+
## Structuring Documentation
|
34
|
+
|
35
|
+
Raw documenation is kept in XML files with content written in Markdown.
|
36
|
+
I've choosen XML because it is great at storing and organizing large
|
37
|
+
amounts of text. Don't worry, you won't have to mess with XML that much.
|
38
|
+
|
39
|
+
You can (and should) structure your documenation in separate files. You
|
40
|
+
can think of each file has a chapter in your documentation. You may have
|
41
|
+
one file that generally describes your program and a file for each
|
42
|
+
feature.
|
43
|
+
|
44
|
+
A document is composed of sections. A section is composed of textual
|
45
|
+
content written in markdown, a reference written in markdown, and zero,
|
46
|
+
one, or more examples written in markdown. Here is an example document.
|
47
|
+
|
48
|
+
```xml
|
49
|
+
<document>
|
50
|
+
<title>introduction</title>
|
51
|
+
<position>1</position>
|
52
|
+
<section title="about">
|
53
|
+
<docs>
|
54
|
+
<![cdata[
|
55
|
+
# write your markdown here
|
56
|
+
]>
|
57
|
+
</docs>
|
58
|
+
<reference title="about">
|
59
|
+
<![cdata[
|
60
|
+
# write some stuff describing your "about" interface
|
61
|
+
]]>
|
62
|
+
</reference>
|
63
|
+
<examples>
|
64
|
+
<example language="ruby">
|
65
|
+
<![cdata[
|
66
|
+
# write some markdown for your ruby example
|
67
|
+
]]>
|
68
|
+
</example>
|
69
|
+
<example language="python">
|
70
|
+
<![cdata[
|
71
|
+
# write some markdown for your python example
|
72
|
+
]]>
|
73
|
+
</example>
|
74
|
+
</examples>
|
75
|
+
</section>
|
76
|
+
</document>
|
77
|
+
```
|
78
|
+
|
79
|
+
There is a lot to take in from that example so let's break it down!
|
80
|
+
|
81
|
+
### The document Element
|
82
|
+
|
83
|
+
Is the root element and must contain a `title`, `position` and one or
|
84
|
+
more `sections`. A `<document>` signifys a new chapter of your
|
85
|
+
documentation. The `title` will be placed in a `h1` and subsequent
|
86
|
+
section titles will be placed in `<h2>`s. The `<position>` element
|
87
|
+
determines the order.
|
88
|
+
|
89
|
+
### The section Element
|
90
|
+
|
91
|
+
Sections are a way to group textual documentation, examples, and
|
92
|
+
technical documentation. You can give a `<section>` a `title` attribute.
|
93
|
+
This will create a TOC entry under the parent `<document>`'s `<title>`.
|
94
|
+
You can omit `title` if you do not want a header or TOC entry generated.
|
95
|
+
You usually skip this for the first section since most of the time you
|
96
|
+
don't want a header right under another without any text in between. You
|
97
|
+
may include `<examples>` or `<reference>` if you wish, but you should
|
98
|
+
always include `<docs>`. The examples and reference material will always
|
99
|
+
be shown with the associated docs. You can omit `<docs>` but that
|
100
|
+
doesn't make any sense! Who's going to figure out what they're doing
|
101
|
+
without the docs.
|
102
|
+
|
103
|
+
### The reference Element
|
104
|
+
|
105
|
+
The Reference element contains technical documentation for the
|
106
|
+
associated docs. It may be a method signature or whatever you can think
|
107
|
+
of. The reference will always be shown with the associated docs and with
|
108
|
+
any examples. You may also give the `<reference>` a `title` attribute.
|
109
|
+
`<references>` with a `title` attribute will be included in the index.
|
110
|
+
You may omit the `title` if you do not want this to be indexed. You
|
111
|
+
should always include a reference!
|
112
|
+
|
113
|
+
### The example Element
|
114
|
+
|
115
|
+
Examples contain code for the language or framework specified in the
|
116
|
+
`language` attribute. An example will always be shown with the reference
|
117
|
+
and associated docs. You can `omit` the language if you want. If you do,
|
118
|
+
the example will be shown for **all** different languages.
|
119
|
+
You can write whatever you want for the content. It
|
120
|
+
can be text, lists, as well as code. The generator makes no assumption
|
121
|
+
about what will be in the example, just that it will be markdown. A
|
122
|
+
different version of the documentation will be generated for each
|
123
|
+
language specified in your documents.
|
124
|
+
|
125
|
+
### General Output
|
126
|
+
|
127
|
+
Your docs will always be show on the left. If a reference is present, it
|
128
|
+
will be shown exactly opposite of the docs. If examples are present,
|
129
|
+
they will be shown below the reference.
|
130
|
+
|
131
|
+
### Generating the Table of Contents
|
132
|
+
|
133
|
+
A TOC will be generated according to `<title>` elements inside the
|
134
|
+
`<document>` and `title` attributes on the `<section>` element. Here are
|
135
|
+
some example XML documents and generated table of contents.
|
136
|
+
|
137
|
+
```xml
|
138
|
+
<document>
|
139
|
+
<title>Introduction</title>
|
140
|
+
<position>1</position>
|
141
|
+
<section>
|
142
|
+
<docs>Foo</docs>
|
143
|
+
</section>
|
144
|
+
<section title="Overview">
|
145
|
+
<docs>Bar</docs>
|
146
|
+
</section>
|
147
|
+
<section title="Response Codes">
|
148
|
+
<docs>Bar</docs>
|
149
|
+
</section>
|
150
|
+
</document>
|
151
|
+
```
|
152
|
+
|
153
|
+
```xml
|
154
|
+
<document>
|
155
|
+
<title>Response Codes</title>
|
156
|
+
<position>2</position>
|
157
|
+
<section title="Success">
|
158
|
+
<docs>Foo</docs>
|
159
|
+
</section>
|
160
|
+
<section title="Redirection">
|
161
|
+
<docs>Bar</docs>
|
162
|
+
</section>
|
163
|
+
<section title="Failure">
|
164
|
+
<docs>Bar</docs>
|
165
|
+
</section>
|
166
|
+
</document>
|
167
|
+
```
|
168
|
+
|
169
|
+
1. Introduction
|
170
|
+
1. Overview
|
171
|
+
2. Reponse Codes
|
172
|
+
2. Rreponse Codes
|
173
|
+
1. Success
|
174
|
+
2. Redirection
|
175
|
+
3. Failure
|
176
|
+
|
177
|
+
## Markdown
|
178
|
+
|
179
|
+
I love Markdown. It's mucher easy to read and write than textile.
|
180
|
+
[Redcarpet](https://github.com/tanoku/redcarpet) is used for parsing. It includes some more features like
|
181
|
+
fenced code blocks with language specification (yay!), tables, and a few
|
182
|
+
other tricks. You can read their documentation for the complete list of
|
183
|
+
available features. You may also include HTML inside the markdown,
|
184
|
+
however, you cannot use markdown inside the HTML. Common use case for
|
185
|
+
HTML is `<dl>`. Definition lists are part of PHP markdown extra and not
|
186
|
+
supported by Redcarpet.
|
187
|
+
|
188
|
+
[Pygments](http://pygments.org/) is used for syntax highlighting. The generator uses the
|
189
|
+
pygments webservice so you don't have to worry about installing anything
|
190
|
+
python related on your machine. It supports a very large variety of
|
191
|
+
different languages and the generator includes a theme to make all of
|
192
|
+
them look nice and pretty.
|
193
|
+
|
194
|
+
## Linking
|
195
|
+
|
196
|
+
You can also link to sections, documents, or references through out your
|
197
|
+
documentation. Each element will be given an ID which you can use to
|
198
|
+
create an anchor link for. They are prefaced with `d`, `s` or `r`
|
199
|
+
depending on what they are. This is to prevent duplication. The titles
|
200
|
+
are are dasherized aka permalinks. Here are some examples:
|
201
|
+
|
202
|
+
1. `<section title="Introduction and about">` -->
|
203
|
+
"s-introduction-and-about"
|
204
|
+
2. `<reference title="Logging In">` --> "r-logging-in"
|
205
|
+
3. `<title>Messages: SMS, Email, & IM</title>` -->
|
206
|
+
"d-messages-sms-email-im"
|
207
|
+
|
208
|
+
## Installation and Use
|
209
|
+
|
210
|
+
Firs thing you'll need to do is install ruby. I'm assuming you already
|
211
|
+
have this installed or know how to install for your operating system.
|
212
|
+
After that, you can simply install the gem.
|
213
|
+
|
214
|
+
```
|
215
|
+
gem install api_guides
|
216
|
+
```
|
217
|
+
|
218
|
+
The easiest way to generate the docs is from a rake task. Here is an
|
219
|
+
example. Assume your directory sturcture is like this:
|
220
|
+
|
221
|
+
```
|
222
|
+
|- source/
|
223
|
+
---------| guide1.xml
|
224
|
+
---------| guide2.xml
|
225
|
+
---------| guide3.xml
|
226
|
+
|- site/
|
227
|
+
|- Rakefile
|
228
|
+
```
|
229
|
+
|
230
|
+
Here is an example Rakefile:
|
231
|
+
|
232
|
+
```ruby
|
233
|
+
require 'bundler'
|
234
|
+
require 'api_guides'
|
235
|
+
|
236
|
+
task :generate do
|
237
|
+
ApiGuides::Generator.new({
|
238
|
+
:source_path => "#{File.dirname(__FILE__)}/source",
|
239
|
+
:site_path => "#{File.dirname(__FILE__)}/site",
|
240
|
+
:title => 'Cool API',
|
241
|
+
:logo => "#{File.dirname(__FILE__)}/logo.png",
|
242
|
+
:default => "ruby"
|
243
|
+
}).generate
|
244
|
+
end
|
245
|
+
|
246
|
+
task :default => :generate
|
247
|
+
```
|
248
|
+
|
249
|
+
When ever you update your guides just run:
|
250
|
+
|
251
|
+
```bash
|
252
|
+
$ bundle exec rake
|
253
|
+
```
|
254
|
+
|
255
|
+
This will stick all the generated files into `site/`.
|
256
|
+
**Note**: Always deal with absolute paths for certanity.
|
257
|
+
**WARNING**: The contents of `site_path` will be deleted before
|
258
|
+
generation occurs. Don't put anything in there if you want to keep it.
|
259
|
+
|
260
|
+
### Options
|
261
|
+
|
262
|
+
The `ApiGuides::Generator` constructor takes a few options. Here are the
|
263
|
+
details.
|
264
|
+
|
265
|
+
* `:source_path`: Abosolute path to the directory containing all the XML
|
266
|
+
documents.
|
267
|
+
* `:site_path`: Absolute path for the generated files
|
268
|
+
* `:title`: It goes in the navbar and the `<title>` in the finished HTML
|
269
|
+
* `:default`: Required if there are more than one lanauges. This sets
|
270
|
+
the examples on the index page.
|
271
|
+
* `:logo`: _Optional_. Absolute path to an image to put in the navbar.
|
272
|
+
|
273
|
+
## Deploying
|
274
|
+
|
275
|
+
Since the generating documentation is just static HTML, CSS, and images
|
276
|
+
we can deploy to any web server very easily. This section shows you how
|
277
|
+
to write a simple [Rack](http://rack.rubyforge.org/) server so you can deploy it to Heroku.
|
278
|
+
|
279
|
+
First, create `config.ru` file in your directory. Here are the contents:
|
280
|
+
|
281
|
+
```ruby
|
282
|
+
require 'rack-rewrite'
|
283
|
+
|
284
|
+
use Rack::Rewrite do
|
285
|
+
rewrite '/', '/index.html'
|
286
|
+
end
|
287
|
+
|
288
|
+
root = "#{Dir.pwd}/site"
|
289
|
+
run Rack::Directory.new("#{root}")
|
290
|
+
```
|
291
|
+
|
292
|
+
Make sure you update the `root =` line with whatever directory holds all
|
293
|
+
the files.
|
294
|
+
|
295
|
+
You can test it locally by running these commands:
|
296
|
+
|
297
|
+
```bash
|
298
|
+
$ gem install rack rack-rewrite
|
299
|
+
$ rackup -p 3000
|
300
|
+
```
|
301
|
+
Now open `http://localhost:3000` and you should see your documentation.
|
302
|
+
|
303
|
+
Now, create a `Procfile` so heroku knows how to start your application:
|
304
|
+
|
305
|
+
```yml
|
306
|
+
web: bundle exec rackup -p $PORT
|
307
|
+
```
|
308
|
+
|
309
|
+
Now you're ready to deploy your documentation!
|
310
|
+
|
311
|
+
```bash
|
312
|
+
$ bundle exec rake # to generate the documentation if you haven't yet
|
313
|
+
$ git init
|
314
|
+
$ git add -A
|
315
|
+
$ git commit -m 'Update documentation'
|
316
|
+
$ heroku create --stack cedar
|
317
|
+
$ git push heroku master
|
318
|
+
$ heroku open
|
319
|
+
```
|
320
|
+
|
321
|
+
## Credits
|
322
|
+
|
323
|
+
* [Docco](http://jashkenas.github.com/docco/) - For creating the original style. CSS taken from source
|
324
|
+
with some modifications.
|
325
|
+
* [Stripe](https://stripe.com/docs/api) - For inspiring more complex documentation and layout.
|
326
|
+
Although, I think this came out better!
|
327
|
+
* [Rocco](https://github.com/rtomayko/rocco) - For demonstrating how to use Mustache
|
328
|
+
* [Twitter Boostrap](http://twitter.github.com/bootstrap) - For the navbar. I only took what I needed and
|
329
|
+
repackaged them into the `#topbar` selector.
|
330
|
+
|
331
|
+
## Hacking
|
332
|
+
|
333
|
+
Feel free to hack on the code in your own fork. Send me a pull request
|
334
|
+
if you do cool stuff.
|
335
|
+
|
336
|
+
## Documentation
|
337
|
+
|
338
|
+
Documentation is written for Rocco. You can read the annotated source
|
339
|
+
[here](http://threadedlabs.github.com/api_guides).
|
340
|
+
|
341
|
+
## License
|
342
|
+
|
343
|
+
MIT.
|