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_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_highlight_css(view = nil) ⇒ Object
-
#generate_highlight_js(view = nil) ⇒ Object
-
#generate_highlight_ruby_js(view = nil) ⇒ 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
-
#get_common_js(view = nil) ⇒ Object
-
#get_page_link(filename) ⇒ 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
-
#read_post_index_config ⇒ 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
#add_post_to_state_file, #cf_time, #change_config, #clean_slugify, #copy_gem_asset_to_user, #copy_to_clipboard, #d4, #escape_html, #find_asset, #format_date, #generate_missing_asset_svg, #get_asset_path, #get_from_clipboard, #getvars, #list_gem_assets, #make_dir, #make_tree, #need, #parse_commented_file, #post_compare, #post_in_state_file?, #read_commented_file, #read_file, #read_post_state_file, #remove_post_from_state_file, #see, #see_file, #slugify, #substitute, #system!, #tty, #view_dir, #write_file, #write_file!, #write_post_state_file, #ymdhms, #ymdhms_filename
Methods included from Exceptions
#make_exception
Constructor Details
#initialize(name, title, subtitle = "", theme = "standard") ⇒ View
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/scriptorium/view.rb', line 26
def initialize(name, title, subtitle = "", theme = "standard")
msg = "name must be a String, got #{name.class}"
assume(msg) { name.is_a?(String) }
msg = "title must be a String, got #{title.class}"
assume(msg) { title.is_a?(String) }
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) }
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.
8
9
10
|
# File 'lib/scriptorium/view.rb', line 8
def dir
@dir
end
|
#name ⇒ Object
Returns the value of attribute name.
8
9
10
|
# File 'lib/scriptorium/view.rb', line 8
def name
@name
end
|
#subtitle ⇒ Object
Returns the value of attribute subtitle.
8
9
10
|
# File 'lib/scriptorium/view.rb', line 8
def subtitle
@subtitle
end
|
#theme(change = nil) ⇒ Object
Returns the value of attribute theme.
8
9
10
|
# File 'lib/scriptorium/view.rb', line 8
def theme
@theme
end
|
#title ⇒ Object
Returns the value of attribute title.
8
9
10
|
# File 'lib/scriptorium/view.rb', line 8
def title
@title
end
|
Class Method Details
.create_sample_view(repo) ⇒ Object
10
11
12
13
|
# File 'lib/scriptorium/view.rb', line 10
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/scriptorium/view.rb', line 134
def apply_theme(theme)
check_invariants
msg = "theme must be a non-empty String, got #{theme.class} (#{theme.inspect})"
assume(msg) { 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
266
267
268
269
270
271
272
|
# File 'lib/scriptorium/view.rb', line 266
def build_banner(arg)
return build_banner_svg("svg") if arg == "svg"
return build_banner_image(arg)
end
|
#build_banner_image(image_filename) ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'lib/scriptorium/view.rb', line 276
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
# File 'lib/scriptorium/view.rb', line 320
def build_banner_svg(arg)
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
859
860
861
862
863
864
865
866
867
868
869
870
871
|
# File 'lib/scriptorium/view.rb', line 859
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
|
546
547
548
549
550
|
# File 'lib/scriptorium/view.rb', line 546
def (sections)
args = sections["footer"]
return "" unless args
build_section("footer", {}, args)
end
|
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/scriptorium/view.rb', line 251
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
552
553
554
555
556
557
|
# File 'lib/scriptorium/view.rb', line 552
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
# File 'lib/scriptorium/view.rb', line 566
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: 5px; overflow-y: auto; position: relative; display: block;">]
html << support_data('post_index/style.css')
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
# File 'lib/scriptorium/view.rb', line 335
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
559
560
561
562
563
564
|
# File 'lib/scriptorium/view.rb', line 559
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/scriptorium/view.rb', line 226
def build_section(section, hash2 = {}, args = "")
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)
write_file(output, temp_txt)
html = read_file(output)
html
end
|
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
# File 'lib/scriptorium/view.rb', line 493
def build_widgets(arg)
check_invariants
msg = "arg must be a String, got #{arg.class}"
assume(msg) { arg.is_a?(String) }
validate_widget_arg(arg)
widgets = arg.split
content = ""
widgets.each do |widget|
validate_widget_name(widget)
begin
widget_class = eval("Scriptorium::Widget::#{widget.capitalize}")
obj = widget_class.new(@repo, self)
obj.generate
content << obj.card
rescue => e
raise CannotBuildWidget("Failed to build widget '#{widget}': #{e.message}")
end
end
if content.strip.empty?
result = content
else
result = <<~HTML
<div class="bootstrap-scope">
#{content}
</div>
HTML
end
verify { result.is_a?(String) }
check_invariants
result
end
|
#content_tag(section) ⇒ Object
158
159
160
|
# File 'lib/scriptorium/view.rb', line 158
def content_tag(section)
"<!-- Section: #{section} -->"
end
|
#define_invariants ⇒ Object
16
17
18
19
20
21
22
23
24
|
# File 'lib/scriptorium/view.rb', line 16
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
|
# File 'lib/scriptorium/view.rb', line 685
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
# File 'lib/scriptorium/view.rb', line 711
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"
when "integrity"
integrity = args
when "crossorigin"
crossorigin = args
end
end
content = %[<script src="#{src}"></script>\n]
content
end
|
#generate_bootstrap_navbar(nav_content) ⇒ Object
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
# File 'lib/scriptorium/view.rb', line 350
def generate_bootstrap_navbar(nav_content)
= parse_navbar_content(nav_content)
html = <<~HTML
<div class="bootstrap-scope">
<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>
</div>
HTML
html
end
|
#generate_dropdown_child(child) ⇒ Object
450
451
452
453
454
455
456
457
458
459
460
|
# File 'lib/scriptorium/view.rb', line 450
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
# File 'lib/scriptorium/view.rb', line 431
def generate_dropdown_item(item)
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 {|child| html << generate_dropdown_child(child) }
html << <<~HTML
</ul>
</li>
HTML
html
end
|
#generate_empty_containers ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/scriptorium/view.rb', line 90
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; width: 100%; box-sizing: border-box;"],
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
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
962
963
964
965
966
967
968
|
# File 'lib/scriptorium/view.rb', line 916
def generate_front_page
index_file = @dir/:output/"index.html"
FileUtils.mkdir_p(File.dirname(index_file))
html_head = generate_html_head(true)
content = build_containers
common = get_common_js
boot = generate_bootstrap_js
highlight_js = generate_highlight_js(true)
highlight_ruby_js = generate_highlight_ruby_js(true)
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}
#{highlight_js.strip}
#{highlight_ruby_js.strip}
#{common.strip}
</body>
</html>
HTML
begin
full_html = ::HtmlBeautifier.beautify(full_html)
rescue NameError, LoadError => e
end
write_file(index_file, full_html)
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_highlight_css(view = nil) ⇒ Object
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
|
# File 'lib/scriptorium/view.rb', line 970
def generate_highlight_css(view = nil)
global_highlight = @root/:config/"highlight_css.txt"
view_highlight = @dir/:config/"highlight_css.txt"
highlight_file = view ? view_highlight : global_highlight
lines = (highlight_file)
href = rel = nil
lines.each do |line|
component, args = line.split(/\s+/, 2)
case component.downcase
when "href"
href = args
when "rel"
rel = args
end
end
%[<link rel="#{rel}" href="#{href}">\n]
end
|
#generate_highlight_js(view = nil) ⇒ Object
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
|
# File 'lib/scriptorium/view.rb', line 988
def generate_highlight_js(view = nil)
global_highlight = @root/:config/"highlight_js.txt"
view_highlight = @dir/:config/"highlight_js.txt"
highlight_file = view ? view_highlight : global_highlight
lines = (highlight_file)
src = nil
lines.each do |line|
component, args = line.split(/\s+/, 2)
case component.downcase
when "src"
src = args
end
end
%[<script src="#{src}"></script>\n]
end
|
#generate_highlight_ruby_js(view = nil) ⇒ Object
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
|
# File 'lib/scriptorium/view.rb', line 1004
def generate_highlight_ruby_js(view = nil)
global_highlight = @root/:config/"highlight_ruby_js.txt"
view_highlight = @dir/:config/"highlight_ruby_js.txt"
highlight_file = view ? view_highlight : global_highlight
lines = (highlight_file)
src = nil
lines.each do |line|
component, args = line.split(/\s+/, 2)
case component.downcase
when "src"
src = args
end
end
%[<script src="#{src}"></script>\n]
end
|
#generate_html_head(view = nil) ⇒ Object
635
636
637
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
|
# File 'lib/scriptorium/view.rb', line 635
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 "highlight"
content << generate_highlight_css(view) when "highlight_custom"
content << support_data('highlight/custom.css')
end
end
content << "</head>\n"
content
end
|
#generate_nav_item(item) ⇒ Object
462
463
464
465
466
467
468
469
470
471
472
473
474
|
# File 'lib/scriptorium/view.rb', line 462
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
# File 'lib/scriptorium/view.rb', line 416
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
# File 'lib/scriptorium/view.rb', line 583
def generate_post_index
posts = @repo.all_posts(self)
config = read_post_index_config
margin_top = config[:"index.margin.top"] || "0px"
str = "<table width=100% cellpadding=#{config[:'entry.cellpadding']} style=\"margin-top: #{margin_top};\">"
posts.each {|post| str << post_index_entry(post) }
str << "</table>"
write_file(@dir/:output/"post_index.html", str)
end
|
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
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
|
# File 'lib/scriptorium/view.rb', line 795
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
slug = post_data[:"post.slug"] || slugify(post_data[:"post.id"], title)
url = "posts/#{slug}#{slug.end_with?('.html') ? '' : '.html'}"
else
title = @title
url = "index.html"
end
require 'uri'
encoded_title = URI.encode_www_form_component(title)
if subreddit.empty?
reddit_url = "https://reddit.com/submit?url=#{escape_html(url)}&title=#{encoded_title}"
else
reddit_url = "https://reddit.com/r/#{subreddit}/submit?url=#{escape_html(url)}&title=#{encoded_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/icons/social/reddit.png" width="32" height="32" alt="Share on Reddit" style="vertical-align: middle;">
</a>]
button_html
end
|
735
736
737
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
|
# File 'lib/scriptorium/view.rb', line 735
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"] ? post_data[:"post.body"][0..200] : nil) || @desc || @subtitle || @title
slug = post_data[:"post.slug"] || (post_data[:"post.id"] ? slugify(post_data[:"post.id"], title) : 'post')
url = "posts/#{slug}#{slug.end_with?('.html') ? '' : '.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
|
#get_common_js(view = nil) ⇒ Object
677
678
679
680
681
682
683
|
# File 'lib/scriptorium/view.rb', line 677
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
# File 'lib/scriptorium/view.rb', line 476
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
|
#inspect ⇒ Object
51
52
53
|
# File 'lib/scriptorium/view.rb', line 51
def inspect
"<View: #@name #{@title.inspect} theme: #@theme>"
end
|
#paginate_posts ⇒ Object
886
887
888
889
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
|
# File 'lib/scriptorium/view.rb', line 886
def paginate_posts
config = read_post_index_config
posts = @repo.all_posts(self)
posts.sort! {|a,b| post_compare(a, b) }
@ppp ||= config[:"posts.per.page"].to_i
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)
margin_top = config[:"index.margin.top"] || "0px"
cellpadding = config[:'entry.cellpadding']
table_open = %(<table width=100% cellpadding=#{cellpadding} style="margin-top: #{margin_top};">)
table_close = %(</table>)
html = String.new
html << table_open
html << page.join
html << table_close
html << %[<div style="position: absolute; bottom: 0; width: 100%;">#{bar}</div>]
write_file(out/"page#{i+1}.html", html)
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’
873
874
875
876
877
878
879
880
881
882
883
884
|
# File 'lib/scriptorium/view.rb', line 873
def (group, count, nth) str = %[<div style="text-align: center;">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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
# File 'lib/scriptorium/view.rb', line 374
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
162
163
164
165
166
167
168
169
|
# File 'lib/scriptorium/view.rb', line 162
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
626
627
628
629
|
# File 'lib/scriptorium/view.rb', line 626
def post_index_array
posts = view_posts.sort {|a,b| post_compare(a, b) }
posts.map {|post| post_index_entry(post)}
end
|
#post_index_entry(post) ⇒ Object
598
599
600
601
602
603
604
605
606
607
608
609
|
# File 'lib/scriptorium/view.rb', line 598
def post_index_entry(post)
template = support_data('templates/index_entry.lt3')
config = read_post_index_config
vars = post.vars.merge(config)
date_format = config[:"entry.date.format"] || "month dd break yyyy"
vars[:formatted_date] = format_date(date_format, post.date)
entry = substitute(vars, template)
entry
end
|
#read_layout ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/scriptorium/view.rb', line 71
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
|
#read_post_index_config ⇒ Object
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
# File 'lib/scriptorium/view.rb', line 611
def read_post_index_config
global_config_file = @repo.root/:config/"post_index_defaults.txt"
global_config = File.exist?(global_config_file) ? (global_config_file) : {}
view_config_file = @dir/:config/"post_index.txt"
view_config = File.exist?(view_config_file) ? (view_config_file) : {}
global_config.merge(view_config)
rescue => e
{}
end
|
#section_append(sec, str) ⇒ Object
171
172
173
174
175
176
|
# File 'lib/scriptorium/view.rb', line 171
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
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
|
# File 'lib/scriptorium/view.rb', line 184
def section_core(section, hash)
cfg = @dir/:config
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
178
179
180
181
182
|
# File 'lib/scriptorium/view.rb', line 178
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
631
632
633
|
# File 'lib/scriptorium/view.rb', line 631
def view_posts
@repo.all_posts(self).sort {|a,b| post_compare(a, b)}
end
|