docs 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/docs ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'docs'
3
+ Docs::Docs.start
data/docs.gemspec CHANGED
@@ -22,4 +22,6 @@ Gem::Specification.new do |s|
22
22
  # s.add_development_dependency "rspec"
23
23
  # s.add_runtime_dependency "rest-client"
24
24
  s.add_runtime_dependency "thor", "~> 0.14.6"
25
+ s.add_runtime_dependency "redcarpet", "~> 2.1.0"
26
+ s.add_runtime_dependency "coderay", "~> 1.0.5"
25
27
  end
@@ -0,0 +1,27 @@
1
+ require 'yaml'
2
+
3
+ module Docs
4
+ class Config
5
+ def initialize(config_file)
6
+ @config = YAML.load(File.read(config_file))
7
+ @environment = ENV['DOCS_ENV'] || 'production'
8
+
9
+ begin
10
+ env = @config.delete('environment')[@environment]
11
+ rescue NoMethodError
12
+ env = {}
13
+ end
14
+
15
+ @config.merge!(env)
16
+ end
17
+
18
+ def method_missing(name, *args, &block)
19
+ name = name.to_s
20
+ if @config.has_key?(name)
21
+ @config[name]
22
+ else
23
+ super
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require 'coderay'
2
+ require 'json'
3
+
4
+ module Docs
5
+ class ERBEnvironment
6
+ attr_writer :examples
7
+ attr_accessor :content
8
+ attr_accessor :config # <%= config.api_endpoint %>
9
+
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
15
+
16
+ def pretty_print_json(json)
17
+ JSON.pretty_generate(JSON.parse(json))
18
+ end
19
+
20
+ def syntax_highlight(str, language)
21
+ CodeRay.scan(str, language).div
22
+ end
23
+
24
+ def get_binding
25
+ binding
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,94 @@
1
+ require 'fileutils'
2
+ require 'digest/md5'
3
+
4
+ require 'erb'
5
+ require 'redcarpet'
6
+
7
+ module Docs
8
+ class Project
9
+
10
+ EXTENSIONS_TO_COMMANDS = {
11
+ '.sh' => 'bash',
12
+ '.rb' => 'ruby'
13
+ }
14
+
15
+ def initialize
16
+ @examples = {:source => {}, :output => {}}
17
+ @erb_environment = ERBEnvironment.new
18
+
19
+ @markdown = markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
20
+ @erb_environment.config = @config = Config.new('config.yaml') if File.exists?('config.yaml')
21
+ end
22
+
23
+ def build
24
+ create_tmp_directory
25
+ truncate_or_create_output_directory
26
+
27
+ load_examples
28
+ build_docs
29
+ copy_public
30
+ end
31
+
32
+ private
33
+ def load_examples
34
+ examples = Dir.glob('examples/**/*')
35
+ examples.each do |example_name|
36
+ next unless File.file?(example_name)
37
+
38
+ example_file = example_name
39
+ if File.fnmatch?('*.erb.*', example_name)
40
+ File.open(example_file = "tmp/#{Digest::MD5.hexdigest(example_name)}#{File.extname(example_name)}", 'w+') do |f|
41
+ f.write(render_erb_string File.read(example_name))
42
+ end
43
+ end
44
+
45
+ @examples[:source][example_name] = File.read(example_file)
46
+
47
+ i = IO.popen("#{EXTENSIONS_TO_COMMANDS[File.extname(example_file)]} #{example_file}")
48
+ @examples[:output][example_name] = i.read
49
+ end
50
+
51
+ @erb_environment.examples = @examples
52
+ end
53
+
54
+ def build_docs
55
+ docs = Dir.glob('docs/**/*.mdown')
56
+ docs.each do |doc|
57
+ output_name = "output/#{doc.sub('docs/', '')[0...-5]}html"
58
+
59
+ erbed = render_erb_string(File.read(doc))
60
+ rendered = @markdown.render(erbed)
61
+
62
+ if @config.template
63
+ @erb_environment.content = rendered
64
+ rendered = render_erb_string File.read(@config.template)
65
+ end
66
+
67
+ File.open(output_name, 'w+') do |f|
68
+ f.write rendered
69
+ end
70
+ end
71
+ end
72
+
73
+ def copy_public
74
+ Dir.glob('public/**').each do |f|
75
+ next unless File.file?(f)
76
+ FileUtils.cp(f, "output/#{f.sub('public/', '')}")
77
+ end
78
+ end
79
+
80
+ def render_erb_string(str)
81
+ ERB.new(str).result(@erb_environment.get_binding)
82
+ end
83
+
84
+ def truncate_or_create_output_directory
85
+ FileUtils.rm_rf('output') if Dir.exists?('output')
86
+ Dir.mkdir('output')
87
+ end
88
+
89
+ def create_tmp_directory
90
+ Dir.mkdir('tmp') unless Dir.exists?('tmp')
91
+ end
92
+
93
+ end
94
+ end
data/lib/docs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Docs
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/docs.rb CHANGED
@@ -1,7 +1,21 @@
1
+ require "bundler/setup"
2
+ require "thor"
3
+
1
4
  require "docs/version"
5
+ require "docs/project"
6
+ require "docs/config"
7
+ require "docs/erb_environment"
2
8
 
3
9
  module Docs
4
- class Docs < Thor
5
-
10
+ class Docs < Thor
11
+
12
+ desc "build [DIRECTORY]", "Builds the current directory (or DIRECTORY) as a docs project into './output'"
13
+ def build(directory = '.')
14
+ previous_directory = Dir.pwd
15
+ Dir.chdir(directory)
16
+ Project.new.build
17
+ Dir.chdir(previous_directory)
18
+ end
19
+
6
20
  end
7
21
  end
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.1
4
+ version: 0.0.2
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-21 00:00:00.000000000Z
12
+ date: 2012-02-22 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
16
- requirement: &70315023762520 !ruby/object:Gem::Requirement
16
+ requirement: &70167636469260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,20 +21,47 @@ dependencies:
21
21
  version: 0.14.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70315023762520
24
+ version_requirements: *70167636469260
25
+ - !ruby/object:Gem::Dependency
26
+ name: redcarpet
27
+ requirement: &70167636468760 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70167636468760
36
+ - !ruby/object:Gem::Dependency
37
+ name: coderay
38
+ requirement: &70167636468300 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.5
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70167636468300
25
47
  description: Documentation generator with support for including code examples and
26
48
  compiling them at compile time
27
49
  email:
28
50
  - ben@benmcredmond.com
29
- executables: []
51
+ executables:
52
+ - docs
30
53
  extensions: []
31
54
  extra_rdoc_files: []
32
55
  files:
33
56
  - .gitignore
34
57
  - Gemfile
35
58
  - Rakefile
59
+ - bin/docs
36
60
  - docs.gemspec
37
61
  - lib/docs.rb
62
+ - lib/docs/config.rb
63
+ - lib/docs/erb_environment.rb
64
+ - lib/docs/project.rb
38
65
  - lib/docs/version.rb
39
66
  homepage: ''
40
67
  licenses: []