Skip to content

Commit edfbdb2

Browse files
readmd.md update8
1 parent c672562 commit edfbdb2

File tree

3 files changed

+142
-151
lines changed

3 files changed

+142
-151
lines changed

.github/scripts/sync-wiki.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import re
5+
from bs4 import BeautifulSoup
6+
import os.path
7+
8+
# Configuration: map of source README files to destination wiki pages
9+
FILE_MAPPINGS = {
10+
'benchmarks/rotatingDrum/readme.md': 'Performance-of-phasicFlow.md',
11+
# Add more mappings as needed
12+
}
13+
14+
def convert_relative_links(content, source_path, repo_name):
15+
"""Convert relative links to absolute GitHub links"""
16+
repo_base_url = f"https://github.com/{repo_name}/blob/main/"
17+
18+
# Find the directory of the source file to correctly resolve relative paths
19+
source_dir = os.path.dirname(source_path)
20+
if source_dir:
21+
source_dir += '/'
22+
23+
# Convert Markdown links: [text](link)
24+
def replace_md_link(match):
25+
link_text = match.group(1)
26+
link_path = match.group(2)
27+
28+
# Skip links that are already absolute
29+
if link_path.startswith(('http://', 'https://', '#')):
30+
return f"[{link_text}]({link_path})"
31+
32+
# Convert relative path to absolute
33+
if link_path.startswith('./'):
34+
link_path = link_path[2:]
35+
elif link_path.startswith('../'):
36+
# Need to resolve the path based on source_dir
37+
path_parts = source_dir.strip('/').split('/')
38+
current_path = link_path
39+
while current_path.startswith('../'):
40+
path_parts.pop()
41+
current_path = current_path[3:]
42+
new_path = '/'.join(path_parts) + '/' + current_path if path_parts else current_path
43+
return f"[{link_text}]({repo_base_url}{new_path})"
44+
45+
absolute_path = f"{source_dir}{link_path}" if not link_path.startswith('/') else link_path[1:]
46+
return f"[{link_text}]({repo_base_url}{absolute_path})"
47+
48+
content = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', replace_md_link, content)
49+
50+
# Preserve HTML img tags with their style attributes and fix src paths
51+
soup = BeautifulSoup(content, 'html.parser')
52+
for img in soup.find_all('img'):
53+
if img.get('src') and not img['src'].startswith(('http://', 'https://')):
54+
src = img['src']
55+
if src.startswith('./'):
56+
src = src[2:]
57+
if not src.startswith('/'):
58+
src = f"{source_dir}{src}"
59+
else:
60+
src = src[1:] # Remove leading slash
61+
62+
img['src'] = f"{repo_base_url}{src}"
63+
64+
# Convert the modified HTML back to string, but only if we found any img tags
65+
if soup.find_all('img'):
66+
# Extract just the modified HTML parts and reinsert them
67+
for img in soup.find_all('img'):
68+
img_str = str(img)
69+
# Find the original img tag in content and replace it
70+
original_img_pattern = re.compile(r'<img[^>]*src=["\']([^"\']*)["\'][^>]*>')
71+
for match in original_img_pattern.finditer(content):
72+
orig_src = match.group(1)
73+
if orig_src in img['src'] or img['src'].endswith(orig_src):
74+
content = content.replace(match.group(0), img_str)
75+
76+
return content
77+
78+
def main():
79+
repo_name = os.environ.get('GITHUB_REPOSITORY', 'PhasicFlow/phasicFlow')
80+
repo_path = os.path.join(os.environ.get('GITHUB_WORKSPACE', '.'), 'repo')
81+
wiki_path = os.path.join(os.environ.get('GITHUB_WORKSPACE', '.'), 'wiki')
82+
83+
for source_rel_path, dest_wiki_page in FILE_MAPPINGS.items():
84+
source_path = os.path.join(repo_path, source_rel_path)
85+
dest_path = os.path.join(wiki_path, dest_wiki_page)
86+
87+
print(f"Processing: {source_path} -> {dest_path}")
88+
89+
if not os.path.exists(source_path):
90+
print(f"Warning: Source file {source_path} does not exist, skipping.")
91+
continue
92+
93+
# Read the source file
94+
with open(source_path, 'r', encoding='utf-8') as f:
95+
content = f.read()
96+
97+
# Convert relative links to absolute
98+
content = convert_relative_links(content, source_rel_path, repo_name)
99+
100+
# Write the modified content to the destination
101+
with open(dest_path, 'w', encoding='utf-8') as f:
102+
f.write(content)
103+
104+
print(f"Successfully synced {source_path} to {dest_path}")
105+
106+
if __name__ == "__main__":
107+
main()

