runeblog 0.0.45 → 0.0.48

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,106 @@
1
+ require 'find'
2
+ require 'yaml'
3
+ require 'livetext'
4
+
5
+ class RuneBlog
6
+ VERSION = "0.0.45"
7
+
8
+ Path = File.expand_path(File.join(File.dirname(__FILE__)))
9
+ DefaultData = Path + "/../data"
10
+
11
+ BlogHeaderPath = DefaultData + "/custom/blog_header.html"
12
+ BlogTrailerPath = DefaultData + "/custom/blog_trailer.html"
13
+
14
+ BlogHeader = File.read(BlogHeaderPath) rescue "not found"
15
+ BlogTrailer = File.read(BlogTrailerPath) rescue "not found"
16
+ end
17
+
18
+ # Continued below...
19
+
20
+ class RuneBlog::Config
21
+ attr_reader :root, :views, :view, :sequence
22
+ attr_writer :view # FIXME
23
+
24
+ def self.create(cfg_file = ".blog")
25
+ RuneBlog.create_new_blog
26
+ end
27
+
28
+ def initialize(cfg_file = ".blog")
29
+ # What views are there? Deployment, etc.
30
+ # Crude - FIXME later
31
+
32
+ lines = File.readlines(cfg_file).map {|x| x.chomp }
33
+ @root = lines[0]
34
+ @view = lines[1]
35
+ dirs = Dir.entries("#@root/views/") - %w[. ..]
36
+ dirs.reject! {|x| ! File.directory?("#@root/views/#{x}") }
37
+ @root = root
38
+ @views = dirs
39
+ @sequence = File.read(root + "/sequence").to_i
40
+ end
41
+
42
+ def next_sequence
43
+ @sequence += 1
44
+ File.open("#@root/sequence", "w") {|f| f.puts @sequence }
45
+ @sequence
46
+ end
47
+
48
+ def viewdir(v)
49
+ @root + "/views/#{v}/"
50
+ end
51
+
52
+ end
53
+
54
+ # Reopening...
55
+
56
+ class RuneBlog
57
+
58
+ def self.exist?
59
+ File.exist?(".blog")
60
+ end
61
+
62
+ def get_config
63
+ end
64
+
65
+ # def initialize
66
+ # # ?
67
+ # end
68
+
69
+ def self.create_new_blog
70
+ #-- what if data already exists?
71
+ result = system("cp -r #{RuneBlog::DefaultData} .")
72
+ raise "Error copying default data" unless result
73
+
74
+ File.open(".blog", "w") do |f|
75
+ f.puts "data"
76
+ f.puts "no_default"
77
+ end
78
+ File.open("data/VERSION", "a") {|f| f.puts "\nBlog created: " + Time.now.to_s }
79
+ end
80
+
81
+ # def self.open_blog
82
+ # end
83
+
84
+ # def self.create_new_post(title, date, view)
85
+ # @template = <<-EOS
86
+ # .mixin liveblog
87
+ #
88
+ # .title #{title}
89
+ # .pubdate #{date}
90
+ # .views #{view}
91
+ #
92
+ # .teaser
93
+ # Teaser goes here.
94
+ # .end
95
+ # Remainder of post goes here.
96
+ # EOS
97
+ #
98
+ # @slug = make_slug(title)
99
+ # @fname = @slug + ".lt3"
100
+ # File.open("#@root/src/#@fname", "w") {|f| f.puts @template }
101
+ # @fname
102
+ # rescue => err
103
+ # error(err, __LINE__, __FILE__)
104
+ # end
105
+
106
+ end
data/lib/repl.rb CHANGED
@@ -2,39 +2,31 @@ require 'runeblog'
2
2
 
3
3
  require 'ostruct'
4
4
 
5
- module RuneBlog::REPL
6
-
7
- def clear
8
- puts "\e[H\e[2J" # clear screen
9
- end
10
-
11
- def red(text)
12
- "\e[31m#{text}\e[0m"
13
- end
14
-
15
- def blue(text)
16
- "\e[34m#{text}\e[0m"
17
- end
18
-
19
- def bold(str)
20
- "\e[1m#{str}\e[22m"
21
- end
22
-
23
- def interpolate(str)
24
- wrap = "<<-EOS\n#{str}\nEOS"
25
- eval wrap
26
- end
5
+ =begin
6
+ Instance vars in original code:
7
+ @bloghead
8
+ @blogtail
9
+ @config
10
+ @date
11
+ @deploy
12
+ @fname
13
+ @main
14
+ @meta
15
+ @root
16
+ @sequence
17
+ @slug
18
+ @template
19
+ @title
20
+ @today
21
+ @view
22
+ =end
27
23
 
