docwu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # doc wu
2
+
3
+ This project rocks and uses MIT-LICENSE.
4
+
5
+ ## A markdown doc generations
6
+
7
+ ## 结构
8
+
9
+ ```
10
+ workspace
11
+
12
+ - assets:
13
+ + javascripts:
14
+ + stylesheets:
15
+ + images:
16
+ - sources:
17
+ - layouts:
18
+ - default.mustache
19
+ - pages:
20
+ - index.mustache
21
+ - posts:
22
+ -
23
+
24
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
File without changes
@@ -0,0 +1,45 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Docwu
3
+ class << self
4
+ attr_writer :config
5
+
6
+ def config
7
+ @config ||= Config.new
8
+ end
9
+
10
+ def configure
11
+ yield self.config ||= Config.new
12
+ end
13
+ end
14
+
15
+ #
16
+ # src_paths
17
+ # asset_paths
18
+ # layout_paths
19
+ # output_path
20
+ #
21
+ class Config
22
+ attr_reader :src_paths, :asset_paths, :layout_paths, :output_path, :data
23
+
24
+ def data= a
25
+ @data ||= a
26
+ end
27
+
28
+ def src_paths= a
29
+ @src_paths ||= a
30
+ end
31
+
32
+ def asset_paths= a
33
+ @asset_paths ||= a
34
+ end
35
+
36
+ def layout_paths= a
37
+ @layout_paths ||= a
38
+ end
39
+
40
+ def output_path= a
41
+ @output_path ||= a
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,51 @@
1
+ module Docwu
2
+ class Folder
3
+ # 一个文件夹下面可能会有很多文件或文件夹的
4
+ attr_reader :posts, :folders, :parent, :path, :worker, :dir
5
+
6
+ def initialize attrs={}
7
+ @path = attrs[:path]
8
+ @worker = attrs[:worker]
9
+ @parent = attrs[:parent]
10
+ @house = attrs[:house]
11
+
12
+ @dir = attrs[:dir]
13
+
14
+ @posts = []
15
+ @folders = []
16
+
17
+ ::Dir.glob("#{self.path}/*").each do |_path|
18
+ _dir = _path.sub(self.path, '')
19
+
20
+ if File.exists?(_path)
21
+ if File.file?(_path) # 如果一个文件
22
+ @posts << ::Docwu::Post.new(:path => _path, :parent => self, :worker => self.worker, :dir => _dir)
23
+ elsif File.directory?(_path) # 如果是一个文件夹
24
+ @folders << self.class.new(:path => _path, :parent => self, :worker => self.worker, :dir => _dir)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ def generate space=nil
31
+ self.folders.each do |folder|
32
+ folder.generate space
33
+ end
34
+
35
+ self.posts.each do |post|
36
+ post.generate space
37
+ end
38
+ end
39
+
40
+ def folder?
41
+ true
42
+ end
43
+
44
+ def post?
45
+ false
46
+ end
47
+
48
+ private
49
+
50
+ end
51
+ end
data/lib/docwu/post.rb ADDED
@@ -0,0 +1,86 @@
1
+ module Docwu
2
+ class Post
3
+ # 一个文件夹下面可能会有很多文件或文件夹的
4
+ attr_reader :parent, :path, :worker, :house, :dir, :content_data
5
+
6
+ def initialize attrs={}
7
+ @path = attrs[:path]
8
+ @house = attrs[:house]
9
+ @parent = attrs[:parent]
10
+ @worker = attrs[:worker]
11
+
12
+ @dir = attrs[:dir]
13
+
14
+ _parse_content = self.parse_content
15
+
16
+ @content_data = {}
17
+
18
+ self.content_data.merge!(_parse_content[:data])
19
+ end
20
+
21
+ def layout
22
+ self.worker.layouts[self.content_data['layout']] || self.worker.layouts['default.mustache']
23
+ end
24
+
25
+ # 渲染
26
+ def generate space=nil
27
+ _parse_content = self.parse_content
28
+
29
+ _content_text = _parse_content[:text]
30
+
31
+ _template = ::Docwu::Utils.read_file(self.layout)
32
+
33
+ _path = self.path
34
+ _dest = self.dest(space)
35
+
36
+ puts " -> generate post: form #{_path} to #{_dest}"
37
+ puts " layout: #{self.layout}"
38
+
39
+ ::Docwu::Render.generate(
40
+ :content_text => _content_text,
41
+ :content_data => self.content_data,
42
+ :dest => _dest,
43
+ :template => _template
44
+ )
45
+ end
46
+
47
+ def dest space
48
+ _dest = "#{self.worker.output_path}"
49
+
50
+ if space
51
+ _dest << "/#{space}"
52
+ end
53
+
54
+ _dest << self.dir
55
+
56
+ _dest
57
+ end
58
+
59
+ def folder?
60
+ false
61
+ end
62
+
63
+ def post?
64
+ true
65
+ end
66
+
67
+ # 解析正文
68
+ def parse_content
69
+ _content = ::File.read(self.path)
70
+
71
+ _content_text = ''
72
+ _content_data = {}
73
+
74
+ _parse_content = _content.to_s.split(/---+\n/)
75
+
76
+ if _parse_content.size > 2 && _parse_content.first.to_s == ''
77
+ _content_text << _parse_content[2]
78
+ # 从上下文中读取配置
79
+ _content_data.merge!(::YAML.load(_parse_content[1]))
80
+ end
81
+
82
+ {:data => _content_data, :text => _content_text}
83
+ end
84
+ end
85
+ end
86
+
@@ -0,0 +1,36 @@
1
+ module Docwu
2
+ class Render
3
+ class << self
4
+ def generate(*args)
5
+ self.new.generate(*args)
6
+ end
7
+ end
8
+
9
+ def generate(options={})
10
+ content_data = options[:content_data] || {}
11
+ content_text = options[:content_text] || ''
12
+ content_result = ''
13
+ dest = options[:dest]
14
+
15
+ # 读取标记类型
16
+ marktype = (content_data['marktype'] || 'markdown').to_s
17
+ content_type = (content_data['content_type'] || 'html').to_s
18
+
19
+ case marktype
20
+ when 'markdown'
21
+ _mark_options = [:hard_wrap, :autolink, :no_intraemphasis, :fenced_code, :gh_blockcode]
22
+
23
+ content_result << ::RedcarpetCompat.new(content_text, *_mark_options).to_html
24
+ else
25
+ # FIXME: no
26
+ end
27
+
28
+ content_data['content'] = content_result
29
+
30
+ puts " --------------------> write to: #{dest}, #{content_result}"
31
+ # 页面的内容
32
+ # ::Docwu::Utils.write_file dest, ::MustacheRender::Mustache.render(template, data.merge('page' => context_data))
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,50 @@
1
+ # http://tobyho.com/2009/09/16/http-server-in-5-lines-with/
2
+ # https://github.com/rack/rack/blob/master/lib/rack/handler/thin.rb
3
+ # https://github.com/macournoyer/thin/blob/master/lib/thin/server.rb
4
+ module Docwu
5
+ class Server
6
+
7
+ # staticMe = Rack::Builder.new do
8
+ # run Rack::Directory.new( Dir.pwd )
9
+ # end
10
+ #
11
+ # Rack::Handler::Thin.run(staticMe, :port => 8080
12
+
13
+ require 'rack'
14
+
15
+ def self.process(options={})
16
+ options['destination'] ||= ::Docwu.config.output_path
17
+ destination = options['destination']
18
+ FileUtils.mkdir_p(destination)
19
+
20
+ options['port'] ||= 5656
21
+ options['host'] ||= '0.0.0.0'
22
+
23
+ staticMe = Rack::Builder.new do
24
+ run Rack::Directory.new(destination)
25
+ end
26
+
27
+ Rack::Handler::Thin.run(staticMe, :Port => options['port'], :Host => options['host'])
28
+ # Rack::Handler::WEBrick.run(staticMe, :Port => options['port'], :Host => options['host'])
29
+ # Rack::Handler::Mongrel.run(staticMe, :Port => options['port'], :Host => options['host'])
30
+ end
31
+
32
+ # require 'webrick'
33
+ # include WEBrick
34
+
35
+ # def self.process(options={})
36
+ # options['destination'] ||= ::Docwu.config.output_path
37
+ # destination = options['destination']
38
+ # FileUtils.mkdir_p(destination)
39
+
40
+ # options['port'] ||= 5656
41
+ # options['host'] ||= '127.0.0.1'
42
+ # options['baseurl'] ||= '/'
43
+
44
+ # server = WEBrick::HTTPServer.new :Port => options['port']
45
+ # server.mount options['baseurl'], WEBrick::HTTPServlet::FileHandler, destination
46
+ # trap('INT') { server.stop }
47
+ # server.start
48
+ # end
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ module Docwu
2
+ class Utils
3
+ class << self
4
+
5
+ def read_file path
6
+ ::File.read(path)
7
+ end
8
+
9
+ def write_file path, content=''
10
+ ::FileUtils.mkdir_p(::File.dirname(dst))
11
+
12
+ file = ::File.new(dst, 'w')
13
+ file.write(content)
14
+ file.close
15
+ end
16
+
17
+ # 复制文件 或者 文件夹
18
+ def copy_with_path(src, dst)
19
+ ::FileUtils.mkdir_p(::File.dirname(dst))
20
+ ::FileUtils.cp_r(src, dst)
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Docwu
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,120 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Docwu
3
+ class Worker
4
+
5
+ # - folder1
6
+ # - assets:
7
+ # + javascripts
8
+ # + images
9
+ # + files
10
+ # - doc:
11
+ # + 市场相关
12
+ # = 个人相关
13
+ # + happy
14
+ # + john
15
+ # - xiaozhang
16
+ # + learning
17
+ # + 基本文档
18
+ # - folder2
19
+ # - layouts:
20
+ # - application.mustache
21
+ # - doc:
22
+ # + 基础资料
23
+ # - assets:
24
+ # + javascripts
25
+ # + images
26
+ # - layouts:
27
+ # - application.mustache
28
+ #
29
+ # data:
30
+ # src_paths
31
+ # asset_paths
32
+ # layout_paths
33
+ # output_path
34
+ #
35
+ attr_reader :src_paths, # 源文件地址们
36
+ :output_path, # 要输出的路径
37
+ :folders, # 项目文件夹们
38
+ :asset_paths, # assets路径
39
+ :layouts, # layouts路径
40
+ :data # 数据
41
+
42
+ def initialize attrs={}
43
+ @data = attrs[:data] || Docwu.config.data || {}
44
+ @src_paths = attrs[:src_paths] || Docwu.config.src_paths
45
+
46
+ # 关于目录
47
+ @folders = {}
48
+ @layouts = {}
49
+
50
+ # 静态文件目录 --------------------------------------------------------
51
+ @asset_paths = []
52
+
53
+ (attrs[:asset_paths] || Docwu.config.asset_paths || []).each do |_path|
54
+ if File.exists?(_path) && File.directory?(_path)
55
+ @asset_paths < _path
56
+ end
57
+ end
58
+
59
+ layout_paths = attrs[:layout_paths] || Docwu.config.layout_paths || []
60
+
61
+ # 计算出当前所有的asset_paths
62
+ @src_paths.each do |_space, _path|
63
+ _asset_path = "#{_path}/assets"
64
+
65
+ if File.exists?(_asset_path) && File.directory?(_asset_path)
66
+ @asset_paths << _asset_path
67
+ end
68
+
69
+ _layout_path = "#{_path}/layouts"
70
+
71
+ if File.exists?(_layout_path) && File.directory?(_layout_path)
72
+ layout_paths << _layout_path
73
+ end
74
+
75
+ _folder_path = "#{_path}/doc"
76
+
77
+ puts " _folder_path---> #{_folder_path}"
78
+
79
+ if File.exists?(_folder_path) && File.directory?(_folder_path)
80
+ @folders[_space] = ::Docwu::Folder.new(:path => _folder_path, :worker => self, :dir => (_folder_path.sub(_path, '')))
81
+ end
82
+ end
83
+
84
+ layout_paths.each do |_path|
85
+ Dir.glob("#{_path}/**/*").each do |_dir|
86
+ if File.exists?(_dir) && File.file?(_dir)
87
+ @layouts[_dir.sub("#{_path}/", '')] = _dir
88
+ end
89
+ end
90
+ end
91
+
92
+ @output_path = attrs[:output_path] || Docwu.config.output_path
93
+
94
+ # puts self.output_path
95
+ # puts self.folders
96
+ # puts self.layouts
97
+ # puts self.asset_paths
98
+ end
99
+
100
+ # 输出:
101
+ # TODO: 先生成临时目录, 然后 -> deploy
102
+ def generate
103
+ # 删除要输出的路径
104
+ FileUtils.rm_rf(self.output_path)
105
+
106
+ # 复制 assets 文件进去
107
+ self.asset_paths.each do |_path|
108
+ FileUtils.cp_r("#{_path}", "#{self.output_path}/")
109
+ end
110
+
111
+ self.folders.each do |space, folder|
112
+ folder.generate space
113
+ end
114
+
115
+ end
116
+
117
+ private
118
+
119
+ end
120
+ end
data/lib/docwu.rb ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "#{File.dirname(__FILE__)}/docwu/config"
3
+ require "#{File.dirname(__FILE__)}/docwu/utils"
4
+ require "#{File.dirname(__FILE__)}/docwu/worker"
5
+ require "#{File.dirname(__FILE__)}/docwu/render"
6
+ require "#{File.dirname(__FILE__)}/docwu/folder"
7
+ require "#{File.dirname(__FILE__)}/docwu/post"
8
+ require "#{File.dirname(__FILE__)}/docwu/category"
9
+ require "#{File.dirname(__FILE__)}/docwu/server"
10
+
11
+ require 'redcarpet'
12
+ require 'coderay'
13
+ require 'yaml'
14
+ require 'fileutils'
15
+ require 'mustache_render'
16
+
17
+ module Docwu
18
+
19
+ def self.generate
20
+ Docwu::Worker.new.generate
21
+ end
22
+
23
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :docwu do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: docwu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - happy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redcarpet
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: coderay
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mustache_render
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: thin
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Description of Docfive.
79
+ email:
80
+ - andywang7259@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - lib/docwu/server.rb
86
+ - lib/docwu/render.rb
87
+ - lib/docwu/config.rb
88
+ - lib/docwu/utils.rb
89
+ - lib/docwu/version.rb
90
+ - lib/docwu/worker.rb
91
+ - lib/docwu/post.rb
92
+ - lib/docwu/category.rb
93
+ - lib/docwu/folder.rb
94
+ - lib/docwu.rb
95
+ - lib/tasks/docwu_tasks.rake
96
+ - MIT-LICENSE
97
+ - Rakefile
98
+ - README.md
99
+ homepage: http://github.com/xiuxian123/docwu
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ segments:
112
+ - 0
113
+ hash: -1575743025569223070
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ segments:
121
+ - 0
122
+ hash: -1575743025569223070
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.25
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Summary of Docfive.
129
+ test_files: []