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
- #block_code(code, language) ⇒ Object
-
#code_block(input) ⇒ Object
convert “‘ruby title “` like code block.
- #codespan(code) ⇒ Object
- #liquid_code_block(input) ⇒ Object
- #render_octopress_like_code_block(lang, str, caption, options) ⇒ Object
- #render_plain(code) ⇒ Object
Instance Method Details
#block_code(code, language) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/ultra_markdown/filter/code_block.rb', line 98 def block_code(code, language) lexer = Rouge::Lexer.find(language) if lexer formatter = ::Rouge::Formatters::HTML.new(:wrap => true, :css_class => 'highlight', :wrapper_tag => "div" ) formatter.format(lexer.lex(code)) else render_plain(code) end 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 = nil = $1 || '' lang = nil str = $2 if =~ AllOptions lang = $1 = "<figcaption><span>#{$2}\n</span><a href='#{$3}'>#{$4 || 'link'}</a></figcaption>" elsif =~ LangCaption lang = $1 = "<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, , ) end end |
#codespan(code) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ultra_markdown/filter/code_block.rb', line 114 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 = nil = $1 str = $2 lang = nil if =~ /\s*lang:(\S+)/i lang = $1 = .sub(/\s*lang:(\S+)/i,'') end if =~ CaptionUrlTitle file = $1 = "<figcaption><span>#{$1}\n</span><a href='#{$2}'>#{$3 || 'link'}</a></figcaption>" elsif =~ Caption file = $1 = "<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, , ) 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, , ) if is_rss code = block_code(str, "text") "<figure class='figure-code code'>#{}#{code}</figure>" elsif lang.nil? || lang == 'plain' code = block_code(str, nil) "<figure class='figure-code code'>#{}#{code}</figure>" elsif lang == 'mathjax' "<script type=\"math/tex; mode=display\">#{str}</script>" else if lang.include? "-raw" raw = "``` #{.sub('-raw', '')}\n" raw += str raw += "\n```\n" else code = block_code(str, lang) "<figure class='figure-code code'>#{}#{code}</figure>" end end end |
#render_plain(code) ⇒ Object
108 109 110 111 112 |
# File 'lib/ultra_markdown/filter/code_block.rb', line 108 def render_plain(code) lexer = Rouge::Lexer.find("text") formatter = ::Rouge::Formatters::HTML.new(:wrap => true, :css_class => 'highlight', :wrapper_tag => "div" ) formatter.format(lexer.lex(code)) end |