static_cms 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/static_cms CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'static_cms'
4
- ::StaticCms::Runner.start
4
+ ::StaticCms::Application.start
@@ -4,3 +4,6 @@ title: Profile (Short Version)
4
4
  %dl
5
5
  %dt Name
6
6
  %dd John Smith
7
+ %dt E-mail
8
+ %dd
9
+ %img.e_mail_address{src: 'images/e_mail_address.svg'}
@@ -1,6 +1,7 @@
1
1
  require 'static_cms'
2
2
 
3
3
  ::SITE = StaticCms::Site.new
4
+
4
5
  task default: :generate
5
6
  desc "Generate all pages."
6
7
  task generate: SITE.page_alist.keys
@@ -2,6 +2,6 @@ pre, code, samp
2
2
  white-space: pre-wrap
3
3
  img
4
4
  width: 100%
5
- &.mail_address
5
+ &.e_mail_address
6
6
  width: 27ex
7
7
  vertical-align: middle
@@ -0,0 +1,16 @@
1
+ require 'thor'
2
+
3
+ class ::StaticCms::Application < Thor
4
+ require 'ruby_patch'
5
+ extend ::RubyPatch::AutoLoad
6
+
7
+ desc "init [DIR]", "Initialize DIR directory for StaticCms [default: .]."
8
+ def init(dir = '.')
9
+ ::StaticCms::Commands.init(dir)
10
+ end
11
+
12
+ desc "update", "Update templates, sources, statics (and more)."
13
+ def update
14
+ ::StaticCms::Commands.update
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ class ::StaticCms::Article
2
+ require 'ruby_patch'
3
+ extend ::RubyPatch::AutoLoad
4
+
5
+ @alist = Hash.new{|h, k| raise KeyError, k.to_s}
6
+
7
+ def self.alist
8
+ @alist
9
+ end
10
+
11
+ PARAMS_DEFAULT = {
12
+ 'title' => '',
13
+ }
14
+
15
+ attr_accessor :pages, :name, :file, :data
16
+ attr_accessor *(PARAMS_DEFAULT.keys)
17
+
18
+ def initialize(name)
19
+ @pages = []
20
+ @name = name
21
+ @file = File.join('articles', name) + '.html.yhaml'
22
+ yaml, @data = File.read(@file).split(/^\.\.\.$/, 2)
23
+ params = PARAMS_DEFAULT.merge(YAML.load(yaml))
24
+ params.each{|k, v| instance_variable_set("@#{k}", v)}
25
+ self.class.alist[@name] = self
26
+ end
27
+
28
+ def self.load
29
+ `git ls-files articles`.split\
30
+ .select{|f| f =~ /\.html\.yhaml\z/}\
31
+ .map{|f| f.sub(/\Aarticles\//, '')}\
32
+ .map{|f| File.basename(f, '.html.yhaml')}\
33
+ .each{|name| self.new(name)}
34
+ end
35
+
36
+ def self.use(page, name)
37
+ self.load unless self.alist.has_key?(name)
38
+ raise ArgumentError, name unless self.alist.has_key?(name)
39
+
40
+ article = self.alist[name]
41
+ article.pages << page
42
+ article
43
+ end
44
+ end
@@ -0,0 +1,31 @@
1
+ module ::StaticCms::Commands
2
+ require 'fileutils'
3
+ require 'ruby_patch'
4
+ extend ::RubyPatch::AutoLoad
5
+
6
+ SITE_TEMPLATE_DIR = File.join(__DIR__, '..', '..', 'data', 'site_template')
7
+
8
+ def self.init(dir = '.')
9
+ FileUtils.mkdir_p(dir)
10
+ success = false
11
+ FileUtils.cd(dir){|dir|
12
+ success = system <<-EOS
13
+ git init > #{File::NULL}
14
+ git commit --allow-empty -m 'Empty commit.' > #{File::NULL}
15
+ cp -r #{File.join(SITE_TEMPLATE_DIR, '*')} . > #{File::NULL}
16
+ git add . > #{File::NULL}
17
+ git commit -m 'Copied from site template.' > #{File::NULL}
18
+ EOS
19
+ }
20
+
21
+ success
22
+ end
23
+
24
+ def self.update
25
+ site_template_dir = File.join(__DIR__, '../data/site_template')
26
+ FileUtils.cp_r(File.join(site_template_dir, 'templates'), '.')
27
+ FileUtils.cp_r(File.join(site_template_dir, 'sources'), '.')
28
+ FileUtils.cp_r(File.join(site_template_dir, 'statics'), '.')
29
+ FileUtils.cp(File.join(site_template_dir, 'rakefile'), '.')
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ module ::StaticCms::Helpers
2
+ require 'haml'
3
+ require 'ruby_patch'
4
+ extend ::RubyPatch::AutoLoad
5
+
6
+ EXT_TO_LANG = Hash.new(:general)\
7
+ .merge({
8
+ '.f90' => :fortran,
9
+ '.f95' => :fortran,
10
+ '.f03' => :fortran,
11
+ '.f08' => :fortran,
12
+ '.f' => :fortran,
13
+ '.f77' => :fortran,
14
+ '.rb' => :ruby,
15
+ })
16
+
17
+ module_function
18
+
19
+ def to_plain(text)
20
+ escaped_text = ::Haml::Helpers.html_escape(text)
21
+ ::Haml::Helpers.preserve(escaped_text)
22
+ end
23
+
24
+ def to_code(text, lang = :general)
25
+ pre_text = to_plain(text)
26
+ "<pre class='code'><code class='#{lang}'>#{pre_text}</code></pre>"
27
+ end
28
+ end
@@ -0,0 +1,105 @@
1
+ class ::StaticCms::Page
2
+ require 'fileutils'
3
+ require 'haml'
4
+ require 'ruby_patch'
5
+ extend ::RubyPatch::AutoLoad
6
+
7
+ PARAMS_DEFAULT ={
8
+ 'visible' => true,
9
+ 'title' => '',
10
+ 'template' => 'default',
11
+ 'articles' => [],
12
+ 'statics' => [],
13
+ 'sources' => [],
14
+ 'base' => nil,
15
+ }
16
+
17
+ attr_accessor :site, :name, :dir, :template_file
18
+ attr_accessor *(PARAMS_DEFAULT.keys)
19
+
20
+ def initialize(site, name, params = {})
21
+ @site = site
22
+ @name = name
23
+ @dir = File.join("root", @name)
24
+ params = PARAMS_DEFAULT.merge(params)
25
+ @template = params.delete('template')
26
+ @template_file = File.join('templates', @template) + '.html.haml'
27
+ @articles = params.delete('articles').map{|name| ::StaticCms::Article.use(self, name)}
28
+ @statics = params.delete('statics').map{|file| File.join('statics', file)}
29
+ @sources = params.delete('sources').map{|file| File.join('sources', file)}
30
+ @base = params.delete('base') || @site.www
31
+ params.each{|k, v|
32
+ instance_variable_set("@#{k}", v)
33
+ }
34
+ end
35
+
36
+ def generate
37
+ FileUtils.mkdir_p(@dir)
38
+
39
+ if @visible
40
+ @statics.each{|file| cp_if_new(file)}
41
+ @sources.each{|file| compile(file)}
42
+ template_file = @template_file
43
+ else
44
+ @statics.each{|file| FileUtils.rm_rf(static_target(file))}
45
+ @sources.each{|file| FileUtils.rm_rf(compile_target(file))}
46
+ template_file = 'templates/moved.html.haml'
47
+ end
48
+
49
+ html = ::Haml::Engine.new(File.read(template_file)).render(self)
50
+ target = File.join(@dir, 'index.html')
51
+ open(target, 'w'){|io|
52
+ io.write(html)
53
+ io.flush
54
+ io.fsync
55
+ }
56
+ end
57
+
58
+ private
59
+
60
+ def haml(text)
61
+ ::Haml::Engine.new(text).render(self)
62
+ end
63
+
64
+ def nth_page_from_self(n)
65
+ index = (@site.page_alist.to_a.index{|name, page| name == @name} + n)%@site.page_alist.size
66
+ @site.page_alist.to_a[index][1]
67
+ end
68
+
69
+ def link_to_article(name)
70
+ article = ::StaticCms::Article.alist[name]
71
+ page = article.pages.first
72
+ url = File.join(@site.www, page.name, "index.html\##{name}")
73
+
74
+ "<a href='#{url}'>#{article.title}</a>"
75
+ end
76
+
77
+ def cp_if_new(source)
78
+ target = static_target(source)
79
+ if !File.file?(target) || File.mtime(source) > File.mtime(target)
80
+ FileUtils.cp(source, target)
81
+ FileUtils.chmod(0644, target)
82
+ end
83
+ end
84
+
85
+ def compile(source)
86
+ ext = File.extname(source)
87
+ target = compile_target(source)
88
+
89
+ case ext
90
+ when /\A\.sass\z/
91
+ system "sass #{source} > #{target}"
92
+ else
93
+ raise NotImplementedError, file
94
+ end
95
+ end
96
+
97
+ def static_target(source)
98
+ File.join(@dir, File.basename(source))
99
+ end
100
+
101
+ def compile_target(source)
102
+ ext = File.extname(source)
103
+ File.join(@dir, File.basename(source, ext))
104
+ end
105
+ end
@@ -1,4 +1,5 @@
1
1
  require 'haml'
2
+ require 'static_cms'
2
3
 
3
4
  module ::Haml
4
5
  module Filters
@@ -1,7 +1,10 @@
1
1
  module ::StaticCms::Patch
2
+ require 'ruby_patch'
3
+ extend ::RubyPatch::AutoLoad
4
+
2
5
  %w(
3
6
  haml
4
7
  ).each{|lib|
5
- require "static_cms/patch/#{lib}"
8
+ require File.join('static_cms', 'patch', lib)
6
9
  }
7
10
  end
@@ -0,0 +1,31 @@
1
+ class ::StaticCms::Site
2
+ require 'yaml'
3
+ require 'ruby_patch'
4
+ extend ::RubyPatch::AutoLoad
5
+
6
+ CONFIG_DEFAULT = {
7
+ 'lang' => 'en',
8
+ 'www' => nil,
9
+ 'dir' => File.join(__DIR__, 'root'),
10
+ 'author' => '',
11
+ 'robots' => 'noarchive',
12
+ 'favicon' => 'images/favicon.png',
13
+ 'charset' => 'utf-8',
14
+ }
15
+
16
+ attr_accessor :page_alist
17
+ attr_accessor *(CONFIG_DEFAULT.keys)
18
+
19
+ def initialize
20
+ config = CONFIG_DEFAULT.merge(YAML.load_file('config.yaml'))
21
+ @www = config.delete('www')
22
+ @www += '/' unless @www[-1] == '/'
23
+ config.each{|k, v| instance_variable_set("@#{k}", v)}
24
+ @page_alist = Hash.new{|h, k| raise KeyError, k}
25
+ YAML.load_file('pages.yaml')\
26
+ .sort\
27
+ .each{|name, params|
28
+ @page_alist[name] = ::StaticCms::Page.new(self, name, params)
29
+ }
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module ::StaticCms
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
data/lib/static_cms.rb CHANGED
@@ -1,251 +1,7 @@
1
1
  module StaticCms
2
2
  require 'fileutils'
3
- require 'ruby_patch'
4
-
5
3
  require 'static_cms/version'
6
4
  require 'static_cms/patch'
7
-
8
- SITE_TEMPLATE_DIR = File.join(__DIR__, '../data/site_template')
9
-
10
- module Command
11
- def self.init(dir = '.')
12
- FileUtils.mkdir_p(dir)
13
- success = false
14
- FileUtils.cd(dir){|dir|
15
- success = system <<-EOS
16
- git init > #{File::NULL}
17
- git commit --allow-empty -m 'Empty commit.' > #{File::NULL}
18
- cp -r #{File.join(SITE_TEMPLATE_DIR, '*')} . > #{File::NULL}
19
- git add . > #{File::NULL}
20
- git commit -m 'Copied from site template.' > #{File::NULL}
21
- EOS
22
- }
23
-
24
- success
25
- end
26
-
27
- def self.update
28
- site_template_dir = File.join(__DIR__, '../data/site_template')
29
- FileUtils.cp_r(File.join(site_template_dir, 'templates'), '.')
30
- FileUtils.cp_r(File.join(site_template_dir, 'sources'), '.')
31
- FileUtils.cp_r(File.join(site_template_dir, 'statics'), '.')
32
- FileUtils.cp(File.join(site_template_dir, 'rakefile'), '.')
33
- end
34
- end
35
-
36
- class Site
37
- require 'yaml'
38
-
39
- CONFIG_DEFAULT = {
40
- 'lang' => 'en',
41
- 'www' => nil,
42
- 'dir' => File.join(__DIR__, 'root'),
43
- 'author' => '',
44
- 'robots' => 'noarchive',
45
- 'favicon' => 'images/favicon.png',
46
- 'charset' => 'utf-8',
47
- }
48
-
49
- attr_accessor :page_alist
50
- attr_accessor *(CONFIG_DEFAULT.keys)
51
-
52
- def initialize
53
- config = CONFIG_DEFAULT.merge(YAML.load_file('config.yaml'))
54
- @www = config.delete('www')
55
- @www += '/' unless @www[-1] == '/'
56
- config.each{|k, v| instance_variable_set("@#{k}", v)}
57
- @page_alist = Hash.new{|h, k| raise KeyError, k}
58
- YAML.load_file('pages.yaml')\
59
- .sort\
60
- .each{|name, params|
61
- @page_alist[name] = ::StaticCms::Page.new(self, name, params)
62
- }
63
- end
64
- end
65
-
66
- class Page
67
- require 'fileutils'
68
- require 'haml'
69
-
70
- PARAMS_DEFAULT ={
71
- 'visible' => true,
72
- 'title' => '',
73
- 'template' => 'default',
74
- 'articles' => [],
75
- 'statics' => [],
76
- 'sources' => [],
77
- 'base' => nil,
78
- }
79
-
80
- attr_accessor :site, :name, :dir, :template_file
81
- attr_accessor *(PARAMS_DEFAULT.keys)
82
-
83
- def initialize(site, name, params = {})
84
- @site = site
85
- @name = name
86
- @dir = File.join("root", @name)
87
- params = PARAMS_DEFAULT.merge(params)
88
- @template = params.delete('template')
89
- @template_file = File.join('templates', @template) + '.html.haml'
90
- @articles = params.delete('articles').map{|name| ::StaticCms::Article.use(self, name)}
91
- @statics = params.delete('statics').map{|file| File.join('statics', file)}
92
- @sources = params.delete('sources').map{|file| File.join('sources', file)}
93
- @base = params.delete('base') || @site.www
94
- params.each{|k, v|
95
- instance_variable_set("@#{k}", v)
96
- }
97
- end
98
-
99
- def generate
100
- FileUtils.mkdir_p(@dir)
101
-
102
- if @visible
103
- @statics.each{|file| cp_if_new(file)}
104
- @sources.each{|file| compile(file)}
105
- template_file = @template_file
106
- else
107
- @statics.each{|file| FileUtils.rm_rf(static_target(file))}
108
- @sources.each{|file| FileUtils.rm_rf(compile_target(file))}
109
- template_file = 'templates/moved.html.haml'
110
- end
111
-
112
- html = ::Haml::Engine.new(File.read(template_file)).render(self)
113
- target = File.join(@dir, 'index.html')
114
- open(target, 'w'){|io|
115
- io.write(html)
116
- io.flush
117
- io.fsync
118
- }
119
- end
120
-
121
- private
122
-
123
- def haml(text)
124
- ::Haml::Engine.new(text).render(self)
125
- end
126
-
127
- def nth_page_from_self(n)
128
- index = (@site.page_alist.to_a.index{|name, page| name == @name} + n)%@site.page_alist.size
129
- @site.page_alist.to_a[index][1]
130
- end
131
-
132
- def link_to_article(name)
133
- article = ::StaticCms::Article.alist[name]
134
- page = article.pages.first
135
- url = File.join(@site.www, page.name, "index.html\##{name}")
136
-
137
- "<a href='#{url}'>#{article.title}</a>"
138
- end
139
-
140
- def cp_if_new(source)
141
- target = static_target(source)
142
- if !File.file?(target) || File.mtime(source) > File.mtime(target)
143
- FileUtils.cp(source, target)
144
- FileUtils.chmod(0644, target)
145
- end
146
- end
147
-
148
- def compile(source)
149
- ext = File.extname(source)
150
- target = compile_target(source)
151
-
152
- case ext
153
- when /\A\.sass\z/
154
- system "sass #{source} > #{target}"
155
- else
156
- raise NotImplementedError, file
157
- end
158
- end
159
-
160
- def static_target(source)
161
- File.join(@dir, File.basename(source))
162
- end
163
-
164
- def compile_target(source)
165
- ext = File.extname(source)
166
- File.join(@dir, File.basename(source, ext))
167
- end
168
- end
169
-
170
- module Helpers
171
- require 'haml'
172
-
173
- EXT_TO_LANG = Hash.new(:general)\
174
- .merge({
175
- '.f90' => :fortran,
176
- '.f95' => :fortran,
177
- '.f03' => :fortran,
178
- '.f08' => :fortran,
179
- '.f' => :fortran,
180
- '.f77' => :fortran,
181
- '.rb' => :ruby,
182
- })
183
-
184
- module_function
185
-
186
- def to_plain(text)
187
- escaped_text = ::Haml::Helpers.html_escape(text)
188
- ::Haml::Helpers.preserve(escaped_text)
189
- end
190
-
191
- def to_code(text, lang = :general)
192
- pre_text = to_plain(text)
193
- "<pre class='code'><code class='#{lang}'>#{pre_text}</code></pre>"
194
- end
195
- end
196
-
197
- class Article
198
- @alist = Hash.new{|h, k| raise KeyError, k.to_s}
199
-
200
- def self.alist
201
- @alist
202
- end
203
-
204
- PARAMS_DEFAULT = {
205
- 'title' => '',
206
- }
207
-
208
- attr_accessor :pages, :name, :file, :data
209
- attr_accessor *(PARAMS_DEFAULT.keys)
210
-
211
- def initialize(name)
212
- @pages = []
213
- @name = name
214
- @file = File.join('articles', name) + '.html.yhaml'
215
- yaml, @data = File.read(@file).split(/^\.\.\.$/, 2)
216
- params = PARAMS_DEFAULT.merge(YAML.load(yaml))
217
- params.each{|k, v| instance_variable_set("@#{k}", v)}
218
- self.class.alist[@name] = self
219
- end
220
-
221
- def self.load
222
- `git ls-files articles`.split\
223
- .select{|f| f =~ /\.html\.yhaml\z/}\
224
- .map{|f| f.sub(/\Aarticles\//, '')}\
225
- .map{|f| File.basename(f, '.html.yhaml')}\
226
- .each{|name| self.new(name)}
227
- end
228
-
229
- def self.use(page, name)
230
- self.load unless self.alist.has_key?(name)
231
- raise ArgumentError, name unless self.alist.has_key?(name)
232
-
233
- article = self.alist[name]
234
- article.pages << page
235
- article
236
- end
237
- end
238
-
239
- require 'thor'
240
- class Runner < Thor
241
- desc "init [DIR]", "Initialize DIR directory for StaticCms [default: .]."
242
- def init(dir = '.')
243
- ::StaticCms::Command.init(dir)
244
- end
245
-
246
- desc "update", "Update templates, sources, statics (and more)."
247
- def update
248
- ::StaticCms::Command.update
249
- end
250
- end
5
+ require 'ruby_patch'
6
+ extend ::RubyPatch::AutoLoad
251
7
  end
data/test/test_init.rb CHANGED
@@ -4,6 +4,6 @@ class TestInitAndGenerate < ::MiniTest::Unit::TestCase
4
4
  require 'tmpdir'
5
5
 
6
6
  def test_init
7
- assert ::StaticCms::Command.init(Dir.mktmpdir)
7
+ assert ::StaticCms::Commands.init(Dir.mktmpdir)
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static_cms
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
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-10-03 00:00:00.000000000 Z
12
+ date: 2012-10-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: haml
16
- requirement: &69398750 !ruby/object:Gem::Requirement
16
+ requirement: &84273480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *69398750
24
+ version_requirements: *84273480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sass
27
- requirement: &69397340 !ruby/object:Gem::Requirement
27
+ requirement: &84272190 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.2'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *69397340
35
+ version_requirements: *84272190
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ruby_patch
38
- requirement: &69396770 !ruby/object:Gem::Requirement
38
+ requirement: &84270960 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '1.1'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *69396770
46
+ version_requirements: *84270960
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: thor
49
- requirement: &69396130 !ruby/object:Gem::Requirement
49
+ requirement: &84269260 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0.16'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *69396130
57
+ version_requirements: *84269260
58
58
  description: Simple static contents management system based on Haml and Sass.
59
59
  email:
60
60
  executables:
@@ -78,8 +78,14 @@ files:
78
78
  - data/site_template/templates/moved.html.haml
79
79
  - data/site_template/templates/nav.html.haml
80
80
  - lib/static_cms.rb
81
+ - lib/static_cms/application.rb
82
+ - lib/static_cms/article.rb
83
+ - lib/static_cms/commands.rb
84
+ - lib/static_cms/helpers.rb
85
+ - lib/static_cms/page.rb
81
86
  - lib/static_cms/patch.rb
82
87
  - lib/static_cms/patch/haml.rb
88
+ - lib/static_cms/site.rb
83
89
  - lib/static_cms/version.rb
84
90
  - rakefile
85
91
  - static_cms.gemspec