Class: Scriptorium::API
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#apply_theme(theme) ⇒ Object
-
#asset_exists?(filename, target: 'global', view: nil, include_gem: true) ⇒ Boolean
-
#build_rsync_destination(config) ⇒ Object
Build rsync destination from deployment config.
-
#bulk_copy_assets(filenames, from: 'global', to: 'view', view: nil) ⇒ Object
-
#can_deploy?(view = nil) ⇒ Boolean
-
#clone_theme(source_theme, new_name) ⇒ Object
-
#copy_asset(filename, from = 'global', to = 'view', from_id = nil, to_id = nil, **kwargs) ⇒ Object
-
#copy_global_assets_to_view(view_name) ⇒ Object
-
#copy_post_assets_to_view(view_name) ⇒ Object
-
#copy_view_assets_to_output(view_name) ⇒ Object
-
#create_backup(type: :incremental, label: nil) ⇒ Object
-
#create_draft(title: nil, body: nil, views: nil, tags: nil, blurb: nil) ⇒ Object
-
#create_page(view_name, page_name, title, content) ⇒ Object
-
#create_post(title, body, views: nil, tags: nil, blurb: nil) ⇒ Object
Post creation with convenience defaults.
-
#create_repo(path) ⇒ Object
-
#create_view(name, title, subtitle = "", theme: "standard") ⇒ Object
-
#define_invariants ⇒ Object
-
#delete_asset(filename, target = 'global', target_id = nil, **kwargs) ⇒ Object
-
#delete_backup(backup_name) ⇒ Object
-
#delete_draft(draft_path) ⇒ Object
-
#delete_post(id) ⇒ Object
-
#deploy(view = nil, dry_run: false) ⇒ Object
-
#draft(title: nil, body: nil, views: nil, tags: nil, blurb: nil) ⇒ Object
-
#drafts ⇒ Object
-
#edit_config(view = nil) ⇒ Object
-
#edit_deploy_config ⇒ Object
-
#edit_file(path) ⇒ Object
-
#edit_layout(view = nil) ⇒ Object
Convenience file editing methods.
-
#edit_post(post_id, mock: false) ⇒ Object
-
#edit_repo_config ⇒ Object
-
#edit_widget_data(view = nil, widget) ⇒ Object
-
#execute_deploy_rsync(source_dir, destination) ⇒ Object
Execute deployment rsync with validation.
-
#finish_draft(draft_path) ⇒ Object
-
#generate_front_page(view = nil) ⇒ Object
-
#generate_post(post_id) ⇒ Object
-
#generate_post_index(view = nil) ⇒ Object
-
#generate_view(view = nil) ⇒ Object
-
#generate_widget(widget_name) ⇒ Object
-
#get_asset_dimensions(filename, target: 'global', view: nil, include_gem: true) ⇒ Object
-
#get_asset_info(filename, target: 'global', view: nil, post: nil, include_gem: true) ⇒ Object
-
#get_asset_path(filename, target: 'global', view: nil, post: nil, include_gem: true) ⇒ Object
-
#get_asset_size(filename, target: 'global', view: nil, include_gem: true) ⇒ Object
-
#get_asset_type(filename) ⇒ Object
-
#get_backup_directory ⇒ Object
-
#get_deployed_posts(view = nil) ⇒ Object
-
#get_image_dimensions(file_path) ⇒ Object
-
#get_post_states(view = nil) ⇒ Object
-
#initialize(testmode: false) ⇒ API
constructor
-
#link_post(id, view = nil) ⇒ Object
-
#list_assets(target = 'global', target_id = nil, include_gem: true, **kwargs) ⇒ Object
Asset management methods.
-
#list_backups ⇒ Object
-
#lookup_view(view_name) ⇒ Object
-
#mark_post_deployed(num, view = nil) ⇒ Object
Deployment state management.
-
#mark_post_undeployed(num, view = nil) ⇒ Object
-
#open_repo(path) ⇒ Object
-
#parse_deploy_config(config_content) ⇒ Object
Parse deployment configuration file.
-
#post(id) ⇒ Object
-
#post_add_tag(id, tag) ⇒ Object
-
#post_add_view(id, view) ⇒ Object
-
#post_attrs(post_id, *keys) ⇒ Object
-
#post_deleted?(num) ⇒ Boolean
-
#post_deployed?(num, view = nil) ⇒ Boolean
-
#post_published?(num, view = nil) ⇒ Boolean
-
#post_remove_tag(id, tag) ⇒ Object
-
#post_remove_view(id, view) ⇒ Object
-
#posts(view = nil, include_deleted: false, published: false) ⇒ Object
-
#publish_post(num, view = nil) ⇒ Object
-
#repo_exists?(path) ⇒ Boolean
-
#restore_backup(backup_name, strategy: :safe) ⇒ Object
-
#root ⇒ Object
-
#search_posts(**criteria) ⇒ Object
-
#select_posts(&block) ⇒ Object
Post selection and search.
-
#system_themes ⇒ Object
-
#testing ⇒ Object
-
#theme_exists?(theme_name) ⇒ Boolean
-
#themes_available ⇒ Object
-
#undelete_post(id) ⇒ Object
-
#undeploy_post(num, view = nil) ⇒ Object
-
#unlink_post(id, view = nil) ⇒ Object
-
#unpublish_post(num, view = nil) ⇒ Object
-
#update_post(id, fields) ⇒ Object
-
#upload_asset(file_path, target = 'global', target_id = nil, filename: nil, **kwargs) ⇒ Object
-
#user_themes ⇒ Object
-
#validate_rsync_destination(destination) ⇒ Object
Validate rsync destination format.
-
#version ⇒ Object
-
#view(name = nil) ⇒ Object
-
#views ⇒ Object
-
#views_for(post_or_id) ⇒ Object
-
#widgets_available ⇒ Object
Methods included from Contract
#assume, #check_invariants, enabled?, #invariant, #verify
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_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, #system!, #tty, #view_dir, #write_file, #write_file!, #write_post_state_file, #ymdhms, #ymdhms_filename
Methods included from Exceptions
#make_exception
Constructor Details
#initialize(testmode: false) ⇒ API
Returns a new instance of API.
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/scriptorium/api.rb', line 17
def initialize(testmode: false)
msg = "testmode must be true or false, got #{testmode}"
assume(msg) { [true, false].include?(testmode) }
@testing = testmode
@repo = nil
define_invariants
verify { @testing == testmode }
check_invariants
end
|
Instance Attribute Details
#current_view ⇒ Object
Returns the value of attribute current_view.
9
10
11
|
# File 'lib/scriptorium/api.rb', line 9
def current_view
@current_view
end
|
#repo ⇒ Object
Returns the value of attribute repo.
9
10
11
|
# File 'lib/scriptorium/api.rb', line 9
def repo
@repo
end
|
Instance Method Details
#apply_theme(theme) ⇒ Object
94
95
96
|
# File 'lib/scriptorium/api.rb', line 94
def apply_theme(theme)
@repo.view.apply_theme(theme)
end
|
#asset_exists?(filename, target: 'global', view: nil, include_gem: true) ⇒ Boolean
1268
1269
1270
|
# File 'lib/scriptorium/api.rb', line 1268
def asset_exists?(filename, target: 'global', view: nil, include_gem: true)
!get_asset_info(filename, target: target, view: view, include_gem: include_gem).nil?
end
|
#build_rsync_destination(config) ⇒ Object
Build rsync destination from deployment config
1612
1613
1614
1615
1616
1617
|
# File 'lib/scriptorium/api.rb', line 1612
def build_rsync_destination(config)
if config['user'] && config['server'] && config['path']
return "#{config['user']}@#{config['server']}:#{config['path']}"
end
nil
end
|
#bulk_copy_assets(filenames, from: 'global', to: 'view', view: nil) ⇒ Object
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
|
# File 'lib/scriptorium/api.rb', line 1453
def bulk_copy_assets(filenames, from: 'global', to: 'view', view: nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if to == 'view' && view.nil?
results = []
filenames.each do |filename|
begin
target_path = copy_asset(filename, from: from, to: to, view: view)
results << { filename: filename, success: true, target: target_path }
rescue => e
results << { filename: filename, success: false, error: e.message }
end
end
results
end
|
#can_deploy?(view = nil) ⇒ Boolean
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
|
# File 'lib/scriptorium/api.rb', line 1486
def can_deploy?(view = nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
status_file = @repo.root/"views"/view/"config"/"status.txt"
return false unless File.exist?(status_file)
status_content = (status_file)
deploy_status = status_content.any? { |line| line.start_with?('deploy ') && line.split(/\s+/, 2)[1] == 'y' }
return false unless deploy_status
deploy_file = @repo.root/"views"/view/"config"/"deploy.txt"
return false unless File.exist?(deploy_file)
deploy_content = read_file(deploy_file)
required_fields = ['user', 'server', 'docroot', 'path']
return false unless required_fields.all? { |field| deploy_content.include?(field) }
deploy_config = (deploy_file)
server, user = deploy_config[:server], deploy_config[:user]
ok = ssh_keys_configured?(server, user)
return false if !ok
true
end
|
#clone_theme(source_theme, new_name) ⇒ Object
586
587
588
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
|
# File 'lib/scriptorium/api.rb', line 586
def clone_theme(source_theme, new_name)
unless theme_exists?(source_theme)
raise ThemeNotFound(source_theme)
end
if theme_exists?(new_name)
raise ThemeAlreadyExists(new_name)
end
unless new_name.match?(/^[a-zA-Z0-9_-]+$/)
raise ThemeNameInvalid(new_name)
end
source_dir = @repo.root/:themes/source_theme
target_dir = @repo.root/:themes/new_name
require 'fileutils'
FileUtils.cp_r(source_dir, target_dir)
new_name
end
|
#copy_asset(filename, from = 'global', to = 'view', from_id = nil, to_id = nil, **kwargs) ⇒ Object
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
|
# File 'lib/scriptorium/api.rb', line 1272
def copy_asset(filename, from = 'global', to = 'view', from_id = nil, to_id = nil, **kwargs)
if kwargs.any?
from = kwargs[:from] || from
to = kwargs[:to] || to
from_id = kwargs[:view] || from_id
to_id = kwargs[:view] || to_id if to == 'view'
end
source_path = case from
when 'gem'
gem_spec = Gem.loaded_specs['scriptorium']
if gem_spec
"#{gem_spec.full_gem_path}/assets/#{filename}"
else
File.expand_path("assets/#{filename}")
end
when 'global'
@repo.root/"assets"/filename
when 'library'
@repo.root/"assets"/"library"/filename
when 'view'
from_id ||= @repo.current_view&.name
raise ViewTargetNil if from_id.nil?
@repo.root/"views"/from_id/"assets"/filename
when 'post'
raise ArgumentError, "Post ID required for post assets" if from_id.nil?
post_id = from_id.to_i
post_num = d4(post_id)
@repo.root/"posts"/post_num/"assets"/filename
else
raise InvalidFormatError("source", from)
end
target_path = case to
when 'global'
@repo.root/"assets"/filename
when 'library'
@repo.root/"assets"/"library"/filename
when 'view'
to_id ||= @repo.current_view&.name
raise ViewTargetNil if to_id.nil?
@repo.root/"views"/to_id/"assets"/filename
when 'post'
raise ArgumentError, "Post ID required for post assets" if to_id.nil?
post_id = to_id.to_i
post_num = d4(post_id)
@repo.root/"posts"/post_num/"assets"/filename
else
raise InvalidFormatError("target", to)
end
unless File.exist?(source_path)
raise FileNotFoundError(source_path)
end
FileUtils.mkdir_p(File.dirname(target_path))
FileUtils.cp(source_path, target_path)
target_path
end
|
#copy_global_assets_to_view(view_name) ⇒ Object
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
|
# File 'lib/scriptorium/api.rb', line 931
def copy_global_assets_to_view(view_name)
view = @repo.lookup_view(view_name)
return unless view
view_assets_dir = view.dir/:assets
global_assets_dir = @repo.root/:assets
FileUtils.mkdir_p(view_assets_dir) unless Dir.exist?(view_assets_dir)
if Dir.exist?(global_assets_dir)
Dir.glob("#{global_assets_dir}/**/*").each do |global_path|
next unless File.file?(global_path)
rel = Pathname.new(global_path).relative_path_from(Pathname.new(global_assets_dir))
dest = view_assets_dir/rel
FileUtils.mkdir_p(File.dirname(dest))
FileUtils.cp(global_path, dest) unless File.exist?(dest)
end
end
end
|
#copy_post_assets_to_view(view_name) ⇒ Object
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
|
# File 'lib/scriptorium/api.rb', line 953
def copy_post_assets_to_view(view_name)
view = @repo.lookup_view(view_name)
return unless view
view_assets_dir = view.dir/:assets
posts = @repo.all_posts.select { |post| post.views&.include?(view_name) }
posts.each do |post|
post_assets_dir = @repo.root/"posts"/post.num/"assets"
view_post_assets_dir = view_assets_dir/"posts"/post.num
next unless Dir.exist?(post_assets_dir)
FileUtils.mkdir_p(view_post_assets_dir)
Dir.glob("#{post_assets_dir}/*").each do |post_asset|
next unless File.file?(post_asset)
filename = File.basename(post_asset)
view_asset_path = view_post_assets_dir/filename
FileUtils.cp(post_asset, view_asset_path) unless File.exist?(view_asset_path)
end
end
end
|
#copy_view_assets_to_output(view_name) ⇒ Object
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
|
# File 'lib/scriptorium/api.rb', line 984
def copy_view_assets_to_output(view_name)
view = @repo.lookup_view(view_name)
return unless view
view_assets_dir = view.dir/:assets
output_assets_dir = view.dir/:output/:assets
return unless Dir.exist?(view_assets_dir)
FileUtils.mkdir_p(output_assets_dir)
Dir.glob("#{view_assets_dir}/**/*").each do |asset_path|
next unless File.file?(asset_path)
relative_path = Pathname.new(asset_path).relative_path_from(Pathname.new(view_assets_dir))
output_asset_path = output_assets_dir/relative_path
FileUtils.mkdir_p(File.dirname(output_asset_path))
FileUtils.cp(asset_path, output_asset_path)
end
end
|
#create_backup(type: :incremental, label: nil) ⇒ Object
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
|
# File 'lib/scriptorium/api.rb', line 1657
def create_backup(type: :incremental, label: nil)
check_invariants
msg = "type must be :full or :incremental, got #{type}"
assume(msg) { [:full, :incremental].include?(type) }
msg = "@repo must be a Scriptorium::Repo, got #{@repo.class}"
assume(msg) { @repo.is_a?(Scriptorium::Repo) }
backup_dir = get_backup_directory
data_dir = backup_dir/"data"
FileUtils.mkdir_p(data_dir)
sleep(1)
if type == :full
temp_backup_path = data_dir/"temp-full-backup"
FileUtils.mkdir_p(temp_backup_path)
copy_repo_to_backup(temp_backup_path)
else
temp_backup_path = data_dir/"temp-incr-backup"
FileUtils.mkdir_p(temp_backup_path)
copy_changed_files_to_backup(temp_backup_path)
end
timestamp = Time.now.strftime("%Y%m%d-%H%M%S")
backup_name = "#{timestamp}-#{type == :full ? 'full' : 'incr'}"
final_backup_path = data_dir/backup_name
FileUtils.mkdir_p(final_backup_path)
create_backup_info(final_backup_path, type, backup_name)
compress_backup_data(temp_backup_path, final_backup_path/"data.tar.gz")
FileUtils.rm_rf(temp_backup_path)
update_backup_manifest(backup_name, type, label)
cleanup_old_backups
verify { File.exist?(final_backup_path) }
verify { File.exist?(final_backup_path/"data.tar.gz") }
check_invariants
backup_name
end
|
#create_draft(title: nil, body: nil, views: nil, tags: nil, blurb: nil) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/scriptorium/api.rb', line 167
def create_draft(title: nil, body: nil, views: nil, tags: nil, blurb: nil)
views ||= @repo.current_view&.name
raise ViewTargetNil if views.nil?
@repo.create_draft(
title: title,
body: body,
views: views,
tags: tags,
blurb: blurb
)
end
|
#create_page(view_name, page_name, title, content) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/scriptorium/api.rb', line 180
def create_page(view_name, page_name, title, content)
view = @repo.lookup_view(view_name)
raise ViewTargetNil if view.nil?
page_content = <<~LT3
.title #{title}
#{content}
LT3
page_file = "#{@repo.root}/views/#{view_name}/pages/#{page_name}.lt3"
write_file(page_file, page_content)
page_name
end
|
#create_post(title, body, views: nil, tags: nil, blurb: nil) ⇒ Object
Post creation with convenience defaults
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/scriptorium/api.rb', line 122
def create_post(title, body, views: nil, tags: nil, blurb: nil)
check_invariants
msg = "title must be a String, got #{title.class}"
assume(msg) { title.is_a?(String) }
msg = "body must be a String, got #{body.class}"
assume(msg) { body.is_a?(String) }
msg = "views must be nil, String, or Array, got #{views.class}"
assume(msg) { views.nil? || views.is_a?(String) || views.is_a?(Array) }
msg = "tags must be nil, String, or Array, got #{tags.class}"
assume(msg) { tags.nil? || tags.is_a?(String) || tags.is_a?(Array) }
msg = "blurb must be nil or String, got #{blurb.class}"
assume(msg) { blurb.nil? || blurb.is_a?(String) }
msg = "@repo must be a Scriptorium::Repo, got #{@repo.class}"
assume(msg) { @repo.is_a?(Scriptorium::Repo) }
views ||= @repo.current_view&.name
raise ViewTargetNil if views.nil?
post = @repo.create_post(
title: title,
body: body,
views: views,
tags: tags,
blurb: blurb
)
verify { post.is_a?(Scriptorium::Post) }
check_invariants
post
end
|
#create_repo(path) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/scriptorium/api.rb', line 33
def create_repo(path)
check_invariants
msg = "path must be a non-empty String, got #{path.class} (#{path.inspect})"
assume(msg) { path.is_a?(String) && !path.empty? }
raise RepoDirAlreadyExists if repo_exists?(path)
Scriptorium::Repo.create(path)
@repo = Scriptorium::Repo.open(path)
verify { @repo.is_a?(Scriptorium::Repo) }
check_invariants
end
|
#create_view(name, title, subtitle = "", theme: "standard") ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/scriptorium/api.rb', line 58
def create_view(name, title, subtitle = "", theme: "standard")
check_invariants
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) }
msg = "subtitle must be a String, got #{subtitle.class}"
assume(msg) { subtitle.is_a?(String) }
msg = "theme must be a String, got #{theme.class}"
assume(msg) { theme.is_a?(String) }
msg = "@repo must be a Scriptorium::Repo, got #{@repo.class}"
assume(msg) { @repo.is_a?(Scriptorium::Repo) }
@repo.create_view(name, title, subtitle, theme: theme)
verify { @repo.is_a?(Scriptorium::Repo) }
check_invariants
self
end
|
#define_invariants ⇒ Object
12
13
14
15
|
# File 'lib/scriptorium/api.rb', line 12
def define_invariants
invariant { [true, false].include?(@testing) }
invariant { @repo.nil? || @repo.is_a?(Scriptorium::Repo) }
end
|
#delete_asset(filename, target = 'global', target_id = nil, **kwargs) ⇒ Object
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
|
# File 'lib/scriptorium/api.rb', line 1339
def delete_asset(filename, target = 'global', target_id = nil, **kwargs)
if kwargs.any?
target = kwargs[:target] || target
target_id = kwargs[:view] || target_id
end
target_file = case target
when 'global'
@repo.root/"assets"/filename
when 'library'
@repo.root/"assets"/"library"/filename
when 'view'
target_id ||= @repo.current_view&.name
raise ViewTargetNil if target_id.nil?
@repo.root/"views"/target_id/"assets"/filename
when 'post'
raise ArgumentError, "Post ID required for post assets" if target_id.nil?
post_id = target_id.to_i
post_num = d4(post_id)
@repo.root/"posts"/post_num/"assets"/filename
else
raise InvalidFormatError("target", target)
end
unless File.exist?(target_file)
raise FileNotFoundError(target_file)
end
File.delete(target_file)
true
end
|
#delete_backup(backup_name) ⇒ Object
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
|
# File 'lib/scriptorium/api.rb', line 1791
def delete_backup(backup_name)
check_invariants
backup_dir = get_backup_directory
backup_path = backup_dir/"data"/backup_name
raise BackupNotFound, "Backup '#{backup_name}' not found" unless File.exist?(backup_path)
FileUtils.rm_rf(backup_path)
update_backup_manifest_remove(backup_name)
verify { !File.exist?(backup_path) }
check_invariants
true
end
|
#delete_draft(draft_path) ⇒ Object
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
|
# File 'lib/scriptorium/api.rb', line 1027
def delete_draft(draft_path)
raise DraftPathNil if draft_path.nil?
raise DraftPathEmpty if draft_path.to_s.strip.empty?
unless draft_path.to_s.end_with?('-draft.lt3')
raise DraftFileInvalid(draft_path)
end
unless File.exist?(draft_path)
raise DraftFileNotFound(draft_path)
end
File.delete(draft_path)
true
end
|
#delete_post(id) ⇒ Object
422
423
424
|
# File 'lib/scriptorium/api.rb', line 422
def delete_post(num)
@repo.delete_post(num)
end
|
#deploy(view = nil, dry_run: false) ⇒ Object
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
|
# File 'lib/scriptorium/api.rb', line 1518
def deploy(view = nil, dry_run: false)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
raise DeploymentNotReady(view) unless can_deploy?(view)
published_posts = posts(view, published: true)
undeployed_posts = published_posts.select { |post| !post_deployed?(post.id, view) }
deploy_file = @repo.root/"views"/view/"config"/"deploy.txt"
deploy_config = (deploy_file)
required_fields = [:user, :server, :docroot, :path]
missing_fields = required_fields - deploy_config.keys
missing = missing_fields.join(', ')
raise DeploymentFieldsMissing(missing) unless missing.empty?
output_dir = @repo.root/"views"/view/"output"
remote_path = "#{deploy_config[:user]}@#{deploy_config[:server]}:#{deploy_config[:docroot]}/#{deploy_config[:path]}"
cmd = "rsync -r -z -l #{output_dir}/ #{remote_path}/"
if dry_run
puts "DRY RUN: Would execute: #{cmd}"
puts "Output directory: #{output_dir}"
puts "Remote path: #{remote_path}"
puts "Deployment config: #{deploy_config}"
puts "Posts to deploy: #{undeployed_posts.map(&:id).join(', ')}"
return true
end
log_file = "/tmp/deployment.log"
File.open(log_file, 'a') do |f|
f.puts "=== DEPLOYMENT DEBUG #{Time.now} ==="
f.puts " Source directory: #{output_dir}"
f.puts " Remote path: #{remote_path}"
f.puts " Rsync command: #{cmd}"
f.puts " Source directory exists: #{Dir.exist?(output_dir)}"
f.puts " Source files: #{Dir.children(output_dir).join(', ')}"
f.puts " Current working directory: #{Dir.pwd}"
f.puts " Repo root: #{@repo.root}"
end
result = system(cmd)
File.open("/tmp/deployment.log", 'a') do |f|
f.puts " Rsync result: #{result}"
f.puts " Exit status: #{$?.exitstatus}"
f.puts " Exit success: #{$?.success?}"
end
if result
undeployed_posts.each do |post|
mark_post_deployed(post.id, view)
end
true
else
raise DeploymentFailed($?.exitstatus)
end
end
|
#draft(title: nil, body: nil, views: nil, tags: nil, blurb: nil) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/scriptorium/api.rb', line 154
def draft(title: nil, body: nil, views: nil, tags: nil, blurb: nil)
views ||= @repo.current_view&.name
raise ViewTargetNil if views.nil?
@repo.create_draft(
title: title,
body: body,
views: views,
tags: tags,
blurb: blurb
)
end
|
#drafts ⇒ Object
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
|
# File 'lib/scriptorium/api.rb', line 1014
def drafts
drafts_dir = @repo.root/:drafts
return [] unless Dir.exist?(drafts_dir)
draft_files = Dir.children(drafts_dir).select { |f| f.end_with?('-draft.lt3') }
draft_files.map do |filename|
path = drafts_dir/filename
title = (path)
{ path: path, title: title }
end
end
|
#edit_config(view = nil) ⇒ Object
660
661
662
663
664
|
# File 'lib/scriptorium/api.rb', line 660
def edit_config(view = nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
edit_file("views/#{view}/config.txt")
end
|
#edit_deploy_config ⇒ Object
677
678
679
|
# File 'lib/scriptorium/api.rb', line 677
def edit_deploy_config
edit_file("config/deploy.txt")
end
|
#edit_file(path) ⇒ Object
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
# File 'lib/scriptorium/api.rb', line 729
def edit_file(path)
raise EditFilePathNil if path.nil?
raise EditFilePathEmpty if path.to_s.strip.empty?
editor_file = @repo.root/"config/editor.txt"
editor = if File.exist?(editor_file)
read_file(editor_file).strip
else
ENV['EDITOR'] || 'vim'
end
system!(editor, path)
end
|
#edit_layout(view = nil) ⇒ Object
Convenience file editing methods
654
655
656
657
658
|
# File 'lib/scriptorium/api.rb', line 654
def edit_layout(view = nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
edit_file("views/#{view}/layout.txt")
end
|
#edit_post(post_id, mock: false) ⇒ Object
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
|
# File 'lib/scriptorium/api.rb', line 681
def edit_post(post_id, mock: false)
if post_deleted?(post_id)
raise PostDeleted, "Post #{post_id} is deleted"
end
post = @repo.post(post_id)
source_path = @repo.root/"posts/#{post.num}/source.lt3"
body_path = @repo.root/"posts/#{post.num}/body.html"
if File.exist?(source_path)
before_checksum = Digest::MD5.file(source_path).hexdigest
if mock.is_a?(Array) && mock.include?(:checksum)
after_checksum = mock[mock.index(:checksum) + 1]
else
edit_file(source_path) unless mock
after_checksum = Digest::MD5.file(source_path).hexdigest
end
else
raise "Cannot edit post #{post_id}: source.lt3 file not found"
end
if before_checksum != after_checksum
@repo.views.each do |view|
if post_deployed?(post_id, view.name)
mark_post_undeployed(post_id, view.name)
end
if post_published?(post_id, view.name)
unpublish_post(post_id, view.name)
end
end
@repo.generate_post(post_id)
true else
false end
end
|
#edit_repo_config ⇒ Object
673
674
675
|
# File 'lib/scriptorium/api.rb', line 673
def edit_repo_config
edit_file("config/repo.txt")
end
|
666
667
668
669
670
671
|
# File 'lib/scriptorium/api.rb', line 666
def edit_widget_data(view = nil, widget)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
raise WidgetNameNil if widget.nil?
edit_file("views/#{view}/widgets/#{widget}/list.txt")
end
|
#execute_deploy_rsync(source_dir, destination) ⇒ Object
Execute deployment rsync with validation
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
|
# File 'lib/scriptorium/api.rb', line 1625
def execute_deploy_rsync(source_dir, destination)
unless validate_rsync_destination(destination)
puts " ❌ Invalid destination format: #{destination}"
puts " Expected format: user@server:path"
return false
end
cmd = "rsync -r -z -l #{source_dir}/ #{destination}/"
puts " Executing: #{cmd}"
result = system(cmd)
puts " rsync completed with result: #{result}"
result
end
|
#finish_draft(draft_path) ⇒ Object
196
197
198
|
# File 'lib/scriptorium/api.rb', line 196
def finish_draft(draft_path)
@repo.finish_draft(draft_path)
end
|
#generate_front_page(view = nil) ⇒ Object
201
202
203
204
205
206
|
# File 'lib/scriptorium/api.rb', line 201
def generate_front_page(view = nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
@repo.generate_front_page(view)
end
|
#generate_post(post_id) ⇒ Object
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/scriptorium/api.rb', line 225
def generate_post(post_id)
post_dir = @repo.root/:posts/d4(post_id)
if Dir.exist?(post_dir)
@repo.generate_post(post_id)
else
post = @repo.post(post_id)
raise CannotGetPost("Post with ID #{post_id} not found") if post.nil?
@repo.generate_post(post_id)
end
end
|
#generate_post_index(view = nil) ⇒ Object
208
209
210
211
212
213
214
215
|
# File 'lib/scriptorium/api.rb', line 208
def generate_post_index(view = nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
@repo.generate_post_index(view)
end
|
#generate_view(view = nil) ⇒ Object
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
|
# File 'lib/scriptorium/api.rb', line 801
def generate_view(view = nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
begin
v = @repo.lookup_view(view)
view_dir = v.dir
output_dir = v.dir/:output
output_index = output_dir/:index.html
output_post_index = output_dir/:post_index.html
latest_input = Time.at(0)
posts_dir = @repo.root/:posts
if Dir.exist?(posts_dir)
Dir.glob("#{posts_dir}/[0-9][0-9][0-9][0-9]/source.lt3").each do |p|
m = File.mtime(p) rescue nil
latest_input = m if m && m > latest_input
end
Dir.glob("#{posts_dir}/[0-9][0-9][0-9][0-9]/assets/**/*").each do |p|
next unless File.file?(p)
m = File.mtime(p) rescue nil
latest_input = m if m && m > latest_input
end
end
if Dir.exist?(view_dir)
Dir.glob("#{view_dir}/**/*").each do |p|
next unless File.file?(p)
next if p.start_with?(output_dir.to_s)
m = File.mtime(p) rescue nil
latest_input = m if m && m > latest_input
end
end
global_assets = @repo.root/:assets
if Dir.exist?(global_assets)
Dir.glob("#{global_assets}/**/*").each do |p|
next unless File.file?(p)
m = File.mtime(p) rescue nil
latest_input = m if m && m > latest_input
end
end
themes_dir = @repo.root/:themes
if Dir.exist?(themes_dir)
Dir.glob("#{themes_dir}/**/*").each do |p|
next unless File.file?(p)
m = File.mtime(p) rescue nil
latest_input = m if m && m > latest_input
end
end
if File.exist?(output_index) && File.exist?(output_post_index)
out_mtime = File.mtime(output_index) rescue nil
out2_mtime = File.mtime(output_post_index) rescue nil
if out_mtime && out2_mtime && out_mtime >= latest_input && out2_mtime >= latest_input
return true
end
end
rescue => _e
end
copy_global_assets_to_view(view)
copy_post_assets_to_view(view)
regenerate_stale_posts(view)
copy_view_assets_to_output(view)
@repo.generate_post_index(view)
@repo.generate_front_page(view)
true
end
|
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
|
# File 'lib/scriptorium/api.rb', line 621
def generate_widget(widget_name)
raise ViewTargetNil if @repo.current_view.nil?
raise WidgetNameNil if widget_name.nil?
raise WidgetsArgEmpty if widget_name.to_s.strip.empty?
unless widget_name.to_s.match?(/^[a-zA-Z0-9_]+$/)
raise WidgetNameInvalid(widget_name)
end
widget_class_name = widget_name.to_s.capitalize
begin
widget_class = eval("Scriptorium::Widget::#{widget_class_name}")
rescue NameError
raise CannotBuildWidget("Widget class not found: Scriptorium::Widget::#{widget_class_name}")
end
widget = widget_class.new(@repo, @repo.current_view)
widget.generate
true
end
|
#get_asset_dimensions(filename, target: 'global', view: nil, include_gem: true) ⇒ Object
1425
1426
1427
1428
|
# File 'lib/scriptorium/api.rb', line 1425
def get_asset_dimensions(filename, target: 'global', view: nil, include_gem: true)
asset_info = get_asset_info(filename, target: target, view: view, include_gem: include_gem)
asset_info&.dig(:dimensions)
end
|
#get_asset_info(filename, target: 'global', view: nil, post: nil, include_gem: true) ⇒ Object
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
|
# File 'lib/scriptorium/api.rb', line 1233
def get_asset_info(filename, target: 'global', view: nil, post: nil, include_gem: true)
view ||= @repo.current_view&.name
raise ViewTargetNil if target == 'view' && view.nil?
raise ArgumentError, "Post ID required for post assets" if target == 'post' && post.nil?
case target
when 'view'
asset_path = @repo.root/"views"/view/"assets"/filename
return build_asset_info(asset_path) if File.exist?(asset_path)
when 'post'
post_id = post.to_i
post_num = d4(post_id)
asset_path = @repo.root/"posts"/post_num/"assets"/filename
return build_asset_info(asset_path) if File.exist?(asset_path)
when 'global'
asset_path = @repo.root/"assets"/filename
return build_asset_info(asset_path) if File.exist?(asset_path)
when 'library'
asset_path = @repo.root/"assets"/"library"/filename
return build_asset_info(asset_path) if File.exist?(asset_path)
when 'gem'
if include_gem
gem_spec = Gem.loaded_specs['scriptorium']
if gem_spec
gem_asset_path = "#{gem_spec.full_gem_path}/assets/#{filename}"
return build_asset_info(gem_asset_path, filename) if File.exist?(gem_asset_path)
end
end
else
raise InvalidFormatError("target", target)
end
nil
end
|
#get_asset_path(filename, target: 'global', view: nil, post: nil, include_gem: true) ⇒ Object
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
|
# File 'lib/scriptorium/api.rb', line 1373
def get_asset_path(filename, target: 'global', view: nil, post: nil, include_gem: true)
view ||= @repo.current_view&.name
raise ViewTargetNil if target == 'view' && view.nil?
raise ArgumentError, "Post ID required for post assets" if target == 'post' && post.nil?
case target
when 'view'
asset_path = @repo.root/"views"/view/"assets"/filename
return asset_path.to_s if File.exist?(asset_path)
when 'post'
post_id = post.to_i
post_num = d4(post_id)
asset_path = @repo.root/"posts"/post_num/"assets"/filename
return asset_path.to_s if File.exist?(asset_path)
when 'global'
asset_path = @repo.root/"assets"/filename
return asset_path.to_s if File.exist?(asset_path)
when 'library'
asset_path = @repo.root/"assets"/"library"/filename
return asset_path.to_s if File.exist?(asset_path)
when 'gem'
if include_gem
gem_spec = Gem.loaded_specs['scriptorium']
if gem_spec
gem_asset_path = "#{gem_spec.full_gem_path}/assets/#{filename}"
return gem_asset_path if File.exist?(gem_asset_path)
end
end
else
raise InvalidFormatError("target", target)
end
nil
end
|
#get_asset_size(filename, target: 'global', view: nil, include_gem: true) ⇒ Object
1430
1431
1432
1433
|
# File 'lib/scriptorium/api.rb', line 1430
def get_asset_size(filename, target: 'global', view: nil, include_gem: true)
asset_info = get_asset_info(filename, target: target, view: view, include_gem: include_gem)
asset_info&.dig(:size)
end
|
#get_asset_type(filename) ⇒ Object
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
|
# File 'lib/scriptorium/api.rb', line 1435
def get_asset_type(filename)
return nil if filename.nil?
ext = File.extname(filename).downcase
case ext
when '.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.svg'
'image'
when '.pdf', '.doc', '.docx', '.txt', '.md'
'document'
when '.mp4', '.avi', '.mov', '.wmv'
'video'
when '.mp3', '.wav', '.flac'
'audio'
else
'other'
end
end
|
#get_backup_directory ⇒ Object
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
|
# File 'lib/scriptorium/api.rb', line 1646
def get_backup_directory
repo_path = Pathname.new(@repo.root)
repo_parent = repo_path.parent
repo_name = repo_path.basename.to_s
if repo_name == "scriptorium-TEST"
repo_parent/"backup-scriptorium-TEST"
else
repo_parent/"backup-scriptorium"
end
end
|
#get_deployed_posts(view = nil) ⇒ Object
304
305
306
307
|
# File 'lib/scriptorium/api.rb', line 304
def get_deployed_posts(view = nil)
view ||= @repo.current_view&.name
@repo.get_deployed_posts(view)
end
|
#get_image_dimensions(file_path) ⇒ Object
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
|
# File 'lib/scriptorium/api.rb', line 1408
def get_image_dimensions(file_path)
return nil unless File.exist?(file_path)
image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.svg']
return nil unless image_extensions.any? { |ext| file_path.downcase.end_with?(ext) }
return nil unless defined?(FastImage)
dimensions = FastImage.size(file_path)
return dimensions ? "#{dimensions[0]}×#{dimensions[1]}" : nil
rescue => e
return nil
end
|
#get_post_states(view = nil) ⇒ Object
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
# File 'lib/scriptorium/api.rb', line 309
def get_post_states(view = nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
posts = @repo.all_posts(view)
states = {}
posts.each do |post|
published = post_published?(post.id, view)
deployed = post_deployed?(post.id, view)
deleted = @repo.post_deleted?(post.id)
state = ""
state += "P" if published
state += "D" if deployed
state += "X" if deleted
state = "-" if state.empty?
states[post.id] = {
id: post.id,
title: post.title,
state: state,
published: published,
deployed: deployed,
deleted: deleted
}
end
deleted_posts = @repo.all_posts_including_deleted(view)
deleted_posts.each do |post|
if @repo.post_deleted?(post.id)
states[post.id] = {
id: post.id,
title: post.title,
state: "X",
published: false,
deployed: false,
deleted: true
}
end
end
states
end
|
#link_post(id, view = nil) ⇒ Object
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
# File 'lib/scriptorium/api.rb', line 467
def link_post(id, view = nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
post = @repo.post(id)
raise CannotGetPost("Post with ID #{id} not found") if post.nil?
current_views = post.views.strip.split(/\s+/)
new_views = current_views.include?(view) ? current_views : current_views + [view]
result = update_post(id, {views: new_views})
@repo.generate_post(id) if result
result
end
|
#list_assets(target = 'global', target_id = nil, include_gem: true, **kwargs) ⇒ Object
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
|
# File 'lib/scriptorium/api.rb', line 1166
def list_assets(target = 'global', target_id = nil, include_gem: true, **kwargs)
if kwargs.any?
target = kwargs[:target] || target
target_id = kwargs[:view] || target_id
end
assets = []
case target
when 'view'
target_id ||= @repo.current_view&.name
raise ViewTargetNil if target_id.nil?
assets_dir = @repo.root/"views"/target_id/"assets"
if Dir.exist?(assets_dir)
Dir.glob(assets_dir/"*").each do |file|
next unless File.file?(file)
assets << build_asset_info(file)
end
end
when 'post'
raise ArgumentError, "Post ID required for post assets" if target_id.nil?
post_id = target_id.to_i
post_num = d4(post_id)
assets_dir = @repo.root/"posts"/post_num/"assets"
if Dir.exist?(assets_dir)
Dir.glob(assets_dir/"*").each do |file|
next unless File.file?(file)
assets << build_asset_info(file)
end
end
when 'global'
assets_dir = @repo.root/"assets"
if Dir.exist?(assets_dir)
Dir.glob(assets_dir/"*").each do |file|
next unless File.file?(file)
assets << build_asset_info(file)
end
end
when 'library'
assets_dir = @repo.root/"assets"/"library"
if Dir.exist?(assets_dir)
Dir.glob(assets_dir/"*").each do |file|
next unless File.file?(file)
assets << build_asset_info(file)
end
end
when 'gem'
if include_gem
gem_spec = Gem.loaded_specs['scriptorium']
if gem_spec
gem_assets_dir = "#{gem_spec.full_gem_path}/assets"
if Dir.exist?(gem_assets_dir)
Dir.glob("#{gem_assets_dir}/**/*").each do |file|
next unless File.file?(file)
relative_path = file.sub("#{gem_assets_dir}/", "")
assets << build_asset_info(file, relative_path)
end
end
end
end
else
raise InvalidFormatError("target", target)
end
assets.sort_by { |asset| asset[:filename] }
end
|
#list_backups ⇒ Object
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
|
# File 'lib/scriptorium/api.rb', line 1712
def list_backups
check_invariants
backup_dir = get_backup_directory
manifest_file = backup_dir/"manifest.txt"
return [] unless File.exist?(manifest_file)
backups = []
File.readlines(manifest_file).each do |line|
line = line.strip
next if line.empty? || line.start_with?('#')
parts = line.split(' ', 3)
next if parts.length < 1
timestamp_type = parts[0]
description = parts.length > 1 ? parts[1..-1].join(' ') : nil
if timestamp_type.match(/^(\d{8}-\d{6})-(full|incr)$/)
timestamp_str = $1
type = $2 == 'full' ? :full : :incremental
begin
timestamp = Time.strptime(timestamp_str, "%Y%m%d-%H%M%S")
backups << {
name: timestamp_type,
type: type,
description: description,
timestamp: timestamp,
size: calculate_backup_size(timestamp_type),
file_count: count_backup_files(timestamp_type)
}
rescue ArgumentError
next
end
end
end
backups.sort_by { |b| b[:timestamp] }.reverse
end
|
#lookup_view(view_name) ⇒ Object
112
113
114
|
# File 'lib/scriptorium/api.rb', line 112
def lookup_view(target)
@repo&.lookup_view(target)
end
|
#mark_post_deployed(num, view = nil) ⇒ Object
Deployment state management
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/scriptorium/api.rb', line 276
def mark_post_deployed(num, view = nil)
check_invariants
msg = "num must be an Integer, got #{num.class}"
assume(msg) { num.is_a?(Integer) }
msg = "@repo must be a Scriptorium::Repo, got #{@repo.class}"
assume(msg) { @repo.is_a?(Scriptorium::Repo) }
@repo.mark_post_deployed(num, view)
check_invariants
end
|
#mark_post_undeployed(num, view = nil) ⇒ Object
288
289
290
291
292
293
294
295
296
297
298
|
# File 'lib/scriptorium/api.rb', line 288
def mark_post_undeployed(num, view = nil)
check_invariants
msg = "num must be an Integer, got #{num.class}"
assume(msg) { num.is_a?(Integer) }
msg = "@repo must be a Scriptorium::Repo, got #{@repo.class}"
assume(msg) { @repo.is_a?(Scriptorium::Repo) }
@repo.mark_post_undeployed(num, view)
check_invariants
end
|
#open_repo(path) ⇒ Object
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/scriptorium/api.rb', line 46
def open_repo(path)
check_invariants
msg = "path must be a non-empty String, got #{path.class} (#{path.inspect})"
assume(msg) { path.is_a?(String) && !path.empty? }
@repo = Scriptorium::Repo.open(path)
verify { @repo.is_a?(Scriptorium::Repo) }
check_invariants
end
|
#parse_deploy_config(config_content) ⇒ Object
Parse deployment configuration file
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
|
# File 'lib/scriptorium/api.rb', line 1591
def parse_deploy_config(config_content)
lines = config_content.strip.split("\n")
config = {}
lines.each do |line|
line = line.strip
next if line.empty? || line.start_with?('#')
if line.match(/^(\w+)\s+(.+)$/)
key = $1.strip
value = $2.strip
config[key] = value
end
end
config
end
|
#post(id) ⇒ Object
417
418
419
|
# File 'lib/scriptorium/api.rb', line 417
def post(id)
@repo.post(id)
end
|
#post_add_tag(id, tag) ⇒ Object
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
# File 'lib/scriptorium/api.rb', line 496
def post_add_tag(id, tag)
post = @repo.post(id)
raise CannotGetPost("Post with ID #{id} not found") if post.nil?
current_tags = post.tags.strip.split(/,\s*/)
new_tags = current_tags.include?(tag) ? current_tags : current_tags + [tag]
result = update_post(id, {tags: new_tags})
@repo.generate_post(id) if result
result
end
|
#post_add_view(id, view) ⇒ Object
484
485
486
487
488
|
# File 'lib/scriptorium/api.rb', line 484
def post_add_view(id, view)
view_name = view.is_a?(String) ? view : view.name
link_post(id, view_name)
end
|
#post_attrs(post_id, *keys) ⇒ Object
412
413
414
415
|
# File 'lib/scriptorium/api.rb', line 412
def post_attrs(post_id, *keys)
post = post_id.is_a?(Integer) ? @repo.post(post_id) : post_id
post.attrs(*keys)
end
|
#post_deleted?(num) ⇒ Boolean
366
367
368
|
# File 'lib/scriptorium/api.rb', line 366
def post_deleted?(num)
@repo.post_deleted?(num)
end
|
#post_deployed?(num, view = nil) ⇒ Boolean
300
301
302
|
# File 'lib/scriptorium/api.rb', line 300
def post_deployed?(num, view = nil)
@repo.post_deployed?(num, view)
end
|
#post_published?(num, view = nil) ⇒ Boolean
271
272
273
|
# File 'lib/scriptorium/api.rb', line 271
def post_published?(num, view = nil)
@repo.post_published?(num, view)
end
|
#post_remove_tag(id, tag) ⇒ Object
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
# File 'lib/scriptorium/api.rb', line 516
def post_remove_tag(id, tag)
post = @repo.post(id)
raise CannotGetPost("Post with ID #{id} not found") if post.nil?
current_tags = post.tags.strip.split(/,\s*/)
new_tags = current_tags - [tag]
result = update_post(id, {tags: new_tags})
@repo.generate_post(id) if result
result
end
|
#post_remove_view(id, view) ⇒ Object
490
491
492
493
494
|
# File 'lib/scriptorium/api.rb', line 490
def post_remove_view(id, view)
view_name = view.is_a?(String) ? view : view.name
unlink_post(id, view_name)
end
|
#posts(view = nil, include_deleted: false, published: false) ⇒ Object
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
# File 'lib/scriptorium/api.rb', line 396
def posts(view = nil, include_deleted: false, published: false)
view ||= @repo.current_view&.name
if include_deleted
posts = @repo.all_posts_including_deleted(view)
else
posts = @repo.all_posts(view)
end
if published
posts = posts.select { |post| post_published?(post.id, view) }
end
posts
end
|
#publish_post(num, view = nil) ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/scriptorium/api.rb', line 245
def publish_post(num, view = nil)
check_invariants
msg = "num must be an Integer, got #{num.class}"
assume(msg) { num.is_a?(Integer) }
msg = "@repo must be a Scriptorium::Repo, got #{@repo.class}"
assume(msg) { @repo.is_a?(Scriptorium::Repo) }
post = @repo.publish_post(num, view)
verify { post.is_a?(Scriptorium::Post) }
check_invariants
post
end
|
#repo_exists?(path) ⇒ Boolean
29
30
31
|
# File 'lib/scriptorium/api.rb', line 29
def repo_exists?(path)
Dir.exist?(path)
end
|
#restore_backup(backup_name, strategy: :safe) ⇒ Object
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
|
# File 'lib/scriptorium/api.rb', line 1755
def restore_backup(backup_name, strategy: :safe)
check_invariants
backup_dir = get_backup_directory
backup_path = backup_dir/"data"/backup_name
raise BackupNotFound, "Backup '#{backup_name}' not found" unless File.exist?(backup_path)
case strategy
when :safe
pre_restore = create_backup(type: :full, label: "pre-restore-#{backup_name}")
sleep(2)
restore_from_backup(backup_path)
verify { File.exist?(@repo.root/"posts") }
check_invariants
{ restored: backup_name, pre_restore: pre_restore }
when :merge
restore_files_from_backup(backup_path)
verify { File.exist?(@repo.root/"posts") }
check_invariants
{ restored: backup_name, strategy: :merge }
when :destroy
restore_from_backup(backup_path)
verify { File.exist?(@repo.root/"posts") }
check_invariants
{ restored: backup_name, strategy: :destroy }
else
raise ArgumentError, "Invalid restore strategy: #{strategy}. Must be :safe, :merge, or :destroy"
end
end
|
#root ⇒ Object
82
83
84
|
# File 'lib/scriptorium/api.rb', line 82
def root
@repo.root
end
|
#search_posts(**criteria) ⇒ Object
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
# File 'lib/scriptorium/api.rb', line 755
def search_posts(**criteria)
all_posts = @repo.all_posts
matching_posts = []
all_posts.each do |post|
matches_all_criteria = true
criteria.each do |field, pattern|
field_value = case field
when :title
post.title
when :body
body_file = post.dir/"body.html"
File.exist?(body_file) ? read_file(body_file) : ""
when :tags
post.tags
when :blurb
post.blurb
else
raise UnknownSearchField(field)
end
if pattern.is_a?(Regexp)
matches_all_criteria = false unless field_value.match?(pattern)
else
matches_all_criteria = false unless field_value.include?(pattern.to_s)
end
break unless matches_all_criteria
end
matching_posts << post if matches_all_criteria
end
matching_posts
end
|
#select_posts(&block) ⇒ Object
Post selection and search
746
747
748
749
750
751
752
753
|
# File 'lib/scriptorium/api.rb', line 746
def select_posts(&block)
all_posts = @repo.all_posts
all_posts.select(&block)
end
|
#system_themes ⇒ Object
552
553
554
555
556
557
558
559
560
561
|
# File 'lib/scriptorium/api.rb', line 552
def system_themes
themes = []
system_file = @repo.root/:themes/"system.txt"
if File.exist?(system_file)
themes = read_file(system_file, lines: true, chomp: true)
end
themes
end
|
#testing ⇒ Object
90
91
92
|
# File 'lib/scriptorium/api.rb', line 90
def testing
@testing
end
|
#theme_exists?(theme_name) ⇒ Boolean
580
581
582
583
584
|
# File 'lib/scriptorium/api.rb', line 580
def theme_exists?(theme_name)
themes = themes_available
themes.include?(theme_name)
end
|
#themes_available ⇒ Object
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
# File 'lib/scriptorium/api.rb', line 537
def themes_available
themes = []
themes_dir = @repo.root/:themes
if Dir.exist?(themes_dir)
Dir.children(themes_dir).each do |item|
next if item == "system.txt" || item.start_with?(".")
next unless Dir.exist?(themes_dir/item)
themes << item
end
end
themes
end
|
#undelete_post(id) ⇒ Object
362
363
364
|
# File 'lib/scriptorium/api.rb', line 362
def undelete_post(num)
@repo.undelete_post(num)
end
|
#undeploy_post(num, view = nil) ⇒ Object
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
# File 'lib/scriptorium/api.rb', line 372
def undeploy_post(num, view = nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
unless post_deployed?(num, view)
puts "Post #{num} is not deployed in view '#{view}'"
return false
end
mark_post_undeployed(num, view)
@repo.generate_post(num)
deploy(view)
puts "Post #{num} undeployed and redeployed in view '#{view}'"
true
end
|
#unlink_post(id, view = nil) ⇒ Object
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
# File 'lib/scriptorium/api.rb', line 444
def unlink_post(id, view = nil)
view ||= @repo.current_view&.name
raise ViewTargetNil if view.nil?
post = @repo.post(id)
raise CannotGetPost("Post with ID #{id} not found") if post.nil?
current_views = post.views.strip.split(/\s+/)
new_views = current_views - [view]
result = update_post(id, {views: new_views})
@repo.generate_post(id) if result
result
end
|
#unpublish_post(num, view = nil) ⇒ Object
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/scriptorium/api.rb', line 259
def unpublish_post(num, view = nil)
check_invariants
msg = "num must be an Integer, got #{num.class}"
assume(msg) { num.is_a?(Integer) }
msg = "@repo must be a Scriptorium::Repo, got #{@repo.class}"
assume(msg) { @repo.is_a?(Scriptorium::Repo) }
@repo.unpublish_post(num, view)
check_invariants
end
|
#update_post(id, fields) ⇒ Object
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
|
# File 'lib/scriptorium/api.rb', line 1101
def update_post(id, fields)
post = @repo.post(id)
source_file = post.dir/"source.lt3"
return false unless File.exist?(source_file)
lines = read_file(source_file, lines: true, chomp: false)
updated = false
fields.each do |field, value|
value_array = Array(value)
case field
when :tags
new_value = value_array.join(", ")
else
new_value = value_array.join(' ')
end
lines.map! do |line|
if line.strip.start_with?(".#{field}")
= line.match(/(\s+#.*)$/)
= ? [1] : ""
timestamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
= " # updated #{field} #{timestamp}"
updated = true
".#{field} #{new_value}#{}#{}\n"
else
line
end
end
end
return false unless updated
write_file(source_file, lines.join)
true
end
|
#upload_asset(file_path, target = 'global', target_id = nil, filename: nil, **kwargs) ⇒ Object
890
891
892
893
894
895
896
897
898
899
900
901
902
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
|
# File 'lib/scriptorium/api.rb', line 890
def upload_asset(file_path, target = 'global', target_id = nil, filename: nil, **kwargs)
if kwargs.any?
target = kwargs[:target] || target
target_id = kwargs[:view] || target_id
end
unless File.exist?(file_path)
raise FileNotFoundError(file_path)
end
filename ||= File.basename(file_path)
target_dir = case target
when 'global'
@repo.root/"assets"
when 'library'
@repo.root/"assets"/"library"
when 'view'
target_id ||= @repo.current_view&.name
raise ViewTargetNil if target_id.nil?
@repo.root/"views"/target_id/"assets"
when 'post'
raise ArgumentError, "Post ID required for post uploads" if target_id.nil?
post_id = target_id.to_i
post_num = d4(post_id)
@repo.root/"posts"/post_num/"assets"
else
raise InvalidFormatError("target", target)
end
FileUtils.mkdir_p(target_dir)
target_file = target_dir/filename
FileUtils.cp(file_path, target_file)
target_file
end
|
#user_themes ⇒ Object
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
# File 'lib/scriptorium/api.rb', line 563
def user_themes
themes = []
themes_dir = @repo.root/:themes
system_themes_list = system_themes
if Dir.exist?(themes_dir)
Dir.children(themes_dir).each do |item|
next if item == "system.txt" || item.start_with?(".")
next unless Dir.exist?(themes_dir/item)
next if system_themes_list.include?(item)
themes << item
end
end
themes
end
|
#validate_rsync_destination(destination) ⇒ Object
Validate rsync destination format
1620
1621
1622
|
# File 'lib/scriptorium/api.rb', line 1620
def validate_rsync_destination(destination)
destination =~ /^[^@]+@[^:]+:.+/
end
|
#view(name = nil) ⇒ Object
99
100
101
102
103
104
105
106
|
# File 'lib/scriptorium/api.rb', line 99
def view(name = nil)
if name.nil?
@repo.current_view
else
result = @repo.view(name)
result
end
end
|
#views ⇒ Object
108
109
110
|
# File 'lib/scriptorium/api.rb', line 108
def views
@repo&.views || []
end
|
#views_for(post_or_id) ⇒ Object
116
117
118
119
|
# File 'lib/scriptorium/api.rb', line 116
def views_for(post_or_id)
post = post_or_id.is_a?(Integer) ? @repo.post(post_or_id) : post_or_id
post.views&.split(/\s+/) || []
end
|
615
616
617
618
619
|
# File 'lib/scriptorium/api.rb', line 615
def widgets_available
widgets_file = @repo.root/:config/"widgets.txt"
return [] unless File.exist?(widgets_file)
read_file(widgets_file, lines: true, chomp: true)
end
|