neruda 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 763f0127b37c30ad1d8bf6217259349364b5b0c3
4
- data.tar.gz: e1e24d906ba72ff2432b27d2bafb960dd068b281
3
+ metadata.gz: e7b04958f3e5482160f89ecc7fda88258c6fc521
4
+ data.tar.gz: 0b534ba550a1a09068e39b37ee7194e45a22c54b
5
5
  SHA512:
6
- metadata.gz: f5067b657dc79aa4cec67e5be77d6cf475e3ee29a83da58a0583f502ae2fc55b44072577b6b17e90df46effc2e59fe214b02927bf2827f093525e6451d2e9c95
7
- data.tar.gz: b1c4ffd1196f3c65267fca521bdb92a983888ec8b64d310262b7a4e437debd748334b9af16307609415cbc41062ad571d9cdd805c9b7671fcdf455e59bba43e1
6
+ metadata.gz: b16aa22a09ca945a513280104244b20298e9a2fb7d992d41ceb65479bd9307a8053778e804d5c0daae0371825e2b2f5762373bd0a34035f747a71894d21e35ad
7
+ data.tar.gz: 26992b799de7fcbada2dfc2bc1e0f624d13f806c77ebd43fa0c96595bce5b06a5eccee5c46051003a0d8f5955774066582128719b593f69610cb0079612a6eff
data/bin/pablo CHANGED
@@ -4,117 +4,107 @@
4
4
 
5
5
  require 'yaml'
6
6
  require 'rainbow'
7
+ require 'optparse'
7
8
  require 'fileutils'
8
9
 
9
- def help
10
- unless File.exist? 'config/config.yml'
11
- STDERR.puts 'Please init your website: ' +
12
- Rainbow('pablo init').yellow
13
- exit 1
10
+ # Methods used to play with bundler
11
+ module BundlerHelper
12
+ def bundler_is_present?
13
+ return false unless File.exist?('Gemfile')
14
+ ENV['PATH'].split(':').each do |folder|
15
+ return true if File.exist? File.join(folder, 'bundle')
16
+ end
17
+ false
14
18
  end
15
19
 
16
- options = ['start', 'stop', 'new', 'compile']
17
- options << 'capify' if File.exist? 'Capfile'
18
-
19
- STDERR.puts 'Usage: ' + Rainbow("pablo [ #{options.join(' | ')} ]").yellow
20
- end
21
-
22
- def bundler_is_present?
23
- ENV['PATH'].split(':').each do |folder|
24
- return true if File.exist? File.join(folder, 'bundle')
20
+ def rake_or_bundle_run(cmd)
21
+ cmd.insert(1, '-s') unless @options[:verbose]
22
+ cmd = ['bundle', 'exec'] + cmd if bundler_is_present?
23
+ puts Rainbow(cmd.join(' ')).blue if @options[:verbose]
24
+ exec(*cmd)
25
25
  end
26
- false
27
26
  end
28
27
 
29
- def capify
30
- unless File.exist? 'Capfile'
31
- STDERR.puts Rainbow('ERROR: Capfile does not exist.').red
32
- exit 1
28
+ # Methods used with pablo init method
29
+ module PabloInit
30
+ def init_path
31
+ FileUtils.mkdir_p 'config'
32
+ FileUtils.mkdir_p 'public'
33
+ FileUtils.mkdir_p 'tmp/pids'
34
+ FileUtils.mkdir_p 'private/characters'
35
+ FileUtils.mkdir_p 'private/sceneries'
36
+ FileUtils.mkdir_p 'private/chapters'
37
+ FileUtils.mkdir_p 'private/notes'
38
+ FileUtils.mkdir_p 'private/epubs'
33
39
  end
34
40
 
