我建議尋找到一個語法高亮顯示。 Pygments能夠生成HTML,RTF等。如果您選擇輸出HTML文件,則可以使用像wkhtmltopdf這樣的工具將突出顯示語法的HTML文件轉換爲PDF文件。
或者,Pygments可以做LaTeX。如果您熟悉LaTeX,那麼您可以讓Pygments生成LaTeX輸出,並使用pdflatex
生成您的PDF文件。
你說你是在OS X上安裝Pygments來做,打開一個終端,輸入:
sudo easy_install Pygments
這將安裝一個程序pygmentize
,你可以用它來改變代碼。
接下來,安裝wkhtmltopdf。現在
,你可以把一個文件,語法高亮顯示,並將其轉換爲PDF:
pygmentize -l ruby -f html -O full,style=vim test.rb > test.html
wkhtmltopdf test.html test.pdf
在這裏,我將展示一個Ruby腳本的轉換。當然,如果你正在轉換Lua腳本,你會想使用-l lua
。
然後,您可以將這些命令合併到一個shell腳本中,以遞歸方式遍歷目錄,例如,
#!/bin/bash
# Change this to the repository directory
REPOSITORY=/path/to/the/repo
# Iterate over the repository
while read source_file
do
filename=$(basename $source_file)
dir=$(dirname $source_file)
# For each .lua file found, generate an HTML file in /tmp
pygmentize -l lua -f html -O full,style=vim $source_file > /tmp/${filename}.html
# Convert the HTML file to a PDF file in the same directory
# as the .lua file
wkhtmltopdf /tmp/${filename}.html ${dir}/${filename}.pdf
done < <(find $REPOSITORY -type f -iname '*.lua')
把它放在一個名爲的文件中。然後,鍵入:
chmod +x convert.sh
./convert.sh
來源
2015-10-31 18:12:13
ctc
您不能只是將某些輸出重定向到PDF文件,並期望它是有效的PDF。 PDF具有特定的格式。 – ctc
@ctc感謝您的迴應!你有什麼建議去做這件事?即使使用'.word'格式,也沒問題 –