Module: Backend::HelpsHelper

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

Instance Method Summary collapse

Instance Method Details

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


39
40
41
42
43
44
45
46
# File 'app/helpers/backend/helps_helper.rb', line 39

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


53
54
55
56
57
58
# File 'app/helpers/backend/helps_helper.rb', line 53

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

#help_shown?Boolean

Returns:

  • (Boolean)

48
49
50
# File 'app/helpers/backend/helps_helper.rb', line 48

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

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

Transforms text to HTML like in wikis.


61
62
63
64
65
66
67
68
69
70
71
72
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
141
142
143
# File 'app/helpers/backend/helps_helper.rb', line 61

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

  (1..6).each do |x|
    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!

  content.html_safe
end