35
- capfile = File.new('Capfile', 'a')
36
- capfile.write <<~EOF
37
- neruda_spec = Gem::Specification.find_by_name 'neruda'
38
- Dir.glob("\#{neruda_spec.gem_dir}/lib/tasks/*.rake").each { |r| import r }
39
- Dir.glob("\#{neruda_spec.gem_dir}/lib/tasks/capistrano/*.rake").each { |r| import r }
40
- EOF
41
- end
42
-
43
- def _init_path
44
- FileUtils.mkdir_p 'config'
45
- FileUtils.mkdir_p 'public'
46
- FileUtils.mkdir_p 'tmp/pids'
47
- FileUtils.mkdir_p 'private/orgs'
48
- FileUtils.mkdir_p 'private/epubs'
49
- end
41
+ def init_config
42
+ config = {}
43
+ print 'Name of your website: '
44
+ config['title'] = STDIN.gets.strip
45
+ print 'Your author name: '
46
+ config['author'] = STDIN.gets.strip
47
+ print 'Main language of your website [en_US]: '
48
+ lang = STDIN.gets.strip
49
+ lang = 'en_US' if lang == ''
50
+ config['lang'] = lang
51
+
52
+ main_title = config['title'].tr(' ', '_').downcase.gsub(/[^a-z0-9_]/, '')
53
+ print "Filename of the main generated epub [#{main_title}]: "
54
+ epub = STDIN.gets.strip
55
+ if epub == ''
56
+ epub = main_title
57
+ else
58
+ epub = File.basename(epub, '.epub')
59
+ end
60
+ config['book_filename'] = epub
61
+ config['chapters'] = []
50
62
 
51
- def _init_config
52
- config = {}
53
- print 'Name of your website: '
54
- config['title'] = STDIN.gets.strip
55
- print 'Your author name: '
56
- config['author'] = STDIN.gets.strip
57
- print 'Main language of your website [en_US]: '
58
- lang = STDIN.gets.strip
59
- lang = 'en_US' if lang == ''
60
- config['lang'] = lang
61
-
62
- main_title = config['title'].tr(' ', '_').downcase.gsub(/[^a-z0-9_]/, '')
63
- print "Filename of the main generated epub [#{main_title}]: "
64
- epub = STDIN.gets.strip
65
- if epub == ''
66
- epub = main_title
67
- else
68
- epub = File.basename(epub, '.epub')
63
+ IO.write 'config/config.yml', config.to_yaml
69
64
  end
70
- config['book_filename'] = epub
71
- config['chapters'] = []
72
65
 
73
- IO.write 'config/config.yml', config.to_yaml
74
- end
66
+ def init_assets
67
+ unless Dir.exist? 'views'
68
+ FileUtils.cp_r File.join(__dir__, '../lib/assets'), 'views'
69
+ end
75
70
 
76
- def _init_assets
77
- unless Dir.exist? 'views'
78
- FileUtils.cp_r File.join(__dir__, '../lib/assets'), 'views'
71
+ return if File.exist? 'public/style.css'
72
+ FileUtils.mv 'views/style.css', 'public/style.css'
79
73
  end
80
74
 
81
- return if File.exist? 'public/style.css'
82
- FileUtils.mv 'views/style.css', 'public/style.css'
83
- end
84
-
85
- def _init_rackup
86
- unless File.exist? 'Rakefile'
87
- FileUtils.copy File.join(__dir__, '../docs/Rakefile.example'), 'Rakefile'
88
- end
75
+ def init_rackup
76
+ unless File.exist? 'Rakefile'
77
+ FileUtils.copy File.join(__dir__, '../docs/Rakefile.example'), 'Rakefile'
78
+ end
89
79
 
90
- return if File.exist? 'config.ru'
91
- rackup_conf = <<~EOF
80
+ return if File.exist? 'config.ru'
81
+ rackup_conf = <<~EOF
92
82
  # frozen_string_literal: true
93
83
 
94
84
  require 'neruda'
95
85
  run Neruda::App
96
86
  EOF
97
- IO.write('config.ru', rackup_conf)
98
- end
87
+ IO.write('config.ru', rackup_conf)
88
+ end
99
89
 
