#!/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"
按目录结构生成pdf结构树
最新推荐文章于 2024-08-21 17:15:49 发布