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

cf_time, change_config, clean_slugify, copy_gem_asset_to_user, copy_to_clipboard, d4, escape_html, generate_missing_asset_svg, get_asset_path, get_from_clipboard, getvars, list_gem_assets, make_dir, make_tree, need, read_commented_file, read_file, see, see_file, slugify, substitute, system!, view_dir, write_file, write_file!, ymdhms

Methods included from Contract

assume, check_invariants, enabled?, invariant, verify

Constructor Details

#initialize(root) ⇒ Repo

repo



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/scriptorium/repo.rb', line 96

def initialize(root)    # repo
  assume { 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



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

def repo
  @repo
end

.rootObject (readonly)

class level



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

def root
  @root
end

.testingObject

Returns the value of attribute testing.



10
11
12
# File 'lib/scriptorium/repo.rb', line 10

def testing
  @testing
end

Instance Attribute Details

#current_viewObject (readonly)

instance attrs



16
17
18
# File 'lib/scriptorium/repo.rb', line 16

def current_view
  @current_view
end

#rootObject (readonly)

instance attrs



16
17
18
# File 'lib/scriptorium/repo.rb', line 16

def root
  @root
end

#viewsObject (readonly)

instance attrs



16
17
18
# File 'lib/scriptorium/repo.rb', line 16

def views
  @views
end

Class Method Details

.create(path = nil, testmode: false) ⇒ Object



24
25
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
# File 'lib/scriptorium/repo.rb', line 24

def self.create(path = nil, testmode: false)
  assume { 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"
  parent = path ? "." : home
  file = path || ".scriptorium"
  @root = parent/file
  raise self.RepoDirAlreadyExists(@root) if Dir.exist?(@root)
  make_tree(parent, <<~EOS)
    #@root
    ├── 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)
  write_file(@root/:config/"bootstrap_js.txt",  @predef.bootstrap_js)
  write_file(@root/:config/"bootstrap_css.txt", @predef.bootstrap_css)
  write_file(@root/:config/"common.js",         @predef.common_js)
  write_file(@root/:config/"widgets.txt",       @predef.available_widgets)
  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



78
79
80
81
82
83
# File 'lib/scriptorium/repo.rb', line 78

def self.destroy
  assume { Scriptorium::Repo.testing }
  raise self.TestModeOnly unless Scriptorium::Repo.testing
  system!("rm -rf #@root", "destroying repository")
  verify { !Dir.exist?(@root) }
end

.exist?Boolean

Returns:

  • (Boolean)


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

def self.exist?
  dir = Scriptorium::Repo.root
  return false if dir.nil?
  Dir.exist?(dir)
end

.generate_os_helpers(root) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/scriptorium/repo.rb', line 589

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



71
72
73
74
75
76
# File 'lib/scriptorium/repo.rb', line 71

def self.open(root)
  assume { 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



516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/scriptorium/repo.rb', line 516

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

#autopost_to_reddit(post_data, subreddit = nil) ⇒ Object



569
570
571
# File 'lib/scriptorium/repo.rb', line 569

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



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
# File 'lib/scriptorium/repo.rb', line 241

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
  theme = @current_view.theme
  views ||= @current_view.name   # initial_post wants a String!
  views, tags = Array(views), Array(tags)
  id = incr_post_num
  
  # Create content file (no ID, no created date)
  content = @predef.initial_post_content(title: title, blurb: blurb, 
                                        views: views, tags: tags, body: body)
  write_file(content_name, content)
  
  # Create metadata file (with ID and created date)
   = @predef.(num: id, 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



383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/scriptorium/repo.rb', line 383

def create_post(title: nil, views: nil, tags: nil, body: nil, blurb: nil)
  assume { title.nil? || title.is_a?(String) }
  assume { views.nil? || views.is_a?(Array) || views.is_a?(String) }
  assume { tags.nil? || tags.is_a?(Array) || tags.is_a?(String) }
  assume { body.nil? || body.is_a?(String) }
  assume { blurb.nil? || blurb.is_a?(String) }
  name = create_draft(title: title, views: views, tags: tags, body: body, blurb: blurb)
  num = finish_draft(name)
  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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
# File 'lib/scriptorium/repo.rb', line 162

def create_view(name, title, subtitle = "", theme: "standard")
  assume { name.is_a?(String) }
  assume { 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 CannotCreateViewNameInvalid(name)
  end
  
  raise ViewDirAlreadyExists(name) if view_exist?(name)
  make_tree(@root/:views, <<~EOS)
  #{name}/
  ├── 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)
  ├── widgets/             # Widgets for view
  └── staging/             # Staging area prior to deployment
  EOS

  ### 

  dir = "#@root/views/#{name}"
  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
  write_file(dir/:config/"bootstrap_js.txt",  @predef.bootstrap_js)
  write_file(dir/:config/"bootstrap_css.txt", @predef.bootstrap_css)
  write_file(dir/:config/"common.js",         @predef.common_js)
  write_file(dir/:config/"social.txt",        @predef.social_config)
  write_file(dir/:config/"reddit.txt",        @predef.reddit_config)
  write_file(dir/:config/"deploy.txt",        @predef.deploy_text % {view: name, domain: "example.com"})
  write_file(dir/:config/"status.txt",        @predef.status_txt)
  view = open_view(name)
  @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
  view.apply_theme(theme)
  verify { view.is_a?(Scriptorium::View) }
  return view
end

#define_invariantsObject

Invariants



90
91
92
93
94
# File 'lib/scriptorium/repo.rb', line 90

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

#finish_draft(name) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/scriptorium/repo.rb', line 277

def finish_draft(name)
  id = last_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



559
560
561
562
# File 'lib/scriptorium/repo.rb', line 559

def generate_front_page(view)
  view = lookup_view(view)
  view.generate_front_page
end

#generate_post(num) ⇒ Object



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
# File 'lib/scriptorium/repo.rb', line 438

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 }
  # live = Livetext.customize(mix: "lt3scriptor", call: ".nopara", vars: vars)
  # text = live.xform_file(content_file)
  # vars, _body = live.vars.vars, live.body
  
  live = Livetext.customize(mix: "lt3scriptor", call: ".nopara", vars: vars)
  body, vars = live.process(file: content_file)

  # Create or update metadata from post content
  if File.exist?()
    # Preserve existing metadata (like post.published timestamp)
     = getvars()
     = (num, vars)
    # Merge existing metadata over defaults
    .each do |key, value|
      [key] = value
    end
  else
    # Create new metadata
     = (num, vars)
  end
  
  # Write metadata file
  lines = .map { |k, v| sprintf("%-18s  %s", k, v) }
  write_file(, lines.join("\n"))
  
  # Merge metadata into vars, but don't override content vars
  .each { |key, value| vars[key] = value unless vars.key?(key) }
  
  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)
    theme = view.theme 
    vars[:"post.id"] = num.to_s  # Always use the post number as ID
    vars[:"post.body"] = body
    template = @predef.post_template("standard")
    set_pubdate(vars)
    # 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



529
530
531
532
# File 'lib/scriptorium/repo.rb', line 529

def generate_post_index(view)
  view = lookup_view(view)
  view.generate_post_index
end

#get_published_posts(view = nil) ⇒ Object



433
434
435
436
# File 'lib/scriptorium/repo.rb', line 433

def get_published_posts(view = nil)
  all_posts = all_posts(view)
  all_posts.select { |post| (post.id) }
end

#incr_post_numObject



271
272
273
274
275
# File 'lib/scriptorium/repo.rb', line 271

def incr_post_num
  num = last_post_num + 1
  write_file(postnum_file, num.to_s)
  num
end

#last_post_numObject



267
268
269
# File 'lib/scriptorium/repo.rb', line 267

def last_post_num
  read_file(postnum_file).to_i
end

#lookup_view(target) ⇒ Object

View methods…



129
130
131
132
133
134
135
136
137
138
# File 'lib/scriptorium/repo.rb', line 129

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

#open_view(name) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/scriptorium/repo.rb', line 229

def open_view(name)
  vhash = getvars(view_dir(name)/"config.txt")
  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



534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/scriptorium/repo.rb', line 534

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
  nil
end

#post_published?(num) ⇒ Boolean

Returns:

  • (Boolean)


423
424
425
426
427
428
429
430
431
# File 'lib/scriptorium/repo.rb', line 423

def (num)
  validate_post_id(num)
   = @root/:posts/d4(num)/"meta.txt"
      return false unless File.exist?()
  
   = getvars()
  result = [:"post.published"] != "no"
  result
end

#postnum_fileObject



85
86
87
# File 'lib/scriptorium/repo.rb', line 85

def postnum_file
  "#@root/config/last_post_num.txt"
end

#publish_post(num) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/scriptorium/repo.rb', line 397

def publish_post(num)
  validate_post_id(num)
   = @root/:posts/d4(num)/"meta.txt"
  
  # Read current metadata if it exists
   = {}
   = getvars() if File.exist?()
  
  # Check if already published
  if [:"post.published"] != "no" && [:"post.published"] != nil
    raise "Post #{num} is already published"
  end
  
  # Update published timestamp
  [:"post.published"] = ymdhms
  
  # Write updated metadata
  lines = .map { |k, v| sprintf("%-18s  %s", k, v) }
  write_file(, lines.join("\n"))
  
  # Generate the post (this will preserve the updated metadata)
  generate_post(num)
  
  self.post(num)
end

#redditObject

Reddit integration



565
566
567
# File 'lib/scriptorium/repo.rb', line 565

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

#reddit_configured?Boolean

Returns:

  • (Boolean)


573
574
575
# File 'lib/scriptorium/repo.rb', line 573

def reddit_configured?
  reddit.configured?
end

#tree(file = nil) ⇒ Object



293
294
295
296
297
# File 'lib/scriptorium/repo.rb', line 293

def tree(file = nil)
  cmd = "tree #@root"
  cmd << " >#{file}" if file
  system!(cmd, "generating tree structure")
end

#view(change = nil) ⇒ Object

get/set current view



146
147
148
149
150
151
152
# File 'lib/scriptorium/repo.rb', line 146

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

Returns:

  • (Boolean)


158
159
160
# File 'lib/scriptorium/repo.rb', line 158

def view_exist?(name)
  Dir.exist?("#@root/views/#{name}")
end