Skip to content

Instantly share code, notes, and snippets.

@umaar
Forked from skmetz/pygmentize_code.sh
Created February 5, 2023 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umaar/9c14d225fd477fc63690fa875c9c6865 to your computer and use it in GitHub Desktop.
Save umaar/9c14d225fd477fc63690fa875c9c6865 to your computer and use it in GitHub Desktop.
Script to create syntax highlighted RTF files
#!/bin/bash
# I edited the ruby lexer (line 637 in the following file...
# [skm@skmair2:/Library/Python/2.7/site-packages/pygments/lexers/agile.py
# to remove spacial handling of the word 'name'
# so I could use it as a variable/method name.
# This script come from Katrina, with these instructions:
# I've also attached the presentation.py file which is where the style
# is defined. The colors are normal hex values, so if you know how you
# want your code, you can tweak this yourself.
# I had to stick this in
# /Library/Python/2.7/site-packages/Pygments-1.6rc1-py2.7.egg/pygments/styles/
# for pygments to know where to find it, you might have different
# versions, of course.
for file in `ls -1 . | grep .rb`
do
outfile="colorized/`basename $file .rb`.rtf"
style="style=default"
font="fontface=Consolas"
lexer="ruby"
format="rtf"
fontsize="48" # twice the actual size, so 48 = 24
blanklinesize="24" # ditto
# sed command to insert overall fontsize into rtf file
sed1="sed s/\\\\f0/\\\\f0\\\\fs$fontsize/g"
# sed command to change the fontsize for a blank line.
# blank lines contain only ^\par$, so this changes
# \par (where $fontsize=48 and $blanklinesize=24) to
# \fo\fs24\par\f0\fs48
sed2="sed s/^\\\\par$/\\\\f0\\\\fs$blanklinesize\\\\par\\\\f0\\\\fs$fontsize/g"
pygmentize -l $lexer -P $style -P "$font" -f $format $file | $sed1 | $sed2 > $outfile
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment