Module: Backend::HelpsHelper

Defined in:
app/helpers/backend/helps_helper.rb

Overview

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2013 Brice Texier

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Instance Method Summary collapse

Instance Method Details

#article(name, options = {}) ⇒ Object

def search_article(article = nil)

session[:help_history] = [] unless session[:help_history].is_a? [].class
article ||= "#{controller.controller_path}-#{self.action_name}"
file = nil
for locale in [I18n.locale, I18n.default_locale]
  for f, attrs in Ekylibre.helps
    next if attrs[:locale].to_s != locale.to_s
    kontroller, aktion = article.to_s.split("-")[0..1]
    possibilities = [article]
    possibilities << kontroller + "-edit" if action == "update"
    possibilities << kontroller + "-new" if ["create", "update", "edit"].include?(action)
    possibilities << kontroller + "-index"
    file_name = possibilities.detect{|name| attrs[:name]==name}
    file = f and break unless file_name.blank?
  end
  break unless file.nil?
end
if file and session[:side] and article != session[:help_history].last
  session[:help_history] << file
end
file ||= article.to_sym
return file

end


63
64
65
66
67
68
69
70
# File 'app/helpers/backend/helps_helper.rb', line 63

def article(name, options = {})
  return unless file = find_article(name)
  content = nil
  File.open(Ekylibre.helps[I18n.locale][file][:file], 'rb:UTF-8') { |f| content = f.read }
  content = content.split(/\n/)[1..-1].join("\n") if options.delete(:without_title)
  content = wikize(content.to_s, options)
  content
end

#article_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)

35
36
37
# File 'app/helpers/backend/helps_helper.rb', line 35

def article_exist?(name)
  !find_article(name).nil?
end

#find_article(name) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/backend/helps_helper.rb', line 21

def find_article(name)
  if Ekylibre.helps[I18n.locale]
    kontroller, aktion = name.to_s.split('-')[0..1]
    possibilities = [name]
    possibilities << kontroller + '-edit' if aktion == 'update'
    possibilities << kontroller + '-new' if %w(create update edit).include?(aktion)
    possibilities << kontroller + '-index'
    return possibilities.detect do |p|
      Ekylibre.helps[I18n.locale].key?(p)
    end
  end
  nil
end

#help(file) ⇒ Object

Open an help file and returns corresponding HTML


77
78
79
80
81
82
# File 'app/helpers/backend/helps_helper.rb', line 77

def help(file)
  f = File.open(file, 'rb:UTF-8')
  content = f.read
  f.close
  wikize(content)
end

#help_shown?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'app/helpers/backend/helps_helper.rb', line 72

def help_shown?
  !current_user.preference('interface.helps.collapsed', true, :boolean).value
end

#wikize(content, options = {}) ⇒ Object

Transforms text to HTML like in wikis.


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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/helpers/backend/helps_helper.rb', line 85

def wikize(content, options = {})
  # AJAX fails with XHTML entities because there is no DOCTYPE in AJAX response

  # French rules
  content.gsub!(/[\,\s]+(\.{3,}|…)/, '...')
  content.gsub!('...', '&#8230;')
  content.gsub!(/(\w)(\?|\:)([\s$])/, '\1~\2\3')
  content.gsub!(/(\w+)[\ \~]+(\?|\:)/, '\1~\2')

  content.gsub!(/\~/, '&#160;')

  content.gsub!(/^\ \ \*\ +(.*)\ *$/, '<ul><li>\1</li></ul>')
  content.gsub!(/<\/ul>\n<ul>/, '')
  content.gsub!(/^\ \ \-\ +(.*)\ *$/, '<ol><li>\1</li></ol>')
  content.gsub!(/<\/ol>\n<ol>/, '')
  content.gsub!(/^\ \ \?\ +(.*)\ *$/, '<dl><dt>\1</dt></dl>')
  content.gsub!(/^\ \ \!\ +(.*)\ *$/, '<dl><dd>\1</dd></dl>')
  content.gsub!(/<\/dl>\n<dl>/, '')

  content.gsub!(/^>>>\ +(.*)\ *$/, '<p class="notice">\1</p>')
  content.gsub!(/<\/p>\n<p class="notice">/, '<br/>')
  content.gsub!(/^!!!\ +(.*)\ *$/, '<p class="warning">\1</p>')
  content.gsub!(/<\/p>\n<p class="warning">/, '<br/>')

  content.gsub!(/\{\{\ *[^\}\|]+\ *(\|[^\}]+)?\}\}/) do |data|
    data = data.squeeze(' ')[2..-3].split('|')
    align = { '  ' => 'center', ' x' => 'right', 'x ' => 'left', 'xx' => '' }[(data[0][0..0] + data[0][-1..-1]).gsub(/[^\ ]/, 'x')]
    title = data[1] || data[0].split(/[\:\\\/]+/)[-1].humanize
    src = data[0].strip
    if src =~ /^icon:/
      icon_name = src.split(':')[1]
      "<i class='icon icon-#{icon_name}'></i>"
    else
      src = image_path(src)
      '<img class="md md-' + align + '" alt="' + title + '" title="' + title + '" src="' + src + '"/>'
    end
  end

  options[:url] ||= {}
  content = content.gsub(/\[\[>[^\|]+\|[^\]]*\]\]/) do |link|
    link = link[3..-3].split('|')
    url = link[0].split(/[\#\?\&]+/)
    url = options[:url].merge(controller: "/#{url[0]}", action: (url[1] || :index))
    # TODO: clean authorization system
    surl = url_for(url) # Permit to test URL
    (options[:no_link] || !authorized?(url) ? link[1] : link_to(link[1].html_safe, surl))
  end

  options[:method] = :get
  content = content.gsub(/\[\[[\w\-]+\|[^\]]*\]\]/) do |link|
    link = link[2..-3].split('|')
    url = url_for(options[:url].merge(id: link[0]))
    link_to(link[1].html_safe, url, { :remote => true, 'data-type' => :html }.merge(options)) # REMOTE
  end

  content = content.gsub(/\[\[[\w\-]+\]\]/) do |link|
    link = link[2..-3]
    url = url_for(options[:url].merge(id: link))
    link_to(link.html_safe, url, { :remote => true, 'data-type' => :html }.merge(options)) # REMOTE
  end

  for x in 1..6
    n = 7 - x
    content.gsub!(/^\s*\={#{n}}\s*([^\=]+)\s*\=*/, "<h#{x}>\\1</h#{x}>")
  end
  content.gsub!(/\<h1\>.*\<\/h1\>/, '')

  content.gsub!(/^\ \ (.*\w+.*)$/, '  <pre>\1</pre>')

  content.gsub!(/([^\:])\/\/([^\s][^\/]+)\/\//, '\1<em>\2</em>')
  content.gsub!(/\'\'([^\s][^\']+)\'\'/, '<code>\1</code>')
  content.gsub!(/(^)([^\s\<][^\s].*)($)/, '<p>\2</p>') unless options[:without_paragraph]
  content.gsub!(/^\s*(\<a.*)\s*$/, '<p>\1</p>')

  content.gsub!(/\*\*([^\s\*]+)\*\*/, '<strong>\1</strong>')
  content.gsub!(/\*\*([^\s\*][^\*]*[^\s\*])\*\*/, '<strong>\1</strong>')
  content.gsub!(/(^|[^\*])\*([^\*]|$)/, '\1&lowast;\2')
  content.gsub!("</p>\n<p>", "\n")

  content.strip!

  # raise StandardError.new content
  content.html_safe
end