按目录结构生成pdf结构树


#!/bin/bash

## 按 sidebar_position 生成目录
function generateSidebar() {
  find $1 -maxdepth 1 -type f  -exec grep sidebar_position {} + | awk -F: '{print $3 ,$1}';
}

## 按 _category_.json 生成目录
function generateCategorySidebar() {
  find $1 -mindepth 2 -maxdepth 2  -type f -name _category_.json  -exec grep -r  position {} +  | sed 's/_category_.json//;s/ //;s/,//' |  awk -F: '{print $3 ,$1}'
}

## 没有 sidebar_position 的文件
function generateOtherMd() {
   find $1 \( -type f -name "_category_.json" \) -prune -o -maxdepth 1  -type f  -exec grep -L sidebar_position {} + | awk -F: '{print 10000, $1}';
}

function generateMap() {
    subfile=$(echo $1 | sed 's/^\.\///')
    mkdir -p /tmp/docs/$subfile
    tmpfile=/tmp/docs/$subfile/$(date +%s);
    touch $tmpfile;
    generateSidebar $1 >> $tmpfile
    generateCategorySidebar $1 >> $tmpfile
    generateOtherMd $1 >> $tmpfile
    subdirs=$(cat $tmpfile | sort -n | awk '{print $2}')
    for subdir in $subdirs; do
        if [ -d $subdir ]; then
            # echo "dir  $subdir"
            currentDir=$(echo $subdir | sed 's/\/$//')
            if [ -f $currentDir/_category_.json ]; then
                dirlabel=$(grep '"label":.*' -o $currentDir/_category_.json  | sed 's/"label": "\(.*\)",/\1/')
                echo "<subdir title='$dirlabel'>"
                generateMap $currentDir
                echo "</subdir>"
            fi

        else
            title=$(grep -o '^title:.*' $subdir | sed 's/title: //')
            echo "<item title='$title' file='$subdir'>"
        fi
    done
}

function getAllDir() {
      for dir in $(find $1 \( -type d -name "_res" -o -name "partials" -o -name "assets" \) -prune -o -maxdepth 1  -type d -print); do
        if [ $dir != "$1" ]; then
            echo "generate $dir map"
            generateMap $dir > $dir/map.xml
        fi
    done
}

date "+%Y-%m-%d %H:%M:%S"
getAllDir .
date "+%Y-%m-%d %H:%M:%S"

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从未、淡定

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值