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 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 in the code.
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. They are stored as shell scripts written in either BASH or Ruby. Both the source of these files and the output when run is made available to your documentation files at build time. These are stored outside of the actual documentation so you can write tests for them. To include the source of an example named `examples/create_a_user.sh`:
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 in your documentation the output of the bash script:
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
- If `template` is set in `config.yaml`, all markdown files will be compiled with `template` used as the template. Within template you can include the compiled Markdown:
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
@@ -1,3 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
- require 'docs'
2
+ unless ENV['DOCS_DEV']
3
+ require 'docs'
4
+ else
5
+ $:.unshift(f = File.join(File.dirname(__FILE__), '../lib'))
6
+ require "#{f}/docs"
7
+ end
8
+
3
9
  Docs::Project.start
data/lib/docs.rb CHANGED
@@ -5,3 +5,6 @@ require "docs/version"
5
5
  require "docs/project"
6
6
  require "docs/config"
7
7
  require "docs/erb_environment"
8
+ require "docs/example"
9
+ require 'docs/example/helper'
10
+ require "docs/erb_renderer"
data/lib/docs/config.rb CHANGED
@@ -2,7 +2,7 @@ require 'yaml'
2
2
 
3
3
  module Docs
4
4
  class Config
5
- def initialize(config_file)
5
+ def initialize(config_file = 'config.yaml')
6
6
  @config = YAML.load(File.read(config_file))
7
7
  @environment = ENV['DOCS_ENV'] || 'production'
8
8
 
@@ -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
- # <%= example :source, "curl/users/getting.sh" %>
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
@@ -0,0 +1,15 @@
1
+ require 'erb'
2
+
3
+ module Docs
4
+ class ERBRenderer
5
+
6
+ def initialize(erb_environment)
7
+ @erb_environment = erb_environment
8
+ end
9
+
10
+ def render(str)
11
+ ERB.new(str).result(@erb_environment.get_binding)
12
+ end
13
+
14
+ end
15
+ end
@@ -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('config.yaml')
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 = Dir.glob('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 = render_erb_string(File.read(doc))
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 = render_erb_string File.read(@config.template)
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
@@ -1,3 +1,3 @@
1
1
  module Docs
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/template/config.yaml CHANGED
@@ -2,4 +2,6 @@ directories:
2
2
  temp: tmp
3
3
  output: output
4
4
  assets: public
5
+ docs: docs
6
+ examples: examples
5
7
  template: application.erb
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
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-22 00:00:00.000000000Z
12
+ date: 2012-02-23 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
16
- requirement: &70229066023880 !ruby/object:Gem::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: *70229066023880
24
+ version_requirements: *70238574194100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: redcarpet
27
- requirement: &70229066023380 !ruby/object:Gem::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: *70229066023380
35
+ version_requirements: *70238574193600
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: coderay
38
- requirement: &70229066022920 !ruby/object:Gem::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: *70229066022920
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