将源代码转换为语法突出显示的图像

发布于 2024-09-27 16:58:50 字数 935 浏览 9 评论 0原文

背景

OpenOffice Writer 缺乏链接到 ASCII 文本源文件、应用语法突出显示、将其包装在框架中以及在源文件发生更改时更新框架内容的能力。但是,OpenOffice Writer 可以链接到图像,并且当图像发生变化时会自动更新图像。

问题

图像需要高分辨率(300 dpi 或更高),并具有适合白色背景(即打印页面)的语法着色。

问题

如何从源代码文件自动创建高质量的图像,如:

  • SQL;
  • PostgreSQL 函数;
  • 爪哇;
  • bash 脚本; R和
  • PL/R?

尝试

大多数尝试都是以下主题的变体:

$ enscript --color -f Courier12 -B -1 --highlight=sql -h -o - source.sql |\
  convert - -trim -border 10 source.png

这种方法存在一些问题:

  1. 缺乏分辨率(使用 -resample-密度 没有提供任何改进) 。
  2. 语法突出显示不适合白页(可能会更改 enscript 的颜色主题)。
  3. 使用 Courier100 会生成多个 .png 文件,需要将这些文件拼接在一起。
  4. -border 10 意外地将背景颜色从白色更改为浅灰色。

手动解决方案

将源文件转换为 PostScript(完全避免 ImageMagick),然后将它们导入 GIMP 将产生所需的结果。不幸的是,该解决方案涉及一些手动工作,而我的 GIMP 批处理编程经验几乎为零。

Background

OpenOffice Writer lacks the ability to link to an ASCII text source file, apply syntax highlighting, wrap it in a frame, and update the frame contents whenever the source file changes. However, OpenOffice Writer can link to images, and will update the images automatically when they change.

Problem

The images need to be high-resolution (300 dpi or greater) with syntax colouring appropriate for a white background (i.e., a printed page).

Question

How can high-quality images be created automatically from source code files, such as:

  • SQL;
  • PostgreSQL functions;
  • Java;
  • bash scripts; and
  • R and PL/R?

Attempts

Most attempts have been a variation on the following theme:

$ enscript --color -f Courier12 -B -1 --highlight=sql -h -o - source.sql |\
  convert - -trim -border 10 source.png

