retter 0.2.5 → 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -11
  3. data/.travis.yml +4 -2
  4. data/ChangeLog.md +11 -5
  5. data/Gemfile +4 -1
  6. data/LICENSE.txt +22 -0
  7. data/README.md +163 -246
  8. data/Rakefile +25 -5
  9. data/bin/retter +2 -22
  10. data/lib/retter.rb +57 -43
  11. data/lib/retter/cli.rb +46 -0
  12. data/lib/retter/cli/edit.rb +33 -0
  13. data/lib/retter/cli/hooks.rb +46 -0
  14. data/lib/retter/cli/list.rb +15 -0
  15. data/lib/retter/cli/new.rb +20 -0
  16. data/lib/retter/cli/preview.rb +9 -0
  17. data/lib/retter/cli/publish.rb +22 -0
  18. data/lib/retter/deprecated.rb +160 -0
  19. data/lib/retter/entry.rb +31 -106
  20. data/lib/retter/entry/article.rb +36 -0
  21. data/lib/retter/entry/find_methods.rb +38 -0
  22. data/lib/retter/entry/model_base.rb +10 -0
  23. data/lib/retter/entry/pagination.rb +23 -0
  24. data/lib/retter/entry/sort_methods.rb +7 -0
  25. data/lib/retter/entry/utils.rb +12 -0
  26. data/lib/retter/initializing.rb +45 -0
  27. data/lib/retter/repository.rb +99 -12
  28. data/lib/retter/retterfile.rb +67 -0
  29. data/lib/retter/retterfile/context.rb +57 -0
  30. data/lib/retter/static_site.rb +34 -0
  31. data/lib/retter/static_site/app.rb +13 -0
  32. data/lib/retter/static_site/app/application.rb +94 -0
  33. data/lib/retter/{generator/skel/entries → static_site/app/assets/images}/.gitkeep +0 -0
  34. data/lib/retter/{generator/skel → static_site/app/assets}/images/orange/bg_body.jpg +0 -0
  35. data/lib/retter/{generator/skel → static_site/app/assets}/images/orange/bg_entry.jpg +0 -0
  36. data/lib/retter/{generator/skel → static_site/app/assets}/images/orange/bg_header.png +0 -0
  37. data/lib/retter/{generator/skel → static_site/app/assets}/images/orange/ic_li01.gif +0 -0
  38. data/lib/retter/{generator/skel/images → static_site/app/assets/javascripts}/.gitkeep +0 -0
  39. data/lib/retter/static_site/app/assets/stylesheets/application.css.scss +3 -0
  40. data/lib/retter/{generator/skel/stylesheets/base.css → static_site/app/assets/stylesheets/base.css.scss} +0 -0
  41. data/lib/retter/{generator/skel/stylesheets/pygments.css → static_site/app/assets/stylesheets/highlight.css.scss} +0 -0
  42. data/lib/retter/static_site/app/assets/stylesheets/orange.css.scss +262 -0
  43. data/lib/retter/static_site/app/assets/stylesheets/retter.css.scss +198 -0
  44. data/lib/retter/static_site/app/config.ru +7 -0
  45. data/lib/retter/static_site/app/controllers.rb +113 -0
  46. data/lib/retter/static_site/app/helpers.rb +36 -0
  47. data/lib/retter/{generator/skel/layouts/profile.html.haml → static_site/app/templates/about/show.html.haml} +0 -0
  48. data/lib/retter/static_site/app/templates/entries/articles/show.html.haml +7 -0
  49. data/lib/retter/static_site/app/templates/entries/index.html.haml +13 -0
  50. data/lib/retter/static_site/app/templates/entries/index.rss.haml +24 -0
  51. data/lib/retter/static_site/app/templates/entries/show.html.haml +11 -0
  52. data/lib/retter/static_site/app/templates/index/show.html.haml +14 -0
  53. data/lib/retter/static_site/app/templates/layouts/application.html.haml +34 -0
  54. data/lib/retter/static_site/builder.rb +106 -0
  55. data/lib/retter/static_site/cli.rb +25 -0
  56. data/lib/retter/static_site/cli/build.rb +85 -0
  57. data/lib/retter/static_site/cli/edit.rb +17 -0
  58. data/lib/retter/static_site/cli/migrate.rb +93 -0
  59. data/lib/retter/static_site/cli/new.rb +50 -0
  60. data/lib/retter/static_site/cli/preview.rb +66 -0
  61. data/lib/retter/static_site/markdown.rb +25 -0
  62. data/lib/retter/static_site/markdown/code_ray_renderer.rb +13 -0
  63. data/lib/retter/static_site/markdown/pygments_renderer.rb +18 -0
  64. data/lib/retter/static_site/markdown_entry.rb +111 -0
  65. data/lib/retter/static_site/monkey/sprockets_task.rb +10 -0
  66. data/lib/retter/version.rb +1 -1
  67. data/retter.gemspec +59 -43
  68. data/skel/.gitignore +2 -0
  69. data/skel/Retterfile +16 -0
  70. data/skel/config.ru +22 -0
  71. data/{lib/retter/generator/skel/javascripts → skel/retters}/.gitkeep +0 -0
  72. data/skel/retters/today.md +3 -0
  73. data/spec/cli/build_spec.rb +87 -0
  74. data/spec/cli/edit_spec.rb +76 -0
  75. data/spec/cli/invocation_spec.rb +19 -0
  76. data/spec/cli/list_spec.rb +64 -0
  77. data/spec/cli/migrate_spec.rb +65 -0
  78. data/spec/cli/new_spec.rb +30 -0
  79. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/.gitignore +0 -0
  80. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/Retterfile +6 -3
  81. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/config.ru +0 -0
  82. data/{lib/retter/generator/skel/retters/.gitkeep → spec/fixtures/sites/site-0.2.5/entries.html} +0 -0
  83. data/spec/fixtures/sites/site-0.2.5/entries.rss +0 -0
  84. data/spec/fixtures/sites/site-0.2.5/entries/.gitkeep +0 -0
  85. data/spec/fixtures/sites/site-0.2.5/images/.gitkeep +0 -0
  86. data/spec/fixtures/sites/site-0.2.5/images/orange/bg_body.jpg +0 -0
  87. data/spec/fixtures/sites/site-0.2.5/images/orange/bg_entry.jpg +0 -0
  88. data/spec/fixtures/sites/site-0.2.5/images/orange/bg_header.png +0 -0
  89. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/index.html +0 -0
  90. data/spec/fixtures/sites/site-0.2.5/javascripts/.gitkeep +0 -0
  91. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/layouts/article.html.haml +0 -0
  92. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/layouts/entries.html.haml +0 -0
  93. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/layouts/entry.html.haml +0 -0
  94. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/layouts/index.html.haml +0 -0
  95. data/spec/fixtures/sites/site-0.2.5/layouts/profile.html.haml +7 -0
  96. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/layouts/retter.html.haml +0 -0
  97. data/spec/fixtures/sites/site-0.2.5/profile.html +0 -0
  98. data/spec/fixtures/sites/site-0.2.5/retters/.gitkeep +0 -0
  99. data/spec/fixtures/sites/site-0.2.5/stylesheets/base.css +22 -0
  100. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/stylesheets/orange.css +0 -0
  101. data/spec/fixtures/sites/site-0.2.5/stylesheets/pygments.css +288 -0
  102. data/{lib/retter/generator/skel → spec/fixtures/sites/site-0.2.5}/stylesheets/retter.css +0 -0
  103. data/spec/retter/deprecated_spec.rb +106 -0
  104. data/spec/retter/initializing_spec.rb +86 -0
  105. data/spec/retter/retterfile_spec.rb +32 -0
  106. data/spec/spec_helper.rb +38 -27
  107. data/spec/support/example_helper.rb +46 -0
  108. data/spec/support/matchers.rb +14 -0
  109. data/spec/support/test_site.rb +59 -0
  110. metadata +329 -236
  111. data/lib/retter/binder.rb +0 -50
  112. data/lib/retter/command.rb +0 -192
  113. data/lib/retter/config.rb +0 -96
  114. data/lib/retter/configurable.rb +0 -26
  115. data/lib/retter/entries.rb +0 -115
  116. data/lib/retter/generator.rb +0 -7
  117. data/lib/retter/generator/base.rb +0 -65
  118. data/lib/retter/generator/creator.rb +0 -34
  119. data/lib/retter/generator/skel/Gemfile +0 -5
  120. data/lib/retter/markdown.rb +0 -31
  121. data/lib/retter/markdown/code_ray_renderer.rb +0 -11
  122. data/lib/retter/markdown/pygments_renderer.rb +0 -16
  123. data/lib/retter/page.rb +0 -43
  124. data/lib/retter/page/article.rb +0 -54
  125. data/lib/retter/page/base.rb +0 -97
  126. data/lib/retter/page/entries.rb +0 -17
  127. data/lib/retter/page/entry.rb +0 -46
  128. data/lib/retter/page/feed.rb +0 -63
  129. data/lib/retter/page/index.rb +0 -17
  130. data/lib/retter/page/profile.rb +0 -17
  131. data/lib/retter/page/view_helper.rb +0 -66
  132. data/lib/retter/preprint.rb +0 -34
  133. data/spec/command/callback_spec.rb +0 -27
  134. data/spec/command/clean_spec.rb +0 -20
  135. data/spec/command/commit_spec.rb +0 -40
  136. data/spec/command/edit_spec.rb +0 -119
  137. data/spec/command/list_spec.rb +0 -42
  138. data/spec/command/open_spec.rb +0 -12
  139. data/spec/command/preview_spec.rb +0 -40
  140. data/spec/command/rebind_spec.rb +0 -276
  141. data/spec/support/example_group_helper.rb +0 -98
