Module: EngineOfWar::TemplateEngines::Filters

Includes:
Padrino::Helpers::TagHelpers
Included in:
Markdown, Textile
Defined in:
lib/engine_of_war/template_engines/filters.rb

Constant Summary collapse

BASE_IMAGE_URL =
"/images"

Instance Method Summary collapse

Instance Method Details

#block_is_template?(_) ⇒ Boolean

Required to confuse padrino

Returns:

  • (Boolean)


74
75
76
# File 'lib/engine_of_war/template_engines/filters.rb', line 74

def block_is_template?(_)
  false
end

#code_filter(txt) ⇒ Object



6
7
8
9
10
11
# File 'lib/engine_of_war/template_engines/filters.rb', line 6

def code_filter(txt)
  txt.gsub(/@@@ *(\w*)\r?\n? *(.+?)\r?\n?@@@/m) do
    klass = $1.present? ? " class=\"#{$1.downcase}\"" : ""
    "<pre><code#{klass}>#{$2}</code></pre>"
  end
end

#image_basedir(basename) ⇒ Object



60
61
62
# File 'lib/engine_of_war/template_engines/filters.rb', line 60

def image_basedir(basename)
  File.join(EngineOfWar::App.settings.public_folder, BASE_IMAGE_URL, basename)
end

#image_directory_exists?(basename) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/engine_of_war/template_engines/filters.rb', line 56

def image_directory_exists?(basename)
  File.directory?(image_basedir(basename))
end

#image_filter(txt) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/engine_of_war/template_engines/filters.rb', line 13

def image_filter(txt)
  txt.gsub(/^%([<>]?)([0-9a-zA-Z_.-]+)([^%]*)%/) do
    float    = $1
    image_id = $2
    size     = $3

    float = :right if float == '>'
    float = :left  if float == '<'

    uploaded_image_tag(image_id, :size => size, :float => float)
  end
end

#meta_for_image(basename) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/engine_of_war/template_engines/filters.rb', line 64

def meta_for_image(basename)
  file = File.join(image_basedir(basename), "meta.yml")
  if File.exist?(file)
    YAML.load_file(file).delete_if {|k, v| v.blank? }
  else
    {}
  end
end

#unknown_image_tag(name) ⇒ Object



52
53
54
# File 'lib/engine_of_war/template_engines/filters.rb', line 52

def unknown_image_tag(name)
  "<div class='warning'>unknown image #{name}</div>"
end

#uploaded_image_tag(image_name, opts = {}) ⇒ Object



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
# File 'lib/engine_of_war/template_engines/filters.rb', line 26

def uploaded_image_tag(image_name, opts = {})
  opts.symbolize_keys

  opts[:size] = "original" if opts[:size].blank?
  opts[:size].strip!

  basename, ext = image_name.split('.')
  image_src = "#{BASE_IMAGE_URL}/#{basename}/#{opts[:size]}.#{ext}"
  meta = meta_for_image(basename)

  return unknown_image_tag(image_name) unless image_directory_exists?(basename)

  image_options = { :src => image_src }
  image_options[:class] = "float-#{opts[:float]}" if opts[:float]
  image_options[:alt]   = meta[:description] if meta[:description]

  href = meta[:source_url]

  img_tag =  :img, "", image_options
  if href
    return "<a href='#{href}'>#{img_tag}</a>"
  else
    return img_tag
  end
end