28
- def colored_slug(slug)
29
- red(slug[0..3])+blue(slug[4..-1])
30
- end
24
+ module RuneBlog::REPL
31
25
 
32
26
  ### error
33
27
 
34
- def error(err, line, file)
35
- str = "\n Error: (line #{line} of #{File.basename(file)}) "
36
- str << err.to_s
37
- puts red(str)
28
+ def error(err)
29
+ str = "\n Error: #{red(err)}"
38
30
  puts err.backtrace
39
31
  end
40
32
 
@@ -46,11 +38,17 @@ module RuneBlog::REPL
46
38
  STDIN.gets.chomp.send(meth)
47
39
  end
48
40
 
41
+ def yesno(prompt, meth = :to_s)
42
+ print prompt
43
+ STDOUT.flush
44
+ STDIN.gets.chomp.upcase[0] == "Y"
45
+ end
46
+
49
47
  ### quit
50
48
 
51
49
  def cmd_quit(arg)
52
50
  raise "Glitch: Got an argument" if arg != []
53
- puts
51
+ puts "\n "
54
52
  exit
55
53
  end
56
54
 
@@ -63,48 +61,36 @@ module RuneBlog::REPL
63
61
 
64
62
  ### new_blog!
65
63
 
66
- def cmd_new_blog!(arg)
64
+ def new_blog!(arg)
67
65
  raise "Glitch: Got an argument" if arg != []
68
- unless File.exist?(".blog")
69
- yn = ask(red(" No .blog found. Create new blog? "))
70
- if yn.upcase == "Y"
71
- #-- what if data already exists?
72
- result = system("cp -r #{RuneBlog::DefaultData} .")
73
- raise "Error copying default data" unless result
74
-
75
- File.open(".blog", "w") do |f|
76
- f.puts "data"
77
- f.puts "no_default"
78
- end
79
- File.open("data/VERSION", "a") {|f| f.puts "\nBlog created: " + Time.now.to_s }
80
- end
81
- end
66
+ return if RuneBlog.exist?
67
+ yn = yesno(red(" No .blog found. Create new blog? "))
68
+ RuneBlog.create_new_blog if yn
82
69
  rescue => err
83
- error(err, __LINE__, __FILE__)
70
+ error(err)
84
71
  end
85
72
 
86
73
  ### make_slug
87
74
 
75
+ # What to do with this?
76
+
88
77
  def make_slug(title, seq=nil)
89
- num = '%04d' % (seq || @config.next_sequence) # FIXME can do better
78
+ num = '%04d' % (seq || @blog.next_sequence) # FIXME can do better
90
79
  slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
91
80
  "#{num}-#{slug}"
92
81
  end
93
82
 
94
- ### read_config
95
-
96
- def read_config
97
- # Crude - FIXME later
98
- cfg_file = ".blog"
99
- new_blog! unless File.exist?(cfg_file)
100
- @config = RuneBlog::Config.new(cfg_file)
83
+ ### open_blog
101
84
 
102
- @view = @config.view # current view
103
- @sequence = @config.sequence
104
- @root = @config.root
105
- @config
85
+ def open_blog # Crude - FIXME later
86
+ # new_blog!([]) unless RuneBlog.exist?
87
+ @blog = RuneBlog.new
88
+ @view = @blog.view # current view
89
+ @sequence = @blog.sequence
90
+ @root = @blog.root
91
+ @blog
106
92
  rescue => err
107
- error(err, __LINE__, __FILE__)
93
+ error(err)
108
94
  end
109
95
 
110
96
  ### create_empty_post
@@ -128,7 +114,7 @@ Remainder of post goes here.
128
114
  File.open("#@root/src/#@fname", "w") {|f| f.puts @template }
129
115
  @fname
130
116
  rescue => err
131
- error(err, __LINE__, __FILE__)
117
+ error(err)
132
118
  end
133
119
 
134
120
  ### edit_initial_post
@@ -152,29 +138,29 @@ Remainder of post goes here.
152
138
  result = system("open 'http://#{server}/#{spath}'")
153
139
  raise "Problem opening http://#{server}/#{spath}" unless result
154
140
  rescue => err
155
- error(err, __LINE__, __FILE__)
141
+ error(err)
156
142
  end
157
143
 
158
144
  ### open_local
159
145
 
160
146
  def open_local
