Class: Me2Text::Link

Inherits:
Token
  • Object
show all
Defined in:
lib/me2text/token.rb

Constant Summary

Constants inherited from Token

Token::ESCAPE_CHAR, Token::REGEX_ME2LINK, Token::REGEX_URL

Instance Attribute Summary collapse

Attributes inherited from Token

#text

Instance Method Summary collapse

Methods inherited from Token

join_tokens, #to_s, tokenize, tokenize_keyword, tokenize_me2link, tokenize_plaintext

Constructor Details

#initialize(label, link, include_ws = false) ⇒ Link

Returns a new instance of Link.



229
230
231
232
233
# File 'lib/me2text/token.rb', line 229

def initialize(label, link, include_ws = false)
  @text = label.gsub(ESCAPE_CHAR, "\"")
  @link = link.gsub("'","%27").gsub("\"","%22").gsub("<", "%3C").gsub(">", "%3E")
  @include_ws = include_ws   
end

Instance Attribute Details

#include_wsObject

Returns the value of attribute include_ws.



227
228
229
# File 'lib/me2text/token.rb', line 227

def include_ws
  @include_ws
end

Returns the value of attribute link.



226
227
228
# File 'lib/me2text/token.rb', line 226

def link
  @link
end

Instance Method Details

#length(options = {}) ⇒ Object



273
274
275
# File 'lib/me2text/token.rb', line 273

def length(options = {})
  textize(@text,options).length
end

#to_html(options = {}) ⇒ Object



261
262
263
264
265
266
267
# File 'lib/me2text/token.rb', line 261

def to_html(options = {})
  if options[:open_new_window]
    "<a href='#{link}' target='_blank'>#{to_label(:html,options)}</a>"
  else
    "<a href='#{link}'>#{to_label(:html,options)}</a>"
  end
end

#to_label(format, options = {}) ⇒ Object



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
# File 'lib/me2text/token.rb', line 235

def to_label(format,options = {})
  options = options.merge({
    :linklize => false
  })
  
  result = if format == :html
    htmlize(@text, options)
  elsif format == :text
    textize(@text, options)
  else
    @text
  end
  
  if result.strip.length == 0
    if format == :html 
      result = htmlize(" " + link + " ",options)
    elsif format == :text
      result = textize(" " + link + " " ,options)
    else
      @text
    end
  end
  
  return result
end

#to_text(options = {}) ⇒ Object



269
270
271
# File 'lib/me2text/token.rb', line 269

def to_text(options = {})
  to_label(:text, options)
end

#truncate(nos, format, options = {}) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/me2text/token.rb', line 277

def truncate(nos, format, options = {})
  options = {
    :ellipsis => ""
  }.merge(options)
  
  val = self.to_label(format,options)
  if (val.length - nos) <= 0 
    #빼면 아무것도 없다면..
    ##이 링크는 버린다
    options[:ellipsis]
  else
    val = val.split(//u)[0...(val.length-nos) - 1]
    val << options[:ellipsis]
  end
  
  @text= val 
  [to_s(format,options), length(options)]
end