100
- def _init_bundler
101
- return if File.exist? 'Gemfile'
102
- puts ''
103
- puts 'Bundler has been detected in your path.'
104
- # puts ''
105
- # print 'Do you want to use [o]rg files, [m]arkdown files or [b]oth' \
106
- # ' (default is org): '
107
- # markup = STDIN.gets.strip.downcase
108
- # if markup == 'm'
109
- # markup_gem = 'gem \'kramdown\''
110
- # elsif markup == 'b'
111
- # markup_gem = "gem 'kramdown'\ngem 'org-ruby'"
112
- # else
113
- # markup_gem = 'gem \'org-ruby\''
114
- # end
115
- markup_gem = 'gem \'org-ruby\''
116
-
117
- gemfile = <<~EOF
90
+ def init_bundler
91
+ return if File.exist? 'Gemfile'
92
+ puts ''
93
+ puts 'Bundler has been detected in your path.'
94
+ # puts ''
95
+ # print 'Do you want to use [o]rg files, [m]arkdown files or [b]oth' \
96
+ # ' (default is org): '
97
+ # markup = STDIN.gets.strip.downcase
98
+ # if markup == 'm'
99
+ # markup_gem = 'gem \'kramdown\''
100
+ # elsif markup == 'b'
101
+ # markup_gem = "gem 'kramdown'\ngem 'org-ruby'"
102
+ # else
103
+ # markup_gem = 'gem \'org-ruby\''
104
+ # end
105
+ markup_gem = 'gem \'org-ruby\''
106
+
107
+ gemfile = <<~EOF
118
108
  # frozen_string_literal: true
119
109
 
120
110
  source 'https://rubygems.org'
@@ -129,102 +119,159 @@ def _init_bundler
129
119
  gem 'rubocop'
130
120
  end
131
121
  EOF
132
- IO.write('Gemfile', gemfile)
133
- puts ''
134
- puts 'To complete this installation, you must now run: ' +
135
- Rainbow('bundle install').yellow
122
+ IO.write('Gemfile', gemfile)
123
+ puts ''
124
+ puts 'To complete this installation, you must now run: ' +
125
+ Rainbow('bundle install').yellow
126
+ end
136
127
  end
137
128
 
138
- def init
139
- puts Rainbow('Creating main folders…').blue
140
- _init_path
141
- puts Rainbow('Populating config file…').blue
142
- _init_config
143
- puts Rainbow('Copying template files…').blue
144
- _init_assets
145
- puts Rainbow('Installing rake files…').blue
146
- _init_rackup
147
- puts ''
148
- puts Rainbow('Neruda has been successfully installed.').green
149
- _init_bundler if bundler_is_present?
150
- end
129
+ # Main pablo class
130
+ class Pablo
131
+ def initialize
132
+ @options = {
133
+ verbose: false
134
+ }
151
135
 
152
- def create_new(title)
153
- if title == ''
154
- filename = 'new'
155
- else
156
- filename = title.tr(' ', '_').downcase.gsub(/[^a-z0-9_]/, '')
157
- end
136
+ parse_options
158
137
 
159
- filename = "private/orgs/#{filename}.org"
138
+ if ARGV[0] == 'init'
139
+ pablo_init
160
140
 
161
- FileUtils.mkdir_p 'private/orgs' unless Dir.exist? 'private/orgs'
162
- config = YAML.load_file('config/config.yml')
163
- IO.write filename, <<~EOF
164
- #+title: #{title}
165
- #+date: <#{Date.today.strftime('%Y-%m-%d %a.')}>
166
- #+author: #{config['author']}
141
+ elsif ARGV[0] == 'capify'
142
+ capify
167
143
 
144
+ elsif ['start', 'run'].include?(ARGV[0])
145
+ start
168
146
 
169
- EOF
147
+ elsif ARGV[0] == 'stop'
148
+ rake_or_bundle_run ['rake', 'sinatra:stop']
170
149
 
171
- editor = ENV['EDITOR'] || ENV['VISUAL'] || 'emacs'
172
- exec editor, filename
173
- end
150
+ elsif ARGV[0] == 'compile'
151
+ rake_or_bundle_run ['rake', 'chapters:build_epubs']
152
+ # rake_or_bundle_run ['rake', 'book:make']
174
153
 
175
- if ARGV[0] == 'init'
176
- init
154
+ elsif ARGV[0] == 'new'
155
+ new_chapter
177
156
 
