Class: TextFilter

Inherits:
Object
  • Object
show all
Defined in:
app/models/text_filter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, description: nil, markup: nil, filters: [], params: nil) ⇒ TextFilter

Returns a new instance of TextFilter.



8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/text_filter.rb', line 8

def initialize(name: nil,
               description: nil,
               markup: nil,
               filters: [],
               params: nil)
  @name = name
  @description = description
  @markup = markup
  @filters = filters
  @params = params
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'app/models/text_filter.rb', line 6

def description
  @description
end

#filtersObject

Returns the value of attribute filters.



6
7
8
# File 'app/models/text_filter.rb', line 6

def filters
  @filters
end

#markupObject

Returns the value of attribute markup.



6
7
8
# File 'app/models/text_filter.rb', line 6

def markup
  @markup
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'app/models/text_filter.rb', line 6

def name
  @name
end

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'app/models/text_filter.rb', line 6

def params
  @params
end

Class Method Details

.allObject



79
80
81
82
83
84
85
86
87
# File 'app/models/text_filter.rb', line 79

def self.all
  [
    markdown,
    smartypants,
    markdown_smartypants,
    textile,
    none,
  ]
end

.filter_text(text, filters) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/text_filter.rb', line 28

def self.filter_text(text, filters)
  map = TextFilterPlugin.filter_map

  filters.each do |filter|
    next if filter.nil?

    filter_class = map[filter.to_s]
    next unless filter_class

    text = filter_class.filtertext(text)
  end

  text
end

.find_or_default(name) ⇒ Object



24
25
26
# File 'app/models/text_filter.rb', line 24

def self.find_or_default(name)
  make_filter(name) || none
end

.make_filter(name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/text_filter.rb', line 89

def self.make_filter(name)
  case name
  when "markdown"
    markdown
  when "smartypants"
    smartypants
  when "markdown smartypants"
    markdown_smartypants
  when "textile"
    textile
  when "none"
    none
  end
end

.markdownObject



104
105
106
107
108
# File 'app/models/text_filter.rb', line 104

def self.markdown
  new(name: "markdown",
      description: "Markdown",
      markup: "markdown")
end

.markdown_smartypantsObject



117
118
119
120
121
122
# File 'app/models/text_filter.rb', line 117

def self.markdown_smartypants
  new(name: "markdown smartypants",
      description: "Markdown with SmartyPants",
      markup: "markdown",
      filters: [:smartypants])
end

.noneObject



130
131
132
133
134
# File 'app/models/text_filter.rb', line 130

def self.none
  new(name: "none",
      description: "None",
      markup: "none")
end

.smartypantsObject



110
111
112
113
114
115
# File 'app/models/text_filter.rb', line 110

def self.smartypants
  new(name: "smartypants",
      description: "SmartyPants",
      markup: "none",
      filters: [:smartypants])
end

.textileObject



124
125
126
127
128
# File 'app/models/text_filter.rb', line 124

def self.textile
  new(name: "textile",
      description: "Textile",
      markup: "textile")
end

Instance Method Details

#commenthelpObject



68
69
70
71
72
73
74
75
76
77
# File 'app/models/text_filter.rb', line 68

def commenthelp
  filter_map = TextFilterPlugin.filter_map

  help = [filter_map[markup]]
  filters.each { |f| help.push(filter_map[f.to_s]) }

  help.map do |f|
    f.help_text.blank? ? "" : "#{BlueCloth.new(f.help_text).to_html}\n"
  end.join("\n")
end

#filter_text(text) ⇒ Object



43
44
45
# File 'app/models/text_filter.rb', line 43

def filter_text(text)
  self.class.filter_text(text, [:macropre, markup, :macropost, filters].flatten)
end

#helpObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/text_filter.rb', line 47

def help
  filter_map = TextFilterPlugin.filter_map
  filter_types = TextFilterPlugin.available_filter_types

  help = []
  help.push(filter_map[markup])
  filter_types["macropre"].sort_by(&:short_name).each { |f| help.push f }
  filter_types["macropost"].sort_by(&:short_name).each { |f| help.push f }
  filters.each { |f| help.push(filter_map[f.to_s]) }

  help_text = help.map do |f|
    if f.help_text.blank?
      ""
    else
      "<h3>#{f.display_name}</h3>\n#{BlueCloth.new(f.help_text).to_html}\n"
    end
  end

  help_text.join("\n")
end

#sanitize(*args, &blk) ⇒ Object



20
21
22
# File 'app/models/text_filter.rb', line 20

def sanitize(*args, &blk)
  self.class.sanitize(*args, &blk)
end