如何配置'less'来显示格式化的markdown文件?

时间:2021-10-29 09:17:08

I would like to have less display *.md markdown files with some formatting -- like I know less can, for manpages, etc. I am running Ubuntu 12.04.

我希望显示更少的显示* .md降价文件与一些格式 - 比如我知道更少,可以用于联机帮助页等。我正在运行Ubuntu 12.04。

I am as far as putting a user defined filter into .lessfilter:

我将用户定义的过滤器放入.lessfilter中:

#!/bin/sh
case "$1" in
  *.md)
    fn=/tmp/$1.$$.html
    markdown "$1" | html2txt > $fn  ### LOSES FORMATTING
    cat $fn                         ### TO STDOUT???
    ;;
  *)
  # We don't handle this format
  exit 1
esac
# No further processing by lesspipe necessary
exit 0

So, the main questions are:

所以,主要问题是:

  • How can I pass some basic formatting information to less as well, instead of losing it with html2txt
  • 如何将一些基本格式信息传递给较少的,而不是使用html2txt丢失它
  • Is it correct to just print the new content to stdout? Or could I just write the *.html to file disk and let less handle that html at its own digression (seeing the html-extension and acting on it?)
  • 将新内容打印到stdout是否正确?或者我可以只将* .html写入文件磁盘,让更少的人处理它自己的题外话(看到html扩展并对其进行操作?)

3 个解决方案

#1


9  

Take a look at Pandoc. It can convert files from markdown format to groff man pages which you can then view in man.

看看Pandoc吧。它可以将文件从markdown格式转换为groff手册页,然后您可以在man中查看。

Your .lessfilter script would be:

你的.lessfilter脚本将是:

case "$1" in
  *.md)
    pandoc -s -f markdown -t man "$1" | man -l -
    ;;

Alternatively, convert it to html using the markdown command and then use the lynx browser to view it, but this didn't work too well for me.

或者,使用markdown命令将其转换为html,然后使用lynx浏览器查看它,但这对我来说效果不佳。

case "$1" in
  *.md)
    markdown "$1" | lynx -stdin
    ;;

And, yes, the lessfilter script must write to stdout.

并且,是的,lessfilter脚本必须写入stdout。

#2


5  

Dogbane's answer is great, but if you use groff -T utf8 -man instead of man -l to do the formatting, then the bold, italic, etc. come through. As seen here: https://*.com/a/20197316/2674930.

Dogbane的答案很棒,但是如果你使用groff -T utf8 -man而不是man -l来进行格式化,那么粗体,斜体等都会通过。如下所示:https://*.com/a/20197316/2674930。

#3


3  

This didn't work on my version of MacOSX (10.10.5 Yosemite). The man page doesn't mention a .lessfilter either. Here is what I did (after reading MAN page - thanks to this question for the prompt and hints).

这不适用于我的MacOSX版本(10.10.5优胜美地)。该手册页也未提及.lessfilter。这是我做的(阅读MAN页面后 - 感谢这个问题的提示和提示)。

I created the scripts lessopen.sh and lessclose.sh in my ~/bin. Respectively they are:

我在〜/ bin中创建了脚本lessopen.sh和lessclose.sh。它们分别是:

#!/bin/bash

case "$1" in
  *.md)
    pandoc -s -f markdown -t man "$1" | groff -T utf8 -man > /tmp/less.$$
    if [ -s /tmp/less.$$ ]; then
        echo /tmp/less.$$
    else
        rm -f /tmp/less.$$
    fi
    ;;
esac

and

#!/bin/sh
rm $2

The return from the lessopen.sh is the name of the file with the contents to lessen. Or if nothing then the original file is used. The -s tests if the file is NOT zero size. The lessclose.sh cleans up.

lessopen.sh的返回是文件的名称,其中包含要减少的内容。或者,如果没有,则使用原始文件。 -s测试文件是否为零大小。 lessclose.sh清理。

Then in my ~/.bash_profile is:

然后在我的〜/ .bash_profile中:

export LESSOPEN="lessopen.sh %s"
export LESSCLOSE="less-close.sh %s %s"

I also had to install pandoc - groff already existed

我还必须安装pandoc - groff已经存在

brew install pandoc

Then simply:

那简单地说:

less README.md

to read it rendered.

阅读它呈现。

#1


9  

Take a look at Pandoc. It can convert files from markdown format to groff man pages which you can then view in man.

看看Pandoc吧。它可以将文件从markdown格式转换为groff手册页,然后您可以在man中查看。

Your .lessfilter script would be:

你的.lessfilter脚本将是:

case "$1" in
  *.md)
    pandoc -s -f markdown -t man "$1" | man -l -
    ;;

Alternatively, convert it to html using the markdown command and then use the lynx browser to view it, but this didn't work too well for me.

或者,使用markdown命令将其转换为html,然后使用lynx浏览器查看它,但这对我来说效果不佳。

case "$1" in
  *.md)
    markdown "$1" | lynx -stdin
    ;;

And, yes, the lessfilter script must write to stdout.

并且,是的,lessfilter脚本必须写入stdout。

#2


5  

Dogbane's answer is great, but if you use groff -T utf8 -man instead of man -l to do the formatting, then the bold, italic, etc. come through. As seen here: https://*.com/a/20197316/2674930.

Dogbane的答案很棒,但是如果你使用groff -T utf8 -man而不是man -l来进行格式化,那么粗体,斜体等都会通过。如下所示:https://*.com/a/20197316/2674930。

#3


3  

This didn't work on my version of MacOSX (10.10.5 Yosemite). The man page doesn't mention a .lessfilter either. Here is what I did (after reading MAN page - thanks to this question for the prompt and hints).

这不适用于我的MacOSX版本(10.10.5优胜美地)。该手册页也未提及.lessfilter。这是我做的(阅读MAN页面后 - 感谢这个问题的提示和提示)。

I created the scripts lessopen.sh and lessclose.sh in my ~/bin. Respectively they are:

我在〜/ bin中创建了脚本lessopen.sh和lessclose.sh。它们分别是:

#!/bin/bash

case "$1" in
  *.md)
    pandoc -s -f markdown -t man "$1" | groff -T utf8 -man > /tmp/less.$$
    if [ -s /tmp/less.$$ ]; then
        echo /tmp/less.$$
    else
        rm -f /tmp/less.$$
    fi
    ;;
esac

and

#!/bin/sh
rm $2

The return from the lessopen.sh is the name of the file with the contents to lessen. Or if nothing then the original file is used. The -s tests if the file is NOT zero size. The lessclose.sh cleans up.

lessopen.sh的返回是文件的名称,其中包含要减少的内容。或者,如果没有,则使用原始文件。 -s测试文件是否为零大小。 lessclose.sh清理。

Then in my ~/.bash_profile is:

然后在我的〜/ .bash_profile中:

export LESSOPEN="lessopen.sh %s"
export LESSCLOSE="less-close.sh %s %s"

I also had to install pandoc - groff already existed

我还必须安装pandoc - groff已经存在

brew install pandoc

Then simply:

那简单地说:

less README.md

to read it rendered.

阅读它呈现。