178
- elsif ARGV[0] == 'capify'
179
- capify
157
+ elsif ARGV[0] == 'list'
158
+ list
180
159
 
181
- elsif ['start', 'run'].include?(ARGV[0])
182
- loc_env = ENV['APP_ENV'] || 'development'
183
- if loc_env != 'production'
184
- puts Rainbow("Neruda is run in #{loc_env} environment").green
185
- else
186
- puts Rainbow('>> Neruda is run in production environment <<').yellow
160
+ elsif ARGV[0] == 'board'
161
+ tiddly_file = 'private/notes/tiddly.html'
162
+ unless File.exist? tiddly_file
163
+ FileUtils.copy File.join(__dir__, '../lib/tiddly.html'), tiddly_file
164
+ end
165
+ exec 'gio', 'open', tiddly_file
166
+ end
187
167
  end
188
- puts ''
189
- if bundler_is_present?
190
- puts Rainbow('bundle exec rake sinatra:start').blue
191
- exec 'bundle', 'exec', 'rake', 'sinatra:start'
192
- else
193
- puts Rainbow('rake sinatra:start').blue
194
- exec 'rake', 'sinatra:start'
168
+
169
+ private
170
+
171
+ def parse_options
172
+ optparse = OptionParser.new do |opts|
173
+ opts.banner = <<~EOF
174
+ Usage:
175
+ pablo init
176
+ pablo [options] #{pablo_banner} [params]
177
+
178
+ EOF
179
+ opts.on('-v', '--verbose', 'Output more information') do
180
+ @options[:verbose] = true
181
+ end
182
+ opts.on('-h', '--help', 'Display this screen') do
183
+ STDERR.puts opts
184
+ STDERR.puts <<~EOF
185
+
186
+ Commands:
187
+ init : Initialize your Neruda instance
188
+ start : Start the rack web server (default is thin)
189
+ stop : Stop the web server
190
+ compile: Generate the epubs files
191
+ list : List various book elements
192
+
193
+ Chapters related commands:
194
+ new [title] : Create a new chapter
195
+
196
+ EOF
197
+ exit
198
+ end
199
+ end
200
+
201
+ optparse.parse!
202
+
203
+ return if ARGV[0]
204
+ STDERR.puts Rainbow('ERROR: no command given. Run pablo -h for ' \
205
+ 'options and available commands').red
206
+ exit 1
195
207
  end
196
208
 
197
- elsif ARGV[0] == 'stop'
198
- if bundler_is_present?
199
- puts Rainbow('bundle exec rake sinatra:stop').blue
200
- exec 'bundle', 'exec', 'rake', 'sinatra:stop'
201
- else
202
- puts Rainbow('rake sinatra:stop').blue
203
- exec 'rake', 'sinatra:stop'
209
+ include BundlerHelper
210
+ include PabloInit
211
+
212
+ def pablo_banner
213
+ options = ['start', 'stop', 'new', 'compile', 'list']
214
+ options << 'capify' if File.exist? 'Capfile'
215
+
216
+ "[ #{options.join(' | ')} ]"
204
217
  end
205
218
 
206
- elsif ARGV[0] == 'compile'
207
- if bundler_is_present?
208
- puts Rainbow('bundle exec rake chapters:build_epubs').blue
209
- exec 'bundle', 'exec', 'rake', 'chapters:build_epubs'
210
- puts Rainbow('bundle exec rake chapters:make_book').blue
211
- exec 'bundle', 'exec', 'rake', 'chapters:make_book'
212
- else
213
- puts Rainbow('rake chapters:build_epubs').blue
214
- exec 'rake', 'chapters:build_epubs'
215
- puts Rainbow('rake chapters:make_book').blue
216
- exec 'rake', 'chapters:make_book'
219
+ def pablo_init
220
+ puts Rainbow('Creating main folders…').blue
221
+ init_path
222
+ puts Rainbow('Populating config file…').blue
223
+ init_config
224
+ puts Rainbow('Copying template files…').blue
225
+ init_assets
226
+ puts Rainbow('Installing rake files…').blue
227
+ init_rackup
228
+ puts ''
229
+ puts Rainbow('Neruda has been successfully installed.').green
230
+ init_bundler if bundler_is_present?
217
231
  end
