fixed linter
This commit is contained in:
parent
df3f03e669
commit
3ef87cd083
3 changed files with 38 additions and 6 deletions
|
|
@ -655,10 +655,12 @@ class RendererDocx(BaseRenderer):
|
||||||
content = image_data.get("content", {})
|
content = image_data.get("content", {})
|
||||||
base64_data = ""
|
base64_data = ""
|
||||||
alt_text = "Image"
|
alt_text = "Image"
|
||||||
|
caption = ""
|
||||||
|
|
||||||
if isinstance(content, dict):
|
if isinstance(content, dict):
|
||||||
base64_data = content.get("base64Data", "")
|
base64_data = content.get("base64Data", "")
|
||||||
alt_text = content.get("altText", "Image")
|
alt_text = content.get("altText", "Image")
|
||||||
|
caption = content.get("caption", "")
|
||||||
elif isinstance(content, str):
|
elif isinstance(content, str):
|
||||||
# Content might be base64 string directly (shouldn't happen, but handle it)
|
# Content might be base64 string directly (shouldn't happen, but handle it)
|
||||||
self.logger.warning("Image content is a string, not a dict. This should not happen.")
|
self.logger.warning("Image content is a string, not a dict. This should not happen.")
|
||||||
|
|
@ -669,6 +671,8 @@ class RendererDocx(BaseRenderer):
|
||||||
base64_data = image_data.get("base64Data", "")
|
base64_data = image_data.get("base64Data", "")
|
||||||
if not alt_text or alt_text == "Image":
|
if not alt_text or alt_text == "Image":
|
||||||
alt_text = image_data.get("altText", "Image")
|
alt_text = image_data.get("altText", "Image")
|
||||||
|
if not caption:
|
||||||
|
caption = image_data.get("caption", "")
|
||||||
|
|
||||||
# CRITICAL: Ensure we don't render base64 data as text
|
# CRITICAL: Ensure we don't render base64 data as text
|
||||||
# If base64_data looks like it might be rendered elsewhere, skip it
|
# If base64_data looks like it might be rendered elsewhere, skip it
|
||||||
|
|
@ -712,8 +716,26 @@ class RendererDocx(BaseRenderer):
|
||||||
image_stream.seek(0)
|
image_stream.seek(0)
|
||||||
doc.add_picture(image_stream, width=Inches(6.0))
|
doc.add_picture(image_stream, width=Inches(6.0))
|
||||||
|
|
||||||
if alt_text and alt_text != "Image":
|
# Use caption from section if available, otherwise use alt_text
|
||||||
caption_para = doc.add_paragraph(f"Figure: {alt_text}")
|
if caption:
|
||||||
|
caption_text = caption
|
||||||
|
elif alt_text and alt_text != "Image":
|
||||||
|
# Only use alt_text if it doesn't look like a usageHint
|
||||||
|
if "Render as visual element:" in alt_text:
|
||||||
|
# Extract filename from usageHint if possible
|
||||||
|
parts = alt_text.split("Render as visual element:")
|
||||||
|
if len(parts) > 1:
|
||||||
|
filename = parts[1].strip()
|
||||||
|
caption_text = f"Figure: {filename}"
|
||||||
|
else:
|
||||||
|
caption_text = alt_text
|
||||||
|
else:
|
||||||
|
caption_text = f"Figure: {alt_text}"
|
||||||
|
else:
|
||||||
|
caption_text = None
|
||||||
|
|
||||||
|
if caption_text:
|
||||||
|
caption_para = doc.add_paragraph(caption_text)
|
||||||
caption_para.runs[0].italic = True
|
caption_para.runs[0].italic = True
|
||||||
except Exception as embedError:
|
except Exception as embedError:
|
||||||
# Image decoding or embedding failed
|
# Image decoding or embedding failed
|
||||||
|
|
|
||||||
|
|
@ -895,11 +895,21 @@ class RendererPdf(BaseRenderer):
|
||||||
captionStyle.textColor = self._hexToColor(styles.get("paragraph", {}).get("color", "#666666"))
|
captionStyle.textColor = self._hexToColor(styles.get("paragraph", {}).get("color", "#666666"))
|
||||||
elements.append(Paragraph(f"<i>{caption}</i>", captionStyle))
|
elements.append(Paragraph(f"<i>{caption}</i>", captionStyle))
|
||||||
elif alt_text and alt_text != "Image":
|
elif alt_text and alt_text != "Image":
|
||||||
# Use alt text as caption if no caption provided
|
# Use alt text as caption if no caption provided, but avoid usageHint format
|
||||||
|
if "Render as visual element:" in alt_text:
|
||||||
|
# Extract filename from usageHint if possible
|
||||||
|
parts = alt_text.split("Render as visual element:")
|
||||||
|
if len(parts) > 1:
|
||||||
|
filename = parts[1].strip()
|
||||||
|
caption_text = f"Figure: {filename}"
|
||||||
|
else:
|
||||||
|
caption_text = alt_text
|
||||||
|
else:
|
||||||
|
caption_text = f"Figure: {alt_text}"
|
||||||
captionStyle = self._createNormalStyle(styles)
|
captionStyle = self._createNormalStyle(styles)
|
||||||
captionStyle.fontSize = 10
|
captionStyle.fontSize = 10
|
||||||
captionStyle.textColor = self._hexToColor(styles.get("paragraph", {}).get("color", "#666666"))
|
captionStyle.textColor = self._hexToColor(styles.get("paragraph", {}).get("color", "#666666"))
|
||||||
elements.append(Paragraph(f"<i>Figure: {alt_text}</i>", captionStyle))
|
elements.append(Paragraph(f"<i>{caption_text}</i>", captionStyle))
|
||||||
|
|
||||||
return elements
|
return elements
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -628,7 +628,7 @@ class ContentGenerator:
|
||||||
"base64Data": imageDoc.get("base64Data"),
|
"base64Data": imageDoc.get("base64Data"),
|
||||||
"altText": altText,
|
"altText": altText,
|
||||||
"mimeType": mimeType,
|
"mimeType": mimeType,
|
||||||
"caption": section.get("metadata", {}).get("caption")
|
"caption": section.get("caption") or section.get("metadata", {}).get("caption")
|
||||||
}]
|
}]
|
||||||
|
|
||||||
logger.info(f"Successfully integrated image {imageRefId} for section {section.get('id')} (source={imageSource})")
|
logger.info(f"Successfully integrated image {imageRefId} for section {section.get('id')} (source={imageSource})")
|
||||||
|
|
@ -702,7 +702,7 @@ class ContentGenerator:
|
||||||
# Use image_prompt as alt text if generation_hint is generic
|
# Use image_prompt as alt text if generation_hint is generic
|
||||||
altText = section.get("image_prompt", "Image")[:100] # Limit length
|
altText = section.get("image_prompt", "Image")[:100] # Limit length
|
||||||
|
|
||||||
caption = section.get("metadata", {}).get("caption")
|
caption = section.get("caption") or section.get("metadata", {}).get("caption")
|
||||||
|
|
||||||
section["elements"] = [{
|
section["elements"] = [{
|
||||||
"url": f"data:image/png;base64,{base64Data}",
|
"url": f"data:image/png;base64,{base64Data}",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue