Skip to content

Instantly share code, notes, and snippets.

@dwurf
Created September 6, 2020 05:45
Show Gist options
  • Save dwurf/95cdfdf5cd4d1890c8526cea2423cfeb to your computer and use it in GitHub Desktop.
Save dwurf/95cdfdf5cd4d1890c8526cea2423cfeb to your computer and use it in GitHub Desktop.
Half a submission for the pycon 2020 Rube Codeberg challenge - unfinished
Display the source blob
Display the rendered blob
Raw
<svg height="200" width="200" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink"><text fill="green" x="30" y="100">Hello World! 🦄<animateTransform attributeName="transform" attributeType="XML" begin="0s" dur="10s" from="0" repeatCount="indefinite" to="360" type="rotate"></animateTransform></text></svg>
from bs4 import BeautifulSoup
soup = BeautifulSoup(
'''<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events"><text /></svg>''',
features='html.parser'
)
get_text = lambda: soup.svg.find('text')
def append_string(c):
text = get_text()
text.string = text.string + c if text.string else c
if __name__ == '__main__':
# Set size
soup.svg['height'] = 200
soup.svg['width'] = 200
# Set text origin
get_text()['x'] = 30
get_text()['y'] = 100
get_text()['fill'] = 'green'
# Set content. Might as well write it character by character
for c in 'Hello World! 🦄': append_string(c)
# Set animation
animation_tag = soup.new_tag('animateTransform')
animation_tag['attributeName'] = "transform"
animation_tag['attributeType'] = "XML"
animation_tag['type'] = "rotate"
animation_tag['from'] = "0"
animation_tag['to'] = "360"
animation_tag['begin'] = "0s"
animation_tag['dur'] = "10s"
animation_tag['repeatCount'] = "indefinite"
get_text().append(animation_tag)
# Write
with open('hello.svg', 'wb') as f: f.write(soup.encode())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment