image detection not working
This commit is contained in:
@@ -3,7 +3,6 @@ import requests
|
||||
import re
|
||||
from bs4 import BeautifulSoup
|
||||
import subprocess
|
||||
import time
|
||||
import logging
|
||||
|
||||
# Configure logging
|
||||
@@ -204,31 +203,21 @@ def push_to_gitea_wiki(local_dir, pages):
|
||||
if found_images:
|
||||
logger.info(f"Found images on page '{title}': {found_images}")
|
||||
|
||||
# Download images referenced in the content
|
||||
# First, we need to find all image URLs and download them
|
||||
# Look for Redmine-style image tags like !image.png!
|
||||
def replace_image(match):
|
||||
image_name = match.group(1)
|
||||
# Process all !image! patterns in the content
|
||||
original_content = markdown_content
|
||||
|
||||
# Try to find the actual image URL by searching for it in the page content
|
||||
# This is a simplified approach - we'll assume the image name matches
|
||||
# what's found in Redmine's image system
|
||||
if image_name:
|
||||
# In Redmine, images are usually at /attachments/... path
|
||||
image_url = f"/attachments/download/{image_name}"
|
||||
logger.info(f"Attempting to download image: {image_name}")
|
||||
# Find all image tags in the format !image_name!
|
||||
image_tags = re.findall(r'!(.*?)!', original_content)
|
||||
|
||||
downloaded_path = download_image(image_url, local_dir)
|
||||
if downloaded_path:
|
||||
return f""
|
||||
else:
|
||||
# If download fails, keep the original format
|
||||
return f"!{image_name}!"
|
||||
return match.group(0)
|
||||
|
||||
# We need to handle both !image.png! and  formats
|
||||
# For now, we'll process !image.png! patterns specifically
|
||||
markdown_content = re.sub(image_pattern, replace_image, markdown_content)
|
||||
# For each image tag, attempt to download it and replace it with Markdown syntax
|
||||
for image_tag in image_tags:
|
||||
if image_tag and not image_tag.startswith('\\'):
|
||||
logger.info(f"Found image tag: {image_tag}")
|
||||
# Replace the specific tag with our download logic
|
||||
markdown_content = markdown_content.replace(
|
||||
f"!{image_tag}!",
|
||||
handle_image_tag(image_tag, local_dir)
|
||||
)
|
||||
|
||||
# Write to file
|
||||
with open(filepath, 'w', encoding='utf-8') as f:
|
||||
@@ -245,6 +234,30 @@ def push_to_gitea_wiki(local_dir, pages):
|
||||
|
||||
logger.info("✅ Wiki pages pushed to Gitea!")
|
||||
|
||||
def handle_image_tag(image_name, local_dir):
|
||||
"""Handle replacement of image tags with downloaded images"""
|
||||
# Check if the image has a valid extension (.png, .jpg, or .jpeg)
|
||||
valid_extensions = ('.png', '.jpg', '.jpeg')
|
||||
if not any(image_name.lower().endswith(ext) for ext in valid_extensions):
|
||||
logger.warning(f"Invalid image extension in tag: {image_name}")
|
||||
return f"!{image_name}!" # Return original format for invalid extensions
|
||||
|
||||
if image_name and not image_name.startswith('\\'):
|
||||
# In Redmine, images are usually at /attachments/... path
|
||||
image_url = f"/attachments/download/{image_name}"
|
||||
logger.info(f"Attempting to download image: {image_name}")
|
||||
|
||||
# Try to download the image
|
||||
downloaded_path = download_image(image_url, local_dir)
|
||||
if downloaded_path:
|
||||
return f""
|
||||
else:
|
||||
# If download fails, keep the original format
|
||||
return f"!{image_name}!"
|
||||
else:
|
||||
# This is likely a regex capture group that wasn't meant to be an image
|
||||
return f"!{image_name}!"
|
||||
|
||||
def main():
|
||||
# Step 1: Get wiki pages from Redmine
|
||||
logger.info("Fetching Redmine wiki pages...")
|
||||
|
||||
Reference in New Issue
Block a user