bash 适用于 UNIX 系统的 less-style markdown 查看器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7599447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
less-style markdown viewer for UNIX systems
提问by Keith Thompson
I have a Markdown string in JavaScript, and I'd like to display it (with bolding, etc) in a less(or, I suppose, more)-style viewer for the command line.
我在 JavaScript 中有一个 Markdown 字符串less,我想more在命令行的(或者,我想,)样式的查看器中显示它(带有粗体等)。
For example, with a string
例如,使用字符串
"hello\n" +
"_____\n" +
"*world*!"
I would like to have output pop up with scrollable content that looks like
我想让输出弹出看起来像的可滚动内容
hello
world
你好
世界
Is this possible, and if so how?
这可能吗,如果可以,怎么办?
回答by Keith Thompson
Pandoccan convert Markdown to groff man pages.
Pandoc可以将 Markdown 转换为 groff 手册页。
This (thanks to nenopera's comment):
这(感谢 nenopera 的评论):
pandoc -s -f markdown -t man foo.md | man -l -
should do the trick. The -soption tells it to generate proper headers and footers.
应该做的伎俩。该-s选项告诉它生成正确的页眉和页脚。
There may be other markdown-to-*roff converters out there; Pandoc just happens to be the first one I found.
可能还有其他 markdown-to-*roff 转换器;Pandoc 恰好是我找到的第一个。
Another alternative is the markdowncommand (apt-get install markdownon Debian systems), which converts Markdown to HTML. For example:
另一种选择是markdown命令(apt-get install markdown在 Debian 系统上),它将 Markdown 转换为 HTML。例如:
markdown README.md | lynx -stdin
(assuming you have the lynxterminal-based web browser).
(假设您有lynx基于终端的网络浏览器)。
Or (thanks to Danny's suggestion) you can do something like this:
或者(感谢 Danny 的建议)您可以执行以下操作:
markdown README.md > README.html && xdg-open README.html
where xdg-open(on some systems) opens the specified file or URL in the preferred application. This will probably open README.htmlin your preferred GUI web browser (which isn't exactly "less-style", but it might be useful).
其中xdg-open(在某些系统上)在首选应用程序中打开指定的文件或 URL。这可能会README.html在您首选的 GUI Web 浏览器中打开(这并不完全是“风格较少”,但它可能很有用)。
回答by Joel Cross
I tried to write this in a comment above, but I couldn't format my code block correctly. To write a 'less filter', try, for example, saving the following as ~/.lessfilter:
我试图在上面的评论中写下这个,但我无法正确格式化我的代码块。例如,要编写“较少过滤器”,请尝试将以下内容另存为~/.lessfilter:
#!/bin/sh
case "" in
*.md)
extension-handler ""
pandoc -s -f markdown -t man ""|groff -T utf8 -man -
;;
*)
# We don't handle this format.
exit 1
esac
# No further processing by lesspipe necessary
exit 0
Then, you can type less FILENAME.mdand it will be formatted like a manpage.
然后,您可以键入less FILENAME.md,它的格式将类似于联机帮助页。
回答by Red Pill
If you are into colorsthen maybe this is worth checking as well:
如果您喜欢颜色,那么也许这也值得检查:


It can be used straightforward also from within other programs, or python modules.
它也可以在其他程序或 python 模块中直接使用。
And it has a lotof styles, like over 200 for markdown and code which can be combined.
它有很多样式,比如超过 200 种 Markdown 和可以组合的代码。


Disclaimer
免责声明
It is pretty alpha there may be still bugs
I'm the author of it, maybe some people like it ;-)
这是相当 alpha 可能仍然存在错误
我是它的作者,也许有些人喜欢它;-)
回答by Alban
回答by mpen
I wrote a couple functions based on Keith's answer:
我根据基思的回答写了几个函数:
mdt() {
markdown "$*" | lynx -stdin
}
mdb() {
local TMPFILE=$(mktemp)
markdown "$*" > $TMPFILE && ( xdg-open $TMPFILE > /dev/null 2>&1 & )
}
If you're using zsh, just place those two functions in ~/.zshrcand then call them from your terminal like
如果您正在使用zsh,只需将这两个函数放入~/.zshrc,然后从您的终端调用它们,例如
mdt README.md
mdb README.md
"t" is for "terminal", "b" is for browser.
“t”代表“终端”,“b”代表浏览器。
回答by lfender6445
Using OSX I prefer to use this command
使用 OSX 我更喜欢使用这个命令
brew install pandoc
pandoc -s -f markdown -t man README.md | groff -T utf8 -man | less
Convert markupm, format document with groff, and pipe into less
将 markupm、用 groff 格式化文档和管道转换为 less
credit: http://blog.metamatt.com/blog/2013/01/09/previewing-markdown-files-from-the-terminal/
信用:http: //blog.metamatt.com/blog/2013/01/09/previewing-markdown-files-from-the-terminal/
回答by Dr Beco
This is an alias that encapsulates a function:
这是封装了一个函数的别名:
alias mdless='_mdless() { if [ -n "" ] ; then if [ -f "" ] ; then cat <(echo ".TH 7 `date --iso-8601` Dr.Beco Markdown") <(pandoc -t man ) | groff -K utf8 -t -T utf8 -man 2>/dev/null | less ; fi ; fi ;}; _mdless '
Explanation
解释
alias mdless='...': creates an alias formdless_mdless() {...};: creates a temporary function to be called afterwards_mdless: at the end, call it (the function above)
alias mdless='...': 创建别名mdless_mdless() {...};: 创建一个临时函数以供之后调用_mdless: 最后,调用它(上面的函数)
Inside the function:
函数内部:
if [ -n "$1" ] ; then: if the first argument is not null then...if [ -f "$1" ] ; then: also, if the file exists and is regular then...cat arg1 arg2 | groff... : cat sends this two arguments concatenated to groff; the arguments being:- arg1:
<(echo ".TH $1 7date --iso-8601Dr.Beco Markdown"): something that starts the file andgroffwill understand as the header and footer notes. This substitutes the empty header from-skey onpandoc. - arg2:
<(pandoc -t man $1): the file itself, filtered bypandoc, outputing themanstyle of file$1
- arg1:
| groff -K utf8 -t -T utf8 -man 2>/dev/null: piping the resulting concatenated file togroff:-K utf8sogroffunderstands the input file code-tso it displays correctly tables in the file-T utf8so it output in the correct format-manso it uses the MACROpackage to outputs the file inmanformat2>/dev/nullto ignore errors (after all, its a raw file being transformed in man by hand, we don't care the errors as long as we can see the file in a not-so-much-ugly format).
| less: finally, shows the file paginating it withless(I've tried to avoid this pipe by usinggrofferinstead ofgroff, butgrofferis not as robust aslessand some files hangs it or do not show at all. So, let it go through one more pipe, what the heck!
if [ -n "$1" ] ; then: 如果第一个参数不为空,那么...if [ -f "$1" ] ; then: 另外,如果文件存在并且是常规文件,那么...cat arg1 arg2 | groff... : cat 发送这两个连接到 groff 的参数;论据是:- arg1:
<(echo ".TH $1 7date --iso-8601Dr.Beco Markdown"): 启动文件并groff理解为页眉和页脚注释的内容。这将替换-skey on 中的空标头pandoc。 - arg2::
<(pandoc -t man $1): 文件本身,通过 过滤pandoc,输出man文件的样式$1
- arg1:
| groff -K utf8 -t -T utf8 -man 2>/dev/null:将生成的连接文件通过管道传输到groff:-K utf8所以groff理解输入文件代码-t所以它在文件中正确显示表格-T utf8所以它以正确的格式输出-man所以它使用MACRO包以man格式输出文件2>/dev/null忽略错误(毕竟,它是手动转换的原始文件,只要我们能以不太难看的格式查看文件,我们就不关心错误)。
| less: 最后,显示文件对其进行分页less(我试图通过使用groffer代替来避免使用此管道groff,但groffer不如less某些文件挂起它或根本不显示那么健壮。因此,让它再通过一个管道, 有没有搞错!
Add it to your ~/.bash_aliases(or alike)
将其添加到您的~/.bash_aliases(或类似的)
回答by orzechow
I'll post my unix page answerhere, too:
我也会在这里发布我的 unix 页面答案:
An IMHO heavily underestimated command line markdown viewer is the markdown-cli.
恕我直言,被严重低估的命令行降价查看器是markdown-cli。
Installation
安装
npm install markdown-cli --global
Usage
用法
markdown-cli <file>
Features
特征
Probably not noticed much, because it misses any documentation...
But as far as I could figure out by some example markdown files, some things that convinced me:
可能没有注意到太多,因为它错过了任何文档......
但据我所知,通过一些示例降价文件,一些让我信服的事情:
- handles ill formatted files much better (similarly to atom, github, etc.; eg. when blank lines are missing before lists)
- more stable with formatting in headers or lists (bold text in lists breaks sublists in some other viewers)
- proper table formatting
- syntax highlightning
- resolves footnote links to show the link instead of the footnote number (not everyone might want this)
- 更好地处理格式错误的文件(类似于 atom、github 等;例如,当列表前缺少空行时)
- 在标题或列表中格式化更稳定(列表中的粗体文本会破坏其他一些查看器中的子列表)
- 正确的表格格式
- 语法高亮
- 解析脚注链接以显示链接而不是脚注编号(不是每个人都想要这个)
Screenshot
截屏


Drawbacks
缺点
I have realized the following issues
我已经意识到以下问题
- code blocks are flattened (all leading spaces disappear)
- two blank lines appear before lists
- 代码块被展平(所有前导空格消失)
- 两个空行出现在列表之前
回答by sammko
I personally use this script:
我个人使用这个脚本:
#!/bin/bash
id=$(uuidgen | cut -c -8)
markdown > /tmp/md-$id
google-chrome --app=file:///tmp/md-$id
It renders the markdown into HTML, puts it into a file in /tmp/md-...and opens that in a kiosk chrome session with no URI bar etc.. You just pass the md file as an argument or pipe it into stdin. Requires markdown and Google Chrome. Chromium should also work but you need to replace the last line with
它将 Markdown 呈现为 HTML,将其放入一个文件中/tmp/md-...,然后在没有 URI 栏等的 kiosk chrome 会话中打开该文件。您只需将 md 文件作为参数传递或通过管道将其输入标准输入。需要降价和谷歌浏览器。Chromium 也应该可以工作,但您需要将最后一行替换为
chromium-browser --app=file:///tmp/md-$id
If you wanna get fancy about it, you can use some css to make it look nice, I edited the script and made it use Bootstrap3 (overkill) from a CDN.
如果你想喜欢它,你可以使用一些 css 让它看起来不错,我编辑了脚本并让它使用来自 CDN 的 Bootstrap3(overkill)。
#!/bin/bash
id=$(uuidgen | cut -c -8)
markdown > /tmp/md-$id
sed -i "1i <html><head><style>body{padding:24px;}</style><link rel=\"stylesheet\" type=\"text/css\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css\"></head><body>" /tmp/md-$id
echo "</body>" >> /tmp/md-$id
google-chrome --app=file:///tmp/md-$id > /dev/null 2>&1 &

