gm


In [7]:
#! /usr/bin/python

import dominate # 'pip install dominate' must be run once before this
from dominate.tags import *
import re # Only needed because dominate doesn't let us set tab indentation
import tempfile
import webbrowser

def generate_html(title, paragraphs):
    page = dominate.document(title=title)
    with page:
        for paragraph in paragraphs:
            p(paragraph)
    return str(page)

def read_paragraphs():
    to_return = {'title': '', 'paragraphs': []}
    to_return['title'] = raw_input("Enter a title:\n")
    print "Enter your paragraph.  Enter a blank line to finish."
    for line in iter(raw_input, ''):
        to_return['paragraphs'].append(line)
    return to_return

if __name__ == '__main__':
    user_input = read_paragraphs()
    html_output = generate_html(user_input['title'], user_input['paragraphs'])
    with open('test.html', 'w') as out:
        # HACK: This is silly, but dominate doesn't provide a way to change the
        # number of spaces used for a tab, but since it gives us exactly half
        # as many as we want, we can double-up on spaces at start of line and
        # get what we want
        out.write(re.sub(r'^\s*', r'\1\1', html_output, flags=re.MULTILINE))
    webbrowser.open(out.name)


Enter a title:
this is a test
Enter your paragraph.  Enter a blank line to finish.
a new paragraph

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-7-74882226ce7c> in <module>()
     30         # as many as we want, we can double-up on spaces at start of line and
     31         # get what we want
---> 32         out.write(re.sub(r'^\s*', r'\1\1', html_output, flags=re.MULTILINE))
     33     webbrowser.open(out.name)

/usr/lib/python2.7/re.pyc in sub(pattern, repl, string, count, flags)
    149     a callable, it's passed the match object and must return
    150     a replacement string to be used."""
--> 151     return _compile(pattern, flags).sub(repl, string, count)
    152 
    153 def subn(pattern, repl, string, count=0, flags=0):

/usr/lib/python2.7/re.pyc in filter(match, template)
    273         return template[1][0]
    274     def filter(match, template=template):
--> 275         return sre_parse.expand_template(template, match)
    276     return filter
    277 

/usr/lib/python2.7/sre_parse.pyc in expand_template(template, match)
    787                 raise error, "unmatched group"
    788     except IndexError:
--> 789         raise error, "invalid group reference"
    790     return sep.join(literals)

error: invalid group reference

In [8]:
print dominate.tags.html(body(p('hello world')))


<html>
  <body>
    <p>hello world</p>
  </body>
</html>

In [5]:
list = ul()
for item in range(4):
    list += li('Item #', item)
print list


<ul>
  <li>Item #0
  </li>
  <li>Item #1
  </li>
  <li>Item #2
  </li>
  <li>Item #3
  </li>
</ul>

In [ ]:
ophtm = open('test.html', 'r')
ophtm.read()

In [ ]: