forked from unimelb/unimelb-design-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroda-render_component.rb
153 lines (129 loc) · 4.13 KB
/
roda-render_component.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
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
152
153
# rubocop:disable Style/FileName, Metrics/ModuleLength
module RodaRenderComponent
#
module InstanceMethods
def get_component(path, opts = {})
@documents = {}
@code_i = 0
raw_documents = []
%w(md html slim).each do |ext|
parent = File.join(opts[:components], path, "*.#{ext}")
raw_documents << Dir.glob(parent)
end
raw_documents.flatten.sort.map { |f| get_section(f, opts) }
@documents['_append'] = [trigger_source_editable] if @code_i > 0
@documents
end
def get_section(f, opts)
section = File.basename(f)[0..1]
@documents[section] = [] unless @documents[section]
@documents[section] << process_section(f, opts)
end
def process_section(f, opts)
puts opts.include? :code_only
case File.extname(f)
when '.md' then
build_markdown(f) unless opts.include? :code_only
when '.slim' then
build_slim(f, opts)
else
build_html(f, opts)
end
end
private
# Render block through tilt template
def slim(content, scope = self)
Slim::Template.new(pretty: true) { content }.render(scope)
end
# Render block through tilt template
def markdown(content)
Tilt::RedcarpetTemplate.new { content }.render
end
# Render slim
def build_slim(f, opts) # rubocop:disable Metrics/AbcSize
params = ''
source = basename_without_index_and_extension(f)[-9..-1] == 'no-source' || (opts.include? :code_only) # rubocop:disable Metrics/LineLength
if source
source = nil
else
source = render_source_block(slim(file_content(f)))
params = ' id="r' + @code_i.to_s + '"'
end
output = '<div' + params + '>' + slim(file_content(f)) + '</div>'
[title_from_filename(f), output, source]
end
def build_html(f, opts) # rubocop:disable Metrics/AbcSize
params = ''
source = basename_without_index_and_extension(f)[-9..-1] == 'no-source' || (opts.include? :code_only) # rubocop:disable Metrics/LineLength
if source
source = nil
else
source = render_source_block(file_content(f))
params = ' id="r' + @code_i.to_s + '"'
end
output = '<div' + params + '>' + file_content(f) + '</div>'
[title_from_filename(f), output, source]
end
def build_markdown(f)
markdown(file_content(f))
end
# Predefined block for displaying example source
def render_source_block(block)
@code_i += 1
'
<section class="code">
<pre class="html" id="s' + @code_i.to_s + '">
' + convert_tags(block) + '
</pre>
</section>
'
end
def trigger_source_editable # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/LineLength
# rubocop:disable Metrics/LineLength
buf = '
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.3/ace.js"></script>
'
@code_i.times do |i|
l = (1 + i).to_s
buf += '
<script>
var e' + l + ' = ace.edit("s' + l + '");
e' + l + '.setTheme("ace/theme/github");
e' + l + '.session.setMode("ace/mode/html");
e' + l + '.session.setOption("useWorker", false);
e' + l + '.getSession().setTabSize(2);
e' + l + '.getSession().setUseSoftTabs(true);
e' + l + '.getSession().on("change", function(e){
var el = document.getElementById("r' + l + '");
el.innerHTML = e' + l + '.getValue();
window.uom.initAllComponents(el);
});
</script>
'
end
buf
end
# Parse front matter and cache
def front_matter(file)
@front_matters ||= {}
unless @front_matters.key? file
@front_matters[file] = FrontMatterParser.parse_file(file)
end
@front_matters[file]
end
def file_settings(file)
front_matter(file).front_matter
end
def file_settings_title(file)
file_settings(file)['title']
end
def file_content(file)
front_matter(file).content
end
# Prep tag block for html display
def convert_tags(block = '')
block = yield if block_given?
block.gsub('<', '<').gsub('>', '>')
end
end
end