Class: DTextRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/dtext_ruby.rb

Constant Summary collapse

MENTION_REGEXP =
/(?<=^| )@\S+/

Class Method Summary collapse

Class Method Details

.h(string) ⇒ Object



11
12
13
# File 'lib/dtext_ruby.rb', line 11

def self.h(string)
  CGI.escapeHTML(string)
end

.parse(str, options = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/dtext_ruby.rb', line 184

def self.parse(str, options = {})
  return "" if str.nil?

  # Make sure quote tags are surrounded by newlines

  unless options[:inline]
    str.gsub!(/\s*\[quote\](?!\])\s*/m, "\n\n[quote]\n\n")
    str.gsub!(/\s*\[\/quote\]\s*/m, "\n\n[/quote]\n\n")
    str.gsub!(/\s*\[code\](?!\])/m, "\n\n[code]\n\n")
    str.gsub!(/\[\/code\]\s*/m, "\n\n[/code]\n\n")
    str.gsub!(/\s*\[spoilers?\](?!\])\s*/m, "\n\n[spoiler]\n\n")
    str.gsub!(/\s*\[\/spoilers?\]\s*/m, "\n\n[/spoiler]\n\n")
    str.gsub!(/^(h[1-6]\.\s*.+)$/, "\n\n\\1\n\n")
    str.gsub!(/\s*\[expand(\=[^\]]*)?\](?!\])\s*/m, "\n\n[expand\\1]\n\n")
    str.gsub!(/\s*\[\/expand\]\s*/m, "\n\n[/expand]\n\n")
    str.gsub!(/\s*\[table\](?!\])\s*/m, "\n\n[table]\n\n")
    str.gsub!(/\s*\[\/table\]\s*/m, "\n\n[/table]\n\n")
  end

  str.gsub!(/(?:\r?\n){3,}/, "\n\n")
  str.strip!
  blocks = str.split(/(?:\r?\n){2}/)
  stack = []
  flags = {}

  html = blocks.map do |block|
    case block
    when /\A(h[1-6])\.\s*(.+)\Z/
      tag = $1
      content = $2

      if options[:inline]
        "<h6>" + parse_inline(content, options) + "</h6>"
      else
        "<#{tag}>" + parse_inline(content, options) + "</#{tag}>"
      end

    when /^\s*\*+ /
      parse_list(block, options)

    when "[quote]"
      if options[:inline]
        ""
      else
        stack << "blockquote"
        "<blockquote>"
      end

    when "[/quote]"
      if options[:inline]
        ""
      elsif stack.last == "blockquote"
        stack.pop
        '</blockquote>'
      else
        ""
      end

    when "[spoiler]"
      stack << "spoiler"
      '<div class="spoiler">'

    when "[/spoiler]"
      if stack.last == "spoiler"
        stack.pop
        "</div>"
      else
        ""
      end

    when "[table]"
      stack << "table"
      flags[:table] = true
      '<table class="striped">'

    when "[/table]"
      if stack.last == "table"
        stack.pop
        flags[:table] = false
        "</table>"
      else
        ""
      end

    when /\[code\](?!\])/
      flags[:code] = true
      stack << "pre"
      '<pre>'

    when /\[\/code\](?!\])/
      flags[:code] = false
      if stack.last == "pre"
        stack.pop
        "</pre>"
      else
        ""
      end

    when /\[expand(?:\=([^\]]*))?\](?!\])/
      stack << "expandable"
      expand_html = '<div class="expandable"><div class="expandable-header">'
      expand_html << "<span>#{h($1)}</span>" if $1
      expand_html << '<input type="button" value="Show" class="expandable-button"/></div>'
      expand_html << '<div class="expandable-content">'
      expand_html

    when /\[\/expand\](?!\])/
      if stack.last == "expandable"
        stack.pop
        '</div></div>'
      end

    else
      if flags[:code]
        CGI.escape_html(block) + "\n\n"
      elsif flags[:table]
        parse_table_elements(block)
      else
        '<p>' + parse_inline(block) + '</p>'
      end
    end
  end

  stack.reverse.each do |tag|
    if tag == "blockquote"
      html << "</blockquote>"
    elsif tag == "div"
      html << "</div>"
    elsif tag == "pre"
      html << "</pre>"
    elsif tag == "spoiler"
      html << "</div>"
    elsif tag == "expandable"
      html << "</div></div>"
    elsif tag == "table"
      html << "</table>"
    end
  end

  html.join("")
