Class: Me2Text::Keyword

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

Constant Summary collapse

KEYWORD_REGEX =
/(\[([^\[\]]+)\])/u

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(keyword, options) ⇒ Keyword

Returns a new instance of Keyword.

Raises:

  • (ArgumentError)


301
302
303
304
305
306
307
308
309
# File 'lib/me2text/token.rb', line 301

def initialize(keyword, options)
  # keyword는 "[키워드]" 같은 형태
  _keyword = keyword.to_s.strip.scan(KEYWORD_REGEX)
  _keyword = _keyword.flatten[1]

  raise ArgumentError.new("키워드가 없습니다.") if _keyword.nil?
  
  @text = _keyword.gsub(ESCAPE_CHAR, "\"")
end

Instance Attribute Details

Returns the value of attribute link.



299
300
301
# File 'lib/me2text/token.rb', line 299

def link
  @link
end

Instance Method Details

#length(options = {}) ⇒ Object



323
324
325
# File 'lib/me2text/token.rb', line 323

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

#to_html(options = {}) ⇒ Object



311
312
313
314
315
316
317
# File 'lib/me2text/token.rb', line 311

def to_html(options = {})
  if options[:keyword_handler] && options[:keyword_handler].is_a?(Proc)
    options[:keyword_handler].call(@text, options)
  else
    "[#{htmlize(@text, options)}]"
  end
end

#to_text(options = {}) ⇒ Object



319
320
321
# File 'lib/me2text/token.rb', line 319

def to_text(options = {})
  "[#{textize(@text, options)}]"
end

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



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/me2text/token.rb', line 327

def truncate(nos, format, options = {})
  options = {
    :ellipsis => "…"
  }.merge(options)
  
  val = self.to_text(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