-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorganize_docs.sh
More file actions
executable file
·124 lines (97 loc) · 3.24 KB
/
organize_docs.sh
File metadata and controls
executable file
·124 lines (97 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
# Documentation Organization Script
# Automatically numbers and moves documents from inbox/ to docs/
# Format: YYYY-MM-DD-NNN-Topic.md
set -e
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}MinZ Documentation Organizer${NC}"
echo "================================"
# Find the highest numbered document in docs/
find_next_number() {
local max_num=0
# Find all numbered docs (new format: YYYY-MM-DD-NNN-*.md)
for file in docs/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]-*.md; do
if [ -f "$file" ]; then
# Extract the number from filename (position after third dash)
num=$(basename "$file" | cut -d'-' -f4 | sed 's/^0*//')
if [ -n "$num" ] && [ "$num" -gt "$max_num" ] 2>/dev/null; then
max_num=$num
fi
fi
done
# Also check old format for compatibility (NNN_*.md)
for file in docs/[0-9][0-9][0-9]_*.md; do
if [ -f "$file" ]; then
num=$(basename "$file" | cut -d'_' -f1 | sed 's/^0*//')
if [ -n "$num" ] && [ "$num" -gt "$max_num" ] 2>/dev/null; then
max_num=$num
fi
fi
done
# Return next number
echo $((max_num + 1))
}
# Format number with leading zeros (001, 002, etc.)
format_number() {
printf "%03d" $1
}
# Sanitize filename (remove special chars, spaces to underscores)
sanitize_name() {
echo "$1" | sed 's/\.md$//' | sed 's/[^a-zA-Z0-9_-]/_/g' | sed 's/__*/_/g' | sed 's/^_//' | sed 's/_$//'
}
# Main processing
main() {
# Create directories if they don't exist
mkdir -p inbox
mkdir -p docs
# Check if there are any files to process
shopt -s nullglob
files=(inbox/*.md)
if [ ${#files[@]} -eq 0 ]; then
echo -e "${YELLOW}No markdown files found in inbox/${NC}"
echo "Place your .md files in the inbox/ folder and run this script again."
exit 0
fi
echo -e "${GREEN}Found ${#files[@]} file(s) to process${NC}"
echo
# Get the starting number
next_num=$(find_next_number)
# Get today's date
today=$(date +%Y-%m-%d)
# Process each file
for file in "${files[@]}"; do
# Get original filename without path
original_name=$(basename "$file")
# Skip .gitkeep
if [ "$original_name" = ".gitkeep" ]; then
continue
fi
# Sanitize the name
clean_name=$(sanitize_name "$original_name")
# Format the number
formatted_num=$(format_number $next_num)
# Create new filename with date prefix: YYYY-MM-DD-NNN-Topic.md
new_name="${today}-${formatted_num}-${clean_name}.md"
new_path="docs/$new_name"
# Move the file
mv "$file" "$new_path"
echo -e "${GREEN}+${NC} $original_name"
echo -e " -> ${BLUE}$new_name${NC}"
echo
# Increment counter
next_num=$((next_num + 1))
done
echo -e "${GREEN}Documentation organized successfully!${NC}"
# Show summary
echo
echo "Summary:"
echo "--------"
echo "Next available number: $(format_number $next_num)"
echo "Total docs in docs/: $(ls -1 docs/*.md 2>/dev/null | wc -l)"
}
# Run main function
main "$@"