Module: NanDoc::Filters::General::TerminalColorToHtml

Included in:
NanDoc::Filters::General
Defined in:
lib/nandoc/filters.rb,
lib/nandoc/filters.rb

Instance Method Summary collapse

Instance Method Details

#terminal_color_to_html(str) ⇒ Object

this sucks.



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/nandoc/filters.rb', line 408

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