Class: Scriptorium::View
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#apply_theme(theme) ⇒ Object
-
#build_banner(arg) ⇒ Object
-
#build_banner_image(image_filename) ⇒ Object
-
#build_banner_svg(arg) ⇒ Object
-
#build_banner_svg_from_file ⇒ Object
-
#build_containers ⇒ Object
-
#build_footer(sections) ⇒ Object
-
#build_header(sections) ⇒ Object
-
#build_left(sections) ⇒ Object
-
#build_main(sections) ⇒ Object
-
#build_nav(arg) ⇒ Object
-
#build_right(sections) ⇒ Object
-
#build_section(section, hash2 = {}, args = "") ⇒ Object
To build a header, I start with two things: config/header.txt (which is user-supplied and has things such as “title” in it); and layout/header.html (which is a template with <header> tags enclosing at least a line like “<!– Section: header –>”.
-
#build_widgets(arg) ⇒ Object
-
#content_tag(section) ⇒ Object
-
#define_invariants ⇒ Object
-
#generate_bootstrap_css(view = nil) ⇒ Object
-
#generate_bootstrap_js(view = nil) ⇒ Object
-
#generate_bootstrap_navbar(nav_content) ⇒ Object
-
#generate_dropdown_child(child) ⇒ Object
-
#generate_dropdown_item(item) ⇒ Object
-
#generate_empty_containers ⇒ Object
-
#generate_front_page ⇒ Object
-
#generate_html_head(view = nil) ⇒ Object
-
#generate_nav_item(item) ⇒ Object
-
#generate_navbar_items(menu_items) ⇒ Object
-
#generate_post_index ⇒ Object
-
#generate_reddit_button(post_data = nil) ⇒ Object
-
#generate_social_meta_tags(args = nil, post_data = nil) ⇒ Object
-
#generate_syntax_css ⇒ Object
-
#get_common_js(view = nil) ⇒ Object
-
#get_page_link(filename) ⇒ Object
-
#highlight_code(code, language = nil) ⇒ Object
-
#initialize(name, title, subtitle = "", theme = "standard") ⇒ View
constructor
-
#inspect ⇒ Object
-
#paginate_posts ⇒ Object
-
#pagination_bar(group, count, nth) ⇒ Object
nth group of total ‘count’.
-
#parse_navbar_content(content) ⇒ Object
-
#placeholder_text(str) ⇒ Object
-
#post_index_array ⇒ Object
-
#post_index_entry(post) ⇒ Object
-
#read_layout ⇒ Object
-
#section_append(sec, str) ⇒ Object
-
#section_core(section, hash) ⇒ Object
-
#section_hash(section) ⇒ Object
-
#view_posts ⇒ Object
Methods included from Contract
#assume, #check_invariants, enabled?, #invariant, #verify
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 Exceptions
#make_exception
Constructor Details
#initialize(name, title, subtitle = "", theme = "standard") ⇒ View
Returns a new instance of View.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/scriptorium/view.rb', line 27
def initialize(name, title, subtitle = "", theme = "standard")
assume { name.is_a?(String) }
assume { title.is_a?(String) }
assume { subtitle.is_a?(String) }
assume { theme.is_a?(String) }
validate_name(name)
validate_title(title)
@name, @title, @subtitle, @theme = name, title, subtitle, theme
@root = Scriptorium::Repo.root
@repo = Scriptorium::Repo.repo
@dir = "#@root/views/#{name}"
@predef = Scriptorium::StandardFiles.new
define_invariants
verify { @name == name }
verify { @title == title }
check_invariants
end
|
Instance Attribute Details
#dir ⇒ Object
Returns the value of attribute dir.
9
10
11
|
# File 'lib/scriptorium/view.rb', line 9
def dir
@dir
end
|
#name ⇒ Object
Returns the value of attribute name.
9
10
11
|
# File 'lib/scriptorium/view.rb', line 9
def name
@name
end
|
#subtitle ⇒ Object
Returns the value of attribute subtitle.
9
10
11
|
# File 'lib/scriptorium/view.rb', line 9
def subtitle
@subtitle
end
|
#theme(change = nil) ⇒ Object
Returns the value of attribute theme.
9
10
11
|
# File 'lib/scriptorium/view.rb', line 9
def theme
@theme
end
|
#title ⇒ Object
Returns the value of attribute title.
9
10
11
|
# File 'lib/scriptorium/view.rb', line 9
def title
@title
end
|
Class Method Details
.create_sample_view(repo) ⇒ Object
11
12
13
14
|
# File 'lib/scriptorium/view.rb', line 11
def self.create_sample_view(repo)
repo.create_view("sample", "My first view", "This is just a sample")
end
|
Instance Method Details
#apply_theme(theme) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/scriptorium/view.rb', line 156
def apply_theme(theme)
check_invariants
assume { theme.is_a?(String) && !theme.empty? }
t = Scriptorium::Theme.new(@root, theme)
need(:file, t.file("layout.txt"), ThemeFileNotFound)
FileUtils.cp(t.file("layout.txt"), @dir/:config)
names = %w[header footer left right main]
lay = @root/:themes/theme/:layout
names.each do |name|
f1, f2 = lay/:config/"#{name}.txt", dir/:config
need(:file, f1, ThemeFileNotFound)
FileUtils.cp(f1, f2)
end
generate_empty_containers
verify { @theme == theme }
check_invariants
end
|
#build_banner(arg) ⇒ Object
293
294
295
296
297
298
299
|
# File 'lib/scriptorium/view.rb', line 293
def build_banner(arg)
return build_banner_svg_from_file if arg == "svg"
return build_banner_image(arg)
end
|
#build_banner_image(image_filename) ⇒ Object
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
357
358
|
# File 'lib/scriptorium/view.rb', line 316
def build_banner_image(image_filename)
image_paths = [
@dir/:assets/image_filename, @repo.root/:assets/image_filename, ]
image_path = image_paths.find { |path| File.exist?(path) }
if image_path
if image_path.to_s.start_with?(@dir.to_s)
relative_path = image_path.to_s.sub(@dir.to_s + "/", "")
else
relative_path = "../assets/#{image_filename}"
end
html = %[<img src='#{relative_path}' alt='Banner Image' style='width: 100%; height: auto;' />]
return html
else
global_assets_dir = @repo.root/:assets
global_image_path = global_assets_dir/image_filename
if File.exist?(global_image_path)
view_assets_dir = @dir/:assets
make_dir(view_assets_dir) unless Dir.exist?(view_assets_dir)
FileUtils.cp(global_image_path, view_assets_dir/image_filename)
relative_path = "assets/#{image_filename}"
html = %[<img src='#{relative_path}' alt='Banner Image' style='width: 100%; height: auto;' />]
return html
else
html = %[<p>Banner image missing: #{image_filename}</p>]
return html
end
end
end
|
#build_banner_svg(arg) ⇒ Object
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
# File 'lib/scriptorium/view.rb', line 360
def build_banner_svg(arg)
bsvg = Scriptorium::BannerSVG.new(@title, @subtitle)
config_file = @dir/:config/"config.txt"
if File.exist?(config_file)
bsvg.(config_file)
else
bsvg.
end
code = bsvg.get_svg
end
|
#build_banner_svg_from_file ⇒ Object
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
# File 'lib/scriptorium/view.rb', line 301
def build_banner_svg_from_file
bsvg = Scriptorium::BannerSVG.new(@title, @subtitle)
svg_config_file = @dir/:config/"svg.txt"
if File.exist?(svg_config_file)
bsvg.(svg_config_file)
else
bsvg.
end
bsvg.get_svg
end
|
#build_containers ⇒ Object
858
859
860
861
862
863
864
865
866
867
868
869
870
|
# File 'lib/scriptorium/view.rb', line 858
def build_containers
sections = read_layout
content = ""
content << (sections)
content << "<!-- before left/main/right -->\n"
content << "<div style='display: flex; flex-grow: 1; height: 100%; flex-direction: row;'>"
content << build_left(sections)
content << build_main(sections)
content << build_right(sections)
content << "</div> <!-- after left/main/right --></div>\n"
content << (sections)
content
end
|
571
572
573
574
575
|
# File 'lib/scriptorium/view.rb', line 571
def (sections)
args = sections["footer"]
return "" unless args
build_section("footer", {}, args)
end
|
278
279
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/scriptorium/view.rb', line 278
def (sections)
args = sections["header"]
return "" unless args
h2 = {
"title" => ->(arg = nil) { " <h1>#{escape_html(@title)}</h1>" },
"subtitle" => ->(arg = nil) { " <p>#{escape_html(@subtitle)}</p>" },
"nav" => ->(arg = nil) { build_nav(arg) },
"banner" => ->(arg = nil) { build_banner(arg) }
}
build_section("header", h2, args)
end
|
#build_left(sections) ⇒ Object
577
578
579
580
581
582
|
# File 'lib/scriptorium/view.rb', line 577
def build_left(sections)
args = sections["left"]
return "" unless args
h2 = { "widget" => ->(arg = nil) { build_widgets(arg) } }
build_section("left", h2, args)
end
|
#build_main(sections) ⇒ Object
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
# File 'lib/scriptorium/view.rb', line 591
def build_main(sections)
args = sections["main"]
return "" unless args
html = " <!-- Section: main (output) -->\n"
html << %[ <div id="main" class="main" style="flex-grow: 1; padding: 10px; overflow-y: auto; position: relative; display: flex; flex-direction: column;">]
html << @predef.post_index_style
if view_posts.empty?
html << " <h1>No posts yet!</h1>"
else
paginate_posts
need(:file, self.dir/:output/"post_index.html")
html << read_file(self.dir/:output/"post_index.html")
end
html << "</div> <!-- end main -->\n"
end
|
#build_nav(arg) ⇒ Object
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
# File 'lib/scriptorium/view.rb', line 375
def build_nav(arg)
nav_file = if arg.nil? || arg.strip.empty?
@dir/:config/"navbar.txt"
else
@dir/:config/"#{arg}"
end
nav_content = read_file(nav_file, missing_fallback: "<p>Navigation not available</p>")
generate_bootstrap_navbar(nav_content)
end
|
#build_right(sections) ⇒ Object
584
585
586
587
588
589
|
# File 'lib/scriptorium/view.rb', line 584
def build_right(sections)
args = sections["right"]
return "" unless args
h2 = { "widget" => ->(arg = nil) { build_widgets(arg) } }
build_section("right", h2, args)
end
|
#build_section(section, hash2 = {}, args = "") ⇒ Object
To build a header, I start with two things:
config/header.txt (which is user-supplied and has things such as "title" in it); and
layout/header.html (which is a template with <header> tags enclosing at least a line
like "<!-- Section: header -->"
get core: I process header.txt line by line, gathering the “core” or “guts” of the header. sub into template: I substitute this into the template contents and write output: write the result to output/panes/header.html
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
|
# File 'lib/scriptorium/view.rb', line 248
def build_section(section, hash2 = {}, args = "")
config = @dir/:config/"#{section}.txt"
template = @dir/:layout/"#{section}.html"
output = @dir/:output/:panes/"#{section}.html"
FileUtils.mkdir_p(File.dirname(output))
need(:file, template)
hash = section_hash(section)
hash.merge!(hash2)
core = section_core(section, hash)
temp_txt = read_file(template)
target = content_tag(section)
temp_txt.sub!(target, core)
begin
write_file(output, temp_txt)
rescue Errno::EACCES, Errno::ENOSPC => e
raise SectionOutputError(output, section, e.message)
end
html = read_file(output)
html
end
|
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
# File 'lib/scriptorium/view.rb', line 535
def build_widgets(arg)
check_invariants
assume { arg.is_a?(String) }
validate_widget_arg(arg)
widgets = arg.split
content = ""
widgets.each do |widget|
validate_widget_name(widget)
widget_class = eval("Scriptorium::Widget::#{widget.capitalize}")
obj = widget_class.new(@repo, self)
obj.generate
content << obj.card
end
verify { content.is_a?(String) }
check_invariants
content
end
|
#content_tag(section) ⇒ Object
179
180
181
|
# File 'lib/scriptorium/view.rb', line 179
def content_tag(section)
"<!-- Section: #{section} -->"
end
|
#define_invariants ⇒ Object
17
18
19
20
21
22
23
24
25
|
# File 'lib/scriptorium/view.rb', line 17
def define_invariants
invariant { @name.is_a?(String) && !@name.empty? }
invariant { @title.is_a?(String) && !@title.empty? }
invariant { @subtitle.is_a?(String) }
invariant { @theme.is_a?(String) && !@theme.empty? }
invariant { @root.is_a?(String) && !@root.empty? }
invariant { @repo.is_a?(Scriptorium::Repo) }
invariant { @dir.is_a?(String) && !@dir.empty? }
end
|
#generate_bootstrap_css(view = nil) ⇒ Object
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
|
# File 'lib/scriptorium/view.rb', line 688
def generate_bootstrap_css(view = nil)
global_boot = @root/:config/"bootstrap_css.txt"
view_boot = @dir/:config/"bootstrap_css.txt"
bs_file = view ? view_boot : global_boot
lines = (bs_file)
href = rel = integrity = crossorigin = nil
lines.each do |line|
component, args = line.split(/\s+/, 2)
case component.downcase
when "href"
href = args
when "rel"
rel = args
when "integrity"
integrity = args
when "crossorigin"
crossorigin = args
end
end
content = %[<link rel="stylesheet" href="#{href}"></link>\n]
content
end
|
#generate_bootstrap_js(view = nil) ⇒ Object
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
|
# File 'lib/scriptorium/view.rb', line 714
def generate_bootstrap_js(view = nil)
global_boot = @root/:config/"bootstrap_js.txt"
view_boot = @dir/:config/"bootstrap_js.txt"
bs_file = view ? view_boot : global_boot
lines = (bs_file)
src = integrity = crossorigin = nil
lines.each do |line|
component, args = line.split(/\s+/, 2)
case component.downcase
when "src"
src = args
when "rel"
rel = args
when "integrity"
integrity = args
when "crossorigin"
crossorigin = args
end
end
content = %[<script src="#{src}"></script>\n]
content
end
|
#generate_bootstrap_navbar(nav_content) ⇒ Object
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
# File 'lib/scriptorium/view.rb', line 390
def generate_bootstrap_navbar(nav_content)
= parse_navbar_content(nav_content)
html = <<~HTML
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
#{generate_navbar_items()}
</ul>
</div>
</div>
</nav>
HTML
html
end
|
#generate_dropdown_child(child) ⇒ Object
492
493
494
495
496
497
498
499
500
501
502
|
# File 'lib/scriptorium/view.rb', line 492
def generate_dropdown_child(child)
link_url, warning = get_page_link(child[:filename])
html = <<~HTML
<li><a class="dropdown-item" href="javascript:void(0)" onclick="load_main('#{link_url}')">#{escape_html(child[:title])}</a></li>
HTML
html << "<!-- #{warning} -->\n" if warning
html
end
|
#generate_dropdown_item(item) ⇒ Object
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
# File 'lib/scriptorium/view.rb', line 469
def generate_dropdown_item(item)
dropdown_id = "dropdown-#{item[:label].downcase.gsub(/\s+/, '-')}"
html = <<~HTML
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
#{escape_html(item[:label])}
</a>
<ul class="dropdown-menu">
HTML
item[:children].each do |child|
html << generate_dropdown_child(child)
end
html << <<~HTML
</ul>
</li>
HTML
html
end
|
#generate_empty_containers ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/scriptorium/view.rb', line 112
def generate_empty_containers
layout_file = @dir/:config/"layout.txt"
return unless File.exist?(layout_file)
flexing = {
header: %[id="header" class="header" style="padding: 10px;"],
footer: %[class="footer" style="background: lightgray; padding: 10px;"],
left: %[class="left" style="width: %{width}; background: #f0f0f0; padding: 10px; flex-grow: 0; flex-shrink: 0;"],
right: %[class="right" style="width: %{width}; background: #f0f0f0; padding: 10px; flex-grow: 0; flex-shrink: 0;"],
main: %[class="main" style="flex-grow: 1; padding: 10px;"]
}
sections = read_layout
lines = sections.keys
lines.each do |section|
args = sections[section] filename = @dir/:layout/"#{section}.html"
tag = section tag = "aside" if section == 'left' || tag == 'right'
inline = flexing[section.to_sym]
if section == "left" || section == "right"
mod = {width: args}
inline = inline % mod
end
content = <<~HTML
<#{tag} #{inline}>
<!-- Section: #{section} -->
</#{tag}>
HTML
write_file(filename, content)
end
end
|
#generate_front_page ⇒ Object
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
|
# File 'lib/scriptorium/view.rb', line 905
def generate_front_page
layout_file = @dir/:config/"layout.txt"
index_file = @dir/:output/"index.html"
panes = @dir/:output/:panes
FileUtils.mkdir_p(File.dirname(index_file))
html_head = generate_html_head(true)
content = build_containers
common = get_common_js
boot = generate_bootstrap_js
full_html = <<~HTML
<!DOCTYPE html>
#{html_head}
<html style="height: 100%; margin: 0;">
<body style="height: 100%; margin: 0; display: flex; flex-direction: column;">
#{content.strip}
#{boot.strip}
#{common.strip}
</body>
</html>
HTML
begin
write_file(index_file, full_html)
rescue Errno::ENOSPC, Errno::EACCES => e
raise FailedToWriteFrontPage(e.message)
end
begin
write_file("/tmp/full.html", full_html)
rescue => e
end
pages_source = @dir/:pages
pages_output = @dir/:output/:pages
if Dir.exist?(pages_source)
FileUtils.mkdir_p(pages_output)
Dir.glob(pages_source/"*").each do |file|
next unless File.file?(file)
FileUtils.cp(file, pages_output/File.basename(file))
end
end
end
|
#generate_html_head(view = nil) ⇒ Object
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
|
# File 'lib/scriptorium/view.rb', line 638
def generate_html_head(view = nil)
global_head = @root/:config/"global-head.txt"
view_head = @dir/:config/"global-head.txt"
head_file = view ? view_head : global_head
which = view ? "view" : "global"
line1 = "<!-- head info from #{which} -->"
lines = (head_file)
content = "<head>\n#{line1}\n<title>#{@title}</title>\n"
lines.each do |line|
component, args = line.split(/\s+/, 2)
case component.downcase
when "charset"
@charset = args
content << %[<meta charset="#{args}">\n]
when "desc"
@desc = args
content << %[<meta name="description" content="#{args}">\n]
when "viewport"
@viewport = args
str = args.split.join(" ")
content << %[<meta name="viewport" content="#{str}">\n]
when "robots"
@robots = args
str = args.split.join(", ")
content << %[<meta name="robots" content="#{str}">\n]
when "bootstrap"
content << generate_bootstrap_css(view)
when "social"
content << generate_social_meta_tags(args)
when "syntax"
content << generate_syntax_css
end
end
content << "</head>\n"
content
end
|
#generate_nav_item(item) ⇒ Object
504
505
506
507
508
509
510
511
512
513
514
515
516
|
# File 'lib/scriptorium/view.rb', line 504
def generate_nav_item(item)
link_url, warning = get_page_link(item[:filename])
html = <<~HTML
<li class="nav-item">
<a class="nav-link" href="javascript:void(0)" onclick="load_main('#{link_url}')">#{escape_html(item[:title])}</a>
</li>
HTML
html << "<!-- #{warning} -->\n" if warning
html
end
|
#generate_navbar_items(menu_items) ⇒ Object
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
# File 'lib/scriptorium/view.rb', line 454
def generate_navbar_items()
html = ""
.each do |item|
case item[:type]
when :dropdown
html << generate_dropdown_item(item)
when :item
html << generate_nav_item(item)
end
end
html
end
|
#generate_post_index ⇒ Object
608
609
610
611
612
613
614
615
616
|
# File 'lib/scriptorium/view.rb', line 608
def generate_post_index
posts = @repo.all_posts(self)
str = ""
posts.each do |post|
str << post_index_entry(post)
end
write_file(@dir/:output/"post_index.html", str)
end
|
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
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
|
# File 'lib/scriptorium/view.rb', line 797
def generate_reddit_button(post_data = nil)
social_config_file = @dir/:config/"social.txt"
return "" unless File.exist?(social_config_file)
social_config = (social_config_file)
reddit_enabled = social_config.any? { |line| line.strip.downcase == "reddit" }
return "" unless reddit_enabled
reddit_config_file = @dir/:config/"reddit.txt"
return "" unless File.exist?(reddit_config_file)
reddit_config = (reddit_config_file)
button_enabled = false
subreddit = ""
hover_text = ""
reddit_config.each do |line|
component, args = line.split(/\s+/, 2)
case component.downcase
when "button"
button_enabled = (args&.downcase == "true")
when "subreddit"
subreddit = args&.strip || ""
when "hover_text"
hover_text = args&.strip || ""
end
end
return "" unless button_enabled
if post_data
title = post_data[:"post.title"] || @title
url = "posts/#{post_data[:"post.slug"] || slugify(post_data[:"post.id"], title)}.html"
else
title = @title
url = "index.html"
end
if subreddit.empty?
reddit_url = "https://reddit.com/submit?url=#{escape_html(url)}&title=#{escape_html(title)}"
else
reddit_url = "https://reddit.com/r/#{subreddit}/submit?url=#{escape_html(url)}&title=#{escape_html(title)}"
end
if hover_text.empty?
hover_text = subreddit.empty? ? "Share on Reddit" : "Share on r/#{subreddit}"
end
button_html = %[<a href="#{reddit_url}" target="_blank" title="#{hover_text}" style="text-decoration: none; margin-right: 8px;">
<img src="assets/reddit-logo.png" width="16" height="16" alt="Share on Reddit" style="vertical-align: middle;">
</a>]
button_html
end
|
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
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
|
# File 'lib/scriptorium/view.rb', line 738
def generate_social_meta_tags(args = nil, post_data = nil)
social_config_file = @dir/:config/"social.txt"
return "" unless File.exist?(social_config_file)
social_config = (social_config_file)
platforms = []
social_config.each do |line|
platform = line.strip.downcase
platforms << platform if platform.match?(/^(facebook|twitter|linkedin|reddit)$/)
end
return "" if platforms.empty?
is_post = !post_data.nil?
if is_post
title = post_data[:"post.title"] || @title
description = post_data[:"post.blurb"] || post_data[:"post.body"]&.truncate(200) || @desc || @subtitle || @title
url = "posts/#{post_data[:"post.slug"] || slugify(post_data[:"post.id"], title)}.html"
type = "article"
else
title = @title
description = @desc || @subtitle || @title
url = "index.html"
type = "website"
end
content = ""
if platforms.include?("facebook") || platforms.include?("linkedin")
content << %[<meta property="og:title" content="#{escape_html(title)}">\n]
content << %[<meta property="og:type" content="#{type}">\n]
content << %[<meta property="og:url" content="#{url}">\n]
content << %[<meta property="og:description" content="#{escape_html(description)}">\n]
content << %[<meta property="og:site_name" content="#{escape_html(@title)}">\n]
if is_post && post_data[:"post.pubdate"]
content << %[<meta property="article:published_time" content="#{post_data[:"post.pubdate"]}">\n]
end
end
if platforms.include?("twitter")
content << %[<meta name="twitter:card" content="summary">\n]
content << %[<meta name="twitter:title" content="#{escape_html(title)}">\n]
content << %[<meta name="twitter:description" content="#{escape_html(description)}">\n]
content << %[<meta name="twitter:url" content="#{url}">\n]
end
content
end
|
#generate_syntax_css ⇒ Object
966
967
968
969
|
# File 'lib/scriptorium/view.rb', line 966
def generate_syntax_css
highlighter = Scriptorium::SyntaxHighlighter.new
"<style>\n#{highlighter.generate_css}\n</style>\n"
end
|
#get_common_js(view = nil) ⇒ Object
680
681
682
683
684
685
686
|
# File 'lib/scriptorium/view.rb', line 680
def get_common_js(view = nil)
global_js = @root/:config/"common.js"
view_js = @dir/:config/"common.js"
js_file = view ? view_js : global_js
code = read_file(js_file)
return %[<script>#{code}</script>\n]
end
|
#get_page_link(filename) ⇒ Object
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
# File 'lib/scriptorium/view.rb', line 518
def get_page_link(filename)
page_file = @dir/:pages/"#{filename}.html"
if File.exist?(page_file)
link_url = "pages/#{filename}.html"
warning = nil
else
link_url = "pages/#{filename}.html"
warning = "Warning: Page file '#{filename}.html' not found in pages directory"
end
[link_url, warning]
end
|
#highlight_code(code, language = nil) ⇒ Object
971
972
973
974
|
# File 'lib/scriptorium/view.rb', line 971
def highlight_code(code, language = nil)
highlighter = Scriptorium::SyntaxHighlighter.new
highlighter.highlight(code, language)
end
|
#inspect ⇒ Object
48
49
50
|
# File 'lib/scriptorium/view.rb', line 48
def inspect
"<View: #@name #{@title.inspect} theme: #@theme>"
end
|
#paginate_posts ⇒ Object
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
|
# File 'lib/scriptorium/view.rb', line 885
def paginate_posts
posts = @repo.all_posts(self)
posts.sort! {|a,b| cf_time(b.pubdate, a.pubdate) }
ppp = 10 pages = []
posts.each_slice(ppp).with_index do |group, i|
pages << group.map {|post| post_index_entry(post) }
end
out = self.dir/:output
pages.each.with_index do |page, i|
bar = (page, pages.size, i+1)
page << %[<div style="position: absolute; bottom: 0; width: 100%;">#{bar}</div>]
write_file(out/"page#{i+1}.html", page.join)
end
post_index_link = out/"post_index.html"
File.delete(post_index_link) if File.exist?(post_index_link)
FileUtils.ln(out/"page1.html", post_index_link)
end
|
nth group of total ‘count’
872
873
874
875
876
877
878
879
880
881
882
883
|
# File 'lib/scriptorium/view.rb', line 872
def (group, count, nth) str = %[<div style="align-self: flex-end;">Pages: ]
1.upto(count) do |i|
if i == nth str << "<b>[#{i}]</b> "
else
str << %[<a href="javascript:void(0)" style="text-decoration: none;"
onclick="load_main('page#{i}.html')">#{i} </a>]
end
end
str << "<br><br></div>"
end
|
#parse_navbar_content(content) ⇒ Object
412
413
414
415
416
417
418
419
420
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
|
# File 'lib/scriptorium/view.rb', line 412
def parse_navbar_content(content)
= []
current_dropdown = nil
content.lines.each do |line|
line = line.rstrip next if line.empty? || line.start_with?('#')
if line.start_with?('=')
label = line[1..-1].strip
current_dropdown = { type: :dropdown, label: label, children: [] }
<< current_dropdown
elsif line.start_with?(' ')
if current_dropdown
clean_line = line.strip
if clean_line.include?(' ') parts = clean_line.split(/\s{2,}/, 2) if parts.length >= 2
title, filename = parts[0], parts[1]
current_dropdown[:children] << { type: :child, title: title, filename: filename }
end
end
end
elsif line.start_with?('-')
clean_line = line[1..-1].strip
if clean_line.include?(' ') parts = clean_line.split(/\s{2,}/, 2) if parts.length >= 2
title, filename = parts[0], parts[1]
<< { type: :item, title: title, filename: filename }
end
end
end
end
end
|
#placeholder_text(str) ⇒ Object
183
184
185
186
187
188
189
190
|
# File 'lib/scriptorium/view.rb', line 183
def placeholder_text(str)
if str.start_with?("@")
file = @dir/:config/:text/"#{str[1..]}"
read_file(file, missing_fallback: "[Missing: #{file}]")
else
str
end
end
|
#post_index_array ⇒ Object
628
629
630
631
|
# File 'lib/scriptorium/view.rb', line 628
def post_index_array
posts = view_posts.sort {|a,b| cf_time(b.pubdate, a.pubdate) }
posts.map {|post| post_index_entry(post)}
end
|
#post_index_entry(post) ⇒ Object
618
619
620
621
622
623
624
625
626
|
# File 'lib/scriptorium/view.rb', line 618
def post_index_entry(post)
num, title, pubdate, blurb = post.attrs(:id, :title, :pubdate, :blurb)
template = @predef.index_entry
entry = substitute(post, template)
entry
end
|
#read_layout ⇒ Object
-
The theme provides layout/config/header.txt with default content instructions.
-
When the theme is applied, header.txt is copied to views/VIEW/config/.
-
A placeholder layout/header.html is created in views/VIEW/layout/ with <!– HEADER CONTENT –>.
-
The file views/VIEW/config/header.txt is parsed to generate actual HTML.
-
That HTML replaces the placeholder and is written to views/VIEW/output/panes/header.html.
-
Later, output/panes/header.html is included when assembling views/VIEW/output/index.html.
That process is clean and logical. I see only minor points worth considering:
Copying header.txt from theme to view config/ is irreversible by design—once copied, any theme updates won’t affect the view’s header.txt. That’s good for isolation, but it might be worth exposing a way to “reapply” or “sync” a theme’s layout/config/ if desired. Placeholder files like layout/header.html in layout/ may be unnecessary once output/panes/header.html is reliably generated. If they exist solely for the <!– CONTENT –> tags, consider templating that in-memory instead. You may want to enforce (or warn) if config/header.txt is missing or invalid at generation time, to catch misconfigured views. If you add more optional components (like navbars, banners, etc.), consider adding light validation or doc comments to header.txt to aid future users/editors.
But overall, the process is robust and well thought-out. No major changes needed.
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/scriptorium/view.rb', line 93
def read_layout
layout_file = @dir/:config/"layout.txt"
need(:file, layout_file, LayoutFileMissing)
lines = (layout_file)
containers = {}
secs = []
lines.each do |line|
sec, args = line.split(/\s+/, 2)
containers[sec] = (args || "")
secs << sec
end
directives = %w[header footer left right main]
secs.each {|sec| raise LayoutHasUnknownTag(sec) unless directives.include?(sec)}
directives.each {|sec| raise LayoutHasDuplicateTags(sec) if lines.count(sec) > 1}
containers
end
|
#section_append(sec, str) ⇒ Object
192
193
194
195
196
197
|
# File 'lib/scriptorium/view.rb', line 192
def section_append(sec, str)
file = @dir/:config/"#{sec}.txt"
text = read_file(file)
text << str
write_file(file, text)
end
|
#section_core(section, hash) ⇒ Object
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
|
# File 'lib/scriptorium/view.rb', line 205
def section_core(section, hash)
cfg = @dir/:config
template = @dir/:layout/"#{section}.html"
sectxt = cfg/"#{section}.txt"
lines = (sectxt)
if lines.empty? && section != "main"
section_append(section, "\ntext This is #{section}...")
lines = (sectxt)
end
result = "<!-- Section: #{section} (output) -->\n"
lines.each do |line|
component, arg = line.split(/\s+/, 2)
if component.nil? || component.strip.empty?
result << "<!-- Invalid config line: #{line.inspect} -->\n"
next
end
component = component.downcase
if hash.key?(component)
result << hash[component].call(arg)
else
result << "<!-- Unknown component: #{component} -->\n"
end
end
result
end
|
#section_hash(section) ⇒ Object
199
200
201
202
203
|
# File 'lib/scriptorium/view.rb', line 199
def section_hash(section)
hash = Hash.new { |hash, key| ->(arg = nil) { "<!-- Not defined for key: #{key} -->\n" } }
hash["text"] = ->(arg) { " <p>" + placeholder_text(arg) + "</p>\n" }
hash
end
|
#view_posts ⇒ Object
633
634
635
636
|
# File 'lib/scriptorium/view.rb', line 633
def view_posts
posts = []
@repo.all_posts(self).sort_by {|post| post.pubdate}
end
|