Skip to content

Commit

Permalink
+ recursive subnav, remplace le tag au lieu de réecrire le md
Browse files Browse the repository at this point in the history
  • Loading branch information
gllmAR committed Sep 15, 2024
1 parent f6820be commit 6410e83
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 46 deletions.
155 changes: 118 additions & 37 deletions .run/20_generate_subnav.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,32 @@ get_title_from_readme() {
# Function to check if README.md contains the generateSubNav comment
contains_generate_subnav_comment() {
local readme_path="$1"
grep -q "<!-- generateSubNav -->" "$readme_path"
grep -q '<!-- generateSubNav -->' "$readme_path"
}

# Function to generate README.md in subfolders
generate_readme_in_subfolders() {
# Function to generate the subnavigation content recursively
generate_subnav_content() {
local parent_dir="$1"
local readme_path="$parent_dir/README.md"

# Extract title from the current README.md
local title
title=$(get_title_from_readme "$readme_path")
if [[ -z "$title" ]]; then
title=$(basename "$parent_dir")
fi
local indent_level="$2"
local content_lines=()

# Start building the new README.md content
local new_content="# $title\n\n<!-- generateSubNav -->\n\n"
# Remove trailing slash from parent_dir if any
parent_dir="${parent_dir%/}"

# Iterate over subdirectories and add links to each
# Iterate over subdirectories
for subdir in "$parent_dir"/*/; do
if [[ -d "$subdir" ]]; then
# Remove trailing slash from subdir
subdir="${subdir%/}"

local base_dir
base_dir=$(basename "$subdir")

# Skip excluded directories
if [[ " ${EXCLUDED_DIRS[*]} " =~ " ${base_dir} " ]]; then
continue
fi

local subdir_readme="$subdir/README.md"
local subdir_title

Expand All @@ -50,36 +53,115 @@ generate_readme_in_subfolders() {
subdir_title="$base_dir"
fi

# Clean up paths to avoid double slashes
local clean_parent_dir="${parent_dir%/}"
local clean_base_dir="${base_dir%/}"

# Combine paths and ensure no double slashes
local absolute_path="$clean_parent_dir/$clean_base_dir/"
absolute_path="/${absolute_path#"$ROOT_DIR/"}" # Remove root directory and ensure no leading //

# Replace any instances of double slashes (except the leading slash)
absolute_path=$(echo "$absolute_path" | sed 's|//|/|g')
# Get relative path from ROOT_DIR
local relative_path="${subdir#$ROOT_DIR/}" # Remove ROOT_DIR and following slash
relative_path="${relative_path#/}" # Remove any leading slash
relative_path="${relative_path%/}" # Remove any trailing slash

# Build the link
local link
if [[ -n "$relative_path" ]]; then
link="/${relative_path}/"
else
link="/"
fi

new_content+="* [$subdir_title]($absolute_path)\n"
echo "Generated link for $absolute_path with title '$subdir_title'"
else
echo "Skipped directory (no README.md): $subdir"
# Build the line with proper indentation
local indent=""
for ((i=0; i<indent_level; i++)); do
indent+=" " # 4 spaces
done
content_lines+=("${indent}* [${subdir_title}](${link})")

# Recurse into subdirectories
local sub_content
sub_content=$(generate_subnav_content "$subdir" $((indent_level + 1)))
if [[ -n "$sub_content" ]]; then
content_lines+=("$sub_content")
fi
fi
fi
done

# Write the new content back to the README.md
echo -e "$new_content" > "$readme_path"
# Join the content lines with newlines
local content=$(printf "%s\n" "${content_lines[@]}")
echo "$content"
}

# Function to update README.md with the new subnav content
update_readme_with_subnav() {
local readme_path="$1"
local subnav_content="$2"
local tmpfile=$(mktemp)
local in_section=0
local start_tag_found=0
local end_tag_found=0

while IFS= read -r line || [ -n "$line" ]; do
if [[ "$line" == *'<!-- generateSubNav -->'* ]]; then
echo "$line" >> "$tmpfile"
in_section=1
start_tag_found=1
# Write the subnav content immediately after the start tag
echo "$subnav_content" >> "$tmpfile"
elif [[ "$line" == *'<!-- generateSubNavEnd -->'* ]]; then
echo "$line" >> "$tmpfile"
in_section=0
end_tag_found=1
elif [[ $in_section -eq 0 ]]; then
echo "$line" >> "$tmpfile"
fi
# If in_section is 1 (between tags), skip the line
done < "$readme_path"

if [[ $start_tag_found -eq 1 && $end_tag_found -eq 0 ]]; then
# Add the end tag if it was missing
echo "<!-- generateSubNavEnd -->" >> "$tmpfile"
fi

if [[ $start_tag_found -eq 0 ]]; then
# Start tag not found; append the content at the end of tmpfile
{
echo -e "\n<!-- generateSubNav -->"
echo "$subnav_content"
echo "<!-- generateSubNavEnd -->"
} >> "$tmpfile"
fi

# Replace the original file with the updated content
mv "$tmpfile" "$readme_path"
}

# Function to generate README.md in subfolders
generate_readme_in_subfolders() {
local parent_dir="$1"
local readme_path="$parent_dir/README.md"

# Extract title from the current README.md
local title
title=$(get_title_from_readme "$readme_path")
if [[ -z "$title" ]]; then
title=$(basename "$parent_dir")
fi

# Generate the subnav content
local subnav_content
subnav_content=$(generate_subnav_content "$parent_dir" 0)

# Update the README.md with the new subnav content
update_readme_with_subnav "$readme_path" "$subnav_content"
}

# Function to walk through directories and generate the subnav content
generate_subnav() {
local dir_path="$1"

# Remove trailing slash from dir_path if any
dir_path="${dir_path%/}"

# Check for README.md in the current directory
if [[ -f "$dir_path/README.md" ]]; then
# Check if README.md contains the generateSubNav comment and has subdirectories
# Check if README.md contains the generateSubNav comment
if contains_generate_subnav_comment "$dir_path/README.md"; then
generate_readme_in_subfolders "$dir_path"
fi
Expand All @@ -91,6 +173,9 @@ generate_subnav() {
for subdir in "$dir_path"/*/; do
# Ensure subdir is a directory
if [[ -d "$subdir" ]]; then
# Remove trailing slash from subdir
subdir="${subdir%/}"

local base_dir
base_dir=$(basename "$subdir")
if [[ ! " ${EXCLUDED_DIRS[*]} " =~ " ${base_dir} " ]]; then
Expand All @@ -100,9 +185,5 @@ generate_subnav() {
done
}

# Start from the current directory
for dir in */; do
if [[ -d "$dir" ]]; then
generate_subnav "$dir"
fi
done
# Start from the root directory
generate_subnav "$ROOT_DIR"
3 changes: 1 addition & 2 deletions activites/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Activités

<!-- generateSubNav -->

* [Sortie Mutek ](/activites/0_sortie_mutek/)
* [Présenter l'œuvres multimédia](/activites/1_corpus_multimedia/)
* [Scénariser l'interactivité](/activites/2_scenariser/)

<!-- generateSubNavEnd -->
4 changes: 3 additions & 1 deletion contenus/1_presentation/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Présenter

<!-- generateSubNav -->

* [Docsify](/contenus/1_presentation/docsify/)
* [GIT](/contenus/1_presentation/git/)
* [Github](/contenus/1_presentation/github/)
* [Publication de Pages GitHub](/contenus/1_presentation/github/pages/)
* [Intégration Continue](/contenus/1_presentation/github/scriptCI/)
* [Markdown](/contenus/1_presentation/markdown/)
* [vscode.dev](/contenus/1_presentation/vscodeDev/)
<!-- generateSubNavEnd -->

3 changes: 1 addition & 2 deletions contenus/1_presentation/github/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Github

<!-- generateSubNav -->

* [Publication de Pages GitHub](/contenus/1_presentation/github/pages/)
* [Intégration Continue](/contenus/1_presentation/github/scriptCI/)

<!-- generateSubNavEnd -->
3 changes: 1 addition & 2 deletions contenus/2_scenarisation/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Scénariser

<!-- generateSubNav -->

* [Communiquer une idée](/contenus/2_scenarisation/00_ideation/)
* [Scénario cinématographique](/contenus/2_scenarisation/10_scenario_cinema/)
* [Scénario interactif](/contenus/2_scenarisation/11_scenario_interactif/)
Expand All @@ -14,4 +13,4 @@
* [Technologies](/contenus/2_scenarisation/40_technologie/)
* [Mermaid](/contenus/2_scenarisation/80_mermaid/)
* [Draw.io](/contenus/2_scenarisation/81_drawio/)

<!-- generateSubNavEnd -->
24 changes: 22 additions & 2 deletions contenus/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
# Contenus

test après
<!-- generateSubNav -->

* [Présenter](/contenus/1_presentation/)
* [Docsify](/contenus/1_presentation/docsify/)
* [GIT](/contenus/1_presentation/git/)
* [Github](/contenus/1_presentation/github/)
* [Publication de Pages GitHub](/contenus/1_presentation/github/pages/)
* [Intégration Continue](/contenus/1_presentation/github/scriptCI/)
* [Markdown](/contenus/1_presentation/markdown/)
* [vscode.dev](/contenus/1_presentation/vscodeDev/)
* [Scénariser](/contenus/2_scenarisation/)
* [Communiquer une idée](/contenus/2_scenarisation/00_ideation/)
* [Scénario cinématographique](/contenus/2_scenarisation/10_scenario_cinema/)
* [Scénario interactif](/contenus/2_scenarisation/11_scenario_interactif/)
* [Le narratif dans l'interactif](/contenus/2_scenarisation/12_scenario_narratif/)
* [Ambiances](/contenus/2_scenarisation/20_moodboard/)
* [Scénarimage](/contenus/2_scenarisation/30_scenarimage/)
* [Inventaire multimédia](/contenus/2_scenarisation/31_inventaire_multimedia/)
* [Arborescence](/contenus/2_scenarisation/32_arborescence/)
* [Nomenclature](/contenus/2_scenarisation/33_nomenclature/)
* [Technologies](/contenus/2_scenarisation/40_technologie/)
* [Mermaid](/contenus/2_scenarisation/80_mermaid/)
* [Draw.io](/contenus/2_scenarisation/81_drawio/)
<!-- generateSubNavEnd -->

test avant

0 comments on commit 6410e83

Please sign in to comment.