161
- result = system("open #{@config.viewdir(@view)}/index.html")
162
- raise "Problem opening #{@config.viewdir(@view)}/index.html" unless result
147
+ result = system("open #{@blog.viewdir(@view)}/index.html")
148
+ raise "Problem opening #{@blog.viewdir(@view)}/index.html" unless result
163
149
  rescue => err
164
- error(err, __LINE__, __FILE__)
150
+ error(err)
165
151
  end
166
152
 
167
153
  def cmd_deploy(arg)
168
154
  # TBD clunky FIXME
169
155
  raise "Glitch: Got an argument" if arg != []
170
156
  @deploy ||= {}
171
- deployment = @config.viewdir(@view) + "deploy"
157
+ deployment = @blog.viewdir(@view) + "deploy"
172
158
  raise "File '#{deployment}' not found" unless File.exist?(deployment)
173
159
 
174
160
  lines = File.readlines(deployment).map {|x| x.chomp }
175
161
  @deploy[@view] = lines
176
162
  user, server, sroot, spath = *lines
177
- vdir = @config.viewdir(@view)
163
+ vdir = @blog.viewdir(@view)
178
164
  files = ["#{vdir}/index.html"]
179
165
  files += Dir.entries(vdir).grep(/^\d\d\d\d/).map {|x| "#{vdir}/#{x}" }
180
166
  files.reject! {|f| File.mtime(f) < File.mtime("#{vdir}/last_deployed") }
@@ -195,7 +181,7 @@ Remainder of post goes here.
195
181
  File.write("#{vdir}/last_deployed", files)
196
182
  puts red("finished.")
197
183
  rescue => err
198
- error(err, __LINE__, __FILE__)
184
+ error(err)
199
185
  end
200
186
 
201
187
  ### process_post
@@ -207,11 +193,11 @@ Remainder of post goes here.
207
193
  @meta = @main.process_file(path, binding)
208
194
  raise "process_file returned nil" if @meta.nil?
209
195
 
210
- @meta.slug = make_slug(@meta.title, @config.sequence)
196
+ @meta.slug = make_slug(@meta.title, @blog.sequence)
211
197
  @meta.slug = file.sub(/.lt3$/, "")
212
198
  @meta
213
199
  rescue => err
214
- error(err, __LINE__, __FILE__)
200
+ error(err)
215
201
  end
216
202
 
217
203
  ### reload_post
@@ -223,7 +209,7 @@ Remainder of post goes here.
223
209
  @meta.slug = file.sub(/.lt3$/, "")
224
210
  @meta
225
211
  rescue => err
226
- error(err, __LINE__, __FILE__)
212
+ error(err)
227
213
  end
228
214
 
229
215
  ### posting
@@ -264,7 +250,7 @@ Remainder of post goes here.
264
250
  f.puts @blogtail
265
251
  end
266
252
  rescue => err
267
- error(err, __LINE__, __FILE__)
253
+ error(err)
268
254
  end
269
255
 
270
256
  ### create_dir
@@ -279,7 +265,7 @@ Remainder of post goes here.
279
265
 
280
266
  def link_post_view(view)
281
267
  # Create dir using slug (index.html, metadata?)
282
- vdir = @config.viewdir(view)
268
+ vdir = @blog.viewdir(view)
283
269
  dir = vdir + @meta.slug + "/"
284
270
  create_dir(dir + "assets")
285
271
  File.write("#{dir}/metadata.yaml", @meta.to_yaml)
@@ -288,7 +274,7 @@ Remainder of post goes here.
288
274
  File.write(dir + "index.html", post)
289
275
  generate_index(view)
290
276
  rescue => err
291
- error(err, __LINE__, __FILE__)
277
+ error(err)
292
278
  end
293
279
 
294
280
  ### find_asset
@@ -339,7 +325,7 @@ Remainder of post goes here.
339
325
  # assets = find_all_assets(@meta.assets, views)
340
326
  puts
341
327
  rescue => err
342
- error(err, __LINE__, __FILE__)
328
+ error(err)
343
329
  end
344
330
 
345
331
  ### rebuild_post
@@ -348,7 +334,7 @@ Remainder of post goes here.
348
334
  reload_post(file)
349
335
  publish_post(@meta) # FIXME ??
350
336
  rescue => err
351
- error(err, __LINE__, __FILE__)
337
+ error(err)
352
338
  end
353
339
 
354
340
  ### rebuild
@@ -361,16 +347,16 @@ Remainder of post goes here.
361
347
  files = files.sort.reverse
362
348
  files.each {|file| rebuild_post(file) }
363
349
  rescue => err
364
- error(err, __LINE__, __FILE__)
350
+ error(err)
365
351
  end
366
352
 
