Module: Zena::Use::ZafuTemplates::Common

Included in:
TextDocument::AssetHelper
Defined in:
lib/zena/use/zafu_templates.rb

Constant Summary collapse

DEFAULT_PATH =
%r{^\/?\$([\+\-\w\/]+)}
DEFAULT_TEMPLATES_PATH =
"#{Zena::ROOT}/app/views/zafu"

Instance Method Summary collapse

Instance Method Details

#default_template_url(opts = {}) ⇒ Object

Default template content for a specified mode



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/zena/use/zafu_templates.rb', line 172

def default_template_url(opts = {})
  if opts[:format] && opts[:format] != 'html'
    raise ActiveRecord::RecordNotFound
  elsif %w{+login +index +adminLayout +popupLayout +notFound admin}.include?(opts[:mode])
    zafu_url =['$default', "Node-#{opts[:mode]}"].map(&:to_filename).join('/')
  elsif opts[:mode]
    raise ActiveRecord::RecordNotFound
  else
    zafu_url =['$default', 'Node'].map(&:to_filename).join('/')
  end

  # File path:
  rel_path  = current_site.zafu_path + "/#{zafu_url}/#{lang_path}/_main.erb"
  path      = SITES_ROOT + rel_path
  
  if !File.exists?(path)
    rebuild_template(nil, opts.merge(:zafu_url => zafu_url, :rel_path => rel_path, :dev_mode => (dev_mode? && opts[:mode] != '+popupLayout')))
  end

  rel_path
end

#find_document_for_template(src, section_id = nil) ⇒ Object

Return a document for a given path and current directory. This method also returns a new current_directory built from ‘/[skin name]/dirname/in/skin’ Search order for documents depends on the leading ‘/’. With a leading slash “/joy/special/Node”:

  1. Search for a Document with fullpath [joy skin fullpath]/special/Node

  2. Search anywhere in the skin for a document named “Node”

Without a leading slash “special/Node”

  1. Search for a Document with fullpath [current directory]/special/Node

  2. Search anywhere in the master skin for a document named ‘Node’



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/zena/use/zafu_templates.rb', line 204

def find_document_for_template(src, section_id = nil)
  src = src.split('/') unless src.kind_of?(Array)

  if src[0] == ''
    # Starts with '/' : first part of the path is a Skin
    section_id = nil
    # remove blank
    src.shift
    # get skin
    return nil unless skin = get_skin(src.shift)
    section_id = skin.id
  elsif section_id.nil? && @skin
    section_id = @skin.id
    # does not start with '/' : look in current directory
  end

  self.asset_cache.cache_with_path(section_id, src) do
    unless document = secure(Document) { Document.find_by_path(src, section_id) }
      # find anywhere in Skin
      document = secure(Document) { Document.find_by_title(src.last, :conditions => ['section_id = ?', section_id]) }
    end
    document
  end
end

#fullpath_from_template_url(template_url = , build = false) ⇒ Object

Return the full path from a template’s url. The expected url is of the form ‘/skin/Klass-mode/partial’



153
154
155
# File 'lib/zena/use/zafu_templates.rb', line 153

def fullpath_from_template_url(template_url=params[:t_url], build=false)
  "#{SITES_ROOT}#{template_path_from_template_url('',template_url,build)}"
end

#get_template_text(path, section_id = nil, opts = {}) ⇒ Object

Return a template’s content from an url. If the url does not start with a ‘/’, we try by replacing the first element with the current skin_name and if it does not work, we try with the full url. If the url start with a ‘/’ we use the full url directly.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/zena/use/zafu_templates.rb', line 47

