import sys

with open('tmp_home_current.html', 'r', encoding='utf-8') as f:
    html = f.read()

img_start = html.find('<!-- wp:group {"layout":{"type":"constrained","contentSize":"1100px"},"style":{"spacing":{"padding":{"top":"40px","bottom":"20px","left":"24px","right":"24px"}}}} -->')
if img_start == -1:
    print("Could not find image start")
    sys.exit(1)

img_end_str = '<!-- /wp:group -->'
img_end = html.find(img_end_str, img_start)
if img_end == -1:
    print("Could not find image end")
    sys.exit(1)

img_end += len(img_end_str)

img_block = html[img_start:img_end]

# remove it from html
html = html[:img_start] + html[img_end:]

# strip empty lines where the block was
html = '\n'.join([line for line in html.split('\n') if line.strip() != '' or '<' in line])

# modify img_block
new_img_block = img_block.replace('<!-- wp:image {"align":"center"} -->', '<!-- wp:image {"align":"center","width":"275px","className":"is-resized"} -->')
new_img_block = new_img_block.replace('<figure class="wp-block-image aligncenter">', '<figure class="wp-block-image aligncenter is-resized">')
new_img_block = new_img_block.replace('max-width:100%', 'width:275px;max-width:100%')

# Add it at the top
html = new_img_block + '\n\n' + html

# Now for the saying block
saying_marker = '<h2>What they\'re Saying</h2>'
saying_idx = html.find(saying_marker)

if saying_idx == -1:
    print("Could not find saying marker")
    sys.exit(1)

saying_start = html.rfind('<!-- wp:group', 0, saying_idx)
saying_end = html.find('<!-- wp:heading', saying_start)

saying_block_start = html[saying_start:saying_end]

new_saying_block_start = saying_block_start.replace('"bottom":"60px"', '"bottom":"0px"').replace('padding-bottom:60px', 'padding-bottom:0px')

html = html[:saying_start] + new_saying_block_start + html[saying_end:]

with open('tmp_home_current.html', 'w', encoding='utf-8') as f:
    f.write(html)

print("Success")
