docs 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.mdown +25 -5
- data/bin/docs +7 -1
- data/lib/docs.rb +3 -0
- data/lib/docs/config.rb +1 -1
- data/lib/docs/erb_environment.rb +3 -5
- data/lib/docs/erb_renderer.rb +15 -0
- data/lib/docs/example.rb +59 -0
- data/lib/docs/example/helper.rb +15 -0
- data/lib/docs/project.rb +16 -33
- data/lib/docs/spec.rb +19 -0
- data/lib/docs/version.rb +1 -1
- data/template/config.yaml +2 -0
- metadata +12 -8
data/README.mdown
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Docs
|
2
|
-
Docs is a documentation generator for Ruby. It is intended for documenting things like HTTP APIs where the documentation doesn't make sense
|
2
|
+
Docs is a documentation generator for Ruby. It is intended for documenting things like HTTP APIs where the documentation doesn't make sense within the code.
|
3
3
|
|
4
4
|
## Install
|
5
5
|
|
@@ -23,25 +23,45 @@ It will create the following file structure:
|
|
23
23
|
|
24
24
|
The `docs` directory contains your documentation (written in Markdown, using the `.mdown` file extension), these are compiled to HTML at build time. The Markdown files are pre-processed with ERB, primarily so you can include your examples (up next).
|
25
25
|
|
26
|
-
The `examples` directory contains the examples used within your code.
|
26
|
+
The `examples` directory contains the examples used within your code. The source of these files is made available to all documentation files at build time. If the files are written in either BASH or Ruby, using the `.sh` and `.rb` extensions, respesctively, the output of these scripts when run is also made available. These are stored outside of the actual documentation so you can test them (ensuring you don't ship bad docs!).
|
27
|
+
|
28
|
+
You can specify that an example is preprocessed with ERB by prefixing its actual file extension with `.erb`. e.g. `examples/create_a_user.sh` would become `examples/create_a_user.erb.sh`. This is useful for including config variables set in `config.yaml` (see below).
|
29
|
+
|
30
|
+
To include the source of an example named `examples/create_a_user.sh`:
|
27
31
|
|
28
32
|
```erb
|
29
33
|
<%= examples :source, 'examples/create_a_user.sh' %>
|
30
34
|
```
|
31
35
|
|
32
|
-
If you would like to show
|
36
|
+
If you would like to show the output:
|
33
37
|
|
34
38
|
```erb
|
35
39
|
<%= examples :output, 'examples/create_a_user.sh' %>
|
36
40
|
```
|
37
41
|
|
38
|
-
The `config.yaml` file contains some config variables for Docs. Any variable set here is accessible within your documentation:
|
42
|
+
The `config.yaml` file contains some config variables for Docs. Any variable set here is accessible within your documentation and within examples pre-processed with ERB:
|
39
43
|
|
40
44
|
```erb
|
41
45
|
<%= config.template %>
|
42
46
|
```
|
43
47
|
|
44
|
-
|
48
|
+
`config.yaml` also supports environment dependent variables. These are useful for setting things like your API endpoint, so you can compile the docs locally or against production. e.g.
|
49
|
+
|
50
|
+
```yaml
|
51
|
+
environment:
|
52
|
+
production:
|
53
|
+
api_endpoint: "http://api.intercom.io/"
|
54
|
+
development:
|
55
|
+
api_endpoint: "http://intercom.dev"
|
56
|
+
```
|
57
|
+
|
58
|
+
The default environment is `production`, to change the environment set the value of the `DOCS_ENV` environment variable. In the example above if docs was run in the `production` environment, `api_endpoint` would be set as a top level attribute in `config` and the variables set in `development` would be inaccessible. e.g.
|
59
|
+
|
60
|
+
```erb
|
61
|
+
<%= config.api_endpoint %>
|
62
|
+
```
|
63
|
+
|
64
|
+
If `template` is set in `config.yaml`, all markdown files will be compiled with `template` used as the template. Within the template you can include the compiled Markdown:
|
45
65
|
|
46
66
|
```erb
|
47
67
|
<%= content %>
|
data/bin/docs
CHANGED
data/lib/docs.rb
CHANGED
data/lib/docs/config.rb
CHANGED
data/lib/docs/erb_environment.rb
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
require 'coderay'
|
2
2
|
require 'json'
|
3
3
|
|
4
|
+
require 'docs/example/helper'
|
5
|
+
|
4
6
|
module Docs
|
5
7
|
class ERBEnvironment
|
6
8
|
attr_writer :examples
|
7
9
|
attr_accessor :content
|
8
10
|
attr_accessor :config # <%= config.api_endpoint %>
|
9
11
|
|
10
|
-
|
11
|
-
def example(source_or_output, name)
|
12
|
-
raise ArgumentError, "Must specify :source or :output as first argument" unless [:source, :output].include?(source_or_output)
|
13
|
-
@examples[source_or_output]["examples/#{name}"]
|
14
|
-
end
|
12
|
+
include ExampleHelper
|
15
13
|
|
16
14
|
def pretty_print_json(json)
|
17
15
|
begin
|
data/lib/docs/example.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'docs/config'
|
2
|
+
require 'docs/erb_environment'
|
3
|
+
require 'docs/example/helper'
|
4
|
+
|
5
|
+
module Docs
|
6
|
+
class Example
|
7
|
+
EXTENSIONS_TO_COMMANDS = {
|
8
|
+
'.sh' => 'bash',
|
9
|
+
'.rb' => 'ruby'
|
10
|
+
}
|
11
|
+
|
12
|
+
def initialize(example_name, erb_renderer=nil)
|
13
|
+
@config = Config.new
|
14
|
+
@erb = erb_renderer || new_erb_renderer
|
15
|
+
|
16
|
+
@source = nil
|
17
|
+
@output = nil
|
18
|
+
|
19
|
+
@file_name = example_name
|
20
|
+
@rendered_file_name = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def output
|
24
|
+
return @output if @output
|
25
|
+
|
26
|
+
source if (is_erb? && !@rendered_file_name)
|
27
|
+
|
28
|
+
file_name = @rendered_file_name || @file_name
|
29
|
+
i = IO.popen("#{EXTENSIONS_TO_COMMANDS[File.extname(file_name)]} #{file_name}")
|
30
|
+
|
31
|
+
@output = i.read
|
32
|
+
end
|
33
|
+
|
34
|
+
def source
|
35
|
+
@source ||= (is_erb?) ? render_erb_template : File.read(@file_name)
|
36
|
+
end
|
37
|
+
|
38
|
+
def is_erb?
|
39
|
+
@is_erb ||= File.fnmatch?('*.erb.*', @file_name)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
def new_erb_renderer
|
44
|
+
erb_environment = ERBEnvironment.new
|
45
|
+
erb_environment.config = Config.new
|
46
|
+
ERBRenderer.new erb_environment
|
47
|
+
end
|
48
|
+
|
49
|
+
def render_erb_template
|
50
|
+
rendered = @erb.render(File.read(@file_name))
|
51
|
+
|
52
|
+
File.open(@rendered_file_name = "#{@config.directories['temp']}/#{Digest::MD5.hexdigest(@file_name)}#{File.extname(@file_name)}", 'w+') do |f|
|
53
|
+
f.write rendered
|
54
|
+
end
|
55
|
+
|
56
|
+
rendered
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Docs
|
2
|
+
module ExampleHelper
|
3
|
+
def examples(examples_path, erb_renderer)
|
4
|
+
Dir.glob(examples_path).inject({}) do |hsh, example_name|
|
5
|
+
hsh[example_name] = Example.new(example_name, erb_renderer)
|
6
|
+
hsh
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def example(source_or_output, name)
|
11
|
+
raise ArgumentError, "Must specify :source or :output as first argument" unless [:source, :output].include?(source_or_output)
|
12
|
+
@examples["examples/#{name}"].send(source_or_output)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/docs/project.rb
CHANGED
@@ -1,18 +1,15 @@
|
|
1
1
|
require 'fileutils'
|
2
2
|
require 'digest/md5'
|
3
|
-
|
4
3
|
require 'erb'
|
4
|
+
|
5
5
|
require 'redcarpet'
|
6
|
+
require 'docs/example/helper'
|
6
7
|
|
7
8
|
module Docs
|
8
9
|
class Project < Thor
|
9
10
|
|
10
11
|
include Thor::Actions
|
11
|
-
|
12
|
-
EXTENSIONS_TO_COMMANDS = {
|
13
|
-
'.sh' => 'bash',
|
14
|
-
'.rb' => 'ruby'
|
15
|
-
}
|
12
|
+
include ExampleHelper
|
16
13
|
|
17
14
|
desc "setup", "Creates a new Docs Project in current directory"
|
18
15
|
def setup
|
@@ -23,9 +20,10 @@ module Docs
|
|
23
20
|
def build
|
24
21
|
@examples = {:source => {}, :output => {}}
|
25
22
|
@erb_environment = ERBEnvironment.new
|
23
|
+
@erb = ERBRenderer.new(@erb_environment)
|
26
24
|
@markdown = markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
|
27
25
|
|
28
|
-
@erb_environment.config = @config = Config.new
|
26
|
+
@erb_environment.config = @config = Config.new
|
29
27
|
['temp', 'output'].each do |dir|
|
30
28
|
dir = @config.directories[dir]
|
31
29
|
remove_file dir if File.exists? dir
|
@@ -37,39 +35,28 @@ module Docs
|
|
37
35
|
copy_assets
|
38
36
|
end
|
39
37
|
|
38
|
+
desc "spec", "Run example tests"
|
39
|
+
def spec
|
40
|
+
require 'rspec/core'
|
41
|
+
RSpec::Core::Runner.autorun
|
42
|
+
end
|
43
|
+
|
40
44
|
private
|
41
45
|
def load_examples
|
42
|
-
examples =
|
43
|
-
examples.each do |example_name|
|
44
|
-
next unless File.file?(example_name)
|
45
|
-
|
46
|
-
example_file = example_name
|
47
|
-
if File.fnmatch?('*.erb.*', example_name)
|
48
|
-
create_file (example_file = "tmp/#{Digest::MD5.hexdigest(example_name)}#{File.extname(example_name)}"), render_erb_string(File.read(example_name))
|
49
|
-
end
|
50
|
-
|
51
|
-
@examples[:source][example_name] = File.read(example_file)
|
52
|
-
|
53
|
-
unless EXTENSIONS_TO_COMMANDS[File.extname(example_file)].nil?
|
54
|
-
i = IO.popen("#{EXTENSIONS_TO_COMMANDS[File.extname(example_file)]} #{example_file}")
|
55
|
-
@examples[:output][example_name] = i.read
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
@erb_environment.examples = @examples
|
46
|
+
@erb_environment.examples = examples('examples/**/*', @erb)
|
60
47
|
end
|
61
48
|
|
62
49
|
def build_docs
|
63
|
-
docs = Dir.glob('docs/**/*.mdown
|
50
|
+
docs = Dir.glob("#{@config.directories['docs']}/**/*.mdown")
|
64
51
|
docs.each do |doc|
|
65
52
|
output_name = "output/#{doc.sub('docs/', '')[0...-5]}html"
|
66
53
|
|
67
|
-
erbed =
|
54
|
+
erbed = @erb.render(File.read(doc))
|
68
55
|
rendered = @markdown.render(erbed)
|
69
56
|
|
70
57
|
if @config.template
|
71
58
|
@erb_environment.content = rendered
|
72
|
-
rendered =
|
59
|
+
rendered = @erb.render File.read(@config.template)
|
73
60
|
end
|
74
61
|
|
75
62
|
create_file output_name, rendered
|
@@ -82,11 +69,7 @@ module Docs
|
|
82
69
|
FileUtils.cp(f, "output/#{f.sub('public/', '')}")
|
83
70
|
end
|
84
71
|
end
|
85
|
-
|
86
|
-
def render_erb_string(str)
|
87
|
-
ERB.new(str).result(@erb_environment.get_binding)
|
88
|
-
end
|
89
|
-
|
72
|
+
|
90
73
|
def in_directory(directory, &block)
|
91
74
|
previous_directory = Dir.pwd
|
92
75
|
Dir.chdir(directory)
|
data/lib/docs/spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'docs/example'
|
2
|
+
require 'docs/erb_environment'
|
3
|
+
require 'docs/config'
|
4
|
+
|
5
|
+
module Docs
|
6
|
+
module Spec
|
7
|
+
|
8
|
+
include ExampleHelper
|
9
|
+
|
10
|
+
def self.included(base)
|
11
|
+
@erb_environment = ERBEnvironment.new
|
12
|
+
@erb_environment.config = @config = Config.new
|
13
|
+
@erb = ERBRenderer.new(@erb_environment)
|
14
|
+
|
15
|
+
@examples = examples("#{@config.directories['examples']}/**/*", @erb)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
data/lib/docs/version.rb
CHANGED
data/template/config.yaml
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-23 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: thor
|
16
|
-
requirement: &
|
16
|
+
requirement: &70238574194100 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 0.14.6
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70238574194100
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: redcarpet
|
27
|
-
requirement: &
|
27
|
+
requirement: &70238574193600 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 2.1.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70238574193600
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: coderay
|
38
|
-
requirement: &
|
38
|
+
requirement: &70238574193140 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 1.0.5
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70238574193140
|
47
47
|
description: Documentation generator with support for including code examples and
|
48
48
|
compiling them at compile time
|
49
49
|
email:
|
@@ -62,7 +62,11 @@ files:
|
|
62
62
|
- lib/docs.rb
|
63
63
|
- lib/docs/config.rb
|
64
64
|
- lib/docs/erb_environment.rb
|
65
|
+
- lib/docs/erb_renderer.rb
|
66
|
+
- lib/docs/example.rb
|
67
|
+
- lib/docs/example/helper.rb
|
65
68
|
- lib/docs/project.rb
|
69
|
+
- lib/docs/spec.rb
|
66
70
|
- lib/docs/version.rb
|
67
71
|
- template/application.erb
|
68
72
|
- template/config.yaml
|