@@ -0,0 +1,106 @@
1
+ require 'fileutils'
2
+ require 'pathname'
3
+ require 'rails/console/app'
4
+ require 'rake'
5
+ require 'retter/static_site/monkey/sprockets_task'
6
+
7
+ module Retter
8
+ module StaticSite
9
+ module Builder
10
+ class Base
11
+ class << self
12
+ def build(*args)
13
+ builder = new(*args)
14
+
15
+ builder.build_wait
16
+ end
17
+ end
18
+
19
+ def initialize_application
20
+ ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'production'
21
+
22
+ Retter::StaticSite::App::Application.initialize!
23
+ end
24
+
25
+ def build_wait
26
+ pid = fork { build }
27
+ _, status = Process.waitpid2(pid)
28
+
29
+ at_exit do
30
+ Process.kill :TERM, pid rescue nil # avoid orphan
31
+ end
32
+
33
+ status.success?
34
+ end
35
+
36
+ def build
37
+ raise NotImplementedError
38
+ end
39
+ end
40
+
41
+ class Assets < Base
42
+ def build
43
+ initialize_application
44
+
45
+ Retter::StaticSite::App::Application.load_tasks
46
+
47
+ Rake::Task['assets:precompile'].invoke
48
+ end
49
+ end
50
+
51
+ class Pages < Base
52
+ attr_reader :dest_path
53
+
54
+ def initialize(dest_path)
55
+ @dest_path = dest_path
56
+ end
57
+
58
+ def build
59
+ initialize_application
60
+ extend Rails::ConsoleMethods
61
+
62
+ urls.each do |url|
63
+ write fetch(url), dest_path.join(url[1..-1])
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def urls
70
+ html_page = {format: 'html'}
71
+
72
+ static_pages = [
73
+ app.retter_index_path(html_page),
74
+ app.retter_about_path(html_page),
75
+ app.retter_entries_path(html_page),
76
+ app.retter_entries_path(format: 'rss')
77
+ ]
78
+
79
+ entry_pages = Retter::Entry.all.map {|entry| app.retter_entry_path(entry, html_page) }
80
+ article_pages = Retter::Entry::Article.all.map {|article| app.retter_entry_article_path(article.entry, article, html_page) }
81
+
82
+ static_pages + entry_pages + article_pages
83
+ end
84
+
85
+ def fetch(url, retry_limit = 10)
86
+ app.get url
87
+ response = app.response
88
+
89
+ if response.redirect?
90
+ raise 'Redirect loop is too deep' if retry_limit <= 0
91
+
92
+ return fetch(response.redirect_url, retry_limit.pred)
93
+ end
94
+
95
+ response.body
96
+ end
97
+
98
+ def write(body, dest)
99
+ FileUtils.mkdir_p dest.dirname
100
+
101
+ File.binwrite dest, body
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,25 @@
1
+ require 'retter/cli'
2
+
3
+ module Retter
4
+ module StaticSite
5
+ module CLI
6
+ autoload :Build, 'retter/static_site/cli/build'
7
+ autoload :Edit, 'retter/static_site/cli/edit'
8
+ autoload :Migrate, 'retter/static_site/cli/migrate'
9
+ autoload :New, 'retter/static_site/cli/new'
10
+ autoload :Preview, 'retter/static_site/cli/preview'
11
+
12
+ module Command
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+ register Build, 'build', 'build', 'Build all entries'
17
+ register Edit, 'edit', 'edit [KEYOWRD]', 'Open an entry with $EDITOR'
18
+ register Migrate, 'migrate', 'migrate', 'Migrate site for retter current version'
19
+ register New, 'new', 'new NAME', 'Create a new site'
20
+ register Preview, 'preview', 'preview [PATH][--livereload=false]', 'Preview on browser w/ livereload'
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,85 @@
1
+ module Retter
2
+ module StaticSite
3
+ class CLI::Build < Thor::Group
4
+ class_attribute :root_path, :source_path, :build_path, :source_branch, :publish_branch
5
+
6
+ include Retter::CLI::Hooks
7
+
8
+ def build
9
+ if wip_entry = Entry.find(&:wip?)
10
+ wip_entry.commit!
11
+ end
12
+
13
+ Repository.checkout source_branch do
14
+ say_status :commit, "source on #{source_branch}"
15
+ commit_to_source_branch
16
+
17
+ ensuring_working_path do
18
+ say_status :build, 'assets'
19
+ StaticSite::Builder::Assets.build
20
+
21
+ say_status :build, 'pages'
22
+ StaticSite::Builder::Pages.build build_path.join('public')
23
+
24
+ say_status :commit, "files on #{publish_branch}"
25
+ commit_to_publish_branch
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def ensuring_working_path
33
+ FileUtils.mkdir_p build_path.join('public')
34
+ FileUtils.cp_r source_path.join('assets'), build_path
35
+
36
+ yield
37
+ ensure
38
+ FileUtils.rm_r build_path if build_path.exist?
39
+ end
40
+
41
+ def commit_to_source_branch
42
+ Repository.checkout source_branch do |repo|
43
+ repo.add source_path
44
+ repo.add '-u', source_path
45
+ repo.commit '-m', 'Update source'
46
+ end
47
+ end
48
+
49
+ def commit_to_publish_branch
50
+ Repository.checkout publish_branch do |repo|
51
+ if working_on_same_branch?
52
+ %w(assets entries).each do |path|
53
+ next unless File.exist?(path)
54
+
55
+ FileUtils.rm_r path
56
+ repo.add '-u', path
57
+ end
58
+ end
59
+
60
+ repo.checkout 'master', 'Retterfile'
61
+ FileUtils.cp_r Dir.glob(build_path.join('public/*')), Retter.root
62
+
63
+ repo.add '-A'
64
+ repo.commit '-m', 'Build'
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def working_on_same_branch?
71
+ publish_branch.to_s == source_branch.to_s
72
+ end
73
+
74
+ Retter.on_initialize do |config|
75
+ self.root_path = Retter.root
76
+ self.source_path = config.source_path
77
+ self.build_path = config.build_path
78
+ self.source_branch = config.source_branch
79
+ self.publish_branch = config.publish_branch
80
+ end
81
+
82
+ include Deprecated::CLI::Build
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,17 @@
1
+ module Retter
2
+ module StaticSite
3
+ class CLI::Edit < Retter::CLI::Edit
4
+ class_attribute :source_branch
5
+
6
+ def edit
7
+ Repository.checkout source_branch do
8
+ super
9
+ end
10
+ end
11
+
12
+ Retter.on_initialize do |config|
13
+ self.source_branch = config.source_branch
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,93 @@
1
+ module Retter
2
+ module StaticSite
3
+ class CLI::Migrate < Thor::Group
4
+ include Thor::Actions
5
+
6
+ class_attribute :root_path, :api_revision
7
+
8
+ MIGRATION_DIRS = {
9
+ 'retters' => 'source/retters'
10
+ }
11
+
12
+ MIGRATION_FILES = {
13
+ 'layouts/retter.html.haml' => 'source/templates/layouts/application.html.haml',
14
+ 'layouts/index.html.haml' => 'source/templates/index/show.html.haml',
15
+ 'layouts/profile.html.haml' => 'source/templates/about/show.html.haml',
16
+ 'layouts/entries.html.haml' => 'source/templates/entries/index.html.haml',
17
+ 'layouts/entry.html.haml' => 'source/templates/entries/show.html.haml',
18
+ 'layouts/article.html.haml' => 'source/templates/entries/articles/show.html.haml'
19
+ }
20
+
21
+ GSUB_FILES = [
22
+ ['source/templates/layouts/application.html.haml', /\#content\s*=\s*content/m, '#content= yield'],
23
+ ['source/templates/layouts/application.html.haml', /^!!!/, '!!! 5']
24
+ ]
25
+
26
+ REMOVING_FILES = %w(
27
+ retters layouts config.ru
28
+ )
29
+
30
+ def migrate
31
+ ensure_api_revision!
32
+
33
+ Dir.chdir root_path do
34
+ self.destination_root = root_path
35
+
36
+ MIGRATION_DIRS.each do |src, dest|
37
+ directory src, dest
38
+ end
39
+
40
+ MIGRATION_FILES.each do |src, dest|
41
+ copy_file src, dest
42
+ end
43
+
44
+ GSUB_FILES.each do |args|
45
+ gsub_file *args
46
+ end
47
+
48
+ REMOVING_FILES.each do |file|
49
+ remove_file file
50
+ end
51
+
52
+ copy_file 'config.ru'
53
+ create_file 'source/assets/.gitkeep', ''
54
+ create_link 'about.html', 'profile.html'
55
+
56
+ append_file 'Retterfile', <<-DATA.strip_heredoc
57
+ configure api_revision: #{Retter::API_REVISION} do |config|
58
+ # How to configure: https://github.com/hibariya/retter
59
+ end
60
+ DATA
61
+
62
+ say <<-MESSAGE.strip_heredoc
63
+ Migration finished.
64
+ Please commit all changes on #{root_path}.
65
+ And run `retter build` for generate html files.
66
+ MESSAGE
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def ensure_api_revision!
73
+ return if api_revision == 0
74
+
75
+ abort 'Nothing to do'
76
+ end
77
+
78
+ Retter.on_initialize do |config|
79
+ self.root_path = config.root
80
+ self.api_revision = config.api_revision
81
+
82
+ -> {
83
+ def self.source_paths
84
+ [
85
+ root_path,
86
+ File.expand_path('../../../../../skel', __FILE__)
87
+ ]
88
+ end
89
+ }
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,50 @@
1
+ require 'fileutils'
2
+ require 'pathname'
3
+
4
+ module Retter
5
+ module StaticSite
6
+ class CLI::New < Retter::CLI::New
7
+ ROOT_FILES = %w(.gitignore config.ru)
8
+ SOURCE_FILES = %w(
9
+ retters/.gitkeep
10
+ retters/today.md
11
+ assets/images/orange/bg_body.jpg
12
+ assets/images/orange/bg_entry.jpg
13
+ assets/images/orange/bg_header.png
14
+ assets/images/orange/ic_li01.gif
15
+ assets/stylesheets/application.css.scss
16
+ assets/stylesheets/base.css.scss
17
+ assets/stylesheets/highlight.css.scss
18
+ assets/stylesheets/orange.css.scss
19
+ assets/stylesheets/retter.css.scss
20
+ templates/about/show.html.haml
21
+ templates/entries/articles/show.html.haml
22
+ templates/entries/index.html.haml
23
+ templates/entries/show.html.haml
24
+ templates/index/show.html.haml
25
+ templates/layouts/application.html.haml
26
+ )
27
+
28
+ def self.source_paths
29
+ super << File.expand_path('../../app', __FILE__)
30
+ end
31
+
32
+ def install
33
+ Repository.new name do |repo|
34
+ repo.init
35
+
36
+ ROOT_FILES.each do |file|
37
+ copy_file file, %(#{name}/#{file})
38
+ end
39
+
40
+ SOURCE_FILES.each do |file|
41
+ copy_file file, %(#{name}/source/#{file})
42
+ end
43
+
44
+ repo.add '-A'
45
+ repo.commit '-m', 'Initial'
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,66 @@
1
+ require 'guard'
2
+ require 'launchy'
3
+ require 'net/http'
4
+ require 'rack'
5
+
6
+ module Retter
7
+ module StaticSite
8
+ class CLI::Preview < Retter::CLI::Preview
9
+ class_attribute :source_path, :source_branch
10
+
11
+ DEFAULT_ENTRY_POINT = '/entries/last_modified'
12
+
13
+ argument :entry_point, type: :string, required: false, default: DEFAULT_ENTRY_POINT, desc: 'The entry point to site'
14
+ class_option :port, type: :numeric, default: 9292
15
+ class_option :livereload, type: :boolean, default: true
16
+
17
+ def preview
18
+ Repository.checkout source_branch do
19
+ Retter.initialize!
20
+
21
+ invoke_browser_after_startup
22
+ invoke_livereload if options.livereload
23
+
24
+ config = File.join(File.dirname(__FILE__), '../app/config.ru')
25
+
26
+ Rack::Server.new(config: config, Port: options.port).start
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def invoke_browser_after_startup(limit = 30)
33
+ Thread.fork do
34
+ begin
35
+ Net::HTTP.start 'localhost', options.port do
36
+ Launchy.open %(http://localhost:#{options.port}#{entry_point})
37
+ end
38
+ rescue Launchy::Error
39
+ Launchy.open %(http://localhost:#{options.port}#{DEFAULT_ENTRY_POINT})
40
+ rescue
41
+ sleep 0.25
42
+
43
+ limit -= 1
44
+ retry if limit > 0
45
+ end
46
+ end
47
+ end
48
+
49
+ def invoke_livereload
50
+ Thread.fork do
51
+ Guard::UI.logger.level = :error
52
+ Guard.start watchdir: source_path.dirname.to_s, no_interactions: true, guardfile_contents: <<-EOS
53
+ guard 'livereload' do
54
+ watch %r{#{source_path.basename}/.+}
55
+ end
56
+ EOS
57
+ end
58
+ end
59
+
60
+ Retter.on_initialize do |config|
61
+ self.source_path = config.source_path
62
+ self.source_branch = config.source_branch
63
+ end
64
+ end
65
+ end
66
+ end