Class: MarkdownHighlightExtendedFilter
- Inherits:
-
HTML::Pipeline::TextFilter
- Object
- HTML::Pipeline::TextFilter
- MarkdownHighlightExtendedFilter
- Defined in:
- lib/markdown-highlight-extended-filter.rb
Overview
Stolen from github.com/imathis/octopress/blob/master/plugins/backtick_code_block.rb (MIT License)
Constant Summary collapse
- AllOptions =
/([^\s]+)\s+(.+?)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i- LangCaption =
/([^\s]+)\s*(.+)?/i
Instance Method Summary collapse
- #call ⇒ Object
- #format_syntax(text) ⇒ Object
- #highlight_code(code, lang) ⇒ Object
- #lexer_for(lang) ⇒ Object
Instance Method Details
#call ⇒ Object
11 12 13 |
# File 'lib/markdown-highlight-extended-filter.rb', line 11 def call format_syntax(@text) end |
#format_syntax(text) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/markdown-highlight-extended-filter.rb', line 15 def format_syntax(text) text.gsub(/^`{3} *([^\n]+)?\n(.+?)\n`{3}/m) do = $1 || '' str = $2 if =~ AllOptions lang = $1 caption = "<figcaption><span>#{$2}</span><a href='#{$3}'>#{$4 || 'link'}</a></figcaption>" elsif =~ LangCaption lang = $1 caption = "<figcaption><span>#{$2}</span></figcaption>" end if str.match(/\A( {4}|\t)/) str = str.gsub(/^( {4}|\t)/, '') end if lang.nil? || lang == 'plain' code = highlight_code(str.gsub('<','<').gsub('>','>'), "plain") "<figure class='code'>#{caption}#{code}</figure>" else if lang.include? "-raw" raw = "``` #{.sub('-raw', '')}\n" raw += str raw += "\n```\n" else code = highlight_code(str, lang) "<figure class='code'>#{caption}#{code}</figure>" end end end end |
#highlight_code(code, lang) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/markdown-highlight-extended-filter.rb', line 47 def highlight_code(code, lang) if lexer = lexer_for(lang) lexer.highlight(code, :options => { :cssclass => "highlight highlight-#{lang}" }) else "<pre>#{code}</pre>" end end |
#lexer_for(lang) ⇒ Object
56 57 58 |
# File 'lib/markdown-highlight-extended-filter.rb', line 56 def lexer_for(lang) (Linguist::Language[lang] && Linguist::Language[lang].lexer) || Pygments::Lexer[lang] end |