org-mode,如何使用 well-formed 组织链接自动生成漂亮的文件层次结构树

org-mode, how to automatically generate nice file hierarchy trees with well-formed Org links

在使用 Org-Mode 时,我正在寻找一种自动生成 well-formed org-mode 链接树的解决方案。

例如,要创建指向目录下所有 c++ 文件的链接,我需要这样的东西:


更新: 我刚刚尝试了@DamianChrzanowski 的建议,org-fstree 包。然而,我对结果有点失望:

html导出结果更差:

我的结论是套餐不能满足我的需求。无论如何,感谢@DamianChrzanowski 的回答。

安装 linux tree command 后,我得到了满足我所有需求的以下脚本:

#+NAME: createTree
#+BEGIN_SRC sh :results drawer :var toInclude="*.org" :var toExclude="" :var directory="./" :var createLink="true" :exports none
set -e
buffer=$(mktemp /tmp/buffer.XXXXXXXXX)
current_directory=$(pwd)
cd $(eval echo "$directory")
tree -a -P "$toInclude" -I "$toExclude" -if --noreport  --prune \
    | sed "s/.//"  | sed "s/^\///"  > "$buffer"

if [ $(grep --regexp="$" --count "$buffer") -eq 0 ]; then
    echo "**ERROR empty list**"
else
    for f in $(cat "$buffer")
    do 
    filename=$(basename $f)
    ext="${filename##*.}"
    baseFilename="${filename%.*}"
    if [ -f $f ]; then
        # removes org extension (only)
        if [ "$ext" = "org" ]; then
        filename="$baseFilename"
        fi
        # creates org link (or not)
        if [ "$createLink" = true ]; then 
        echo "$(echo "$f" | tr -cd / | tr / \t)+ [[file:"$directory/$f"][$filename]]"
        else
        echo "$(echo "$f" | tr -cd / | tr / \t)+ $filename"
        fi
    else
        echo  "$(echo "$f" | tr -cd / | tr / \t)+ $filename/"
    fi
    done
fi
rm "$buffer"
cd "$current_directory"
#+END_SRC

如果要创建 C++ 代码的文件树,只需使用类似以下内容:

#+CALL: createTree(toInclude="*.[hc]pp",toExclude="*test*",directory="~/MyProject")

另外请注意,当您 export/publish 您的 org-mode 文档时,您可以将它用作站点地图的替代品。只需使用:

* My site content

#+CALL: createTree(toInclude="*.org",toExclude="Setup")

前面的 #+CALL 命令将生成树,就像我在问题中发布的那样。 Org HTML export 后,你会得到类似这样的东西:


命令arguments/options是:

  • toInclude="...":要包含的模式
  • toExclude="...":要排除的模式
  • 目录=“...”:目录
  • createLink="true" 或 "false" :如果为 false,则创建的树没有链接

注意 1: 您可以将脚本存储在任何 .org 文件中并加载它,感谢 Library-of-Babel

在您的 init.el 文件中:

(org-babel-lob-ingest "~/path/to/your/scripts.org")

注 2:我回答了我自己的问题,但我仍然对纯 Emacs-Lisp 解决方案持开放态度。

通常会有一个包裹:-) org-fstree

我刚试过 org-fs-tree 似乎效果不错。这是一个较新的包,看起来 org-fstree 是孤立的。