Dav*_*vis 22 gvim ghostscript imagemagick syntax-highlighting
将各种语法高亮的源文件(C、SQL、Java、PHP、batch、bash)批量转换为高分辨率图像(600dpi),适用于电子书和印刷书籍。
到目前为止的一些尝试:
font标签。html2ps。几乎有效的解决方案是:
将语法突出显示的源代码转换为高分辨率(600dpi)图像的任何其他方法?
谢谢!
以下软件包可用于 Windows 和 Linux 系统,并且是完整、有效的解决方案所必需的:
以下是解决方案的工作原理:
FONT标签)。该脚本为包含长度都在 80 个字符以下的行的源文件生成宽度相同的图像。行超过 80 个字符的源文件会生成尽可能宽的图像以保留整行。
将组件安装到以下位置:
C:\Program Files\VimC:\Program Files\Vim\vim73\colorsC:\Program Files\wkhtmlC:\Program Files\ImageMagickC:\Program Files\GnuWin32注意:ImageMagick 有一个名为 的程序convert.exe,它不能取代 Windowsconvert命令。因此,convert.exe必须在批处理文件中硬编码的完整路径(而不是将 ImageMagick 添加到PATH)。
将 PATH环境变量设置为:
"C:\Program Files\Vim\vim73";"C:\Program Files\wkhtml";"C:\Program Files\GnuWin32\bin"
Run Code Online (Sandbox Code Playgroud)
使用以下命令运行它:
src2png.bat src2png.bat
Run Code Online (Sandbox Code Playgroud)
src2png.bat通过复制以下内容创建一个批处理文件:
@ECHO OFF
SET NUMBERS=-c "set number"
IF "%2" == "" SET NUMBERS=
ECHO Converting %1 to %1.html...
gvim -e %1 -c "set nobackup" %NUMBERS% -c ":colorscheme moria" ^
-c :TOhtml -c wq -c :q
REM Remove all background-color occurrences (without being self-referential)
sed -i "s/background-color: #......; \(.*\)}$/\1 }/g" %1.html
ECHO Converting %1.html to %1.png...
wkhtmltoimage --format png --transparent --minimum-font-size 80 ^
--quality 100 --width 3600 ^
%1.html %1.png
move %1.png %1.orig.png
REM If the text file has lines that exceed 80 characters, don't crop the
REM resulting image. (The book automatically shrinks large images to fit.)
REM The 3950 is the 80 point font at 80 characters with padding for line
REM numbers.
SET LENGTH=0
FOR /F %%l IN ('gawk ^
"BEGIN {x=0} {if( length($0)>x ) x=length()} END {print x;}" %1') ^
DO (
SET LENGTH=%%l
)
SET EXTENT=-extent 3950x
IF %LENGTH% GTR 80 SET EXTENT=
REM Trim the image height, then extend the width for 80 columns, if needed.
REM The result is that all images will be resized the same amount, thus
REM making the font size the same maximum for all source listings. Source
REM files beyond the 80 character limit will be scaled as necessary.
ECHO Trimming %1.png...
"C:\programs\ImageMagick\convert.exe" -format png %1.orig.png ^
-density 150x150 ^
-background none -antialias -trim +repage ^
%EXTENT% ^
-bordercolor none -border 25 ^
%1.png
ECHO Removing old files...
IF EXIST %1.orig.png DEL /q %1.orig.png
IF EXIST %1.html DEL /q %1.html
IF EXIST sed*. DEL /q sed*.
Run Code Online (Sandbox Code Playgroud)
欢迎改进和优化。
注意:最新版本的wkhtmltoimage正确处理覆盖背景颜色。因此,理论上不再需要删除背景颜色 CSS 的行。
| 归档时间: |
|
| 查看次数: |
6830 次 |
| 最近记录: |