There are a few problems with this approach:

  1. The resolution is lacking (using -resample and -density offer no improvement).
  2. The syntax highlighting is unsuitable for a white page (can probably change enscript's colour theme).
  3. Using Courier100 produces several .png files, which would need to be stitched together.
  4. The -border 10 unexpectedly changes the background colour from white to lightgray.

Manual Solution

Converting the source files to PostScript -- avoiding ImageMagick altogether -- and then importing them into The GIMP will produce the desired results. Unfortunately, that solution involves a bit of manual work, and my GIMP batch programming experience is next to nil.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云裳 2024-10-04 16:58:51

另一种选择是使用 catageCodeSnap, PolaCodecarbon-now-cli

CodeSnap 和 PolaCode 是 Visual Studio Code 扩展,从命令行使用它们可能很难,但使用起来很简单。将它们安装到编辑器中时请按照说明进行操作。此解决方案适用于 Visual Studio Code 支持的所有操作系统。

如果您使用基于 Debian 的 Linux 发行版,并且安装了 Docker,则可以使用以下 shell 脚本创建 catage Docker 映像:

#!/bin/bash
sudo docker build -t catage:local - <<EOF
FROM buildkite/puppeteer
USER node
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV PATH=$PATH:/home/node/.npm-global/bin
RUN mkdir /home/node/.npm-global
WORKDIR /home/node/app
RUN npm install -g catage
EOF

以下脚本创建 Carbon-now-cli Docker 映像:

#!/bin/bash
sudo docker build -t carbon-now:local - <<EOF
FROM alekzonder/puppeteer
USER root
RUN apt-get update
RUN apt-get install git -yq
RUN yarn global add carbon-now-cli
USER pptruser
EOF

同样,可以使用 这些 Docker 图像在 shell 脚本中如下所示,以从代码片段生成 PNG 图像:

#!/bin/bash
alias catage="sudo -E docker run --rm -it -v $PWD:/home/node/app catage:local catage"
alias carbon-now="sudo -E docker run --rm -it --init --cap-add=SYS_ADMIN --shm-size=1gb --user pptruser -v $PWD:/home/pptruser/app --workdir /home/pptruser/app carbon-now:local carbon-now"
catage --language java --no-line-numbers --theme Material --frame-title "My Code Snippet" --format png "my_code_snippet.java" "my_code_snippet.png" 
carbon-now "my_code_snippet.java" --target "my_code_snippet" --headless

Docker 方法适用于 Docker 支持的所有操作系统。您可能需要根据操作系统的功能修改上述 shell 脚本示例,但基本命令保持不变。

Another option is to make use of catage, CodeSnap, PolaCode or carbon-now-cli.

CodeSnap and PolaCode are Visual Studio Code extensions, and it may be difficult to use them from the command line, but they are simple to use. Follow the instructions when installing them into your editor. This solution works for all operating systems supported by Visual Studio Code.

If you are using a Debian-based Linux distribution, and you have Docker installed, then you can create a catage Docker image using the following shell script:

#!/bin/bash
sudo docker build -t catage:local - <<EOF
FROM buildkite/puppeteer
USER node
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV PATH=$PATH:/home/node/.npm-global/bin
RUN mkdir /home/node/.npm-global
WORKDIR /home/node/app
RUN npm install -g catage
EOF

Similarly, a carbon-now-cli Docker image can be created with the following script:

#!/bin/bash
sudo docker build -t carbon-now:local - <<EOF
FROM alekzonder/puppeteer
USER root
RUN apt-get update
RUN apt-get install git -yq
RUN yarn global add carbon-now-cli
USER pptruser
EOF

Use these Docker images in a shell script as follows, to generate PNG images from your code snippets:

#!/bin/bash
alias catage="sudo -E docker run --rm -it -v $PWD:/home/node/app catage:local catage"
alias carbon-now="sudo -E docker run --rm -it --init --cap-add=SYS_ADMIN --shm-size=1gb --user pptruser -v $PWD:/home/pptruser/app --workdir /home/pptruser/app carbon-now:local carbon-now"
catage --language java --no-line-numbers --theme Material --frame-title "My Code Snippet" --format png "my_code_snippet.java" "my_code_snippet.png" 
carbon-now "my_code_snippet.java" --target "my_code_snippet" --headless

The Docker approach works for all operating systems supported by Docker. You may have to modify the above shell script examples according to the capabilities of your operating system, but the basic commands remain the same.

夜还是长夜 2024-10-04 16:58:50

软件要求

以下软件包可用于 Windows 和 Linux 系统,并且是完整、有效的解决方案所必需的:

  • gvim - 用于将语法突出显示的源代码导出为 HTML。
  • moria - 语法突出显示的颜色方案。
  • wkhtmltopdf - 将 HTML 文档转换为 PDF 或 PostScript (PS) 文档。
  • Ghostscript - 用于将 PS 转换为 PNG。
  • ImageMagick - 用于修剪 PNG 并添加边框。

一般步骤

该解决方案的工作原理如下:

  1. 将源代码加载到可以添加颜色的编辑器中。
  2. 将源代码导出为 HTML 文档(带有嵌入的 FONT 标记)。
  3. 将 HTML 文档转换为 PS 文件。
  4. 将 PS 文件转换为 PNG 文件。
  5. 修剪 PNG 的白色边框和过于热心的源代码边框。
  6. 使用与 HTML 文档相同的背景颜色在图像周围添加边框。
  7. 删除临时文件。

安装

将组件安装到以下位置:

  • gvim - C:\Program Files\Vim
  • moria - C:\Program Files\ Vim\vim73\colors
  • wkhtmltopdf - C:\Program Files\wkhtml
  • Ghostscript - C:\Program Files\ gs
  • ImageMagick - C:\Program Files\ImageMagick

注意:ImageMagick 有一个名为 convert.exe 的程序,该程序不能取代Windows convert 命令。因此,convert.exe 的完整路径必须在批处理文件中进行硬编码(而不是将 ImageMagick 添加到PATH)。

环境变量

添加或更新以下内容 环境变量

  • GS_LIB = C:\Program Files\gs\gs9.00\lib
  • GS_PROG = C:\Program Files\gs\gs9.00\bin\gswin32.exe
  • PATH = "C:\Program Files\Vim\vim73";"C:\ Program Files\wkhtml";"C:\Program Files\gs\gs9.00\bin"

批处理文件

以下是批处理源文本:

@ECHO OFF

ECHO Converting %1 to %1.html ...

gvim -e %1 -c "set nobackup" -c ":colorscheme moria" -c :TOhtml -c wq -c :q

ECHO Converting %1.html to %1.ps ...

wkhtmltopdf --quiet --dpi 1200 %1.html %1.ps

ECHO Converting %1.pdf to %1.png ...
IF EXIST %1.png DEL /q %1.png

gswin32 -q -dBATCH -dNOPAUSE -dSAFER -dNOPROMPT ^
 -sDEVICE=png16m -dDEVICEXRESOLUTION=600 -dDEVICEYRESOLUTION=600 ^
 -dDEVICEWIDTH=4958 -dDEVICEHEIGHT=7017 -dNOPLATFONTS ^
 -dTextAlphaBits=4 -sOutputFile=%1.png %1.ps

ECHO Trimming %1.png ...

move %1.png %1.orig.png

"C:\Program Files\ImageMagick\convert.exe" -trim +repage -trim +repage ^
  -bordercolor "#f0f0f0" -border 25x25 %1.orig.png %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 %1.ps DEL /q %1.ps

Software Requirements

The following software packages are available for both Windows and Linux systems, and are required for a complete, working solution:

  • gvim - Used to export syntax highlighted source code to HTML.
  • moria - Colour scheme for syntax highlighting.
  • wkhtmltopdf - Converts HTML documents to PDF or PostScript (PS) documents.
  • Ghostscript - Used to convert PS to PNG.
  • ImageMagick - Used to trim the PNG and add a border.

General Steps

Here is how the solution works:

  1. Load the source code into an editor that can add splashes of colour.
  2. Export the source code as an HTML document (with embedded FONT tags).
  3. Convert the HTML document to a PS file.
  4. Convert the PS file to a PNG file.
  5. Trim the white border of the PNG and the overzealous source code border.
  6. Add a border around the image, using the same background colour as the HTML document.
  7. Delete temporary files.

Installation

Install the components into the following locations:

  • gvim - C:\Program Files\Vim
  • moria - C:\Program Files\Vim\vim73\colors
  • wkhtmltopdf - C:\Program Files\wkhtml
  • Ghostscript - C:\Program Files\gs
  • ImageMagick - C:\Program Files\ImageMagick

Note: ImageMagick has a program called convert.exe, which cannot supersede the Windows convert command. Because of this, the full path to convert.exe must be hard-coded in the batch file (as opposed to adding ImageMagick to the PATH).

Environment Variables

Add or update the following environment variables:

  • GS_LIB = C:\Program Files\gs\gs9.00\lib
  • GS_PROG = C:\Program Files\gs\gs9.00\bin\gswin32.exe
  • PATH = "C:\Program Files\Vim\vim73";"C:\Program Files\wkhtml";"C:\Program Files\gs\gs9.00\bin"

Batch File

Here is the batch source text:

@ECHO OFF

ECHO Converting %1 to %1.html ...

gvim -e %1 -c "set nobackup" -c ":colorscheme moria" -c :TOhtml -c wq -c :q

ECHO Converting %1.html to %1.ps ...

wkhtmltopdf --quiet --dpi 1200 %1.html %1.ps

ECHO Converting %1.pdf to %1.png ...
IF EXIST %1.png DEL /q %1.png

gswin32 -q -dBATCH -dNOPAUSE -dSAFER -dNOPROMPT ^
 -sDEVICE=png16m -dDEVICEXRESOLUTION=600 -dDEVICEYRESOLUTION=600 ^
 -dDEVICEWIDTH=4958 -dDEVICEHEIGHT=7017 -dNOPLATFONTS ^
 -dTextAlphaBits=4 -sOutputFile=%1.png %1.ps

ECHO Trimming %1.png ...

move %1.png %1.orig.png

"C:\Program Files\ImageMagick\convert.exe" -trim +repage -trim +repage ^
  -bordercolor "#f0f0f0" -border 25x25 %1.orig.png %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 %1.ps DEL /q %1.ps
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文