Class: Redcarpet::Render::HTMLwithSyntaxHighlight

Inherits:
HTML
  • Object
show all
Defined in:
lib/markdown-ruby-china.rb

Direct Known Subclasses

HTMLwithTopic

Instance Method Summary collapse

Constructor Details

#initialize(extensions = {}) ⇒ HTMLwithSyntaxHighlight

Returns a new instance of HTMLwithSyntaxHighlight.



17
18
19
20
21
22
# File 'lib/markdown-ruby-china.rb', line 17

def initialize(extensions={})
  super(extensions.merge(:xhtml => true,
                         :no_styles => true,
                         :filter_html => true,
                         :hard_wrap => true))
end

Instance Method Details



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/markdown-ruby-china.rb', line 45

def autolink(link, link_type)
  # return link
  if link_type.to_s == "email"
    link
  else
    begin
      # 防止 C 的 autolink 出来的内容有编码错误,万一有就直接跳过转换
      # 比如这句:
      # 此版本并非线上的http://yavaeye.com的源码.
      link.match(/.+?/)
    rescue
      return link
    end
    # Fix Chinese neer the URL
    bad_text = link.to_s.match(/[^\w:\/\-\,\$\!\.=\?&#+\|\%]+/im).to_s
    link = link.to_s.gsub(bad_text, '')
    "<a href=\"#{link}\" rel=\"nofollow\" target=\"_blank\">#{link}</a>#{bad_text}"
  end
end

#block_code(code, lexer) ⇒ Object

如果换行符没识别,那么code代码块将不会被这里调用



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/markdown-ruby-china.rb', line 25

def block_code(code, lexer)
  lexer = begin
    # 支持程序语言,文件名,别名等多种格式
    lexer = (Pygments::Lexer.find(lexer) || Pygments::Lexer.find_by_extname(".#{lexer}") || Pygments::Lexer.find_by_alias(lexer.downcase))
    lexer.aliases[0]
  rescue
    lexer.to_s.split('.')[-1] || 'text'
  end

  opts = {:lexer => lexer, :formatter => 'html', :options => {:encoding => 'utf-8', :linenos => 'table'}}
  begin
    Pygments.highlight(code, opts)
  rescue => e
    puts :language => lexer, :code => code
    puts e
    puts e.backtrace
    Pygments.highlight(code, opts.merge(:lexer => 'text'))
  end
end