MakeBookmarksFile

By Hal Canary, 2009-09-24 12:51:50 (link)
#computers-code

#!/usr/bin/env python
# MakeBookmarksFile - convert a text file containing
# URLs into a HTMl file with clickable links.
#
# Copyright 2008 Hal Canary
#
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and
# associated documentation files (the 'Software'), to
# deal in the Software without restriction, including
# without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the
# following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF
# ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
# EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import sys,re
fi = sys.stdin
fo = sys.stdout
s=fi.readline().strip()
## The first line of the input is a title.
head='<!DOCTYPE html PUBLIC \
"-//W3C//DTD HTML 4.01//EN" \
"http://www.w3.org/TR/html4/strict.dtd">\n\
<html><head>\n<meta http-equiv="Content-Type" \
content="text/html; charset=utf-8">\n\
<title>%s</title>\n</head><body><div>\n\
<h1>%s</h1>\n'
fo.write(head % (s,s))
for line in fi:
  s=line.strip()
  if (s == "") :
    continue
  ## convert & to &amp; and so on.
  s=re.sub('&','&amp;',s)
  s=re.sub('<','&lt;',s)
  s=re.sub('>','&gt;',s)
  s=re.sub('"','&quot;',s)
  fo.write("<a href=\"%s\">%s</a><br>\n" % (s,s))
fo.write('</div></body></html>\n')

(back)