Module: Rmake::Notation

Defined in:
lib/rmake-notation/version.rb,
lib/rmake-notation.rb

Constant Summary collapse

VERSION =
'0.0.15'
DOMAIN =
"rmake.jp"

Instance Method Summary collapse

Instance Method Details

#add_plugin(plugin) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/rmake-notation.rb', line 37

def add_plugin(plugin)
  @plugins ||= []
  
  if (plugin.respond_to?(:target?) && plugin.respond_to?(:execute))
    @plugins << plugin
    true
  else
    false
  end
end

#block_to_html(block) ⇒ Object



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
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
# File 'lib/rmake-notation.rb', line 118

def block_to_html(block)
  case block
  when /\A[\[]/
    parsed_block = self.parse_inline(block)
    block = self.inline_to_html(parsed_block)
  when /(\A[{][{][{])/
    parsed_block = self.parse_inline(block.gsub("{{{", "[").gsub("}}}", "]"))
    block = self.inline_to_html(parsed_block)
  when /^[!]/
    len = block.length - block.gsub!(/^[!]*/, "").length + 2
    s = generate_contents(block)
    a_name = "#{@title_list.length + 1}_#{len}"
    @title_list << [s, len, a_name]
    block = "<a name=\"#{a_name}\"></a><h#{len} id=\"title_#{a_name}\">#{s}</h#{len}>"

  when /^[|][|][|]/
    block = self.process_table_line(block)
    
  when /^[|]/
    s_arr = block.split("\n")
    #s_arr.pop
    s = ""
    line_index = 0
    s_arr.each do |item|
      if line_index == 0
        item = '<tr><th>' + item[item.index('|') + 1..item.rindex('|') - 1] + '</th></tr>'
        s += item.split('|').join('</th><th>')
      else
        item = '<tr><td>' + item[item.index('|') + 1..item.rindex('|') - 1] + '</td></tr>'
        s += item.split('|').join('</td><td>')
      end

      line_index += 1
    end
    block = "<table class=\"table\">#{s}</table>"
    
  when /^[-]/
    block = list(block.split("\n"))

  when /^http[:][\/][\/]/
    block = "<a href='#{block}'>#{block}</a><br />"
    
  when /^https[:][\/][\/]/
    block = "<a href='#{block}'>#{block}</a><br />"
    
  else
    block = self.inline_to_html_from_block(block)
    block = block + "<br />" unless block == ""
  end
  
  block
end

#execute_plugins(command, parsed_block) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/rmake-notation.rb', line 322

def execute_plugins(command, parsed_block)
  result = false
  
  @plugins ||= []
  @plugins.each do |plugin|
    if plugin.target?(command)
      result = plugin.execute(command, parsed_block)
      break
    end
  end
  
  result
rescue => err
  parsed_block << "{{プラグインのエラー}}"
end

#generate_contents(content) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rmake-notation.rb', line 9

def generate_contents(content)
  @title_list ||= []
  
  result = ''
  blocks = self.to_blocks(content)
  
  blocks.each do |b|
    block = self.block_to_html(b)
    result += block
  end
  
  #result += " ********** " +  blocks.join(",")
  result.gsub("-)", "]")
  
rescue => err
  message = err.message + "\n"
  message += err.backtrace.join("\n")
  message += "文章にエラーもしくは不正な文字が含まれているようです。確認してください。"
end

#inline_to_html(parsed_block) ⇒ Object



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
# File 'lib/rmake-notation.rb', line 205

def inline_to_html(parsed_block)
  command = parsed_block[0]
  result = ''

  case command
  when 'link'
    link = parsed_block[1]
    title = parsed_block[1]
    if parsed_block.length > 2
      title = parsed_block[2..parsed_block.length-1].join(" ")
    end
    
    result = "<a href='" + link + "'>" + title + "</a>"
  when "code"
    text = parsed_block[1..parsed_block.length-1].join(" ")
    result = "<div style=\"margin: 0 1em;\"><pre class=\"prettyprint\">" + text + "</pre></div>"
    #p parsed_block
  when "pcode"
    text = parsed_block[1..parsed_block.length-1].join(" ")
    result = "<div style=\"margin: 0 1em;\"><pre style=\"font-family:sans-serif;\" class=\"prettyprint\">" + text + "</pre></div>"
  when "b"
    url = parsed_block[1]
    quote = parsed_block[2..parsed_block.length-1].join(" ")
    result = "<div class='blockquote'><blockquote cite='#{url}'><p class='bq'>#{quote}</p><cite><a href='#{url}'>#{url}</a></cite></blockquote></div>"
  when "image_url"
    url = parsed_block[1]
    result = "<img src=\"#{url}\" />"

  when "game"
    id = parsed_block[1]
    text = parsed_block[2] ? parsed_block[2..parsed_block.length-1] : nil
    text = text ? text.join(" ") : "ゲーム[ID:#{id}]"
    url = "http://#{DOMAIN}/games/#{id}/play"
    result = "<a href=\"#{url}\" title=\"#{text}\">#{text}</a>"
    
  when "item"
    id = parsed_block[1]
    text = parsed_block[2] ? parsed_block[2..parsed_block.length-1] : nil
    text = text ? text.join(" ") : "素材/ゲームデータ[ID:#{id}]"
    url = "http://#{DOMAIN}/published_items/#{id}"
    result = "<a href=\"#{url}\" title=\"#{text}\">#{text}</a>"
    
  when "wiki"
    text = parsed_block[1..parsed_block.length-1].join(" ")
    url = "https://#{DOMAIN}/page/a/" + text
    result = "<a href=\"#{url}\" title=\"#{text}\">#{text}</a>"

  when "jump_target"
    text = parsed_block[1] ? parsed_block[1..parsed_block.length-1] : nil
    result = "<a name=\"#{text.join}\"></a>"
    
  when "jump"
    link = parsed_block[1]
    title = parsed_block[1]
    if parsed_block.length > 2
      title = parsed_block[2..parsed_block.length-1].join(" ")
    end
    
    result = "<a href=\"##{link}\">" + title + "</a>"
    
  when "strike"
    text_data = parsed_block[1..parsed_block.length-1]
    text_data = text_data.is_a?(Array) ? text_data.join(' ') : text_data.to_s

    text = !text_data.blank? ? text_data : nil
    result = "<span style=\"text-decoration:line-through;\">#{text}</span>"

  when "bold"
    text = parsed_block[1] ? parsed_block[1..parsed_block.length-1] : nil

    if text.is_a? Array
      text = text.join
    end

    result = "<span style=\"font-weight:bold;\">#{text}</span>"

  when "memo"
    result = ""

  when "font"
    tags = parsed_block[1] ? parsed_block[1].split("_") : []
    style = ""
    tags.each do |tag|
      if tag == "bold"
        style += "font-weight:bold;"
      elsif tag == "italic"
        style += "font-style: italic;"
      elsif tag == "strike"
        style += "text-decoration:line-through;"
      elsif ["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"].include?(tag)
        style += "font-size:#{tag};"
      else
        style += "color:#{tag};"
      end
    end

    text_data = parsed_block[2..parsed_block.length-1]
    text_data = text_data.is_a?(Array) ? text_data.join(' ') : text_data.to_s
    
    text = !text_data.blank? ? text_data : nil
    result = "<span style=\"#{style}\">#{text}</span>"

  else
    
    result = execute_plugins(command, parsed_block)
    if result
      
    else
      result = "[" + parsed_block.join(" ") + "]"
    end
  end
  
  result
rescue => err
  "<div style=\"color:red;font-weight:bold;\">記述に間違いがあります: [" + parsed_block.join(" ") + "]</div>"
end

#inline_to_html_from_block(block) ⇒ Object



338
339
340
341
342
343
344
# File 'lib/rmake-notation.rb', line 338

def inline_to_html_from_block(block)
  block.gsub!(/[\[].*?[\]]/) {|s|
    s = self.inline_to_html(self.parse_inline(s))
  }
  
  block
end

#list(s_arr) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rmake-notation.rb', line 171

def list(s_arr)
  s_arr << ""
  list_arr = []
  s = ""
  s_arr.each do |item|
    if item.length > 0 && item.index("--") == 0
      list_arr << item[1..item.length-1]
    elsif item.length > 0 && item.index("-") == 0
      if list_arr.length > 0
        child_list = list(list_arr)
        list_arr = []
      else
        child_list = ""
      end
      
      line = generate_contents(item[1..item.length])
      if !child_list.blank?
        s += "#{child_list}</li><li>#{line}" 
      else
        s += "<li>#{line}" 
      end
      
    else
      if list_arr.length > 0
        child_list = list(list_arr)
        s += "#{child_list}"
      end
    end
  end
  s = s + "</li>" if !s.blank?
  s = generate_contents(s)
  "<ul class=\"wikiUl\">#{s}</ul>".gsub('<br />', '')
end

#parse_inline(content) ⇒ Object



346
347
348
# File 'lib/rmake-notation.rb', line 346

def parse_inline(content)
  content[1..content.length-2].sub(/([ ]|\n|\r\n|\r)/, " ").split(/[ ]/)
end

#process_table_line(block) ⇒ Object



57
58
59
60
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
# File 'lib/rmake-notation.rb', line 57

def process_table_line(block)
  header = true
  style = ""
  tag = "td style=\"#{style}\""
  code_reg = /[{][{][{].*?[}][}][}]/m
  
  pcodes = []
  block_text = block
  pcode_count = 0
  while block_text.index(code_reg)
    pcodes << self.generate_contents(block_text.slice(code_reg))
    block_text = block_text.sub(code_reg, "___dummy_pcodes___")
    
    pcode_count += 1
  end
  
  s_arr = block_text.split("\n")
  s = ""
  line_index = 0
  temp_item = ""
  
  s_arr.each do |item|
    temp_item += item

    if item.index("|||") == 0
      temp_item = ""
      if item.index("header:none")
        header = false
      end
      
    elsif temp_item.rindex(/[|]/).to_i == temp_item.length - 2
      item = temp_item
      
      if line_index == 0 && header
        tag = "th style=\"#{style}\""
      end

      item = "<tr><#{tag}>" + item[item.index('|') + 1..item.rindex('|') - 1] + "</#{tag}></tr>"

      items_before = item.split('|')
      items_after = []
      items_before.each do |before|
        items_after << self.generate_contents(before).gsub("<br />", "")
      end

      s += items_after.join("</#{tag}><#{tag}>")
      
      line_index += 1
      temp_item = ""
      tag = "td style=\"#{style}\""
    end

  end
  
  pcodes.each do |pcode|
    s = s.sub("___dummy_pcodes___", pcode)
  end
  
  block = "<table class=\"table\">#{s}</table>"
end

#reflesh_title_listObject



33
34
35
# File 'lib/rmake-notation.rb', line 33

def reflesh_title_list
  @title_list = []
end

#title_listObject



29
30
31
# File 'lib/rmake-notation.rb', line 29

def title_list
  @title_list
end

#to_blocks(content) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/rmake-notation.rb', line 48

def to_blocks(content)
  contents = Array.new
  # content.gsub(/(^[|].*?(\n)[^|])|(^[-].*?(?=((^[^-])|\z)))|(^[{][{][{].*?[}][}][}])|(^[\[].*?[\]])|(^[!]*.*?$)|(^http[:][\/][\/])|(?!^).*?$/m) {|s|
  content.gsub(/(^[|][|][|].*?[|][|][|])|(^[|].*?(?=(^[^|]|\z)))|(^[-].*?(?=((^[^-])|\z)))|(^[{][{][{].*?[}][}][}])|([\[].*?[\]])|(^[!]*.*?$)|(^http[:][\/][\/])|(^https[:][\/][\/])|(?!^).*?$/m) {|s|
    contents << s
  }
  contents
end