Created
July 6, 2013 21:08
-
-
Save jsundram/5941300 to your computer and use it in GitHub Desktop.
Pull the links out of a google doc and format them for use in Markdown. Works on a file that google docs has exported to html.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from BeautifulSoup import BeautifulSoup, SoupStrainer | |
def use(link): | |
return link.get('href', None) and link.text and not link.text.startswith('http') | |
def file_as_string(filename): | |
with open(filename) as f: | |
return f.read() | |
def main(filename): | |
html = file_as_string(filename) | |
links = BeautifulSoup(html, parseOnlyThese=SoupStrainer('a')) | |
links = filter(use, links) | |
# print the link text | |
for i, link in enumerate(links): | |
print "[%s][%d]" % (link.text, i) | |
print "" | |
# print the index of links: | |
for i, link in enumerate(links): | |
try: | |
print "[%d]: %s" % (i, link['href']) | |
except Exception: | |
pass | |
if __name__ == '__main__': | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment