将标头添加到文件而不更改文件
背景
enscript
命令可以将语法突出显示应用于各种类型的源代码文件,包括 SQL 语句、shell 脚本、PHP 代码、HTML 文件等。我正在使用 enscript
为技术手册生成 300dpi 的源代码图像,以便:
- 根据实际源代码生成书籍内容。
- 源代码与本书一起分发,无需任何修改。
- 在编写本书时运行并测试脚本。
问题
以下 shell 脚本几乎按照需要执行转换:
#!/bin/bash
DIRNAME=$(dirname $1)
FILENAME=$(basename $1)
# Remove the extension from the filename.
BASENAME=${FILENAME%%.*}
FILETYPE=${FILENAME##*.}
LIGHTGRAY="#f3f3f3"
enscript --escapes --color -f Courier10 -X ps -B -1 --highlight=$FILETYPE \
$2 -h -o - $1 | \
gs -dSAFER -sDEVICE=pngalpha -dGraphicsAlphaBits=4 -dNOPAUSE -r300 \
-sOutputFile=$BASENAME.png -dBackgroundColor=16$LIGHTGRAY > /dev/null && \
convert -trim $BASENAME.png $BASENAME-trimmed.png && \
mv $BASENAME-trimmed.png $BASENAME.png
问题是背景不是浅灰色。根据 enscript
手册页,--escapes
(-e
) 选项指示文件(即 $1
code>) 中嵌入了 enscript
特定的控制序列。
添加控制序列意味着必须复制代码,这违背了单一源的目的。
解决
方案 enscript 文档暗示应该可以在运行脚本之前将两个文件连接在一起(目标和“标头”),以创建第三个文件:
^@shade{0.85} -- header line
#!/bin/bash -- start of source file
然后在执行命令后删除第三个文件完成。
问题
Q.1. 在不使用第三个文件的情况下,将控制序列和源文件通过管道传送到 enscript
程序的更有效方法是什么? ?
Q.2. 还有哪些其他选项可用于自动突出显示书籍的语法,同时满足我所描述的单一源要求? (例如,用 LyX 写书并使用 LaTeX 命令进行导入和语法突出显示。)
Background
The enscript
command can apply syntax highlighting to various types of source files, including SQL statements, shell scripts, PHP code, HTML files, and more. I am using enscript
to generate 300dpi images of source code for a technical manual to:
- Generate content for the book based on actual source code.
- Distribute the source code along with the book, without any modification.
- Run and test the scripts while writing the book.
Problem
The following shell script performs the conversion almost as desired:
#!/bin/bash
DIRNAME=$(dirname $1)
FILENAME=$(basename $1)
# Remove the extension from the filename.
BASENAME=${FILENAME%%.*}
FILETYPE=${FILENAME##*.}
LIGHTGRAY="#f3f3f3"
enscript --escapes --color -f Courier10 -X ps -B -1 --highlight=$FILETYPE \
$2 -h -o - $1 | \
gs -dSAFER -sDEVICE=pngalpha -dGraphicsAlphaBits=4 -dNOPAUSE -r300 \
-sOutputFile=$BASENAME.png -dBackgroundColor=16$LIGHTGRAY > /dev/null && \
convert -trim $BASENAME.png $BASENAME-trimmed.png && \
mv $BASENAME-trimmed.png $BASENAME.png
The problem is that the background is not a light gray colour. According to the enscript
man page, the --escapes
(-e
) option indicates that the file (i.e., $1
) has enscript
-specific control sequences embedded within it.
Adding the control sequences means having to duplicate code, which defeats the purpose of having a single source.
Solution
The enscript
documentation implies that it should be possible to concatenate two files together (the target and a "header") before running the script, to create a third file:
^@shade{0.85} -- header line
#!/bin/bash -- start of source file
Then delete the third file once the command completes.
Questions
Q.1. What is a more efficient way to pipe the control sequences and the source file to the enscript
program without using a third file?
Q.2. What other options are available to automate syntax highlighting for a book, while honouring the single source requirements I have described? (For example, write the book in LyX and use LaTeX commands for import and syntax highlighting.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Q1 您可以使用大括号“
{}
”进行 I/O 重定向:这假设 enscript 在未给出显式文件名的情况下读取其标准输入;如果没有,您可能需要使用一个选项(可能是“
-i -
”)或一些更严重的魔法,甚至可能是 bash 中的“进程替换”。您还可以使用括号来运行子 shell:
请注意,
cat
之后的分号对于大括号来说是必需的,而对于括号则不需要(并且在左大括号之后需要一个空格) - 例如shell 脚本的奥秘。Q2 我没有任何替代方案可以提供。当我制作一本书时(20 年前,使用 troff),我编写了一个程序将源代码转换为必要的标记,以便这本书是从源代码生成的,但通过自动化过程。
(300 dpi 的分辨率足够高吗?)
编辑
要解决解释转换脚本本身中嵌入的转义序列的
enscript
程序:Q1 You can use braces '
{}
' to do I/O redirection:This assumes that enscript reads its standard input when not given an explicit file name; if not, you may need to use an option (perhaps '
-i -
') or some more serious magic, possibly even 'process substitution' in bash.You could also use parentheses to run a sub-shell:
Note that the semi-colon after
cat
is necessary with braces and not necessary with parentheses (and a space is necessary after the open brace) - such are the mysteries of shell scripting.Q2 I don't have any alternatives to offer. When I produced a book (20 years ago now, using troff), I wrote a program to convert source into the the necessary markup, so that the book was produced from the source code, but by an automated process.
(Is 300 dpi sufficiently high resolution?)
Edit
To work-around the
enscript
program interpreting the escape sequence embedded in the conversion script itself:Q2:将 LaTeX 与列表 包。
Q2: Use LaTeX with the listings package.