|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# Directory containing posts |
4 | | -POSTS_DIR="_posts" |
5 | | - |
6 | | -# Flag to track if any inconsistencies are found |
7 | | -inconsistent=false |
8 | | - |
9 | | -# Iterate over each post file in the _posts directory |
10 | | -for file in "$POSTS_DIR"/*.md; do |
11 | | - # Extract filename without extension |
12 | | - filename=$(basename "$file" .md) |
13 | | - |
14 | | - # Extract date from filename (assumes format YYYY-MM-DD-title) |
15 | | - file_date=$(echo "$filename" | perl -ne 's/^(\d{4}-\d{2}-\d{2})/$1/ && print $1') |
16 | | - |
17 | | - # Extract date from front matter |
18 | | - frontmatter_date=$( cat "$file" | perl -n -e '$front_matter .= $_ if /---/.../---/; END { print $1 if $front_matter =~ /date:\s+([0-9-]+)/;}') |
19 | | - |
20 | | - # Compare file date with front matter date |
21 | | - if [[ "$file_date" = '' ]] || [[ "$file_date" != "$frontmatter_date" ]]; then |
22 | | - echo "Inconsist or missing date info: $file" |
23 | | - echo " Filename date: '$file_date'" |
24 | | - echo " Front matter date: '$frontmatter_date'" |
25 | | - inconsistent=true |
26 | | - else |
27 | | - # echo "Consistent|$file|$filename|$file_date|$frontmatter_date|" |
28 | | - : |
29 | | - fi |
30 | | -done |
31 | | - |
32 | | -# Exit with error if inconsistencies were found |
33 | | -if $inconsistent; then |
34 | | - echo "Inconsistencies detected. Please correct them." |
35 | | - exit 1 |
36 | | -else |
37 | | - echo "All dates are consistent." |
38 | | -fi |
| 3 | + |
| 4 | +function date_from_filename () { |
| 5 | + local f="${1}" |
| 6 | + echo "${f}" | |
| 7 | + perl -n -e 's/^(\d{4}-\d{2}-\d{2})/$1/ && print $1' |
| 8 | +} |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +function main () { |
| 13 | + # Directory containing posts |
| 14 | + POSTS_DIR="_posts" |
| 15 | + |
| 16 | + # Flag to track if any inconsistencies are found |
| 17 | + inconsistent=false |
| 18 | + |
| 19 | + # Iterate over each post file in the _posts directory |
| 20 | + for file in "$POSTS_DIR"/*.md; do |
| 21 | + # Extract filename without extension |
| 22 | + filename=$(basename "$file" .md) |
| 23 | + |
| 24 | + # Extract date from filename (assumes format YYYY-MM-DD-title) |
| 25 | + date_from_filename=$( date_from_filename "$filename" ) |
| 26 | + |
| 27 | + # Extract date from front matter |
| 28 | + frontmatter_date=$( cat "$file" | perl -n -e '$front_matter .= $_ if /---/.../---/; END { print $1 if $front_matter =~ /date:\s+([0-9-]+)/;}') |
| 29 | + |
| 30 | + # Compare file date with front matter date |
| 31 | + if [[ "$date_from_filename" = '' ]] || [[ "$date_from_filename" != "$frontmatter_date" ]]; then |
| 32 | + echo "Inconsist or missing date info: $file" |
| 33 | + echo " Date From Filename: '$date_from_filename'" |
| 34 | + echo " Front matter date: '$frontmatter_date'" |
| 35 | + inconsistent=true |
| 36 | + else |
| 37 | + # echo "Consistent|$file|$filename|$date_from_filename|$frontmatter_date|" |
| 38 | + : |
| 39 | + fi |
| 40 | + done |
| 41 | + |
| 42 | + # Exit with error if inconsistencies were found |
| 43 | + if $inconsistent; then |
| 44 | + echo "Inconsistencies detected. Please correct them." |
| 45 | + exit 1 |
| 46 | + else |
| 47 | + echo "All dates are consistent." |
| 48 | + fi |
| 49 | +} |
| 50 | + |
| 51 | +main "$@" |
0 commit comments