prettyprint icon indicating copy to clipboard operation
prettyprint copied to clipboard

Add support for different output formats

Open mre opened this issue 6 years ago • 8 comments

Would be interesting to also support HTML output, for example.

mre avatar Nov 19 '18 17:11 mre

This seems to be harder than I thought. I tried a few ansi-to-html and ansi-to-svg translators, but none really worked besides aha, which is written in C. I'd prefer to have a Rust pendant for that. It's just a single file of quite readable code, so it can be done. If anyone wants to give it a try, that would be amazing.

mre avatar Dec 01 '18 23:12 mre

I'd be interested in trying this. Some questions:

  • We can only use aha via the MPL 1.1 license or LGPL 2 or later, so it can't be part of prettyprint (though because it's LGPL, it could be an independent crate that's one of prettyprint's dependencies.) Is that acceptable? (A quick Google search for other ansi-to-html projects showed quite a few MIT licenses, so I could work from those instead.)
  • I'm not familiar with the idiom "Rust pendant" — Rust module?
  • Would code that only supports ANSI escape sequences for color be a good first implementation? (Rather than things like "Move cursor to position x;y, delete to end of line, move up one line, overwrite three characters", etc.)

yarrow avatar Dec 02 '18 17:12 yarrow

Wouldn't there be another way by not rendering to ANSI at all? syntect exposes the highlighted code in a structured way that could also be rendered to HTML directly. There is even a function in syntect that does this (check out the syntect::html module).

sharkdp avatar Dec 02 '18 17:12 sharkdp

Thanks for offering your help here @yarrow. I would make the Rust port of aha an independent crate, yes. It would make for a nice library.

I'm not familiar with the idiom "Rust pendant" — Rust module?

Hehe, turns out this doesn't make any sense in English. I was referring to the french word for "counterpart". It's quite common to use that term in German. 😊

Would code that only supports ANSI escape sequences for color be a good first implementation? (Rather than things like "Move cursor to position x;y, delete to end of line, move up one line, overwrite three characters", etc.)

Of course. It sounds like a cool project to build. Limited in scope and well testable.

syntect exposes the highlighted code in a structured way that could also be rendered to HTML directly.

Oh, that's great @sharkdp, I didn't know that. Yeah that would be an alternative approach. I think that I would even prefer that one for inclusion in prettyprint. @yarrow, nonetheless a port of aha might still be a good exercise and it might make for a great standalone tool. However, if you're more interested in looking into syntext::html, that would also be an option. That would be a good candidate for a direct pull request to prettyprint. Up to you.

mre avatar Dec 02 '18 19:12 mre

I'll look into both. (Unless @sharkdp is eager to do the syntect::html implementation, in which case I'll just look into ansi-to-html. ^_^)

yarrow avatar Dec 02 '18 19:12 yarrow

I'm going to work on using syntect for HTML. This is a bit complicated, because bat wrote it's header, footer, and line numbers using ANSI codes directly. To use syntect for those I'll need to convert decorations.rs and preprocessor.rs to return syntect-compatible data, to be turned into ansi by the current code and HTML by syntect. So:

  1. Add tests for decorations and preprocessor to limit regressions;
  2. Refactor those modules;
  3. Add an HTML printer to printer.rs; and
  4. Add the HTML option to builder.rs.

yarrow avatar Dec 06 '18 17:12 yarrow

Sounds like a plan. 👍

mre avatar Dec 07 '18 09:12 mre

I'm still working on this — I got distracted by things like preventing line breaks in the middle of Unicode graphemes. I still intend to do those things, but after the HTML printing piece! I'm back on the main track now.

yarrow avatar Jan 03 '19 17:01 yarrow