218
232
 
219
- elsif ARGV[0] == 'new'
220
- if ARGV[1].nil?
221
- print 'Title: '
222
- title = STDIN.gets.strip
223
- else
224
- title = ARGV[1]
233
+ def capify
234
+ unless File.exist? 'Capfile'
235
+ STDERR.puts Rainbow('ERROR: Capfile does not exist.').red
236
+ exit 1
237
+ end
238
+
239
+ capfile = File.new('Capfile', 'a')
240
+ capfile.write <<~EOF
241
+ neruda_spec = Gem::Specification.find_by_name 'neruda'
242
+ Dir.glob("\#{neruda_spec.gem_dir}/lib/tasks/*.rake").each { |r| import r }
243
+ Dir.glob("\#{neruda_spec.gem_dir}/lib/tasks/capistrano/*.rake").each { |r| import r }
244
+ EOF
245
+ end
246
+
247
+ def start
248
+ loc_env = ENV['APP_ENV'] || 'development'
249
+ if loc_env != 'production'
250
+ puts Rainbow("Neruda is run in #{loc_env} environment").green
251
+ else
252
+ puts Rainbow('>> Neruda is run in production environment <<').yellow
253
+ end
254
+ puts ''
255
+ rake_or_bundle_run ['rake', 'sinatra:start']
225
256
  end
226
- create_new title
227
257
 
228
- else
229
- help
258
+ def new_chapter
259
+ if ARGV[1].nil?
260
+ print 'Title: '
261
+ title = STDIN.gets.strip
262
+ else
263
+ title = ARGV[1]
264
+ end
265
+ rake_or_bundle_run ['rake', "chapters:new[#{title}]"]
266
+ end
267
+
268
+ def list
269
+ if ARGV[1].nil?
270
+ STDERR.puts Rainbow('ERROR: No list type given.').red
271
+ exit 1
272
+ end
273
+ rake_or_bundle_run ['rake', "book:list[#{ARGV[1]}]"]
274
+ end
230
275
  end
276
+
277
+ Pablo.new
data/lib/neruda.rb CHANGED
@@ -72,7 +72,7 @@ class Neruda::App < Sinatra::Base
72
72
  get '/' do
73
73
  @slug = 'index'
74
74
  text_content = ''
75
- if File.exist? File.join('private', 'orgs', 'index.org')
75
+ if File.exist? File.join('private', 'index.org')
76
76
  find_chapter
77
77
  @content = Orgmode::Parser.load @org_file
