Class: WWW_App

Inherits:
BasicObject
Includes:
Kernel
Defined in:
lib/www_app.rb,
lib/www_app/Clean.rb,
lib/www_app/source.rb

Overview

WWW_App ====================================================

Defined Under Namespace

Classes: Clean, Code_ify, Sanitize

Constant Summary collapse

Unescaped =
::Class.new(::StandardError)
Not_Unique =
::Class.new(::StandardError)
Wrong_Parent =
::Class.new(::StandardError)
Invalid_Type =
::Class.new(::StandardError)
HTML_ID_Duplicate =
::Class.new(Not_Unique)
ALWAYS_END_TAGS =
[:script]
SYM_CACHE =
{ attrs: {}, css_props: {}}
Classes =
[]
INVALID_ATTR_CHARS =
/[^a-z0-9\_\-]/i
IMAGE_AT_END =
/image\z/i
HASH =
'#'
DOT =
'.'
BANG =
'!'
NEW_LINE =
"\n"
SPACE =
' '
BLANK =
''
BODY =
'body'
UNDERSCORE =
'_'
Document_Template =
::File.read(__FILE__).split("__END__").last.strip
NO_END_TAGS =
[:br, :input, :link, :meta, :hr, :img]
Methods =
{
  :elements => %w[

    title
    body   div    span

    img
    b      em     i  strong  u  a 
    abbr   blockquote  cite
    br     cite   code 
    ul     ol     li  p  pre  q 
    sup    sub 
    form   input  button

    link

    script

  ].map(&:to_sym),

  :attributes => {
    :all         => [:id, :class],
    :a           => [:href, :rel],
    :form        => [:action, :method, :accept_charset],
    :input       => [:type, :name, :value],
    :style       => [:type],
    :script      => [:type, :src, :language],
    :link        => [:rel, :type, :sizes, :href, :title],
    :meta        => [:name, :http_equiv, :property, :content, :charset],
    :img         => [:src, :width, :height]
  },

  :css => {
    :at_rules       => [ 'font-face', 'media' ],
    :protocols      => [ :relative ],

    :pseudo => %w[
       active checked default dir() disabled
       empty enabled
       first first-child first-of-type fullscreen focus
       hover
       indeterminate in-range invalid
       lang() last-child last-of-type left link
       not() nth-child() nth-last-child() nth-last-of-type() nth-of-type()
       only-child only-of-type optional out-of-range
       read-only read-write required right root
       scope
       target
       valid visited
    ].select { |name| name[/\A[a-z0-9\-]+\Z/] }.map { |name| name.gsub('-', '_').to_sym },

    # From: Sanitize::Config::RELAXED[:css][:properties]
    :properties     => %w[
      background                 bottom                     font_variant_numeric       position
      background_attachment      box_decoration_break       font_variant_position      quotes
      background_clip            box_shadow                 font_weight                resize
      background_color           box_sizing                 height                     right
      background_image           clear                      hyphens                    tab_size
      background_origin          clip                       icon                       table_layout
      background_position        clip_path                  image_orientation          text_align
      background_repeat          color                      image_rendering            text_align_last
      background_size            column_count               image_resolution           text_combine_horizontal
      border                     column_fill                ime_mode                   text_decoration
      border_bottom              column_gap                 justify_content            text_decoration_color
      border_bottom_color        column_rule                left                       text_decoration_line
      border_bottom_left_radius  column_rule_color          letter_spacing             text_decoration_style
      border_bottom_right_radius column_rule_style          line_height                text_indent
      border_bottom_style        column_rule_width          list_style                 text_orientation
      border_bottom_width        column_span                list_style_image           text_overflow
      border_collapse            column_width               list_style_position        text_rendering
      border_color               columns                    list_style_type            text_shadow
      border_image               content                    margin                     text_transform
      border_image_outset        counter_increment          margin_bottom              text_underline_position
      border_image_repeat        counter_reset              margin_left                top
      border_image_slice         cursor                     margin_right               touch_action
      border_image_source        direction                  margin_top                 transform
      border_image_width         display                    marks                      transform_origin
      border_left                empty_cells                mask                       transform_style
      border_left_color          filter                     mask_type                  transition
      border_left_style          float                      max_height                 transition_delay
      border_left_width          font                       max_width                  transition_duration
      border_radius              font_family                min_height                 transition_property
      border_right               font_feature_settings      min_width                  transition_timing_function
      border_right_color         font_kerning               opacity                    unicode_bidi
      border_right_style         font_language_override     order                      unicode_range
      border_right_width         font_size                  orphans                    vertical_align
      border_spacing             font_size_adjust           overflow                   visibility
      border_style               font_stretch               overflow_wrap              white_space
      border_top                 font_style                 overflow_x                 widows
      border_top_color           font_synthesis             overflow_y                 width
      border_top_left_radius     font_variant               padding                    word_break
      border_top_right_radius    font_variant_alternates    padding_bottom             word_spacing
      border_top_style           font_variant_caps          padding_left               word_wrap
      border_top_width           font_variant_east_asian    padding_right              z_index
      border_width               font_variant_ligatures     padding_top
    ].map(&:to_sym)
  }

}
ALLOWED_ATTRS =

