Class: Scriptorium::Repo

Inherits:
Object
  • Object
show all
Extended by:
Contract, Exceptions, Helpers
Includes:
Contract, Exceptions, Helpers
Defined in:
lib/skeleton.rb,
lib/scriptorium/repo.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

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)    # repo
  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
  # Scriptorium::Repo.class_eval { @root, @repo = root, self }
  self.class.instance_variable_set(:@root, root)
  self.class.instance_variable_set(:@repo, self)  
  load_views
  @reddit = nil  # Lazy load Reddit integration
  define_invariants
  verify { @root == root }
  check_invariants
end

Class Attribute Details

.repoObject (readonly)

class level



13
14
15
# File 'lib/scriptorium/repo.rb', line 13

def repo
  @repo
end

.rootObject (readonly)

class level



13
14
15
# File 'lib/scriptorium/repo.rb', line 13

def root
  @root
end

.testingObject

Returns the value of attribute testing.



12
13
14
# File 'lib/scriptorium/repo.rb', line 12

def testing
  @testing
end

Instance Attribute Details

#current_viewObject (readonly)

instance attrs



18
19
20
# File 'lib/scriptorium/repo.rb', line 18

def current_view
  @current_view
end

#rootObject (readonly)

instance attrs



18
19
20
# File 'lib/scriptorium/repo.rb', line 18

def root
  @root
end

#viewsObject (readonly)

instance attrs



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) }
  # Handle backward compatibility: boolean true means testing mode
  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")
  
  # Create global credentials directory and template
  FileUtils.mkdir_p(@root/:credentials)
  copy_support_file('config/reddit_template.txt', @root/:credentials/"reddit.txt")
  

  
  Scriptorium::Theme.create_standard(@root)     # Theme: templates, etc.
  
  # Copy application-wide gem assets to library
  Scriptorium::Theme.copy_gem_assets_to_library(@root)
  
  # Generate OS-specific helper code
  generate_os_helpers(@root)
  
  @repo = self.open(@root)
  Scriptorium::View.create_sample_view(@repo)
  verify { @repo.is_a?(Scriptorium::Repo) }
  return @repo
end

.destroyObject



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/     # macOS
    <<~RUBY
      # Generated at repo creation for macOS
      def open_file(file_path)
        system("open", file_path)
      end
    RUBY
  when /linux/      # Linux
    <<~RUBY
      # Generated at repo creation for Linux
      def open_file(file_path)
        system("xdg-open", file_path)
      end
    RUBY
  when /mswin|mingw|cygwin/  # Windows
    <<~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|
    # Skip deleted posts (directories starting with underscore)
    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|
    # Include both normal and deleted posts
    if id4.start_with?('_')
      # Deleted post - remove underscore prefix and pass deleted: true
      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"
   = "#@root/drafts/#{ts}-draft.meta"
  
  # Whoa - what if different views have different themes??? FIXME 
  # Maybe solution is as simple as: Initial post is not theme-dependent
  views ||= @current_view.name   # initial_post wants a String!
  views, tags = Array(views), Array(tags)
  
  # Create content file (no ID, no created date)
  content = @predef.initial_post(:filled, title: title, blurb: blurb, 
                                 views: views, tags: tags, body: body)
  write_file(content_name, content)
  
  # Create metadata file (no ID for drafts)
   = @predef.(title: title, blurb: blurb, 
                                          views: views, tags: tags)
  write_file(, )
  
  # Return the content file name (for backward compatibility)
  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)
  
  # Add post to unpublished and undeployed lists for all views it belongs to
   = @root/:posts/d4(num)/"meta.txt"
  if File.exist?()
     = getvars()
    views = [:"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)  # Return the Post object
  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)
  
  # Validate name format (only allow alphanumeric, hyphen, underscore)
  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))  # true = view-specific
    copy_support_file('bootstrap/js.txt', dir/:config/"bootstrap_js.txt")
    copy_support_file('bootstrap/css.txt', dir/:config/"bootstrap_css.txt")
    # Highlight.js config files (renamed from prism_* for clarity)
    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")
    
    # Create view credentials directory and template
    FileUtils.mkdir_p(dir/:credentials)
    copy_support_file('config/reddit_template.txt', dir/:credentials/"reddit.txt")
    
    # Copy essential icons to view assets directory
    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")
    
    # Copy UI icons
    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
    
    # Copy social icons
    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
    
    # Copy missing image placeholder
    if File.exist?(@root/:assets/"imagenotfound.jpg")
      FileUtils.cp(@root/:assets/"imagenotfound.jpg", view_assets_dir/"imagenotfound.jpg")
    end
    
    # Create post state tracking files
    write_file(dir/:posts/"unpublished.txt",   "")  # Empty = all posts published
    write_file(dir/:posts/"undeployed.txt",    "")  # Empty = all posts deployed
    
    view = open_view(name)
  rescue => e
    # Clean up partial view directory if creation fails
    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  # Should these be copied from theme??
  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) }  # from theme to view
  
  # Create default SVG configuration using standard files
  write_file(cfg/"svg.txt", @predef.svg_txt)
  
  view.apply_theme(theme)
  verify { view.is_a?(Scriptorium::View) }
  return view
