Class: Redcarpet::Render::HTMLwithSyntaxHighlight

Inherits:
HTML
  • Object
show all
Includes:
Rouge::Plugins::Redcarpet
Defined in:
lib/markdown.rb

Direct Known Subclasses

HTMLwithTopic

Instance Method Summary collapse

Constructor Details

#initialize(extensions = {}) ⇒ HTMLwithSyntaxHighlight

Returns a new instance of HTMLwithSyntaxHighlight.


13
14
15
16
17
18
19
# File 'lib/markdown.rb', line 13

def initialize(extensions = {})
  super(extensions.merge(xhtml: true,
                         no_styles: true,
                         escape_html: true,
                         hard_wrap: true,
                         link_attributes: { target: '_blank' }))
end

Instance Method Details

Fix Chinese neer the URL


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/markdown.rb', line 58

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.match(%r{[^\w:@/\-~,$!.=?&#+|%]+}im).to_s
    link.gsub!(bad_text, '')
    %(<a href="#{link}" rel="nofollow" target="_blank">#{link}</a>#{bad_text})
  end
end

#block_code(code, language) ⇒ Object


21
22
23
24
25
26
27
# File 'lib/markdown.rb', line 21

def block_code(code, language)
  language.downcase! if language.is_a?(String)
  html = super(code, language)
  # 将最后行的 "\n\n" 替换成回 "\n", rouge 0.3.2 的 Bug 导致
  html.gsub!(%r{([\n]+)</code>}, '</code>')
  html
end

#image(link, title, alt_text) ⇒ Object

Extend to support img width ![](foo.jpg =300x) ![](foo.jpg =300x200) Example: gist.github.com/uupaa/f77d2bcf4dc7a294d109


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

def image(link, title, alt_text)
  links = link.split(" ")
  link = links[0]
  if links.count > 1
    # 原本 Markdown 的 title 部分是需要引号的 ![](foo.jpg "Title")
    # ![](foo.jpg =300x)
    title = links[1]
  end

  if title =~ /=(\d+)x(\d+)/
    %(<img src="#{link}" width="#{$1}px" height="#{$2}px" alt="#{alt_text}">)
  elsif title =~ /=(\d+)x/
    %(<img src="#{link}" width="#{$1}px" alt="#{alt_text}">)
  elsif title =~ /=x(\d+)/
    %(<img src="#{link}" height="#{$1}px" alt="#{alt_text}">)
  else
    %(<img src="#{link}" title="#{title}" alt="#{alt_text}">)
  end
end

#table(header, body) ⇒ Object


29
30
31
# File 'lib/markdown.rb', line 29

def table(header, body)
  %(<table class="table table-bordered table-striped">#{header}#{body}</table>)
end