语法高亮工具Pygments

天天写代码, 天天看代码,没有颜色的代码看久了自然会眼花,于是各种高亮工具诞生了。基本上每个IDE都集成了代码语法高亮着色器,在Web应用里面,代码着色可以在用户端用JavaScript实现也可以在服务器端实现,PHP实现的着色工具最多,可惜俺不会,找啊找啊找到了这款神奇的代码语法着色器Pygments,支持很多语言(有一多半语言我都只听说过),俺够用了,而且是Python写的,方便偶在GAE里面折腾。
官方地址:http://pygments.org/

Pygments is a generic syntax highlighter for general use in all kinds of software such as forum systems, wikis or other applications that need to prettify source code.

偶继续折腾GAE
“main.py”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
 
import wsgiref.handlers
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
 
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
 
class MainHandler(webapp.RequestHandler):
  def get(self):
	template_values = {
        }
	path = os.path.join(os.path.dirname(__file__), 'index.html')
	self.response.out.write(template.render(path, template_values))
  def post(self):
	code = self.request.get('content')
	template_values = {
            'codehtml': highlight(code, PythonLexer(), HtmlFormatter(linenos='inline',linenospecial=5)),
            'style':HtmlFormatter().get_style_defs('.highlight')
            }
	path = os.path.join(os.path.dirname(__file__), 'index.html')
	self.response.out.write(template.render(path, template_values))
 
def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)
 
 
if __name__ == '__main__':
  main()

模板文件“index.html”

1
2
3
4
5
6
7
8
9
10
11
12
<html>
  <body>
    <form action="/" method="post">
      <div><textarea name="content" rows="3" cols="60"></textarea></div>
		{{ codehtml }}
      <div><input type="submit" value="Sign Guestbook"></div>
    </form>
	<style type="text/css">
		{{ style}}
	</style>
  </body>
</html>

-EOF-

在GAE中使用reCAPTCHA

Google App Engine是好东东,容易实现一些基于Web的小想法;reCAPTCHA也是好东西,大家都一起来防SPAM。而我就这么一点小乐趣,东搞搞西搞搞。
在GAE上面使用reCAPTCHA也很容易,并且已经有人写出来了具体怎么做,偶也依样画葫芦写了个演示,没有什么实际的功能,纯属瞎折腾看看自己验证码输对了没。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
import wsgiref.handlers
 
import os
from os import environ
from recaptcha.client import captcha
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
 
class MainHandler(webapp.RequestHandler):
  def get(self):
	chtml = captcha.displayhtml(
		public_key = "public_key",
		use_ssl = False,
		error = None)
	template_values = {
	'captchahtml': chtml
        }
    #self.response.out.write('Hello world!')
	path = os.path.join(os.path.dirname(__file__), 'index.html')
	self.response.out.write(template.render(path, template_values))
  def post(self):
	challenge = self.request.get('recaptcha_challenge_field')
	response  = self.request.get('recaptcha_response_field')
	remoteip  = environ['REMOTE_ADDR']
	cResponse = captcha.submit(
                    challenge,
                    response,
                    "PRIVATE-KEY",
                    remoteip)
	if cResponse.is_valid:
          # response was valid
          # other stuff goes here
          template_values = {
            'hello': "Hello world!"
            }
        else:
          error = cResponse.error_code
          chtml = captcha.displayhtml(
			public_key = "public_key",
			use_ssl = False, 
			error = cResponse.error_code)
          template_values = {
            'captchahtml': chtml
            }
 
	path = os.path.join(os.path.dirname(__file__), 'index.html')
	self.response.out.write(template.render(path, template_values))
 
 
def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)
 
 
if __name__ == '__main__':
  main()

-EOF-

Godaddy $0.99 优惠码

近日Godaddy又释放出了一个$0.99的优惠码“99BUYCOM”,有效时间为前5000名使用者或者5月1日前以先到者为准,可以用户新注册或者转移.COM, .US, .MOBI, .BIZ, .NET 及 .ORG域名,必须使用信用卡付款。
-EOF-

GAE开始支持Java

去年的今天(2008年4月8日)Google发布了统一开发平台App Engine,一年一之后的今天,Google开始开放Java支持,众说纷纭的对Java的支持即将揭开面纱。
登录GAE后台后有一行提醒问是否试用Java支持,并且下面会要求接受新的协议,不论是否参与用Java支持,新的协议都是必须同意的。

在点击“Learn More”,就会Java支持试用申请界面(点击“Dismiss”则忽略该信息)

点击“Sign me up!”即进入等待开通队列,预计近日Google便会正式开通正式的支持。

PS:刚去官网看了一下,此次会先开放先到的10,000名报名者,想尝鲜的盆友们抓紧时间吧。
-EOF-