#!/usr/bin/python # """2ch text - HTML converter. """ # # Copyright (c) 2005 Satoshi Fukutomi . # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # import re import sys import fileinput def res_anchor(id): return ('') % (id, id) def escape(msg): msg = msg.replace("&", "&") msg = re.sub(r"&(#\d+|#[Xx][0-9A-Fa-f]+|[A-Za-z0-9]+);", r"&\1;", msg) msg = msg.replace("<", "<") msg = msg.replace(">", ">") msg = msg.replace("\r", "") return msg def format(s): s = s.replace("\n", "
\n") s = re.sub(r"|<\/a>", "", s) s = re.sub(r"(>>|>>|\201\204|\201\164|\201\342|>)(\d+)", "%s%s
" % (res_anchor(r"\2"), r"\g<0>"), s) s = re.sub(r"https?://[\041-\073\075-\177]{2,}", r'\g<0>', s) s = re.sub(r"([^h])(ttps?://[\041-\073\075-\177]{2,})", r'\1\2', s) return s isdt = re.compile(r"^(\d+)").search i = "" buf = "" sys.stdout.write('
\n') for line in fileinput.input(): found = isdt(line) line = escape(line) if found: if i: sys.stdout.write(format(buf.strip())) sys.stdout.write('\n') buf = "" i = found.group(1) sys.stdout.write('
%s
\n' % (i, line.strip())) sys.stdout.write('
' % i) elif i: buf += line sys.stdout.write(format(buf.strip())) sys.stdout.write('
\n') sys.stdout.write('
\n')