Class: Middleman::Vegas::TableFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-vegas/formatters/table_formatter.rb

Overview

When you want to render code with a title, a link, line numbers and highlights in a table style.

Instance Method Summary collapse

Instance Method Details

#caption(options) ⇒ Object

Generates a caption above the code area when there is a title / url



71
72
73
74
75
76
77
78
79
# File 'lib/middleman-vegas/formatters/table_formatter.rb', line 71

def caption(options)
  if options[:title]
    figcaption  = "<figcaption class='code-highlight-caption'><span class='code-highlight-caption-title'>#{options[:title]}</span>"
    figcaption += "<a class='code-highlight-caption-link' href='#{options[:url]}'>#{(options[:link_text] || 'link').strip}</a>" if options[:url]
    figcaption += "</figcaption>"
  else
    ''
  end
end

#expand_tokens_with_newlines(lexed_code) ⇒ Object

The lexed code generates an enumerator of tokens with their values. Before they are rendered to HTML all of the non-text tokens with newlines should be split into several tokens of the same type. This ensures that when they are tableized later the surrounding spans are not broken.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/middleman-vegas/formatters/table_formatter.rb', line 26

def expand_tokens_with_newlines(lexed_code)
  full_lex = []
  lexed_code.each do |token, value|
    if %w[ Text Text.Whitespace ].include? token.qualname
      full_lex << [ token, value ]
    else
      lines = value.split("\n")
      lines.each_with_index do |line, index|
        # if not the last line or the last line had a newline at the end
        suffix = if index < (lines.length - 1) || (index == (lines.length - 1) && value.end_with?("\n"))
          "\n"
        else
          ""
        end

        full_lex << [ token, "#{line}#{suffix}" ]
      end
    end
  end
  full_lex
end

#render(code, metadata) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/middleman-vegas/formatters/table_formatter.rb', line 9

def render(code, )
  lexer = Rouge::Lexer.find_fancy([:lang], code) || Rouge::Lexers::PlainText
  lexed_code = expand_tokens_with_newlines(lexer.lex(code, {}))

  formatter = Rouge::Formatters::HTML.new(wrap: false)
  rendered_code = formatter.format(lexed_code)
  rendered_code = tableize_code(rendered_code, )

  classnames = [ 'code-highlight-figure', [:class].to_s ].join(' ')

  "<figure class='#{classnames}'>#{caption()}#{rendered_code}</figure>"
end

#tableize_code(code, options) ⇒ Object

Given the rendered code it is time to present the information in a table.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/middleman-vegas/formatters/table_formatter.rb', line 49

def tableize_code(code, options)
  start = options[:start] || 1
  lines = options[:linenos] || false
  marks = options[:marks]

  table = "<div class='code-highlight'>"
  table += "<pre class='code-highlight-pre'>"
  code.lines.each_with_index do |line,index|
    classes = 'code-highlight-row'
    classes += lines ? ' numbered' : ' unnumbered'
    if marks.include? index + start
      classes += ' marked-line'
      classes += ' start-marked-line' unless marks.include? index - 1 + start
      classes += ' end-marked-line' unless marks.include? index + 1 + start
    end
    line = line.strip.empty? ? ' ' : line
    table += "<div data-line='#{index + start}' class='#{classes}'><div class='code-highlight-line'>#{line}</div></div>"
  end
  table +="</pre></div>"
end