1
0

update_from_readme.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python3
  2. from bs4 import BeautifulSoup
  3. from markdown import markdown
  4. import fileinput
  5. import re
  6. def ContentToIdValue( content ):
  7. content = content.replace( ' ', '-' )
  8. return re.sub( r'[^\w_-]+', '', content ).lower()
  9. def AddIdsForHeadings( soup ):
  10. def AddIds( headings ):
  11. for heading in headings:
  12. heading[ 'id' ] = ContentToIdValue( heading.get_text() )
  13. for i in range( 1, 7 ):
  14. AddIds( soup.find_all( 'h' + str( i ) ) )
  15. return soup
  16. markdown_lines = list( fileinput.input( mode = 'rb' ) )
  17. # We delete the first two lines because that's the big YCM heading which we
  18. # already have on the page
  19. del markdown_lines[ : 2 ]
  20. markdown_source = b''.join( markdown_lines )
  21. with open( 'index.html', 'r+b' ) as content_file:
  22. content = content_file.read()
  23. new_contents = markdown( markdown_source.decode( 'utf8' ),
  24. extensions = [ 'fenced_code' ] )
  25. new_tags = AddIdsForHeadings( BeautifulSoup( new_contents, 'html5lib' ) )
  26. soup = BeautifulSoup( content.decode( 'utf8' ), 'html5lib' )
  27. elem = soup.find( id = 'markdown-output' )
  28. elem.clear()
  29. for new_elem in new_tags.body.contents:
  30. elem.append( new_elem )
  31. content_file.seek( 0 )
  32. content_file.truncate()
  33. content_file.write( str( soup ).encode( 'utf8' ) )