Show HN: Generate high-res images of code samples with Chrome Headless
github.com
github.com
pygmentize -f png -l python -o test.png test.py
Spinning up a browser for it seems like an odd choice. At least the author acknowledges that.Just fwiw, apparently (i.e., I discovered in the last 10 mins) pygmentize detects file extensions, so
$ pygmentize -o test.png test.py
is equivalent.Here's an example of Pygments C++ output: http://imgur.com/a/7TbMb
And the same file through Prism.js + Puppeteer: https://github.com/mplewis/src2png/blob/master/docs/arduino....
Pygments does a great job of generating detailed PNGs. The default settings aren't quite what I'm looking for, but they look like they could be tweaked to increase the resolution and change the color scheme.
I'm embedding my examples in a Keynote deck, so I'm picky about font family, size, resolution, etc. I bet I could get similar results by writing a custom script for Pygments. Thanks for sharing!
pygmentize -f rtf -O "fontface=Fira Code,fontsize=26" -l prolog familyinteract.pl | pbcopy
(That was for pasting code into Pages; for Keynote you'll probably want a bigger font size.)What happened to the simple screenshot?
This seems to be the flow:
+----------+ +----------+ +-------------+
| | | | | |
| NodeJS +------->+ POI +--------->+ Puppeteer +----+
| | | | | | |
+----------+ +----------+ +-------------+ |
|
|
+----------+ +----------+ +-------------+ |
| | | | | | |
| NodeJS <--------+ POI <----------+ Puppeteer +<---+
| | | | | |
+----------+ +----------+ +-------------+
* drawn with http://asciiflow.com/In the future I'd like to simplify the workflow a lot, including cutting out the dev server.
Oh god, this is horrifying. You have built a monster and it is made of JavaScript.
Yes it is. Yes I have.In my case, I use an Alfred workflow that formats the code snippet and then I can just paste the formatted code into Keynote/Powerpoint. It supports several color themes and it's super fast https://github.com/importre/alfred-hl
1. Open your script/code in vim editor
2. Enable syntax & set the required color scheme
:syntax on :colorscheme darkblue
3. Print the file in PS file format
:hardcopy >/tmp/filename.ps
4. Convert the PS file to PDF format
$ ps2pdf /tmp/filename.ps
5. Now you can open filename.pdf
4. Using ghostscript: gs -sDEVICE=jpeg -dJPEGQ=100 -dNOPAUSE -dBATCH -dSAFER -r300 -sOutputFile=filename.jpg filename.ps
However, congrats on assembling your "Lovecraftian amalgamation of software." :)
If you're posting code on a website, you're right – you should prefer <pre><code> and prism.js or highlight.js :)
1. Write in emacs org-mode code blocks
2. Export to html (requires htmlize)
3. Copy and paste the html from safari to keynote
The Emacs theme determines the syntax highlighting. Org-mode controls the html export (font family, font size, line spacing), with optional line number or output from executions. The copy-pasting from safari to keynote preserves all the formatting.Thanks to this I've learnt about poi, yarn, the Fira Code font...
Also, ever try this technique? https://gist.github.com/jimbojsb/1630790
I've used it with success before.
I'll check out that app on brew, it looks like a cool approach. Thanks!
It seems like it should be pretty easy to print a PDF from Puppeteer though – they expose a #pdf method.
https://github.com/GoogleChrome/puppeteer/blob/master/docs/a...
This is something I find really frustrating in computing. We can do and are constantly doing such awesome and incredible things with computers. But at the same time transferring (copy-pasting) text from one application to another remains a challenge. And a reasonable solution for that is to bring in the 600lb gorilla that is a modern web browser to render fixed-width text into a bitmap so that it can be embedded into a presentation. It just feels so wrong, like the whole field is actually rotten inside under the shiny surface, while the state of the art blazes forwards thousand miles away from the real world. I, as a member of this community, feel utterly powerless to actually make the situation noticeably better, partly because of so much relies on interoperability, and partly because of the massive inertia that modern software carries.
I want out.
EDIT: Apparently, the problem is (or was in 2014) that Keynote doesn't support HTML and Chrome (Atom is Electron-based, right?) doesn't support RTF. Well worth a read: https://apple.stackexchange.com/a/124167
On Android all text formatting is compatible between apps nicely, and can easily be copied between apps.
I can easily do the same with Notepad++ and Powerpoint, just need to copy-paste in RTF.
One time setup
$ python -m venv ve
$ source ve/bin/activate
$ pip install pygments WeasyPrint
Then $ pygmentize -f html src.py | weasyprint - out.pdf
Both pygmentize and weasyprint have many options to play with. I find a set and create a shell script.This also does pngs, etc:
$ pygmentize -f html src.py | weasyprint - out.png $ pygmentize -o out.png src.py
Neat. :TOhtml
I found it to create the prettiest syntax highlighted code. But I'm bias because it produces html using my vim color scheme which I'm partial too :)You can script vim too by specifying a list of commands in a file then running it:
$ vim -S myscript some_fileBut for his use case ('presentation') there are tons of presentation libs/franeworks in HTML/JS/CSS which support code embeddings with syntax highlighting.
Same quality but much easier workflow since code stays editable.
http://tinyletter.com/mbutterick/letters/q-ligatures-in-prog...
* Insert in Text-Editor or IDE with Syntax highlighting * Take a screengrab
I mean it's still nice in terms of being able to script the generation if you have code that updates frequently and you want to keep the pictures current.