什么是潘多克?
Pandoc 是一个开源命令行实用程序,用作格式转换器,在标记语言之间更改文件。 它由 John MacFarlane 于 2006 年创建,并用 Haskell 编写。 该工具与 Windows、CentOS 和大多数类 Unix 系统兼容。
标记语言是一种注释系统,用于以视觉上独特的方式格式化文本。 简而言之,标记语言对于使 Internet 变得漂亮至关重要。
以下是标记语言的一些示例:
- HTML
- XML
- Markdown(被认为是轻量级标记)
在处理使用不同格式的多个文件时,一个很棒的工具是 Pandoc。
Pandoc 的目标是只转换任何给定文档的标记而不修改其源内容。 本文将概述如何在 CentOS 7(也适用于 Red Hat Enterprise Linux)上安装 Pandoc,以及一些基本的使用示例。
如何安装 Pandoc
要求
在开始安装过程之前,成功的过程需要满足一些要求。
- SSH 访问。
- 根权限。
- Enterprise Linux (EPEL) 存储库的额外软件包。
步骤 1. 准备安装 Pandoc
验证本地软件包是否已更新。
LiquidWeb_Centos # sudo yum update
No packages marked for update
如果缺少 EPEL,请安装它。
LiquidWeb_Centos # yum install epel-release
...
Installed:
epel-release.noarch 0:7-11
Complete!
验证是否正确添加了存储库。
LiquidWeb_Centos # yum repolist
...
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64
步骤 2. 安装 Pandoc 包
安装 Pandoc 包时,需要几个依赖项才能使其正常工作。 接受安装以完成该过程。
LiquidWeb_Centos # yum install pandoc
完成后,它会产生类似于下图的输出。
步骤 3. 验证 Pandoc 的版本
使用以下命令检查 Pandoc 的版本。
LiquidWeb_Centos # pandoc -v
pandoc 1.12.3.1
Compiled with texmath 0.6.6, highlighting-kate 0.5.6.
...
Copyright (C) 2006-2013 John MacFarlane
This is free software; see the source for copying conditions. There is no
warranty, not even for merchantability or fitness for a particular purpose.
使用 Cabal 安装 Pandoc
或者,我们可以使用 阴谋,Haskell 库和程序的包管理器,用于安装 Pandoc。
LiquidWeb_Centos # cabal install pandoc
基本用法
Pandoc 的使用比较简单,有一个标准的格式。 我们可以使用 –help 标签或查看手册页以更深入地了解所有支持的格式,但我们通常会使用以下语法。
pandoc -s -f INPUT_FORMAT -t OUTPUT_FORMAT FILE_TO_CONVERT -o OUTPUT_FILE
- -s(独立):根据输出格式生成带有必要元数据的输出。
- -f (from):指定输入格式。
- -t (to):指定输出格式。
- -o(输出):指定一个包含转换结果的新文件。
默认情况下,Pandoc 将转换后的文件发送到标准输出。 因此,如果没有指定输出文件,它将在终端上打印转换结果。
为了 example,假设我们要将文件 test.html 转换为纯文本。 在这种情况下,它会将文档转换为 .txt 文件。 这是终端中显示的文件内容。
LiquidWeb_Centos # cat test.html
<!DOCTYPE html>
<html>
<body>
<h1>A random title</h1>
<p>Some random text!</p>
</body>
</html>
要转换文档,请使用以下命令。 在这里,我们指定输入格式为 HTML,输出纯文本。
LiquidWeb_Centos # pandoc -s -f html -t plain test.html -o test.txt
输出重定向到一个名为 test.txt 的新文件。
LiquidWeb_Centos # cat test.txt
A random title
==============
Some random text!
此外,可以使用外部源代替本地文件。 为了 example要将网站的索引转换为 markdown 并将输出打印到终端(省略 -o 标记),请使用以下语法。
LiquidWeb_Centos # pandoc -s -f html -t markdown https://pandoc.domain.com
Fetching https://pandoc.domain.com...
This is a test
Just testing some things out!
=============================
Thank you!
— The Team
另一个令人兴奋的功能是 Pandoc 在丢失时会尝试从文件扩展名中猜测输入格式。 因此对于 example,这是将 LaTeX 文档转换为 AsciiDoc 的样子。
LiquidWeb_Centos # cat document.tex
documentclass[14pt]{article}
usepackage{lingmacros}
usepackage{tree-dvips}
begin{document}
section*{Various notes}
Academic topic:
subsection*{How to write a paper}
Here it begins (ex{1}).
{small
enumsentence{Structure of A$'$ Projections: [2ex]
begin{tabular}[t]{cccc}
& node{i}{CP} [2ex]
node{ii}{Spec} & &node{iii}{C$'$} [2ex]
&node{iv}{C} & & node{v}{SAgrP}
end{tabular}
nodeconnect{i}{ii}
nodeconnect{i}{iii}
nodeconnect{iii}{iv}
nodeconnect{iii}{v}
}
}
subsection*{Another topic}
Proin enim sem, varius sed finibus sit amet, porttitor eu neque. Curabitur hendrerit rhoncus nibh, eget varius purus finibus sit amet.
end{document}
现在,转换此文档并检查新生成的文件的内容。
LiquidWeb_Centos # pandoc -s -t asciidoc document.tex -o document.txt
LiquidWeb_Centos # cat document.txt
[[various-notes]]
Various notes
-------------
Academic topic:
[[how-to-write-a-paper]]
How to write a paper
~~~~~~~~~~~~~~~~~~~~
Here it begins ().
[[another-topic]]
Another topic
~~~~~~~~~~~~~
Proin enim sem, varius sed finibus sit amet, porttitor eu neque.
Curabitur hendrerit rhoncus nibh, eget varius purus finibus sit amet.
如您所见,Pandoc 是一个强大的工具,可以将标记转换为纯文本,而不会影响文档。
结论
Pandoc 的灵活性是巨大的,与不使用此工具相比,应用程序的范围会有所不同。 本文的范围是涵盖如何安装 Pandoc 的基础知识和过程。 尽管如此,还有更高级的选项,例如为自定义格式添加插件,或支持电子书和 PDF 文档,打开一个全新的用例世界。
如需一年中任何一天的 24 小时帮助,请联系 Liquid Web 最有帮助的托管人之一。 我们是来帮忙的!
