コスティキャンのゲーム論pdf変換スクリプト

そういえば、昔『コスティキャンのゲーム論』のpdf変換スクリプトを作ったので公開します。

これは何?

コスティキャンのゲーム論はゲームを作る人の間では有名な文章みたいです。ただ、このサイトが昔ながらのサイトで、brで刻まれまくっているので結構読みづらく感じました。そこで適当にpdfへの変換スクリプトを書いた…訳ですがなにこのスクリプト読めない…

どんな感じ?

使い方

Python2系Beautiful SoupTeXインスコします。
僕はどうやってやったか忘れましたがTeXに関してはインストーラを使えばいいのではないでしょうか。環境変数をたたくので多分インスコ後に再起動が必要です。PythonもPATHを通しておいてね!
適当にフォルダを作って、その中に適当な名前(test.pyでやりました。)で以下のスクリプトを保存してください。また、同じフォルダにsource.htmlという名前で『コスティキャンのゲーム論』のページをダウンロードしてください。
test.pyを実行してください。test.txtができると思います。(うまくいかなかったら作ったフォルダをカレントディレクトリにしてコマンドラインからやってくだしあ)
最後に、「platex test.txt」を実行して「test.dvi」をつくり、「dvipdfmx test.dvi」で「test.pdf」ができます。これで終わりです。

スクリプト本体

#encoding:cp932
import urllib2
from BeautifulSoup import Tag, NavigableString
from BeautifulSoup import BeautifulSoup
import codecs

def countBreaks(start_):
    cnt = 0
    if start_.__class__ == Tag and start_.name == 'br':
        cnt += 1
    else:
        return 0, start_
    while 1:
        if start_.nextSibling == None:
            break
        else:
            if start_.nextSibling.__class__ is Tag and start_.nextSibling.name == 'br':
                cnt += 1
                start_ = start_.nextSibling
            else:
                if start_.nextSibling == '\n':
                    start_ = start_.nextSibling
                else:
                    break
    return cnt, start_

def parseMain(start_, in3br=False):
    nonfirst = False
    secondhr = False
    inh5 = False
    while(1):
        if nonfirst:
            if start_.nextSibling == None:
                return
            else:
                start_ = start_.nextSibling
        else:
            nonfirst = True
        try:
            if start_.name == u'hr':
                if secondhr:
                    return
                else:
                    secondhr = True
        except AttributeError:
            pass
        if (start_.__class__ is Tag):
            if start_.name == 'br':
                brcnt, start_ = countBreaks(start_)
                if brcnt == 1:
                    yield "", start_
                elif brcnt == 2:
                    yield "\n\n", start_
                elif brcnt == 3:
                    if in3br:
                        print "out 3br"
                        return
                    else:
                        if (start_.nextSibling == None or start_.nextSibling == '\n') or unicode(start_.nextSibling).find(u"さて") != -1:
                            yield "", start_
                        else:
                            yield "\\begin{screen}", start_
                            for i in parseMain(start_, True):
                                start_ = i[1]
                                yield i[0], start_
                            if start_.nextSibling != None:
                                start_ = start_.nextSibling
                            start_ = countBreaks(start_)[1]
                            yield "\\end{screen}", start_
            elif start_.name == 'h4':
                str = unicode(start_.contents[0].string)[0:]
                str = str.replace(u"・", u"")
                yield "\\section{" + str+ "}\n", start_
                inh5 = False
            elif start_.name == 'p':
                for i in parseMain(start_.contents[0]):
                    start_ = i[1]
                    yield i[0], start_
                start_ = start_.parent
            elif start_.name == 'address':
                for i in parseMain(start_.contents[0]):
                    start_ = i[1]
                    yield i[0], start_
                start_ = start_.parent
            elif start_.name == 'strong':
                for i in parseMain(start_.contents[0]):
                    start_ = i[1]
                    yield i[0], start_
                start_ = start_.parent
            elif start_.name == 'span':
                yield unicode(start_.contents[0].string)[0:], start_
            elif start_.name == 'h5':
                if inh5:
                    str = unicode(start_.contents[0].string)[0:]
                    str = str.replace(u"・", u"")
                    yield "\\subsubsection{" + str+ "}\n", start_
                else:
                    str = unicode(start_.contents[0].string)[0:]
                    str = str.replace(u"・", u"")
                    yield "\\subsection{" + str + "}\n", start_
                    inh5 = True
            elif start_.name == 'em':
                yield "\\Conclusion{", start_
                for i in parseMain(start_.contents[0]):
                    start_ = i[1]
                    yield i[0], start_
                yield "}", start_
                start_ = start_.parent
            else:
                pass
        else:
            if start_.string != None:
                str = unicode(start_.string)
                str = str.replace("\n", "")
                str = str.replace("&amp", "")
                str = str.replace(u"→", "")
                yield str, start_
            else:
                yield "", start_
    return

f = codecs.open("source.html", "r")
page = "\n".join(f.readlines())
page = page.replace(" ", "")
f.close()

soup = BeautifulSoup(page)
body = soup.html.body
f = codecs.open("test.txt", "w", "cp932")
header = u"""
\\documentclass{jsarticle}
\\usepackage{ascmac}
\\newcommand{\\Conclusion}[1]{{\\large{→#1}}}
\\begin{document}
\\title{コスティキャンのゲーム論\\\\
{\\small 言葉ではなく、デザインのみが、ゲームを語ってくれる}}
\\date{}
\\maketitle
\\setcounter{tocdepth}{3}
\\tableofcontents

"""
f.write(header)
for i in parseMain(body.findAll('hr')[1]):
    f.write(i[0])
footer = """
\\end{document}
"""
f.write(footer)
f.close()

汚くないですか?

ごめん

unicode(start_.nextSibling).find(u"さて") != -1とかないですよね

ごめん

分からないことあったら

聞いてね!