367
353
  ### relink
368
354
 
369
355
  def cmd_relink(arg)
370
356
  raise "Glitch: Got an argument" if arg != []
371
- @config.views.each {|view| generate_index(view) }
357
+ @blog.views.each {|view| generate_index(view) }
372
358
  rescue => err
373
- error(err, __LINE__, __FILE__)
359
+ error(err)
374
360
  end
375
361
 
376
362
  # ### publish?
@@ -383,12 +369,12 @@ Remainder of post goes here.
383
369
  ### list_views
384
370
 
385
371
  def cmd_list_views(arg)
386
- abort "Config file not read" unless @config
372
+ abort "Config file not read" unless @blog
387
373
  raise "Glitch: Got an argument" if arg != []
388
374
  puts
389
- @config.views.each {|v| puts " #{v}" }
375
+ @blog.views.each {|v| puts " #{v}" }
390
376
  rescue => err
391
- error(err, __LINE__, __FILE__)
377
+ error(err)
392
378
  end
393
379
 
394
380
  ### change_view
@@ -398,25 +384,25 @@ Remainder of post goes here.
398
384
  puts "\n #@view"
399
385
  else
400
386
  arg = arg.first
401
- list = @config.views.grep /^#{arg}/
387
+ list = @blog.views.grep /^#{arg}/
402
388
  if list.size == 1
403
- @view = @config.view = list.first
389
+ @view = @blog.view = list.first
404
390
  puts red("\n View: #{@view}") if arg != @view
405
391
  else
406
392
  puts "view #{arg.inspect} does not exist"
407
393
  end
408
394
  end
409
395
  rescue => err
410
- error(err, __LINE__, __FILE__)
396
+ error(err)
411
397
  end
412
398
 
413
399
  ### new_view
414
400
 
415
401
  def cmd_new_view(arg)
416
402
  arg = arg.first
417
- read_config unless @config
403
+ @blog ||= open_blog
418
404
  arg ||= ask("New view: ") # check validity later
419
- raise "view #{arg} already exists" if @config.views.include?(arg)
405
+ raise "view #{arg} already exists" if @blog.views.include?(arg)
420
406
 
421
407
  dir = @root + "/views/" + arg + "/"
422
408
  create_dir(dir + 'custom')
@@ -426,15 +412,16 @@ Remainder of post goes here.
426
412
  File.write(dir + "custom/blog_header.html", RuneBlog::BlogHeader)
427
413
  File.write(dir + "custom/blog_trailer.html", RuneBlog::BlogTrailer)
428
414
  File.write(dir + "last_deployed", "Initial creation")
429
- @config.views << arg
415
+ @blog.views << arg
430
416
  rescue => err
431
- error(err, __LINE__, __FILE__)
417
+ error(err)
432
418
  end
433
419
 
434
420
  ### import
435
421
 
436
422
  def import(arg = nil)
437
- read_config unless @config
423
+ open_blog unless @blog
424
+
438
425
  arg = nil if arg == ""
439
426
  arg ||= ask("Filename: ") # check validity later
440
427
  name = arg
@@ -449,14 +436,14 @@ Remainder of post goes here.
449
436
  process_post(@fname)
450
437
  publish_post(@meta) # if publish?
451
438
  rescue => err
452
- error(err, __LINE__, __FILE__)
439
+ error(err)
453
440
  end
454
441
 
455
442
  ### new_post
456
443
 
457
444
  def cmd_new_post(arg)
458
445
  raise "Glitch: Got an argument" if arg != []
459
- read_config unless @config
446
+ open_blog unless @blog
460
447
  @title = ask("Title: ")
461
448
  @today = Time.now.strftime("%Y%m%d")
462
449
  @date = Time.now.strftime("%Y-%m-%d")
@@ -467,7 +454,7 @@ Remainder of post goes here.
467
454
  process_post(file) #- FIXME handle each view
468
455
  publish_post(@meta) # if publish?
469
456
  rescue => err
470
- error(err, __LINE__, __FILE__)
457
+ error(err)
471
458
  end
472
459
 
473
460
  ### remove_multiple_posts
@@ -484,10 +471,10 @@ Remainder of post goes here.
484
471
  def cmd_remove_post(arg, safe=true)
485
472
  arg = arg.first
486
473
  id = Integer(arg) rescue raise("'#{arg}' is not an integer")
487
- tag = "#{'%04d' % id}-"
474
+ tag = "#{'%04d' % id}"
488
475
  files = Find.find(@root).to_a
