Skip to content

Commit

Permalink
Set image alignment from style attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
brylie committed Jul 2, 2023
1 parent 0c280b7 commit 863e619
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions content_migration/management/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ class GenericFormattedImageBlock:
link_url: str | None


def create_image_block_from_url(image_url: str, link_url: str | None = None) -> dict:
def create_image_block_from_url(
image_url: str,
link_url: str | None = None,
align: str | None = None,
) -> dict:
"""Create a Wagtial image block from an image URL."""

try:
Expand Down Expand Up @@ -159,7 +163,7 @@ def create_image_block_from_url(image_url: str, link_url: str | None = None) ->
image_chooser_block = {
"image": image,
"width": DEFAULT_IMAGE_WIDTH,
"align": DEFAULT_IMAGE_ALIGN,
"align": align,
"link": link_url,
}

Expand All @@ -183,10 +187,16 @@ def create_block(generic_block: GenericBlock) -> tuple[str, str | dict]:
if generic_block.block_content["link"] is not None # type: ignore
else None
)
align: str = (
generic_block.block_content["align"] # type: ignore
if generic_block.block_content["align"] is not None # type: ignore
else DEFAULT_IMAGE_ALIGN
)
try:
image_block = create_image_block_from_url(
image_url=image_url,
link_url=link_url, # type: ignore
align=align, # type: ignore
)
except requests.exceptions.MissingSchema:
raise BlockFactoryError("Invalid image URL: missing schema")
Expand Down Expand Up @@ -225,6 +235,17 @@ def remove_pullquote_tags(item_string: str) -> str:
return str(soup)


def get_image_align_from_style(style_string: str) -> str | None:
"""Get the image alignment from the style string."""

if "float:left" in style_string or "float: left" in style_string:
return "left"
elif "float:right" in style_string or "float: right" in style_string:
return "right"
else:
return DEFAULT_IMAGE_ALIGN


def adapt_html_to_generic_blocks(html_string: str) -> list[GenericBlock]:
"""Adapt HTML string to a list of generic blocks."""

Expand Down Expand Up @@ -293,6 +314,13 @@ def adapt_html_to_generic_blocks(html_string: str) -> list[GenericBlock]:
image_url = image_tag["src"]
image_url = ensure_absolute_url(image_url)

# get image alignment from style attribute float property
if "style" in image_tag.attrs:
image_style = image_tag["style"]
image_align = get_image_align_from_style(image_style)
else:
image_align = DEFAULT_IMAGE_ALIGN

# make sure the URL contains westernfriend.org
if "westernfriend.org" not in image_url:
raise ValueError(
Expand All @@ -308,6 +336,7 @@ def adapt_html_to_generic_blocks(html_string: str) -> list[GenericBlock]:
image_chooser_block_content = {
"image": image_url,
"link": image_link_url,
"align": image_align,
}

generic_blocks.append(
Expand Down

0 comments on commit 863e619

Please sign in to comment.