Module: UltraMarkdown::Filter::CodeBlock

Includes:
ERB::Util
Included in:
SyntaxConverter
Defined in:
lib/ultra_markdown/filter/code_block.rb

Constant Summary collapse

AllOptions =

for code_block

/([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
LangCaption =
/([^\s]+)\s*(.+)?/i
CaptionUrlTitle =

for liquid_code_block

/(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
Caption =
/(\S[\S\s]*)/

Instance Method Summary collapse

Instance Method Details

#block_code(code, language) ⇒ Object



100
101
102
103
104
# File 'lib/ultra_markdown/filter/code_block.rb', line 100

def block_code(code, language)
  Pygments.highlight(code, :lexer => language)
rescue => e
  return Pygments.highlight(code, :lexer => nil)
end

#code_block(input) ⇒ Object

convert “‘ruby title “` like code block



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ultra_markdown/filter/code_block.rb', line 19

def code_block(input)
    
  input.gsub!(/^`{3} *([^\n]+)?\n(.+?)\n`{3}/m) do
    caption = nil
    options = $1 || ''
    lang = nil
    str = $2
    
    if options =~ AllOptions
      lang = $1
      caption = "<figcaption><span>#{$2}\n</span><a href='#{$3}'>#{$4 || 'link'}</a></figcaption>"
    elsif options =~ LangCaption
      lang = $1
      caption = "<figcaption><span>#{$2}\n</span></figcaption>"
    end
    
    if str.match(/\A( {4}|\t)/)
      str = str.gsub(/^( {4}|\t)/, '')
    end
    
    render_octopress_like_code_block(lang, str, caption, options)
  end
end

#codespan(code) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ultra_markdown/filter/code_block.rb', line 106

def codespan(code)
  return "" if !code
    
  if code[0] == "$" && code[-1] == "$"
    code.gsub!(/^\$/,'')
    code.gsub!(/\$$/,'')
    "<script type=\"math/tex\">#{code}</script>"
  else
    "<code>#{ERB::Util.html_escape(code)}</code>"
  end
end

#liquid_code_block(input) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ultra_markdown/filter/code_block.rb', line 44

def liquid_code_block(input)
    
  input.gsub!(/^\{\% *codeblock([^\n\}]+)?\%\}.?\n?(.+?)\{\% *endcodeblock *\%\}/m) do
    caption = nil
    options = $1
    str = $2
    lang = nil
    
    if options =~ /\s*lang:(\S+)/i
      lang = $1
      options = options.sub(/\s*lang:(\S+)/i,'')
    end
    
    if options =~ CaptionUrlTitle
      file = $1
      caption = "<figcaption><span>#{$1}\n</span><a href='#{$2}'>#{$3 || 'link'}</a></figcaption>"
    elsif options =~ Caption
      file = $1
      caption = "<figcaption><span>#{$1}\n</span></figcaption>"
    end
    if file =~ /\S[\S\s]*\w+\.(\w+)/ && lang.nil?
      lang = $1
    end
    
    if str.match(/\A( {4}|\t)/)
      str = str.gsub(/^( {4}|\t)/, '')
    end
    
    render_octopress_like_code_block(lang, str, caption, options)
  end
end

#render_octopress_like_code_block(lang, str, caption, options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ultra_markdown/filter/code_block.rb', line 77

def render_octopress_like_code_block(lang, str, caption, options)
  if is_rss
    code = block_code(str, "text")
    "<figure class='figure-code code'>#{caption}#{code}</figure>"
  elsif lang.nil? || lang == 'plain'
    code = block_code(str, nil)
    "<figure class='figure-code code'>#{caption}#{code}</figure>"
  elsif lang == 'mathjax'
    "<script type=\"math/tex; mode=display\">#{str}</script>"
  else
    if lang.include? "-raw"
      raw = "``` #{options.sub('-raw', '')}\n"
      raw += str
      raw += "\n```\n"
    else
      code = block_code(str, lang)
      "<figure class='figure-code code'>#{caption}#{code}</figure>"
    end
  end
end