end Methods

Methods[:attributes].inject({}) { |memo, (tag, attrs)|
  attrs.each { |a|
    memo[a] ||= []
    memo[a] << tag
  }
  memo
}
Allowed =
{
  :attr => {}
}

Instance Method Summary collapse

Constructor Details

#initialize(*files) ⇒ WWW_App

class self ==============================================



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
286
287
288
289
290
291
292
293
294
# File 'lib/www_app.rb', line 239

def initialize *files
  @js              = []
  @style           = {}
  @css_arr         = []
  @css_id_override = nil
  @render_it       = true

  @title       = nil
  @scripts     = []
  @body        = []
  @compiled    = nil
  @cache       = {}
  @is_doc      = false
  @page_title  = nil
  @default_ids = {}

  @state = [:create]
  @ids   = {}

  @tag_arr           = []
  @current_tag_index = nil
  @mustache          = nil

  @html_ids          = {}

  tag(:head) {

    @head = tag!

    tag(:style) {
      @style = tag!
      @style[:css] = {}
    }

    tag(:script) {
      tag![:content] = @js
    }

  } # === tag :head

  tag(:body) {

    @body = tag!

    files.each { |file_name|
      eval ::File.read(file_name), nil, file_name
    }

    instance_eval(&(::Proc.new))
  }

  @mustache = ::Mustache.new
  @mustache.template = to_mustache

  freeze
end

Instance Method Details

#*(raw_id) ⇒ Object

Example:

div.*('my_id') { }


345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/www_app.rb', line 345

def * raw_id
  id = ::Escape_Escape_Escape.html_id(raw_id)

  old_id = tag![:attrs][:id]
  fail("Id already set: #{old_id} new: #{id}") if old_id

  fail(HTML_ID_Duplicate, "Id already used: #{id.inspect}, tag index: #{@html_ids[id]}") if @html_ids[id]
  @html_ids[id] = tag![:tag_index]

  tag![:attrs][:id] = id

  if block_given?
    close_tag { yield }
  else
    self
  end
end

#/Object



684
685
686
687
# File 'lib/www_app.rb', line 684

public def /
  fail "No block allowed here: :/" if block_given?
  close_tag
end

#^(*names) ⇒ Object

Example:

div.^(:alert, :red_hot) { 'my content' }


367
368
369
370
371
372
373
374
375
# File 'lib/www_app.rb', line 367

def ^ *names
  tag![:attrs][:class].concat(names).uniq!

  if block_given?
    close_tag { yield }
  else
    self
  end
end

#render(raw_data = {}) ⇒ Object



306
307
308
# File 'lib/www_app.rb', line 306

def render raw_data = {}
  @mustache.render raw_data
end

#render_if(name) ⇒ Object

def new_class



296
297
298
299
# File 'lib/www_app.rb', line 296

def render_if name
  tag(:render_if) { tag![:attrs][:key] = name; yield }
  nil
end

#render_unless(name) ⇒ Object



301
302
303
304
# File 'lib/www_app.rb', line 301

def render_unless name
  tag(:render_unless) { tag![:attrs][:key] = name; yield }
  nil
end