Class: CanvasCc::CanvasCC::Models::Page

Inherits:
Resource
  • Object
show all
Defined in:
lib/canvas_cc/canvas_cc/models/page.rb

Constant Summary collapse

PAGE_ID_POSTFIX =
'_PAGE'
WIKI_CONTENT =
'wiki_content'
BOOK_PATH =
WIKI_CONTENT + '/books'
EDITING_ROLE_TEACHER =
'teachers'
MAX_URL_LENGTH =
80

Constants inherited from Resource

Resource::WEB_CONTENT_TYPE

Instance Attribute Summary collapse

Attributes inherited from Resource

#dependencies, #files, #href, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#attributes

Constructor Details

#initializePage

Returns a new instance of Page.



14
15
16
17
18
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 14

def initialize
  super
  @type = WEB_CONTENT_TYPE
  @front_page = false
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



12
13
14
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 12

def body
  @body
end

#editing_rolesObject

Returns the value of attribute editing_roles.



12
13
14
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 12

def editing_roles
  @editing_roles
end

#front_pageObject

Returns the value of attribute front_page.



12
13
14
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 12

def front_page
  @front_page
end

#titleObject

Returns the value of attribute title.



12
13
14
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 12

def title
  @title
end

#workflow_stateObject

Returns the value of attribute workflow_state.



12
13
14
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 12

def workflow_state
  @workflow_state
end

Class Method Details

.collapse(title, character = " ") ⇒ Object



147
148
149
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 147

def self.collapse(title, character = " ")
  title.sub(/^#{character}*/, "").sub(/#{character}*$/, "").squeeze(character)
end

.convert_accented_entities(title) ⇒ Object



83
84
85
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 83

def self.convert_accented_entities(title)
  title.gsub(/&([A-Za-z])(grave|acute|circ|tilde|uml|ring|cedil|slash);/, '\1')
end

.convert_misc_characters(title) ⇒ Object



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
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 114

def self.convert_misc_characters(title)
  title = title.gsub(/\.{3,}/, " dot dot dot ")
  # Special rules for money
  {
    /(\s|^)\$(\d+)\.(\d+)(\s|$)/ => '\2 dollars \3 cents',
    /(\s|^)£(\d+)\.(\d+)(\s|$)/u => '\2 pounds \3 pence',
  }.each do |found, replaced|
    replaced = " #{replaced} " unless replaced =~ /\\1/
    title.gsub!(found, replaced)
  end
  # Back to normal rules
  {
    /\s*&\s*/ => "and",
    /\s*#/ => "number",
    /\s*@\s*/ => "at",
    /(\S|^)\.(\S)/ => '\1 dot \2',
    /(\s|^)\$(\d*)(\s|$)/ => '\2 dollars',
    /(\s|^)£(\d*)(\s|$)/u => '\2 pounds',
    /(\s|^)¥(\d*)(\s|$)/u => '\2 yen',
    /\s*\*\s*/ => "star",
    /\s*%\s*/ => "percent",
    /\s*(\\|\/)\s*/ => "slash",
  }.each do |found, replaced|
    replaced = " #{replaced} " unless replaced =~ /\\1/
    title.gsub!(found, replaced)
  end
  title.gsub(/(^|\w)'(\w|$)/, '\1\2').gsub(/[\.,:;()\[\]\/\?!\^'"_]/, " ")
end

.convert_misc_entities(title) ⇒ Object



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
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 87

def self.convert_misc_entities(title)
  {
    "#822[01]" => "\"",
    "#821[67]" => "'",
    "#8230" => "...",
    "#8211" => "-",
    "#8212" => "--",
    "#215" => "x",
    "gt" => ">",
    "lt" => "<",
    "(#8482|trade)" => "(tm)",
    "(#174|reg)" => "(r)",
    "(#169|copy)" => "(c)",
    "(#38|amp)" => "and",
    "nbsp" => " ",
    "(#162|cent)" => " cent",
    "(#163|pound)" => " pound",
    "(#188|frac14)" => "one fourth",
    "(#189|frac12)" => "half",
    "(#190|frac34)" => "three fourths",
    "(#176|deg)" => " degrees"
  }.each do |textiled, normal|
    title.gsub!(/&#{textiled};/, normal)
  end
  title.gsub(/&[^;]+;/, "")
end

.convert_name_to_url(name) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 33

def self.convert_name_to_url(name)
  url = urlify(name)
  if url.length > MAX_URL_LENGTH
    url = url[0,MAX_URL_LENGTH][/.{0,#{MAX_URL_LENGTH}}/mu]
  end
  url
end

.path_safeObject



151
152
153
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 151

def self.path_safe
  gsub(/[^a-zA-Z0-9\-_]+/, '-')
end

.remove_formatting(title) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 67

def self.remove_formatting(title)
  title = strip_html_tags(title)
  title = convert_accented_entities(title)
  title = convert_misc_entities(title)
  title = convert_misc_characters(title)
  collapse(title)
end

.replace_whitespace(title, replace = " ") ⇒ Object



143
144
145
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 143

def self.replace_whitespace(title, replace = " ")
  title.gsub(/\s+/, replace)
end

.strip_html_tags(title, leave_whitespace = false) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 75

def self.strip_html_tags(title, leave_whitespace = false)
  name = /[\w:_-]+/
  value = /([A-Za-z0-9]+|('[^']*?'|"[^"]*?"))/
  attr = /(#{name}(\s*=\s*#{value})?)/
  rx = /<[!\/?\[]?(#{name}|--)(\s+(#{attr}(\s+#{attr})*))?\s*([!\/?\]]+|--)?>/
  (leave_whitespace) ?  title.gsub(rx, "").strip : title.gsub(rx, "").gsub(/\s+/, " ").strip
end

.to_url(title) ⇒ Object



61
62
63
64
65
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 61

def self.to_url(title)
  title = remove_formatting(title).downcase
  title = replace_whitespace(title, "-")
  title = collapse(title, "-")
end

.urlify(name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 41

def self.urlify(name)
  name = name.downcase.gsub(/\s/, '-').gsub('.', '-dot-')
  name.gsub!('(', '-')
  name.gsub!(')', '-')
  name.gsub!('_', '-')
  name.gsub!('--', '-')
  name.gsub!('"', '')
  name.gsub!('$', ' dollars ')
  name.gsub!(/[äöüß]/) do |match|
    case match
    when "ä" then 'a'
    when "ö" then 'o'
    when "ü" then 'u'
    when "ß" then 'ss'
    end
  end
  name = to_url(name)
  CGI::escape(name)
end

Instance Method Details

#identifierObject



24
25
26
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 24

def identifier
  @identifier
end

#identifier=(identifier) ⇒ Object



20
21
22
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 20

def identifier=(identifier)
  @identifier = identifier
end

#page_name=(name) ⇒ Object



28
29
30
31
# File 'lib/canvas_cc/canvas_cc/models/page.rb', line 28

def page_name= name
  @title = name
  @href = "#{WIKI_CONTENT}/#{self.class.convert_name_to_url(name)}.html"
end