#!/usr/bin/python
#  $HeadURL$ $LastChangedRevision$

import sys
import re

url_regex = r'(?:mailto|http|https)://(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&//=]*)'

def main():
    while True:
        line = sys.stdin.readline()
        if not line:
            break
        sys.stdout.write(processline(line.rstrip()) + '\n')

def processline(inline):
   outline = ''
   state = 'n'
   while True:
       if len(inline) == 0:
           break

       #  Leading spaces
       m = re.search(r'^( +)', inline)
       if m:
           outline += m.group(1)
           inline = inline[len(m.group(1)):]
           continue
    
       #  Check for bold at beginning
       m = re.search(r'^((?:(.)\2)+)', inline)
       if m:
           (html, state) = switch_if_needed('b', state)
           outline += html
           outline += fix_gtlt_and_urls(''.join([ c for (i,c) in enumerate(m.group(1)) if (i%3)==0 ]))
           inline = inline[len(m.group(1)):]
           continue
    
       #  Check for italic at beginning
       m = re.search(r'^((?:_.)+)', inline)
       if m:
           (html, state) = switch_if_needed('i', state)
           outline += html
           outline += fix_gtlt_and_urls(''.join([ c for (i,c) in enumerate(m.group(1)) if i%3==2 ]))
           inline = inline[len(m.group(1)):]
           continue

       #  Other (not spaces and not something-immediately-followed-by-ctrl-h)
       m = re.search(r'^(.+?)(?: |.|$)', inline)
       if m:
           (html, state) = switch_if_needed('n', state)
           outline += html
           outline += fix_gtlt_and_urls(m.group(1))
           inline = inline[len(m.group(1)):]
           continue

   (html, state) = switch_if_needed('n', state)
   outline += html

   return outline

def fix_gtlt_and_urls(s):
    global url_regex

    s = re.sub('<', '&lt;', s)
    s = re.sub('>', '&gt;', s)
    s = re.sub('(%s)' % (url_regex), r'<a href="\1">\1</a>', s)

    return s

def switch_if_needed(new_state, old_state):
    html = ''
    if new_state == old_state:
        pass
    else:
        if old_state == 'i':
            html += '</i>'
        elif old_state == 'b':
            html += '</b>'
        if new_state == 'i':
            html += '<i>'
        elif new_state == 'b':
            html += '<b>'
    return(html, new_state)

main()