78
78
  text_content = @content.to_html
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+ require 'rainbow'
5
+
6
+ namespace :book do
7
+ desc 'List various book elements'
8
+ task :list, :list_type do |t, args|
9
+ list_type = args[:list_type]
10
+ next unless ['chapters', 'characters', 'sceneries'].include?(list_type)
11
+ Dir.glob("private/#{list_type}/*.org") do |filename|
12
+ file_radix = File.basename(filename, '.org')
13
+ title = file_radix.split('_').map(&:capitalize).join(' ')
14
+ absolute_filename = File.join(Dir.pwd, filename)
15
+ puts Rainbow(title).blue + ": #{absolute_filename}"
16
+ end
17
+ end
18
+
19
+ desc 'Create the org file of the complete book'
20
+ task prepare: 'chapters:index' do
21
+ chapters = YAML.load_file('config/chapters.yml')
22
+ next if chapters.nil?
23
+
24
+ neruda_config = YAML.load_file('config/config.yml')
25
+ final_org = neruda_config['book_filename'] || 'all'
26
+ final_org = "tmp/#{final_org}.org"
27
+
28
+ Dir.mkdir 'tmp' unless Dir.exist? 'tmp'
29
+ File.unlink final_org if File.exist? final_org
30
+
31
+ org_file = File.open(final_org, 'a')
32
+ org_file.write("#+title: #{neruda_config['title']}\n")
33
+ org_file.write("#+author: #{neruda_config['author']}\n")
34
+ org_file.write("#+rights: #{neruda_config['license']}\n")
35
+ org_file.write("#+language: #{neruda_config['lang']}\n\n")
36
+
37
+ chapters.each do |c|
38
+ file_radix = c[:slug]
39
+ filename = "private/chapters/#{file_radix}.org"
40
+ next unless File.exist? filename
41
+ file_content = IO.read(filename)
42
+ file_content.gsub!(/^#\+date:.*$/mi, '')
43
+ file_content.gsub!(/^#\+author:.*$/mi, '')
44
+ file_content.gsub!(/^(\*+)\s+(.*)$/mi, '*\1 \2')
45
+ file_content.gsub!(/^#\+title:\s?(.*)$/mi, '* \1')
46
+
47
+ org_file.write(file_content + "\n")
48
+ end
49
+ org_file.close
50
+
51
+ IO.write('tmp/org_to_convert.yml', [final_org].to_yaml)
52
+ end
53
+
54
+ task make: ['book:prepare', 'chapters:convert_org']
55
+ end
@@ -24,7 +24,7 @@ namespace :chapters do
24
24
  end
25
25
 
26
26
  desc 'Upload the complete book'
27
- task upload_book: ['chapters:make_book', 'chapters:upload_epubs']
27
+ task upload_book: ['book:make', 'chapters:upload_epubs']
28
28
 
29
29
  namespace :purge do
30
30
  desc 'Remove remote orphaned epub files'
@@ -43,7 +43,7 @@ namespace :chapters do
43
43
  filename.delete!("\n")
44
44
  file_radix = File.basename(filename, '.epub')
45
45
  next if file_radix == final_org
46
- org_file = "private/orgs/#{file_radix}.org"
46
+ org_file = "private/chapters/#{file_radix}.org"
47
47
  unless test("[ -e '#{release_path}/#{org_file}' ]")
48
48
  execute :rm, filename
49
49
  end
@@ -9,7 +9,7 @@ namespace :sinatra do
9
9
  if test("[ -e '#{release_path}/tmp/pids/sinatra.pid' ]")
10
10
  execute :pkill, '-F', 'tmp/pids/sinatra.pid'
11
11
  end
12
- execute :rackup, '-E', fetch(:app_env),
12
+ execute :bundle, :exec, :rackup, '-E', fetch(:app_env),
13
13
  '-P', 'tmp/pids/sinatra.pid', '-D'
14
14
  end
15
15
  end
@@ -11,8 +11,7 @@ namespace :chapters do
11
11
  chapters = neruda_config['chapters']
12
12
  next unless chapters.any?
13
13
  chapters.each do |file_radix|
14
- next if file_radix == 'index'
15
- filename = "private/orgs/#{file_radix}.org"
14
+ filename = "private/chapters/#{file_radix}.org"
16
15
  org_to_convert << filename
17
16
  end
18
17
  next if org_to_convert.empty?
@@ -44,7 +43,6 @@ namespace :chapters do
44
43
  epub_to_upload = []
45
44
  org_to_convert.each do |filename|
46
45
  file_radix = File.basename(filename, '.org')
47
- next if file_radix == 'index'
48
46
  epub_file = "private/epubs/#{file_radix}.epub"
49
47
  epub_to_upload << epub_file
50
48
  sh 'pandoc', '-S', "-o #{epub_file}", filename
@@ -66,7 +64,7 @@ namespace :chapters do
66
64
  neruda_config = YAML.load_file('config/config.yml')
67
65
  final_org = neruda_config['book_filename'] || 'all'
68
66
  chapters = neruda_config['chapters']
69
- Dir.glob('private/orgs/*.org') do |filename|
67
+ Dir.glob('private/chapters/*.org') do |filename|
70
68
  file_radix = File.basename(filename, '.org')
71
69
  unless chapters.include? file_radix
72
70
  STDERR.puts "WARNING: #{filename} exists but #{file_radix} " \
@@ -77,7 +75,7 @@ namespace :chapters do
77
75
  Dir.glob('private/epubs/*.epub') do |filename|
78
76
  file_radix = File.basename(filename, '.epub')
79
77
  next if file_radix == final_org
80
- org_file = "private/orgs/#{file_radix}.org"
78
+ org_file = "private/chapters/#{file_radix}.org"
81
79
  File.unlink filename unless File.exist? org_file
82
80
  end
83
81
  end
@@ -89,7 +87,7 @@ namespace :chapters do
89
87
  next if neruda_config['chapters'].nil?
90
88
  chapters = []
91
89
  neruda_config['chapters'].each do |file_radix|
92
- filename = "private/orgs/#{file_radix}.org"
90
+ filename = "private/chapters/#{file_radix}.org"
93
91
  next unless File.exist? filename
94
92
  f = Orgmode::Parser.load filename
95
93
  title = f.in_buffer_settings['TITLE']
@@ -98,40 +96,32 @@ namespace :chapters do
98
96
  IO.write('config/chapters.yml', chapters.to_yaml)
99
97
  end
100
98
 
101
- desc 'Create the org file of the complete book'
102
- task prepare_book: 'chapters:index' do
103
- chapters = YAML.load_file('config/chapters.yml')
104
- next if chapters.nil?
105
-
106
- neruda_config = YAML.load_file('config/config.yml')
107
- final_org = neruda_config['book_filename'] || 'all'
108
- final_org = "tmp/#{final_org}.org"
99
+ desc 'Open an editor to create a new chapter'
100
+ task :new, :title do |_, args|
101
+ if args[:title] == ''
102
+ filename = 'new'
103
+ else
104
+ filename = args[:title].tr(' ', '_').downcase.gsub(/[^a-z0-9_]/, '')
105
+ end
109
106
 
110
- Dir.mkdir 'tmp' unless Dir.exist? 'tmp'
111
- File.unlink final_org if File.exist? final_org
107
+ filename = "private/chapters/#{filename}.org"
112
108
 
113
- org_file = File.open(final_org, 'a')
114
- org_file.write("#+title: #{neruda_config['title']}\n")
115
- org_file.write("#+author: #{neruda_config['author']}\n")
116
- org_file.write("#+rights: #{neruda_config['license']}\n")
117
- org_file.write("#+language: #{neruda_config['lang']}\n\n")
109
+ unless File.exist? filename
110
+ config = YAML.load_file('config/config.yml')
111
+ IO.write filename, <<~EOF
112
+ #+title: #{args[:title]}
113
+ #+date: <#{Date.today.strftime('%Y-%m-%d %a.')}>
114
+ #+author: #{config['author']}
118
115
 
119
- chapters.each do |c|
120
- file_radix = c[:slug]
121
- filename = "private/orgs/#{file_radix}.org"
122
- next unless File.exist? filename
123
- file_content = IO.read(filename)
124
- file_content.gsub!(/^#\+date:.*$/mi, '')
125
- file_content.gsub!(/^#\+author:.*$/mi, '')
126
- file_content.gsub!(/^(\*+)\s+(.*)$/mi, '*\1 \2')
127
- file_content.gsub!(/^#\+title:\s?(.*)$/mi, '* \1')
128
116
 
129
- org_file.write(file_content + "\n")
117
+ EOF
130
118
  end
131
- org_file.close
132
119
 
133
- IO.write('tmp/org_to_convert.yml', [final_org].to_yaml)
120
+ editor = ENV['EDITOR'] || ENV['VISUAL'] || 'emacs'
121
+ if editor.match?(/^emacs/)
122
+ sh editor, '+5', filename
123
+ else
124
+ sh editor, filename
125
+ end
134
126
  end
135
-
136
- task make_book: ['chapters:prepare_book', 'chapters:convert_org']
137
127
  end
@@ -21,7 +21,7 @@ namespace :sinatra do
21
21
  cmd = ['rackup', "-E #{loc_env}", '-P', 'tmp/pids/neruda.pid']
22
22
  cmd << '-D' if loc_env == 'production'
23
23
  begin
24
- sh cmd.join(' ')
24
+ sh(*cmd)
25
25
  rescue Interrupt
26
26
  puts Rainbow(' Kthxbye').blue
27
27
  end