end


104
105
106
107
108
109
110
# File 'lib/dtext_ruby.rb', line 104

def self.parse_aliased_wiki_links(str)
  str.gsub(/\[\[([^\|\]]+)\|([^\]]+)\]\]/m) do
    text = CGI.unescapeHTML($2)
    title = CGI.unescapeHTML($1).tr(" ", "_").downcase
    %{<a href="/wiki_pages/show_or_new?title=#{u(title)}">#{h(text)}</a>}
  end
end


127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dtext_ruby.rb', line 127

def self.parse_id_links(str)
  str = str.gsub(/\bpost #(\d+)/i, %{<a href="/posts/\\1">post #\\1</a>})
  str = str.gsub(/\bforum #(\d+)/i, %{<a href="/forum_posts/\\1">forum #\\1</a>})
  str = str.gsub(/\btopic #(\d+)(?!\/p\d|\d)/i, %{<a href="/forum_topics/\\1">topic #\\1</a>})
  str = str.gsub(/\btopic #(\d+)\/p(\d+)/i, %{<a href="/forum_topics/\\1?page=\\2">topic #\\1/p\\2</a>})
  str = str.gsub(/\bcomment #(\d+)/i, %{<a href="/comments/\\1">comment #\\1</a>})
  str = str.gsub(/\bpool #(\d+)/i, %{<a href="/pools/\\1">pool #\\1</a>})
  str = str.gsub(/\buser #(\d+)/i, %{<a href="/users/\\1">user #\\1</a>})
  str = str.gsub(/\bartist #(\d+)/i, %{<a href="/artists/\\1">artist #\\1</a>})
  str = str.gsub(/\bissue #(\d+)/i, %{<a href="https://github.com/r888888888/danbooru/issues/\\1">issue #\\1</a>})
  str = str.gsub(/\bpixiv #(\d+)(?!\/p\d|\d)/i, %{<a href="http://www.pixiv.net/member_illust.php?mode=medium&illust_id=\\1">pixiv #\\1</a>})
  str = str.gsub(/\bpixiv #(\d+)\/p(\d+)/i, %{<a href="http://www.pixiv.net/member_illust.php?mode=manga_big&illust_id=\\1&page=\\2">pixiv #\\1/p\\2</a>})
end

.parse_inline(str, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dtext_ruby.rb', line 37

def self.parse_inline(str, options = {})
  str.gsub!(/&/, "&amp;")
  str.gsub!(/</, "&lt;")
  str.gsub!(/>/, "&gt;")
  str.gsub!(/\n/m, "<br>") unless options[:ignore_newlines]
  str.gsub!(/\[b\](.+?)\[\/b\]/i, '<strong>\1</strong>')
  str.gsub!(/\[i\](.+?)\[\/i\]/i, '<em>\1</em>')
  str.gsub!(/\[s\](.+?)\[\/s\]/i, '<s>\1</s>')
  str.gsub!(/\[u\](.+?)\[\/u\]/i, '<u>\1</u>')
  str.gsub!(/\[tn\](.+?)\[\/tn\]/i, '<p class="tn">\1</p>')

  str = parse_mentions(str)
  str = parse_links(str)
  str = parse_aliased_wiki_links(str)
  str = parse_wiki_links(str)
  str = parse_post_links(str)
  str = parse_id_links(str)
  str
end


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dtext_ruby.rb', line 79

def self.parse_links(str)
  str.gsub(/("[^"]+":(https?:\/\/|\/)[^\s\r\n<>]+|https?:\/\/[^\s\r\n<>]+|"[^"]+":\[(https?:\/\/|\/)[^\s\r\n<>\]]+\])+/) do |url|
    ch = ""

    if url =~ /^"([^"]+)":\[(.+)\]$/
      text = $1
      url = $2
    else
      if url =~ /^"([^"]+)":(.+)$/
        text = $1
        url = $2
      else
        text = url
      end

      if url =~ /([;,.!?\)\]<>])$/
        url.chop!
        ch = $1
      end
    end

    '<a href="' + url + '">' + text + '</a>' + ch
  end
end

.parse_list(str, options = {}) ⇒ Object



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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/dtext_ruby.rb', line 141

def self.parse_list(str, options = {})
  html = ""
  current_item = ""
  layout = []
  nest = 0

  str.split(/\n/).each do |line|
    if line =~ /^\s*(\*+) (.+)/
      if nest > 0
        html += "<li>#{current_item}</li>"
      elsif not current_item.strip.empty?
        html += "<p>#{current_item}</p>"
      end

      nest = $1.size
      current_item = parse_inline($2)
    else
      current_item += parse_inline(line)
    end

    if nest > layout.size
      html += "<ul>"
      layout << "ul"
    end

    while nest < layout.size
      elist = layout.pop
      if elist
        html += "</#{elist}>"
      end
    end
  end

  html += "<li>#{current_item}</li>"

  while layout.any?
    elist = layout.pop
    html += "</#{elist}>"
  end

  html
end

.parse_mentions(str) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dtext_ruby.rb', line 57

def self.parse_mentions(str)
  str.gsub!(MENTION_REGEXP) do |name| 
    next name unless name =~ /[a-z0-9]/i

    if name =~ /([:;,.!?\)\]<>])$/
      name.chop!
      ch = $1
    else
      ch = ""
    end

    '<a href="/users?name=' + u(CGI.unescapeHTML(name[1..-1])) + '">' + name + '</a>' + ch
  end
  str
end

.parse_post_links(str) ⇒ Object



120
121
122
123
124
125
# File 'lib/dtext_ruby.rb', line 120

def self.parse_post_links(str)
  str.gsub(/\{\{([^\}]+)\}\}/) do
    tags = CGI.unescapeHTML($1)
    %{<a rel="nofollow" href="/posts?tags=#{u(tags)}">#{h(tags)}</a>}
  end
end

.parse_table_elements(str) ⇒ Object



73
74
75
76
77
# File 'lib/dtext_ruby.rb', line 73

def self.parse_table_elements(str)
  str = parse_inline(str, :ignore_newlines => true)
  str.gsub!(/\[(\/?(?:tr|td|th|thead|tbody))\]/, '<\1>')
  str
end


112
113
114
115
116
117
118
# File 'lib/dtext_ruby.rb', line 112

def self.parse_wiki_links(str)
  str.gsub(/\[\[([^\]]+)\]\]/) do
    text = CGI.unescapeHTML($1)
    title = text.tr(" ", "_").downcase
    %{<a href="/wiki_pages/show_or_new?title=#{u(title)}">#{h(text)}</a>}
  end
end

.strip_blocks(string, tag) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dtext_ruby.rb', line 15

def self.strip_blocks(string, tag)
  blocks = string.scan(/\[\/?#{tag}\]|.+?(?=\[\/?#{tag}\]|$)/m)
  n = 0
  stripped = ""
  blocks.each do |block|
    case block
    when "[#{tag}]"
      n += 1

    when "[/#{tag}]"
      n -= 1

    else
      if n == 0
        stripped += block
      end
    end
  end

  stripped.strip
end

.u(string) ⇒ Object



7
8
9
# File 'lib/dtext_ruby.rb', line 7

def self.u(string)
  CGI.escape(string)
end