end

#define_invariantsObject

Invariants



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)
  
  # Check if post exists
   = @root/:posts/d4(num)/"meta.txt"
  unless File.exist?()
    raise CannotGetPost("Post #{num} not found")
  end
  
  # Check if already deleted
  if post_deleted?(num)
    raise PostAlreadyDeleted(num)
  end
  
  # Mark as deleted in metadata
   = getvars()
  [:"post.deleted"] = "yes"
  [:"post.deleted_at"] = ymdhms
  
  # Write updated metadata
  lines = .map { |k, v| sprintf("%-18s  %s", k, v) }
  write_file(, lines.join("\n"))
  
  # Move post directory to deleted location (with underscore prefix)
  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)
  
  # Move content file
  FileUtils.mv(name, posts/id4/"source.lt3")
  
  # Move metadata file (same timestamp, different extension)
   = name.sub('.lt3', '.meta')
  FileUtils.mv(, posts/id4/"meta.txt") if File.exist?()
  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"
   = @root/:posts/d4(num)/"meta.txt"
  
  need(:file, content_file)
  
  # Read content file
  vars = { View: @current_view.name, :"post.id" => num }
  # Mark transform as post-context
  vars[:"post.context"] = "post"
  
  # Merge metadata into vars if metadata file exists
  if File.exist?()
     = getvars()
    vars.merge!()
  end
  
  live = Livetext.customize(mix: "lt3scriptor", call: ".nopara", vars: vars)
  body, vars = live.process(file: content_file)
  
  # Debug: Write vars to 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

  # Use metadata from LiveText processing
  # Filter vars to only include post.* fields for metadata
   = vars.select {|k,v| k.to_s.start_with?("post.") }
  .delete(:"post.body")
  [:"post.slug"] = slugify(num, vars[:"post.title"]) + ".html"
  [:"post.published"] = "no"
  [:"post.deployed"] = "no"
  
  if vars[:"post.created"]
    time = Time.parse(vars[:"post.created"])
    ["post.pubdate"]       = time.strftime("%Y-%m-%d %H:%M:%S") 
    ["post.pubdate.month"] = time.strftime("%B") 
    ["post.pubdate.day"]   = time.strftime("%e") 
    ["post.pubdate.year"]  = time.strftime("%Y")
  end 

  # Write metadata file
  lines = .map { |k, v| sprintf("%-18s  %s", k, v) }
  write_file(, lines.join("\n"))
  
  views = (vars[:"post.views"] || "").strip.split(/\s+/)
  vars[:"post.views"] = views.join(" ")  # Ensure post.views is set in vars
  views.each do |view|  
    view = lookup_view(view)
    vars[:"post.id"] = num.to_s  # Always use the post number as ID
    vars[:"post.body"] = body
    vars[:"post.date"] = self.post(num).date  # Set post.date for templates
    
    
    template = support_data('templates/post.lt3')
    # Add Reddit button if enabled
    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?
    # If no specific view, use global metadata (for backward compatibility)
    all_posts.select { |post| post_deployed?(post.id) }
  else
    # Use view-specific deployed status
    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_numObject



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_numObject



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

