Class: Weaver::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/weaver/page_types/page.rb

Overview

Base page class

Direct Known Subclasses

CenterPage, EmptyPage, RawPage, StructuredPage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, global_settings, options, &block) ⇒ Page

Returns a new instance of Page.



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
# File 'lib/weaver/page_types/page.rb', line 8

def initialize(title, global_settings, options, &block)
  @title = title
  @content = ''
  @body_class = nil
  @anchors = {}
  @global_settings = global_settings
  @options = options
  @scripts = []
  @top_content = ''

  @scripts_once = {}
  @onload_scripts = []

  @requested_scripts = {}
  @requested_css = {}

  @meta_tags = []

  @background = Elements.new(self, @anchors)

  m1 = Elements.new(self, @anchors)
  m1.instance_eval do
    meta charset: 'utf-8'
    meta name: 'viewport', contents: 'width=device-width, initial-scale=1.0'
  end

  @meta_tags << m1

  @block = Proc.new &block

end

Instance Attribute Details

#favicon_nameObject

Returns the value of attribute favicon_name.



6
7
8
# File 'lib/weaver/page_types/page.rb', line 6

def favicon_name
  @favicon_name
end

#favicon_typeObject

Returns the value of attribute favicon_type.



6
7
8
# File 'lib/weaver/page_types/page.rb', line 6

def favicon_type
  @favicon_type
end

#onload_scriptsObject

Returns the value of attribute onload_scripts.



6
7
8
# File 'lib/weaver/page_types/page.rb', line 6

def onload_scripts
  @onload_scripts
end

#scriptsObject

Returns the value of attribute scripts.



6
7
8
# File 'lib/weaver/page_types/page.rb', line 6

def scripts
  @scripts
end

Instance Method Details

#background(&block) ⇒ Object



79
80
81
# File 'lib/weaver/page_types/page.rb', line 79

def background(&block)
  @background.instance_eval(&block)
end

#clear_default_meta!Object



91
92
93
# File 'lib/weaver/page_types/page.rb', line 91

def clear_default_meta!
  @meta_tags = []
end

#create_anchor(name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/weaver/page_types/page.rb', line 40

def create_anchor(name)
  @anchors[name] = [] unless @anchors[name]

  anchor_array = @anchors[name]

  anchor_name = "#{name}#{anchor_array.length}"
  anchor_array << anchor_name

  anchor_name
end

#generate(back_folders, options = {}) ⇒ Object



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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/weaver/page_types/page.rb', line 111

def generate(back_folders, options = {})
  if @options[:cache_file]
    expired = @options[:cache_expired]
    cache_exist = File.exist?("cache/cachedpage#{@options[:cache_file]}")

    if cache_exist && !expired
      puts "Weaver Hit cache for file: #{@options[:cache_file]}"
      puts "- expired: #{expired}"
      puts "- cache_exist: #{cache_exist}"
      return File.read("cache/cachedpage#{@options[:cache_file]}")
    end
    puts "Weaver Miss cache for file: #{@options[:cache_file]}"
    puts "- expired: #{expired}"
    puts "- cache_exist: #{cache_exist}"
  end

  scripts = @scripts.join("\n")

  mod = '../' * back_folders

  style = <<-ENDSTYLE
	<link href="#{mod}css/style.css" rel="stylesheet">
  ENDSTYLE
  if !@options[:theme].nil?
    style = <<-ENDSTYLE
  <link href="#{mod}css/style-#{@options[:theme]}.css" rel="stylesheet">
  ENDSTYLE
  end

  style = '' if options[:style] == :empty

  body_tag = '<body>'

  body_tag = "<body class='#{@body_class}'>" if @body_class

  loading_bar = ''
  if @loading_bar_visible
    loading_bar = "<script src='#{mod}js/plugins/pace/pace.min.js'></script>"
  end

  extra_scripts = @requested_scripts.map do |key, _value|
    <<-SCRIPT_DECL
<script src="#{mod}#{key}"></script>
    SCRIPT_DECL
  end.join "\n"

  extra_css = @requested_css.map do |key, _value|
    <<-STYLESHEET_DECL
<link href="#{mod}#{key}" rel="stylesheet">
    STYLESHEET_DECL
  end.join "\n"

  extra_one_time_scripts = @scripts_once.map do |key, _value|
    <<~SCRIPT_DECL
      #{key}
    SCRIPT_DECL
  end.join "\n"

  onload_scripts = @onload_scripts.map do |value|
    <<~SCRIPT_DECL
      #{value}
    SCRIPT_DECL
  end.join "\n"

  extra_stuff = ''

  if @global_settings[:mixpanel_token]
    extra_stuff += '<!-- start Mixpanel --><script type="text/javascript">(function(e,a){if(!a.__SV){var b=window;try{var c,l,i,j=b.location,g=j.hash;c=function(a,b){return(l=a.match(RegExp(b+"=([^&]*)")))?l[1]:null};g&&c(g,"state")&&(i=JSON.parse(decodeURIComponent(c(g,"state"))),"mpeditor"===i.action&&(b.sessionStorage.setItem("_mpcehash",g),history.replaceState(i.desiredHash||"",e.title,j.pathname+j.search)))}catch(m){}var k,h;window.mixpanel=a;a._i=[];a.init=function(b,c,f){function e(b,a){var c=a.split(".");2==c.length&&(b=b[c[0]],a=c[1]);b[a]=function(){b.push([a].concat(Array.prototype.slice.call(arguments,
