Class: Scriptorium::Repo
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#all_posts(view = nil) ⇒ Object
-
#all_posts_including_deleted(view = nil) ⇒ Object
-
#autopost_to_reddit(post_data, subreddit = nil) ⇒ Object
-
#create_draft(title: nil, blurb: nil, views: nil, tags: nil, body: nil) ⇒ Object
-
#create_post(title: nil, views: nil, tags: nil, body: nil, blurb: nil) ⇒ Object
-
#create_view(name, title, subtitle = "", theme: "standard") ⇒ Object
-
#define_invariants ⇒ Object
-
#delete_post(num) ⇒ Object
-
#finish_draft(name) ⇒ Object
-
#generate_front_page(view) ⇒ Object
-
#generate_post(num) ⇒ Object
-
#generate_post_index(view) ⇒ Object
-
#get_deployed_posts(view = nil) ⇒ Object
-
#incr_post_num ⇒ Object
-
#initialize(root) ⇒ Repo
constructor
-
#last_post_num ⇒ Object
-
#lookup_view(target) ⇒ Object
-
#mark_post_deployed(num, view = nil) ⇒ Object
-
#mark_post_undeployed(num, view = nil) ⇒ Object
-
#open_view(name) ⇒ Object
-
#post(id) ⇒ Object
-
#post_deleted?(num) ⇒ Boolean
-
#post_deployed?(num, view = nil) ⇒ Boolean
-
#post_published?(num, view = nil) ⇒ Boolean
-
#postnum_file ⇒ Object
-
#publish_post(num, view = nil) ⇒ Object
-
#reddit ⇒ Object
-
#reddit_configured? ⇒ Boolean
-
#tree(file = nil) ⇒ Object
-
#undelete_post(num) ⇒ Object
-
#unpublish_post(num, view = nil) ⇒ Object
-
#view(change = nil) ⇒ Object
-
#view_exist?(name) ⇒ Boolean
Methods included from Exceptions
make_exception
Methods included from Helpers
add_post_to_state_file, cf_time, change_config, clean_slugify, copy_gem_asset_to_user, copy_to_clipboard, d4, escape_html, find_asset, format_date, generate_missing_asset_svg, get_asset_path, get_from_clipboard, getvars, list_gem_assets, make_dir, make_tree, need, parse_commented_file, post_compare, post_in_state_file?, read_commented_file, read_file, read_post_state_file, remove_post_from_state_file, see, see_file, slugify, substitute, system!, tty, view_dir, write_file, write_file!, write_post_state_file, ymdhms, ymdhms_filename
Methods included from Contract
assume, check_invariants, enabled?, invariant, verify
Constructor Details
#initialize(root) ⇒ Repo
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/scriptorium/repo.rb', line 106
def initialize(root) msg = "root must be a non-empty String, got #{root.class} (#{root.inspect})"
assume(msg) { root.is_a?(String) && !root.empty? }
@root = root
@predef = Scriptorium::StandardFiles.new
self.class.instance_variable_set(:@root, root)
self.class.instance_variable_set(:@repo, self)
load_views
@reddit = nil define_invariants
verify { @root == root }
check_invariants
end
|
Class Attribute Details
.repo ⇒ Object
13
14
15
|
# File 'lib/scriptorium/repo.rb', line 13
def repo
@repo
end
|
.root ⇒ Object
13
14
15
|
# File 'lib/scriptorium/repo.rb', line 13
def root
@root
end
|
.testing ⇒ Object
Returns the value of attribute testing.
12
13
14
|
# File 'lib/scriptorium/repo.rb', line 12
def testing
@testing
end
|
Instance Attribute Details
#current_view ⇒ Object
18
19
20
|
# File 'lib/scriptorium/repo.rb', line 18
def current_view
@current_view
end
|
#root ⇒ Object
18
19
20
|
# File 'lib/scriptorium/repo.rb', line 18
def root
@root
end
|
#views ⇒ Object
18
19
20
|
# File 'lib/scriptorium/repo.rb', line 18
def views
@views
end
|
Class Method Details
.create(path = nil, testmode: false) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/scriptorium/repo.rb', line 26
def self.create(path = nil, testmode: false)
msg = "path must be nil or String, got #{path.class}"
assume(msg) { path.nil? || path.is_a?(String) }
if testmode == true
Scriptorium::Repo.testing = path
else
Scriptorium::Repo.testing = nil
end
home = ENV['HOME']
@predef = Scriptorium::StandardFiles.new
@root = path || "#{home}/.scriptorium"
raise self.RepoDirAlreadyExists(@root) if Dir.exist?(@root)
make_tree(@root, <<~EOS)
.
├── config/ # Global config files
├── views/ # Views
├── drafts/ # Draft posts (global)
├── posts/ # Global generated posts (slug.html)
├── assets/ # Images, etc.
│ └── library/ # Common images, icons, etc.
└── themes/ # Themes
EOS
postnum_file = "#@root/config/last_post_num.txt"
write_file(postnum_file, "0")
write_file(@root/:config/"global-head.txt", @predef.html_head_content)
copy_support_file('bootstrap/js.txt', @root/:config/"bootstrap_js.txt")
copy_support_file('bootstrap/css.txt', @root/:config/"bootstrap_css.txt")
write_file(@root/:config/"common.js", @predef.common_js)
write_file(@root/:config/"widgets.txt", @predef.available_widgets)
copy_support_file('post_index/config.txt', @root/:config/"post_index_defaults.txt")
FileUtils.mkdir_p(@root/:credentials)
copy_support_file('config/reddit_template.txt', @root/:credentials/"reddit.txt")
Scriptorium::Theme.create_standard(@root)
Scriptorium::Theme.copy_gem_assets_to_library(@root)
generate_os_helpers(@root)
@repo = self.open(@root)
Scriptorium::View.create_sample_view(@repo)
verify { @repo.is_a?(Scriptorium::Repo) }
return @repo
end
|
.destroy ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/scriptorium/repo.rb', line 87
def self.destroy
msg = "Repo.testing must be true in test mode"
assume(msg) { Scriptorium::Repo.testing }
raise self.TestModeOnly unless Scriptorium::Repo.testing
system!("rm -rf #@root", "destroying repository")
verify { !Dir.exist?(@root) }
end
|
.exist? ⇒ Boolean
20
21
22
23
24
|
# File 'lib/scriptorium/repo.rb', line 20
def self.exist?
dir = Scriptorium::Repo.root
return false if dir.nil?
Dir.exist?(dir)
end
|
.generate_os_helpers(root) ⇒ Object
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
|
# File 'lib/scriptorium/repo.rb', line 903
def self.generate_os_helpers(root)
os_code = case RbConfig::CONFIG['host_os']
when /darwin/ <<~RUBY
# Generated at repo creation for macOS
def open_file(file_path)
system("open", file_path)
end
RUBY
when /linux/ <<~RUBY
# Generated at repo creation for Linux
def open_file(file_path)
system("xdg-open", file_path)
end
RUBY
when /mswin|mingw|cygwin/ <<~RUBY
# Generated at repo creation for Windows
def open_file(file_path)
system("start", file_path)
end
RUBY
else
<<~RUBY
# Generated at repo creation for unknown OS
def open_file(file_path)
puts " Unable to open file on this OS"
end
RUBY
end
write_file(root/:config/"os_helpers.rb", os_code)
end
|
.open(root) ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/scriptorium/repo.rb', line 79
def self.open(root)
msg = "root must be a non-empty String, got #{root.class} (#{root.inspect})"
assume(msg) { root.is_a?(String) && !root.empty? }
repo = Scriptorium::Repo.new(root)
verify { repo.is_a?(Scriptorium::Repo) }
repo
end
|
Instance Method Details
#all_posts(view = nil) ⇒ Object
734
735
736
737
738
739
740
741
742
743
744
745
|
# File 'lib/scriptorium/repo.rb', line 734
def all_posts(view = nil)
posts = []
dirs = Dir.children(@root/:posts)
dirs.each do |id4|
next if id4.start_with?('_')
posts << Scriptorium::Post.read(self, id4)
end
return posts if view.nil?
view = lookup_view(view)
posts.select {|x| x.views.include?(view.name) }
end
|
#all_posts_including_deleted(view = nil) ⇒ Object
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
# File 'lib/scriptorium/repo.rb', line 747
def all_posts_including_deleted(view = nil)
posts = []
dirs = Dir.children(@root/:posts)
dirs.each do |id4|
if id4.start_with?('_')
original_id = id4[1..-1]
posts << Scriptorium::Post.read(self, original_id, deleted: true)
else
posts << Scriptorium::Post.read(self, id4)
end
end
return posts if view.nil?
view = lookup_view(view)
posts.select {|x|
views_str = x.views
if views_str.nil? || views_str.strip.empty?
false
else
views_str.strip.split(/\s+/).include?(view.name)
end
}
end
|
#autopost_to_reddit(post_data, subreddit = nil) ⇒ Object
877
878
879
|
# File 'lib/scriptorium/repo.rb', line 877
def autopost_to_reddit(post_data, subreddit = nil)
reddit.autopost(post_data, subreddit)
end
|
#create_draft(title: nil, blurb: nil, views: nil, tags: nil, body: nil) ⇒ Object
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
# File 'lib/scriptorium/repo.rb', line 310
def create_draft(title: nil, blurb: nil, views: nil, tags: nil, body: nil)
ts = Time.now.strftime("%Y%m%d-%H%M%S")
content_name = "#@root/drafts/#{ts}-draft.lt3"
metadata_name = "#@root/drafts/#{ts}-draft.meta"
views ||= @current_view.name views, tags = Array(views), Array(tags)
content = @predef.initial_post(:filled, title: title, blurb: blurb,
views: views, tags: tags, body: body)
write_file(content_name, content)
metadata = @predef.initial_post_metadata(title: title, blurb: blurb,
views: views, tags: tags)
write_file(metadata_name, metadata)
content_name
end
|
#create_post(title: nil, views: nil, tags: nil, body: nil, blurb: nil) ⇒ Object
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
# File 'lib/scriptorium/repo.rb', line 421
def create_post(title: nil, views: nil, tags: nil, body: nil, blurb: nil)
msg = "title must be nil or String, got #{title.class}"
assume(msg) { title.nil? || title.is_a?(String) }
msg = "views must be nil, Array, or String, got #{views.class}"
assume(msg) { views.nil? || views.is_a?(Array) || views.is_a?(String) }
msg = "tags must be nil, Array, or String, got #{tags.class}"
assume(msg) { tags.nil? || tags.is_a?(Array) || tags.is_a?(String) }
msg = "body must be nil or String, got #{body.class}"
assume(msg) { body.nil? || body.is_a?(String) }
msg = "blurb must be nil or String, got #{blurb.class}"
assume(msg) { blurb.nil? || blurb.is_a?(String) }
name = create_draft(title: title, views: views, tags: tags, body: body, blurb: blurb)
num = finish_draft(name)
metadata_file = @root/:posts/d4(num)/"meta.txt"
if File.exist?(metadata_file)
metadata = getvars(metadata_file)
views = metadata[:"post.views"]&.strip&.split(/\s+/) || ["sample"]
views.each do |view_name|
view_obj = lookup_view(view_name)
unpublished_file = view_obj.dir/:posts/"unpublished.txt"
undeployed_file = view_obj.dir/:posts/"undeployed.txt"
add_post_to_state_file(unpublished_file, num)
add_post_to_state_file(undeployed_file, num)
end
end
generate_post(num)
post = self.post(num) verify { post.is_a?(Scriptorium::Post) }
post
end
|
#create_view(name, title, subtitle = "", theme: "standard") ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/scriptorium/repo.rb', line 178
def create_view(name, title, subtitle = "", theme: "standard")
msg = "name must be a String, got #{name.class}"
assume(msg) { name.is_a?(String) }
msg = "title must be a String, got #{title.class}"
assume(msg) { title.is_a?(String) }
validate_view_name(name)
validate_view_title(title)
unless name.match?(/^[a-zA-Z0-9_-]+$/)
raise ViewNameInvalid(name)
end
raise ViewDirAlreadyExists(name) if view_exist?(name)
make_tree(@root/:views/name, <<~EOS)
.
├── config/ # View-specific config files
│ ├── layout.txt # Overall layout for front page
│ ├── footer.txt # Content for footer.html
│ ├── header.txt # Content for header.html
│ ├── left.txt # Content for left.html
│ ├── main.txt # Content for main.html
│ └── right.txt # Content for right.html
├── config.txt # View-specific config file # maybe call settings.txt?
├── layout/ # Unused?
├── pages/ # Static pages for view
├── assets/ # Images, etc. (view-specific)
│ └── missing/ # Missing assets (SVG placeholder files)
├── output/ # Output files (generated HTML)
│ ├── panes/ # Containers from layout.txt
│ │ ├── footer.html # Generated from footer.txt
│ │ ├── header.html # Generated from header.txt
│ │ ├── left.html # Generated from left.txt
│ │ ├── main.html # Generated from main.txt
│ │ └── right.html # Generated from right.txt
│ └── posts/ # Generated posts for view (slug.html)
├── posts/ # Post state tracking
│ ├── unpublished.txt # Posts NOT published in this view (empty = all published)
│ └── undeployed.txt # Posts NOT published in this view (empty = all deployed)
├── widgets/ # Widgets for view
└── staging/ # Staging area prior to deployment
EOS
dir = "#@root/views/#{name}"
begin
write_file!(dir/"config.txt",
"title #{title}",
"subtitle #{subtitle}",
"theme #{theme}")
write_file(dir/:config/"global-head.txt", @predef.html_head_content(true)) copy_support_file('bootstrap/js.txt', dir/:config/"bootstrap_js.txt")
copy_support_file('bootstrap/css.txt', dir/:config/"bootstrap_css.txt")
copy_support_file('highlight/js.txt', dir/:config/"highlight_js.txt")
write_file(dir/:config/"highlight_ruby_js.txt", @predef.highlight_ruby_js)
copy_support_file('highlight/css.txt', dir/:config/"highlight_css.txt")
write_file(dir/:config/"common.js", @predef.common_js)
copy_support_file('config/social.txt', dir/:config/"social.txt")
copy_support_file('config/reddit.txt', dir/:config/"reddit.txt")
write_file(dir/:config/"deploy.txt", @predef.deploy_text % {view: name, domain: "example.com"})
write_file(dir/:config/"status.txt", @predef.status_txt)
copy_support_file('post_index/config.txt', dir/:config/"post_index.txt")
FileUtils.mkdir_p(dir/:credentials)
copy_support_file('config/reddit_template.txt', dir/:credentials/"reddit.txt")
view_assets_dir = dir/:assets
FileUtils.mkdir_p(view_assets_dir)
FileUtils.mkdir_p(view_assets_dir/"icons"/"ui")
FileUtils.mkdir_p(view_assets_dir/"icons"/"social")
if File.exist?(@root/:assets/"icons"/"ui"/"back.png")
FileUtils.cp(@root/:assets/"icons"/"ui"/"back.png", view_assets_dir/"icons"/"ui"/"back.png")
end
if File.exist?(@root/:assets/"icons"/"social"/"reddit.png")
FileUtils.cp(@root/:assets/"icons"/"social"/"reddit.png", view_assets_dir/"icons"/"social"/"reddit.png")
end
if File.exist?(@root/:assets/"imagenotfound.jpg")
FileUtils.cp(@root/:assets/"imagenotfound.jpg", view_assets_dir/"imagenotfound.jpg")
end
write_file(dir/:posts/"unpublished.txt", "") write_file(dir/:posts/"undeployed.txt", "")
view = open_view(name)
rescue => e
FileUtils.rm_rf(dir) if Dir.exist?(dir)
raise CannotCreateView("Failed to create view '#{name}': #{e.message}")
end
@views -= [view]
@views << view
@current_view = view
write_file(@root/:config/"currentview.txt", view.name)
cfg = dir/:config theme_config = @root/:themes/theme/:layout/:config
containers = %w[header.txt footer.txt left.txt right.txt main.txt]
containers.each { |container| FileUtils.cp(theme_config/container, cfg/container) }
write_file(cfg/"svg.txt", @predef.svg_txt)
view.apply_theme(theme)
verify { view.is_a?(Scriptorium::View) }
return view
end
|
#define_invariants ⇒ Object
100
101
102
103
104
|
# File 'lib/scriptorium/repo.rb', line 100
def define_invariants
invariant { @root.is_a?(String) && !@root.empty? }
invariant { @views.is_a?(Array) }
invariant { @current_view.nil? || @current_view.is_a?(Scriptorium::View) }
end
|
#delete_post(num) ⇒ Object
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
|
# File 'lib/scriptorium/repo.rb', line 792
def delete_post(num)
validate_post_id(num)
metadata_file = @root/:posts/d4(num)/"meta.txt"
unless File.exist?(metadata_file)
raise CannotGetPost("Post #{num} not found")
end
if post_deleted?(num)
raise PostAlreadyDeleted(num)
end
metadata = getvars(metadata_file)
metadata[:"post.deleted"] = "yes"
metadata[:"post.deleted_at"] = ymdhms
lines = metadata.map { |k, v| sprintf("%-18s %s", k, v) }
write_file(metadata_file, lines.join("\n"))
post_dir = @root/:posts/d4(num)
deleted_dir = @root/:posts/"_#{d4(num)}"
if Dir.exist?(post_dir)
FileUtils.mv(post_dir, deleted_dir)
end
end
|
#finish_draft(name) ⇒ Object
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
# File 'lib/scriptorium/repo.rb', line 344
def finish_draft(name)
id = incr_post_num
id4 = d4(id)
posts = @root/:posts
make_dir(posts/id4)
make_dir(posts/id4/:assets)
FileUtils.mv(name, posts/id4/"source.lt3")
metadata_name = name.sub('.lt3', '.meta')
FileUtils.mv(metadata_name, posts/id4/"meta.txt") if File.exist?(metadata_name)
id
end
|
#generate_front_page(view) ⇒ Object
867
868
869
870
|
# File 'lib/scriptorium/repo.rb', line 867
def generate_front_page(view)
view = lookup_view(view)
view.generate_front_page
end
|
#generate_post(num) ⇒ Object
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
# File 'lib/scriptorium/repo.rb', line 669
def generate_post(num)
content_file = @root/:posts/d4(num)/"source.lt3"
metadata_file = @root/:posts/d4(num)/"meta.txt"
need(:file, content_file)
vars = { View: @current_view.name, :"post.id" => num }
vars[:"post.context"] = "post"
if File.exist?(metadata_file)
metadata = getvars(metadata_file)
vars.merge!(metadata)
end
live = Livetext.customize(mix: "lt3scriptor", call: ".nopara", vars: vars)
body, vars = live.process(file: content_file)
File.open("/tmp/debug_vars_#{num}.txt", "w") do |f|
f.puts "Vars after LiveText processing:"
vars.each { |k, v| f.puts "#{k} = #{v.inspect}" }
end
metadata_vars = vars.select {|k,v| k.to_s.start_with?("post.") }
metadata_vars.delete(:"post.body")
metadata_vars[:"post.slug"] = slugify(num, vars[:"post.title"]) + ".html"
metadata_vars[:"post.published"] = "no"
metadata_vars[:"post.deployed"] = "no"
if vars[:"post.created"]
time = Time.parse(vars[:"post.created"])
metadata_vars["post.pubdate"] = time.strftime("%Y-%m-%d %H:%M:%S")
metadata_vars["post.pubdate.month"] = time.strftime("%B")
metadata_vars["post.pubdate.day"] = time.strftime("%e")
metadata_vars["post.pubdate.year"] = time.strftime("%Y")
end
lines = metadata_vars.map { |k, v| sprintf("%-18s %s", k, v) }
write_file(metadata_file, lines.join("\n"))
views = (vars[:"post.views"] || "").strip.split(/\s+/)
vars[:"post.views"] = views.join(" ") views.each do |view|
view = lookup_view(view)
vars[:"post.id"] = num.to_s vars[:"post.body"] = body
vars[:"post.date"] = self.post(num).date
template = support_data('templates/post.lt3')
vars[:"reddit_button"] = view.generate_reddit_button(vars)
final = substitute(vars, template)
write_generated_post(vars, view, final)
end
end
|
#generate_post_index(view) ⇒ Object
772
773
774
775
|
# File 'lib/scriptorium/repo.rb', line 772
def generate_post_index(view)
view = lookup_view(view)
view.generate_post_index
end
|
#get_deployed_posts(view = nil) ⇒ Object
588
589
590
591
592
593
594
595
596
597
598
599
600
|
# File 'lib/scriptorium/repo.rb', line 588
def get_deployed_posts(view = nil)
all_posts = all_posts(view)
if view.nil?
all_posts.select { |post| post_deployed?(post.id) }
else
view = lookup_view(view)
undeployed_file = view.dir/:posts/"undeployed.txt"
all_posts.reject { |post| post_in_state_file?(undeployed_file, post.id) }
end
end
|
#incr_post_num ⇒ Object
338
339
340
341
342
|
# File 'lib/scriptorium/repo.rb', line 338
def incr_post_num
num = last_post_num + 1
write_file(postnum_file, num.to_s)
num
end
|
#last_post_num ⇒ Object
334
335
336
|
# File 'lib/scriptorium/repo.rb', line 334
def last_post_num
read_file(postnum_file).to_i
end
|
#lookup_view(target) ⇒ Object
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/scriptorium/repo.rb', line 140
def lookup_view(target)
return target if target.is_a?(Scriptorium::View)
validate_view_target(target)
list = @views.select {|v| v.name == target }
raise CannotLookupView(target) if list.empty?
raise MoreThanOneResult(target) if list.size > 1
return list[0]
end
|
#mark_post_deployed(num, view = nil) ⇒ Object
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
# File 'lib/scriptorium/repo.rb', line 499
def mark_post_deployed(num, view = nil)
validate_post_id(num)
metadata_file = @root/:posts/d4(num)/"meta.txt"
unless File.exist?(metadata_file)
if post_deleted?(num)
raise PostDeleted(num)
else
raise CannotGetPost("Post #{num} not found")
end
end
if view.nil?
view = @current_view&.name || "sample"
end
view_obj = lookup_view(view)
undeployed_file = view_obj.dir/:posts/"undeployed.txt"
if !post_in_state_file?(undeployed_file, num)
raise PostAlreadyDeployed(num)
end
unless post_published?(num, view)
raise PostNotPublished(num)
end
remove_post_from_state_file(undeployed_file, num)
metadata_file = @root/:posts/d4(num)/"meta.txt"
if File.exist?(metadata_file)
metadata = getvars(metadata_file)
if metadata[:"post.deployed"] == "no" || metadata[:"post.deployed"].nil?
metadata[:"post.deployed"] = ymdhms
lines = metadata.map { |k, v| sprintf("%-18s %s", k, v) }
write_file(metadata_file, lines.join("\n"))
end
else
raise CannotGetPost("Post #{num} metadata not found")
end
end
|
#mark_post_undeployed(num, view = nil) ⇒ Object
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
# File 'lib/scriptorium/repo.rb', line 547
def mark_post_undeployed(num, view = nil)
validate_post_id(num)
if view.nil?
view = @current_view&.name || "sample"
end
view_obj = lookup_view(view)
undeployed_file = view_obj.dir/:posts/"undeployed.txt"
add_post_to_state_file(undeployed_file, num)
metadata_file = @root/:posts/d4(num)/"meta.txt"
if File.exist?(metadata_file)
metadata = getvars(metadata_file)
metadata[:"post.deployed"] = "no"
lines = metadata.map { |k, v| sprintf("%-18s %s", k, v) }
write_file(metadata_file, lines.join("\n"))
end
end
|
#open_view(name) ⇒ Object
297
298
299
300
301
302
303
304
305
306
307
308
|
# File 'lib/scriptorium/repo.rb', line 297
def open_view(name)
config_file = view_dir(name)/"config.txt"
vhash = getvars(config_file)
title, subtitle, theme = vhash.values_at(:title, :subtitle, :theme)
view = Scriptorium::View.new(name, title, subtitle, theme)
@views -= [view]
@views << view
view
end
|
#post(id) ⇒ Object
777
778
779
780
781
782
783
784
785
786
787
788
789
790
|
# File 'lib/scriptorium/repo.rb', line 777
def post(id)
validate_post_id(id)
meta = @root/:posts/d4(id)/"meta.txt"
return Scriptorium::Post.new(self, id) if File.exist?(meta)
deleted_meta = @root/:posts/"_#{d4(id)}"/"meta.txt"
return Scriptorium::Post.new(self, id) if File.exist?(deleted_meta)
raise CannotGetPost("Post with ID #{id} not found")
end
|
#post_deleted?(num) ⇒ Boolean
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
|
# File 'lib/scriptorium/repo.rb', line 652
def post_deleted?(num)
validate_post_id(num)
deleted_metadata_file = @root/:posts/"_#{d4(num)}"/"meta.txt"
if File.exist?(deleted_metadata_file)
return true
end
metadata_file = @root/:posts/d4(num)/"meta.txt"
return false unless File.exist?(metadata_file)
metadata = getvars(metadata_file)
metadata[:"post.deleted"] == "yes"
end
|
#post_deployed?(num, view = nil) ⇒ Boolean
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
# File 'lib/scriptorium/repo.rb', line 570
def post_deployed?(num, view = nil)
validate_post_id(num)
if view.nil?
metadata_file = @root/:posts/d4(num)/"meta.txt"
return false unless File.exist?(metadata_file)
metadata = getvars(metadata_file)
return metadata[:"post.deployed"] != "no"
end
view = lookup_view(view)
undeployed_file = view.dir/:posts/"undeployed.txt"
!post_in_state_file?(undeployed_file, num)
end
|
#post_published?(num, view = nil) ⇒ Boolean
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
|
# File 'lib/scriptorium/repo.rb', line 634
def post_published?(num, view = nil)
validate_post_id(num)
if view.nil?
metadata_file = @root/:posts/d4(num)/"meta.txt"
return false unless File.exist?(metadata_file)
metadata = getvars(metadata_file)
return metadata[:"post.published"] != "no"
end
view = lookup_view(view)
unpublished_file = view.dir/:posts/"unpublished.txt"
!post_in_state_file?(unpublished_file, num)
end
|
#postnum_file ⇒ Object
95
96
97
|
# File 'lib/scriptorium/repo.rb', line 95
def postnum_file
"#@root/config/last_post_num.txt"
end
|
#publish_post(num, view = nil) ⇒ Object
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
# File 'lib/scriptorium/repo.rb', line 455
def publish_post(num, view = nil)
validate_post_id(num)
metadata_file = @root/:posts/d4(num)/"meta.txt"
unless File.exist?(metadata_file)
if post_deleted?(num)
raise PostDeleted(num)
else
raise CannotGetPost("Post #{num} not found")
end
end
metadata = getvars(metadata_file)
if view.nil?
view = @current_view&.name || "sample"
end
view_obj = lookup_view(view)
unpublished_file = view_obj.dir/:posts/"unpublished.txt"
if !post_in_state_file?(unpublished_file, num)
raise PostAlreadyPublished(num)
end
remove_post_from_state_file(unpublished_file, num)
if metadata[:"post.published"] == "no" || metadata[:"post.published"].nil?
metadata[:"post.published"] = ymdhms
lines = metadata.map { |k, v| sprintf("%-18s %s", k, v) }
write_file(metadata_file, lines.join("\n"))
end
self.post(num)
end
|
#reddit ⇒ Object
873
874
875
|
# File 'lib/scriptorium/repo.rb', line 873
def reddit
@reddit ||= Scriptorium::Reddit.new(self)
end
|
881
882
883
|
# File 'lib/scriptorium/repo.rb', line 881
def reddit_configured?
reddit.configured?
end
|
#tree(file = nil) ⇒ Object
360
361
362
363
364
|
# File 'lib/scriptorium/repo.rb', line 360
def tree(file = nil)
cmd = "tree #@root"
cmd << " >#{file}" if file
system!(cmd, "generating tree structure")
end
|
#undelete_post(num) ⇒ Object
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
|
# File 'lib/scriptorium/repo.rb', line 824
def undelete_post(num)
validate_post_id(num)
deleted_dir = @root/:posts/"_#{d4(num)}"
unless Dir.exist?(deleted_dir)
raise CannotGetPost("Deleted post #{num} not found")
end
unless post_deleted?(num)
raise PostNotDeleted(num)
end
post_dir = @root/:posts/d4(num)
FileUtils.mv(deleted_dir, post_dir)
metadata_file = @root/:posts/d4(num)/"meta.txt"
if File.exist?(metadata_file)
metadata = getvars(metadata_file)
metadata.delete(:"post.deleted")
metadata.delete(:"post.deleted_at")
lines = metadata.map { |k, v| sprintf("%-18s %s", k, v) }
write_file(metadata_file, lines.join("\n"))
end
end
|
#unpublish_post(num, view = nil) ⇒ Object
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
# File 'lib/scriptorium/repo.rb', line 602
def unpublish_post(num, view = nil)
validate_post_id(num)
if view.nil?
view = @current_view&.name || "sample"
end
view_obj = lookup_view(view)
if post_deployed?(num, view)
raise PostAlreadyDeployed(num)
end
unpublished_file = view_obj.dir/:posts/"unpublished.txt"
add_post_to_state_file(unpublished_file, num)
metadata_file = @root/:posts/d4(num)/"meta.txt"
if File.exist?(metadata_file)
metadata = getvars(metadata_file)
metadata[:"post.published"] = "no"
lines = metadata.map { |k, v| sprintf("%-18s %s", k, v) }
write_file(metadata_file, lines.join("\n"))
end
generate_post(num)
end
|
#view(change = nil) ⇒ Object
162
163
164
165
166
167
168
|
# File 'lib/scriptorium/repo.rb', line 162
def view(change = nil) return @current_view if change.nil?
vnew = change.is_a?(Scriptorium::View) ? change : lookup_view(change)
write_file(@root/:config/"currentview.txt", vnew.name)
@current_view = vnew
@current_view
end
|
#view_exist?(name) ⇒ Boolean
174
175
176
|
# File 'lib/scriptorium/repo.rb', line 174
def view_exist?(name)
Dir.exist?("#@root/views/#{name}")
end
|