def get_template_text(path, section_id = nil, opts = {})
  if path =~ DEFAULT_PATH
    filepath = File.join(DEFAULT_TEMPLATES_PATH, "#{$1}.zafu")
    if text = File.exist?(filepath) ? File.read(filepath) : nil
      return text, path, nil
    end
  elsif @skin.nil? && path == 'Node'
    filepath = File.join(DEFAULT_TEMPLATES_PATH, "default/#{path}.zafu")
    if text = File.exist?(filepath) ? File.read(filepath) : nil
      return text, path, nil
    end
  else
    path = path.split('/').map {|s| String.from_filename(s) }
    if doc = find_document_for_template(path, section_id)
      # text, fullpath (for recursion testing), base_path
      return (doc.text || ''), doc.fullpath, doc.section_id, doc
    else
      nil
    end
  end
end

#save_erb_to_url(template, template_url) ⇒ Object

Callback to save an write an Ajax template to file.



143
144
145
146
147
148
149
# File 'lib/zena/use/zafu_templates.rb', line 143

def save_erb_to_url(template, template_url)
  path = fullpath_from_template_url(template_url, false)
  path += ".erb" unless path =~ /\.\w+\Z/
  FileUtils.mkpath(File.dirname(path)) unless File.exists?(File.dirname(path))
  File.open(path, "wb") { |f| f.syswrite(template) }
  ""
end

#template_url_for_asset(opts) ⇒ Object

Return the zen_path (‘/en/image34.png’) for an asset given its (urlencoded) path (‘img/footer.png’). The rule is not the same whether we are rendering a template and find <img/> <link rel=‘stylesheet’/> tags or if we are parsing assets in a CSS file.



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
# File 'lib/zena/use/zafu_templates.rb', line 73

def template_url_for_asset(opts)
  source = opts[:src]
  if source =~ /\A(.*)\.(\w+)\Z/
    source, format = $1, $2
  end

  if opts[:parse_assets]
    parent_id = opts[:parent].id

    if source =~ /\A(.*)_(\w+)\Z/
      # if the element was not found, maybe it was not a name with underscore but it was an image mode
      src2, mode2 = $1, $2
    end

    paths = []
    if source[0..0] == '/'
      # ignore parent
      parent_id = current_site.root_id
      paths << source[1..-1]
      paths << src2[1..-1] if src2
    else
      paths << source
      paths << src2 if src2
    end

    # Retrieve titles from urlencoding
    paths.map! do |path|
      res    = nil
      par_id = parent_id
      path.split('/').each do |e|
        if e == '..'
          # forces absolute path
          par_id = current_site.root_id
          res ||= opts[:parent].fullpath_as_title
          res.pop
        else
          res ||= []
          res << String.from_filename(e)
        end
      end
      [res, par_id]
    end

    if asset = secure(Document) { Document.find_by_path(*paths[0]) }
    elsif src2 && (asset = secure(Document) { Document.find_by_path(*paths[1]) })
      mode = mode2
    else
      return nil
    end
  else
    src2 = source.split('/').map {|s| String.from_filename(s) }

    if source =~ /\A(.*)_(\w+)\Z/
      source, mode = $1, $2
    end

    source = source.split('/').map {|f| String.from_filename(f) }

    unless asset = find_document_for_template(source, opts[:base_path])
      # '_...' did not mean mode but was an old name.
      mode = nil
      return nil unless asset = find_document_for_template(src2, opts[:base_path])
    end

  end

  data_path(asset, :mode => mode)
end

#valid_template?(content, opts) ⇒ Boolean

Make sure some vital templates never get broken

Returns:

  • (Boolean)


158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/zena/use/zafu_templates.rb', line 158

def valid_template?(content, opts)
  #puts content
  mode = opts[:mode]
  case mode
  when '+login'
    content =~ %r{<form[^>]* action\s*=\s*./session}
  when '+adminLayout'
    content =~ %r{<%= content_for_layout %>} && %r{show_link(:admin_links)}
  else
    true
  end
end

#zafu_node(name, klass) ⇒ Object



229
230
231
# File 'lib/zena/use/zafu_templates.rb', line 229

def zafu_node(name, klass)
  zafu_context[:node] = Zena::Use::NodeContext.new(name, klass)
end