Module: NanDoc::SpecDoc::Playback::Terminal::ColorToHtml

Includes:
Nanoc3::Helpers::HTMLEscape
Included in:
Filters::Fence::Terminal, NanDoc::SpecDoc::Playback::Terminal
Defined in:
lib/nandoc/spec-doc/playback/terminal/color-to-html.rb

Constant Summary collapse

Code2CssClass =

this sucks.

the other side of these associations lives in trollop-subset.css

{
  '1' => 'bright',  '30' => 'black', '31' => 'red', '32' => 'green',
  '33' => 'yellow', '34' => 'blue', '35' => 'magenta', '36' => 'cyan',
  '37' => 'white'
}

Instance Method Summary collapse

Instance Method Details

#prompt_highlight(str) ⇒ Object

TODO:

html escape vis-a-vis blah

look for things that look like prompts and make them brighter



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nandoc/spec-doc/playback/terminal/color-to-html.rb', line 14

def prompt_highlight str
  lines = str.split("\n")
  linez = lines.map do |line|
    line.sub( %r!\A(~[^>]*>)?(.*)\Z! ) do
      tags = [];
      tags.push "<span class='prompt'>#{h($1)}</span>" if $1
      tags.push "<span class='normal'>#{h($2)}</span>" unless $2.empty?
      tags.join('')
    end
  end
  html = linez.join("\n")
  html
end

#prompt_highlight2(prompt, cmd) ⇒ Object



28
29
30
31
# File 'lib/nandoc/spec-doc/playback/terminal/color-to-html.rb', line 28

def prompt_highlight2 prompt, cmd
  "<span class='prompt'>#{h(prompt)}</span>"<<
  "<span class='normal'>#{h(cmd)}</span>\n"
end

#terminal_color_to_html(str) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nandoc/spec-doc/playback/terminal/color-to-html.rb', line 41

def terminal_color_to_html str
  return nil unless str.index("\e[") # save a whale
  scn = StringScanner.new(str)
  sexp = []
  while true
    foo = scn.scan(/(.*?)(?=\e\[)/m)
    if ! foo
      blork = scn.scan_until(/\Z/m) or fail("worglebezoik")
      sexp.push([:content, blork]) unless blork.empty?
      break;
    end
    foo or fail("oopfsh")
    sexp.push([:content, foo]) unless foo.empty?
    bar = scn.scan(/\e\[/) or fail("goff")
    baz = scn.scan(/\d+(?:;\d+)*/)
    baz or fail("narghh")
    if '0'==baz
      sexp.push([:pop])
    else
      sexp.push([:push, *baz.split(';')])
    end
    biff = scn.scan(/m/) or fail("noiflphh")
  end
  html = terminal_colorized_sexp_to_html sexp
  html
end