- 当方のmire.htmモジュールの導入
- Webサーバの導入と.pyでのcgi実行を可能な様設定が、許可する拡張子への変更
- 本ソースコードファイルのCGIフォルダへの投入とグループ所有権それに実行権の設定
HTML CODE_TEXT for BLOG 0.0.2 (C) 2009 Mire GPL license - Mozilla Firefox
このテキストエリアのフォーム内にHTMLタグを含むソースコードをいれ「実行」ボタンをクリックすると、別タブまたは別窓の、同じテキストエリアのフォーム内に、変換されたテキストが収められることになる。これをコピー&貼付けで活用する仕組みだ。尚、貼り付ける返還後のソースそのものを表示したい時には、もう一度実行すれば、欲する変換が出来る。
このツールの変換作業は、文字列メソッドのreplace()を使い「%lt;文字列オブジェクト>.replace('&','&').replace('<','<').replace('>','>').replace('&','&')」で、処理しているだけの代物でPythonらしい超楽ちんな思いをしている。この逆の変換も同様に作れるだろう。
http://127.0.0.1:18296/cgi-bin/html4blog2.py| HTMLのタグをそのまま表示するものに変換します。 |
| |
D:/PythonCGI/cgi-bin/ html4blog.py
#!c:/Python26/python
# -*- coding: UTF8 -*-
# ※ Windows の場合1行目はdummyです
import cgitb; cgitb.enable()
u"""
#####################################################################
# HTML CODE_TEXT for BLOG 0.0.2 (C) Mire 2010-05-14 #
#####################################################################
blog等にHTML文内にHTMLタグを表記したい場合の文字変換を行ないます
USEGE:
1.HTMLタグを含むテキストをテキスト欄に貼付けます。
2.ボタンクリックで別窓または別タブに変換後のテキストが表示されます。
3.このテキストをBlog等にCopy&Paseteして利用します。
【更新履歴】
* 2009-10-15 多分初版 同名のフィルタツールをCGI上ツール化
元々、ERROR処理
* 2009-10-27 00:14 画面レイアウト等を変更した模様
* 2010-05-14 公開分が古いことに気付いたので履歴を書き公開。
"""
__author__ = "Mire in Japan"
__version__ = '0.0.2'
__copyright__ = 'Copyright (c) 2009 Mire'
__license__ = 'GPL'
__url__ = 'http://pythonlife.seesaa.net/article/149895777.html'
debug = 0 #debugレベル
########################################
## 当Tool専用関数の定義contents()のみ ##
########################################
def contents(debug=1):
u"""
コンテンツ「入出力フォーム」を表示します
"""
try:
from mire.htm import display_err, cgi_params #, tag_encode
print u'<table width="590px" style="background-color:#F0F0F0;"><tr>'
print u'<td><font size="-1"><b>HTMLのタグをそのまま表示するものに変換します。</b>\
</font></td></tr><tr><td align="center">',
prms=cgi_params() #> mire.htm.cgi_params(): CGI引数情報の辞書を返す
if debug > 1: #debugレベル2: CGI引数の名前と値を表示
print '<table><tr><td align="right"><pre align="left" style="text-align:center;\
white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap;\
white-space: -o-pre-wrap; word-wrap: break-word; ">'
for p in prms:
print '%s: %s' % (repr(p),repr(prms[p]).replace('&','&')\
.replace('<','<')\
.replace('>','>')\
.replace('&','&'))
print '</pre></td></tr></table>'
if 'html_text' in prms: #cgi引数が存在したら、
try:
html_text=unicode(prms['html_text'],'sjis') # CGIの引数の値はunicode化のこと
except:
html_text=unicode(prms['html_text'],'utf8')
ss = html_text.replace(u'&',u'&').replace(u'<',u'<')\
.replace(u'>',u'>').replace(u'&',u'&') #最後の&の変換がミソ!!
html_text=ss #html_textを置換え
else: #引数がないなら
html_text='' #0長文字列
#コンテンツ本体: table,form textarea,inputタグで入出力画面を表示
print u'<table><form method="post" target="_blank"><tr>'
print u'<td><textarea cols="88" rows="20" style="font-size:8pt;" name="html_text"',
print u'mire_val="html4blog_text(html_text)">%s</textarea></td>' % (html_text)
print u'</tr><tr><td><pre>%s<pre></td>' % (html_text.replace(u'&',u'&'))
print u'</tr><tr><td><center>',
print u'<input type="submit" style="font-size:8pt; width:567px">',
print u'</td></tr></form></table></center></td></tr></table>'
except: #vividな作成部分はhtm.display_err()
if debug > 0: #でdebug情報を取得。運用時はdebug=0
print '<div align="left">' #とし障害内容は表示させない。
display_err(locals()) #>mire.htm.display_err()#Web用の例外処理で障害切分
print '</div'
else: #運用中のdebug message表示はセキュリティ上不適格。通常はdebug=0で以下のmessage
print '<div align="left">Error was occured.<br><br>Please,',
print 'contact your system administrators.<br><br>'
print 'You? Do Your Best!<br><br> Test html4spam.py with debug=2. Good luck.</div>'
########################################
## ##
## 処理実行開始位置 ##
## ##
########################################
try:
from mire.htm import display_err, html_header, html_footer
title = u'HTML CODE_TEXT for BLOG 0.0.2 (C) 2009-2010 Mire GPL license'
body = ['style="right-margin:0px; background-color:#808080;"']
html_header(charset='UTF-8', title=title , body=body)
contents(debug=debug)
html_footer()
except:
if debug > 0:
display_err(locals()) #こちらは最後の抑え
else: #運用中のdebug message表示はセキュリティ上不適格。通常はdebug=0で以下のmessage
print 'Content-type: text/html\n\n<html>\n<head></head><body>'
print '<div>Error was occured.\nPlease, contact your system administrators.<br><br>'
print 'You? Do Your Best!<br><br> Test html4spam.py with debug=2.\n\nGood luck.</div>'
print '</body></html>'
タグ:Python
【Pythonプログラミングの最新記事】
- UUNZIP-0.0.6a Instal..
- Python ScriptをLinuxコ..
- 【Linux】文字化けで解凍不能なZIP..
- 【Linux】文字化けで解凍不能なZIP..
- 【Linux】文字化けで解凍不能なZIP..
- 【Unicode】半角カナ全角かな置替関..
- 文書内の全数字を算用数字表現: all2..
- 辞書指定で複数置替え可能なreplace..
- 【Unicode】半角カナ全角かな置替関..
- 【Unicode】PythonでのUni..
- 【数学】順列出力関数の作成(7)〜下桁の..
- 【数学】順列出力関数の作成(6)〜最終要..
- 【数学】順列出力関数の作成(5)〜固定長..
- 【数学】順列出力関数の作成(4)〜TAB..
- 【数学】順列出力関数の作成(3)〜文字列..
- 【数学】順列出力関数の作成(2)〜tup..
- 【数学】順列出力関数の作成(1)〜再帰関..
- 或る無能なPythonistaのJava..
- Python文字コード作法 〜 Free..
- 【Pythonic化】POST METH..