489
- files = files.grep(/#{tag}/)
490
- return puts red("\n No such post found (#{tag})") if files.empty?
476
+ files = files.grep(/#{tag}-/)
477
+ return puts red("\n No such post found (#{id})") if files.empty?
491
478
 
492
479
  if safe
493
480
  puts
@@ -520,12 +507,12 @@ Remainder of post goes here.
520
507
  def cmd_edit_post(arg)
521
508
  arg = arg.first
522
509
  id = Integer(arg) rescue raise("'#{arg}' is not an integer")
523
- tag = "#{'%04d' % id}-"
510
+ tag = "#{'%04d' % id}"
524
511
  files = Find.find(@root+"/src").to_a
525
- files = files.grep(/#{tag}/)
512
+ files = files.grep(/#{tag}-/)
526
513
  files = files.map {|f| File.basename(f) }
527
514
  return puts red("Multiple files: #{files}") if files.size > 1
528
- return puts red("\n No such post found (#{tag})") if files.empty?
515
+ return puts red("\n No such post found (#{id})") if files.empty?
529
516
 
530
517
  file = files.first
531
518
  result = system("vi #@root/src/#{file}")
@@ -542,7 +529,7 @@ Remainder of post goes here.
542
529
 
543
530
  def cmd_list_posts(arg)
544
531
  raise "Glitch: Got an argument" if arg != []
545
- dir = @config.viewdir(@view)
532
+ dir = @blog.viewdir(@view)
546
533
  Dir.chdir(dir) do
547
534
  posts = Dir.entries(".").grep(/^0.*/)
548
535
  if posts.empty?
@@ -579,7 +566,7 @@ Remainder of post goes here.
579
566
  end
580
567
 
581
568
  def cmd_INVALID(arg)
582
- puts "\n Command '#{red(arg)}' not understood.\n "
569
+ puts "\n Command '#{red(arg)}' was not understood."
583
570
  end
584
571
 
585
572
  def cmd_help(arg)
@@ -588,29 +575,55 @@ Remainder of post goes here.
588
575
 
589
576
  Commands:
590
577
 
591
- h, help This message
592
- q, quit Exit the program
578
+ #{red('h, help ')} This message
579
+ #{red('q, quit ')} Exit the program
580
+ #{red('v, version ')} Print version information
593
581
 
594
- change view VIEW Change current view
595
- cv VIEW Change current view
596
- new view Create a new view
597
- list views List all views available
598
- lsv Same as: list views
582
+ #{red('change view VIEW ')} Change current view
583
+ #{red('cv VIEW ')} Change current view
584
+ #{red('new view ')} Create a new view
585
+ #{red('list views ')} List all views available
586
+ #{red('lsv ')} Same as: list views
599
587
 
600
- p, post Create a new post
601
- new post Same as post (create a post)
602
- lsp, list posts List posts in current view
603
- lsd, list drafts List all posts regardless of view
588
+ #{red('p, post ')} Create a new post
589
+ #{red('new post ')} Same as post (create a post)
590
+ #{red('lsp, list posts ')} List posts in current view
591
+ #{red('lsd, list drafts ')} List all posts regardless of view
604
592
 
605
- rm ID Remove a post
606
- edit ID Edit a post
593
+ #{red('rm ID ')} Remove a post
594
+ #{red('edit ID ')} Edit a post
607
595
 
608
- preview Look at current (local) view in browser
609
- browse Look at current (deployed) view in browser
610
- relink Regenerate index for all views
611
- rebuild Regenerate all posts and relink
612
- deploy Deploy (current view)
596
+ #{red('preview ')} Look at current (local) view in browser
597
+ #{red('browse ')} Look at current (deployed) view in browser
598
+ #{red('relink ')} Regenerate index for all views
599
+ #{red('rebuild ')} Regenerate all posts and relink
600
+ #{red('deploy ')} Deploy (current view)
613
601
  EOS
614
602
  end
615
603
 
604
+ def clear
605
+ puts "\e[H\e[2J" # clear screen
606
+ end
607
+
608
+ def red(text)
609
+ "\e[31m#{text}\e[0m"
610
+ end
611
+
612
+ def blue(text)
613
+ "\e[34m#{text}\e[0m"
614
+ end
615
+
616
+ def bold(str)
617
+ "\e[1m#{str}\e[22m"
618
+ end
619
+
620
+ def interpolate(str)
621
+ wrap = "<<-EOS\n#{str}\nEOS"
622
+ eval wrap
623
+ end
624
+
625
+ def colored_slug(slug)
626
+ red(slug[0..3])+blue(slug[4..-1])
627
+ end
628
+
616
629
  end