View methods…



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)
  
  # Check if post exists in normal location first
   = @root/:posts/d4(num)/"meta.txt"
  unless File.exist?()
    # Check if post is deleted
    if post_deleted?(num)
      raise PostDeleted(num)
    else
      raise CannotGetPost("Post #{num} not found")
    end
  end
  
  if view.nil?
    # Use current view if no view specified
    view = @current_view&.name || "sample"
  end
  
  # Check if already deployed in this view
  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
  
  # Validate that only published posts can be deployed
  unless (num, view)
    raise PostNotPublished(num)
  end
  
  # Remove from undeployed list for this view
  remove_post_from_state_file(undeployed_file, num)
  
  # Update global metadata if this is the first deployment
   = @root/:posts/d4(num)/"meta.txt"
  if File.exist?()
     = getvars()
    if [:"post.deployed"] == "no" || [:"post.deployed"].nil?
      [:"post.deployed"] = ymdhms
      lines = .map { |k, v| sprintf("%-18s  %s", k, v) }
      write_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?
    # Use current view if no view specified
    view = @current_view&.name || "sample"
  end
  
  # Add to undeployed list for this view
  view_obj = lookup_view(view)
  undeployed_file = view_obj.dir/:posts/"undeployed.txt"
  add_post_to_state_file(undeployed_file, num)
  
  # Update global metadata
   = @root/:posts/d4(num)/"meta.txt"
  if File.exist?()
     = getvars()
    [:"post.deployed"] = "no"
    lines = .map { |k, v| sprintf("%-18s  %s", k, v) }
    write_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
  # Remove this line - current view should only be set from currentview.txt
  # @current_view = view
  # write_file(@root/:config/"currentview.txt", view.name)
  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)
  
  # Check normal directory first
  meta = @root/:posts/d4(id)/"meta.txt"
  return Scriptorium::Post.new(self, id) if File.exist?(meta)
  
  # Check deleted directory (with underscore prefix)
  deleted_meta = @root/:posts/"_#{d4(id)}"/"meta.txt"
  return Scriptorium::Post.new(self, id) if File.exist?(deleted_meta)
  
  # Post not found in either location
  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)
  
  # Check deleted location first (with underscore prefix)
   = @root/:posts/"_#{d4(num)}"/"meta.txt"
  if File.exist?()
    return true
  end
  
  # Check normal location
   = @root/:posts/d4(num)/"meta.txt"
  return false unless File.exist?()
  
   = getvars()
  [:"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 no specific view, check global metadata (for backward compatibility)
  if view.nil?
     = @root/:posts/d4(num)/"meta.txt"
    return false unless File.exist?()
    
     = getvars()
    return [:"post.deployed"] != "no"
  end
  
  # Check view-specific deployed status
  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 (num, view = nil)
  validate_post_id(num)
  
  # If no specific view, check global metadata (for backward compatibility)
  if view.nil?
     = @root/:posts/d4(num)/"meta.txt"
    return false unless File.exist?()
    
     = getvars()
    return [:"post.published"] != "no"
  end
  
  # Check view-specific published status
  view = lookup_view(view)
  unpublished_file = view.dir/:posts/"unpublished.txt"
  !post_in_state_file?(unpublished_file, num)
end

#postnum_fileObject



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)
  
  # Check if post exists in normal location first
   = @root/:posts/d4(num)/"meta.txt"
  unless File.exist?()
    # Check if post is deleted
    if post_deleted?(num)
      raise PostDeleted(num)
    else
      raise CannotGetPost("Post #{num} not found")
    end
  end
  
  # Read current metadata
   = getvars()
  
  if view.nil?
    # Use current view if no view specified
    view = @current_view&.name || "sample"
  end
  
  # Check if already published in this view
  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
  
  # View-specific publish - only update the specified view's state
  remove_post_from_state_file(unpublished_file, num)
  
  # If this is the first time publishing this post, update global metadata
  if [:"post.published"] == "no" || [:"post.published"].nil?
    [:"post.published"] = ymdhms
    
    # Write updated metadata
    lines = .map { |k, v| sprintf("%-18s  %s", k, v) }
    write_file(, lines.join("\n"))
  end
  
  self.post(num)
end

#redditObject

Reddit integration



873
874
875
# File 'lib/scriptorium/repo.rb', line 873

def reddit
  @reddit ||= Scriptorium::Reddit.new(self)
end

#reddit_configured?Boolean



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)
  
  # Check if post exists in deleted location
  deleted_dir = @root/:posts/"_#{d4(num)}"
  unless Dir.exist?(deleted_dir)
    raise CannotGetPost("Deleted post #{num} not found")
  end
  
  # Check if already undeleted
  unless post_deleted?(num)
    raise PostNotDeleted(num)
  end
  
  # Move post directory back to normal location
  post_dir = @root/:posts/d4(num)
  FileUtils.mv(deleted_dir, post_dir)
  
  # Remove deleted flag from metadata
   = @root/:posts/d4(num)/"meta.txt"
  if File.exist?()
     = getvars()
    .delete(:"post.deleted")
    .delete(:"post.deleted_at")
    
    # Write updated metadata
    lines = .map { |k, v| sprintf("%-18s  %s", k, v) }
    write_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)
  
  # Check if post is deployed in any view (including current view if none specified)
  if view.nil?
    view = @current_view&.name || "sample"
  end
  
  # Check if post is deployed in this view
  view_obj = lookup_view(view)
  if post_deployed?(num, view)
    raise PostAlreadyDeployed(num)
  end
  
  # Always use view-specific logic when a specific view is provided
  # Add to view-specific unpublished list
  unpublished_file = view_obj.dir/:posts/"unpublished.txt"
  add_post_to_state_file(unpublished_file, num)
  
  # Also update global metadata to "no" if this was the first published view
   = @root/:posts/d4(num)/"meta.txt"
  if File.exist?()
     = getvars()
    [:"post.published"] = "no"
    lines = .map { |k, v| sprintf("%-18s  %s", k, v) }
    write_file(, lines.join("\n"))
  end
  
  # Regenerate the post
  generate_post(num)
end

#view(change = nil) ⇒ Object

get/set current view



162
163
164
165
166
167
168
# File 'lib/scriptorium/repo.rb', line 162

def view(change = nil)   # get/set current view
  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