.github/workflows/sync-wiki.yml

Lines changed: 34 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,47 @@
1-
name: Sync README files to Wiki
1+
name: Sync Wiki
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches:
6+
- main
67
paths:
7-
- '**/README.md'
8-
- '**/readme.md'
8+
- 'benchmarks/*/readme.md'
9+
- '.github/workflows/sync-wiki.yml'
10+
workflow_dispatch:
911

10-
permissions:
11-
contents: write
12-
1312
jobs:
1413
sync-wiki:
1514
runs-on: ubuntu-latest
1615
steps:
17-
- uses: actions/checkout@v3
18-
19-
- name: Configure Git for Wiki
20-
run: |
21-
git config --global user.name "GitHub Actions"
22-
git config --global user.email "actions@github.com"
23-
24-
- name: Clone Wiki Repository
25-
run: git clone https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.wiki.git ./wiki
26-
27-
- name: Copy README files to Wiki
28-
run: |
29-
# Special mappings - add specific README files to specific wiki pages
30-
declare -A special_mappings
31-
special_mappings["benchmarks/rotatingDrum/readme.md"]="Performance-of-phasicFlow.md"
16+
- name: Checkout Repository
17+
uses: actions/checkout@v3
18+
with:
19+
path: repo
3220

33-
# Create an images directory in the wiki if it doesn't exist
34-
mkdir -p ./wiki/images
21+
- name: Checkout Wiki
22+
uses: actions/checkout@v3
23+
with:
24+
repository: ${{ github.repository }}.wiki
25+
path: wiki
3526