0)))}}var d=a;"undefined"!==typeof f?d=a[f]=[]:f="mixpanel";d.people=d.people||[];d.toString=function(b){var a="mixpanel";"mixpanel"!==f&&(a+="."+f);b||(a+=" (stub)");return a};d.people.toString=function(){return d.toString(1)+".people (stub)"};k="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config reset opt_in_tracking opt_out_tracking has_opted_in_tracking has_opted_out_tracking clear_opt_in_out_tracking people.set people.set_once people.unset people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");
for(h=0;h<k.length;h++)e(d,k[h]);a._i.push([b,c,f])};a.__SV=1.2;b=e.createElement("script");b.type="text/javascript";b.async=!0;b.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn4.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn4.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn4.mxpnl.com/libs/mixpanel-2-latest.min.js";c=e.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)}})(document,window.mixpanel||[]);
mixpanel.init("' + @global_settings[:mixpanel_token] + '");</script><!-- end Mixpanel -->'
  end

  if @global_settings[:google_tracking_code]
    extra_stuff += <<~GOOGLE
      <!-- Global site tag (gtag.js) - Google Analytics -->
      <script async src="https://www.googletagmanager.com/gtag/js?id=#{@global_settings[:google_tracking_code]}"></script>
      <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', '#{@global_settings[:google_tracking_code]}');
      </script>
    GOOGLE
  end

  favicon_filename = @favicon_name || 'favicon.ico'
  favicon_mimetype = @favicon_type || 'image/x-icon'

  result = <<~SKELETON
    <!DOCTYPE html>
    <html>
    <!-- Generated using weaver: https://github.com/davidsiaw/weaver -->
    <head>

        #{generate_meta_tags}

        <title>#{@title}</title>

        <link href="#{mod}css/bootstrap.min.css" rel="stylesheet">
        <link href="#{mod}font-awesome/css/font-awesome.css" rel="stylesheet">
        <link href="#{mod}css/plugins/iCheck/custom.css" rel="stylesheet">

        <link rel="icon" href="#{favicon_filename}" type="#{favicon_mimetype}" />

        #{style}

        <link href="#{mod}css/animate.css" rel="stylesheet">

    #{extra_css}

    #{extra_stuff}

    </head>

    #{body_tag}

    <div id="background" style="z-index: -999; position:absolute; left:0px; right:0px; width:100%; height:100%">
    #{@background.generate}
    </div>

    <div id="content" style="z-index: 0">
    #{@top_content}
    #{@content}
    </div>

        <!-- Mainly scripts -->
        <script src="#{mod}js/jquery-3.1.1.min.js"></script>
        <script src="#{mod}js/jquery-ui-1.10.4.min.js"></script>
        <script src="#{mod}js/bootstrap.min.js"></script>
        <script src="#{mod}js/plugins/metisMenu/jquery.metisMenu.js"></script>
        <script src="#{mod}js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
    	<script type="text/x-mathjax-config">
    		MathJax.Hub.Config({
    			asciimath2jax: {
    				delimiters: [['$$$MATH$$$','$$$ENDMATH$$$']]
    			}
    		});
    	</script>
        <script src="#{mod}js/MathJax/MathJax.js?config=AM_HTMLorMML-full" async></script>

    #{extra_scripts}


        <!-- Custom and plugin javascript -->
        <script src="#{mod}js/weaver.js"></script>
        #{loading_bar}

        <script>
    #{scripts}
    #{extra_one_time_scripts}

    $( document ).ready(function() {

    #{onload_scripts}

    });
        </script>



    </body>

    </html>

  SKELETON

  if @options[:cache_file]
    FileUtils.mkdir_p 'cache'
    File.write("cache/cachedpage#{@options[:cache_file]}", result)
  end

  result
end

#generate_meta_tagsObject



102
103
104
105
106
107
108
109
# File 'lib/weaver/page_types/page.rb', line 102

def generate_meta_tags
  content = ""
  @meta_tags.each do |x|
    content += "\n#{x.generate}"
  end

  content
end

#head(&block) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/weaver/page_types/page.rb', line 83

def head(&block)
  $stderr.puts 'head'
  m1 = Elements.new(self, @anchors)
  m1.instance_eval(&block)

  @meta_tags << m1
end

#on_page_load(script) ⇒ Object



71
72
73
# File 'lib/weaver/page_types/page.rb', line 71

def on_page_load(script)
  @onload_scripts << script
end

#request_css(path) ⇒ Object



67
68
69
# File 'lib/weaver/page_types/page.rb', line 67

def request_css(path)
  @requested_css[path] = true
end

#request_js(path) ⇒ Object



55
56
57
# File 'lib/weaver/page_types/page.rb', line 55

def request_js(path)
  @requested_scripts[path] = true
end

#rootObject



51
52
53
# File 'lib/weaver/page_types/page.rb', line 51

def root
  @global_settings[:root]
end

#set_favicon_path(path) ⇒ Object



59
60
61
# File 'lib/weaver/page_types/page.rb', line 59

def set_favicon_path(path)
  @favicon_name = path
end

#set_favicon_type(val) ⇒ Object



63
64
65
# File 'lib/weaver/page_types/page.rb', line 63

def set_favicon_type(val)
  @favicon_type = val
end

#top(&block) ⇒ Object



95
96
97
98
99
100
# File 'lib/weaver/page_types/page.rb', line 95

def top(&block)
  elem = Elements.new(self, @anchors)
  elem.instance_eval(&block)

  @top_content = elem.generate
end

#write_script_once(script) ⇒ Object



75
76
77
# File 'lib/weaver/page_types/page.rb', line 75

def write_script_once(script)
  @scripts_once[script] = true
end