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



63
64
65
66
67
68
69
70
# File 'app/models/text_filter.rb', line 63

def self.all
  [
    markdown,
    smartypants,
    markdown_smartypants,
    none
  ]
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



72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/text_filter.rb', line 72

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

.markdownObject



85
86
87
88
89
# File 'app/models/text_filter.rb', line 85

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

.markdown_smartypantsObject



98
99
100
101
102
# File 'app/models/text_filter.rb', line 98

def self.markdown_smartypants
  new(name: "markdown smartypants",
      description: "Markdown with smart quotes",
      markup: "markdownsmartquotes")
end

.noneObject



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

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

.smartypantsObject



91
92
93
94
95
96
# File 'app/models/text_filter.rb', line 91

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

Instance Method Details

#commenthelpObject



54
55
56
57
58
59
60
61
# File 'app/models/text_filter.rb', line 54

def commenthelp
  help_filters = TextFilterPlugin
    .expand_filter_list([markup, filters].flatten)

  help_filters.map do |f|
    f.help_text.blank? ? "" : CommonMarker.render_html(f.help_text)
  end.join("\n")
end

#filter_text(text) ⇒ Object



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

def filter_text(text)
  all_filters = TextFilterPlugin
    .expand_filter_list([:macropre, markup, :macropost, filters].flatten)

  all_filters.each do |filter|
    text = filter.filtertext(text)
  end

  text
end

#helpObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/text_filter.rb', line 39

def help
  help_filters = TextFilterPlugin
    .expand_filter_list([markup, :macropre, :macropost, filters].flatten)

  help_text = help_filters.map do |f|
    if f.help_text.blank?
      ""
    else
      "<h3>#{f.display_name}</h3>\n#{CommonMarker.render_html(f.help_text, :DEFAULT)}"
    end
  end

  help_text.join("\n")
end

#sanitizeObject



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

def sanitize(...)
  self.class.sanitize(...)
end