36-
# Process mapped files
37-
for rel_path in "${!special_mappings[@]}"; do
38-
if [ -f "./$rel_path" ]; then
39-
wiki_page="${special_mappings[$rel_path]}"
40-
echo "Processing special mapping: $rel_path -> $wiki_page"
41-
42-
# Get the base directory of the readme file
43-
base_dir=$(dirname "./$rel_path")
44-
45-
# Read content of the README file
46-
content=$(cat "./$rel_path")
47-
48-
# Use grep to identify and process image paths instead of regex
49-
echo "Processing Markdown image references..."
50-
for img_ref in $(grep -o '!\[.*\](.*[^)]*)' "./$rel_path" | sed -E 's/!\[.*\]\((.*)\)/\1/'); do
51-
# Skip URLs
52-
if [[ $img_ref == http* ]]; then
53-
continue
54-
fi
55-
56-
# Process markdown image as before
57-
if [[ $img_ref == /* ]]; then
58-
# Absolute path within repository
59-
abs_img_path="./$img_ref"
60-
else
61-
# Relative path to the README
62-
abs_img_path="$base_dir/$img_ref"
63-
fi
64-
65-
# Extract just the filename
66-
img_filename=$(basename "$img_ref")
67-
wiki_img_path="images/$img_filename"
68-
69-
# Copy the image to wiki repository if it exists
70-
if [ -f "$abs_img_path" ]; then
71-
echo "Copying image: $abs_img_path -> ./wiki/$wiki_img_path"
72-
cp -v "$abs_img_path" "./wiki/$wiki_img_path" || echo "Error copying image"
73-
74-
# Escape special characters in the path for sed
75-
escaped_img_path=$(echo "$img_ref" | sed 's/[\/&]/\\&/g')
76-
77-
# Replace the image reference in content - simpler approach with sed
78-
content=$(echo "$content" | sed "s|!\\[.*\\]($escaped_img_path)|![\\1]($wiki_img_path)|g")
79-
echo "Replaced image reference: $img_ref → $wiki_img_path"
80-
else
81-
echo "Warning: Image file not found: $abs_img_path"
82-
# Add more debug info
83-
echo "Current directory: $(pwd)"
84-
echo "Files in $base_dir:"
85-
ls -la "$base_dir"
86-
fi
87-
done
88-
89-
# Process HTML img tags by finding all images and copying them
90-
echo "Processing HTML image references..."
91-
92-
# First, find and copy all images referenced in HTML tags
93-
img_tags_file=$(mktemp)
94-
# Capture complete HTML img tags with all attributes into a file
95-
grep -o '<img[^>]*>' "./$rel_path" > "$img_tags_file" || true
96-
97-
# Create a file to store all image source paths
98-
img_src_file=$(mktemp)
99-
100-
# Extract src attributes from img tags
101-
while IFS= read -r img_tag; do
102-
img_src=$(echo "$img_tag" | grep -o 'src="[^"]*"' | sed 's/src="//;s/"$//')
103-
if [ -n "$img_src" ] && [[ $img_src != http* ]]; then
104-
echo "$img_src" >> "$img_src_file"
105-
fi
106-
done < "$img_tags_file"
107-
108-
# Process each unique image source
109-
if [ -s "$img_src_file" ]; then
110-
sort -u "$img_src_file" | while read -r img_src; do
111-
# Skip empty lines
112-
if [ -z "$img_src" ]; then
113-
continue
114-
fi
115-
116-
# Determine image path
117-
if [[ $img_src == /* ]]; then
118-
abs_img_path="./$img_src"
119-
else
120-
abs_img_path="$base_dir/$img_src"
121-
fi
122-
123-
# Extract filename
124-
img_filename=$(basename "$img_src")
125-
wiki_img_path="images/$img_filename"
126-
127-
# Copy image to wiki
128-
if [ -f "$abs_img_path" ]; then
129-
echo "Copying HTML image: $abs_img_path -> ./wiki/$wiki_img_path"
130-
cp -v "$abs_img_path" "./wiki/$wiki_img_path" || echo "Error copying image"
131-
132-
# Prepare for replacement
133-
escaped_img_src=$(echo "$img_src" | sed 's/[\/&]/\\&/g')
134-
escaped_wiki_path=$(echo "$wiki_img_path" | sed 's/[\/&]/\\&/g')
135-
136-
# Update src path while preserving ALL other attributes
137-
content=$(echo "$content" | sed "s|src=\"$escaped_img_src\"|src=\"$escaped_wiki_path\"|g")
138-
else
139-
echo "Warning: HTML image file not found: $abs_img_path"
140-
fi
141-
done
142-
fi
143-
144-
# Clean up temporary files
145-
rm -f "$img_tags_file" "$img_src_file"
146-
147-
# Debug output
148-
echo "Wiki page content preview (first 100 chars): ${content:0:100}"
149-
150-
# Replace the wiki page with the updated content rather than appending
151-
mkdir -p "$(dirname "./wiki/$wiki_page")" # Ensure directory exists
152-
echo -e "# $(basename "$wiki_page" .md)\n\n$content" > "./wiki/$wiki_page"
153-
echo "Updated wiki page: $wiki_page"
154-
fi
155-
done
156-
157-
- name: Commit and Push to Wiki
158-
working-directory: ./wiki
27+
- name: Set up Python
28+
uses: actions/setup-python@v4
29+
with:
30+
python-version: '3.x'
31+
32+
- name: Install dependencies
33+
run: pip install beautifulsoup4
34+
35+
- name: Sync specific README files to Wiki
36+
run: |
37+
python $GITHUB_WORKSPACE/repo/.github/scripts/sync-wiki.py
38+
env:
39+
GITHUB_REPOSITORY: ${{ github.repository }}
40+
41+
- name: Push changes to wiki
15942
run: |
160-
echo "Files changed in wiki repository:"
161-
git status
43+
cd wiki
44+
git config user.name "${{ github.actor }}"
45+
git config user.email "${{ github.actor }}@users.noreply.github.com"
16246
git add .
163-
git diff-index --quiet HEAD || git commit -m "Sync README files from main repository"
164-
git push || { echo "Push failed, retrying with more details..."; git push --verbose; }
47+
git diff --quiet && git diff --staged --quiet || (git commit -m "Auto sync wiki from main repository" && git push)

benchmarks/rotatingDrum/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This benchmark compares the performance of phasicFlow with a well-stablished com
1313
</div>
1414
</div>
1515

16+
1617
<div align="center">
1718
<img src="./images/phasicFlow_snapshot.png" style="width: 400px;" />
1819
<div align="center">

0 commit comments

Comments
 (0)