Liu 的个人资料唯有仰望是真实的照片日志列表更多 工具 帮助

日志


2007/9/30

javascript beautifier in ruby

更新一下,elfz已经通过无所不能的google看到这篇了,并在他的页面上放上了此帖的链接:P

做web的小朋友们总会看到各种网站他们的很酷的js效果,但是打开我们亲爱的firebug或者webdeveloper show javascritps, 看到是jsmin之后的一行很长的无法阅读的东东。

在网上找到这个家伙的网站http://elfz.laacz.lv/beautify/,提供在线的把jsmin之后一行的js反向格式化成可以阅读的样子。作者提供了php的源代码http://elfz.laacz.lv/beautify/beautify.phps

于是我想将程序员的懒惰本性发挥的更多一些,是否能够把这个东东集成到我们亲爱的webdeveloper show javascripts或者最近小靓身影的YSlow里面去呢, 于是我找来webdeveloper的代码,挺好,只要把beautifier翻译成js的就可以嵌入进去,哈哈,到时看我看到每个开心的网站就能省很大力气去看他们的js代码了。

当然这个只是重新格式化代码,如果网站不只是压缩了js代码,而且进行了混淆,那么这个工具就无能为力了,你就格式话之后看着一个个没意义的变量猜吧,或者人家不让我们看,我们就不看好了。

在翻译成js版本之前,为了活跃一下气氛,最近写代码也不多,大多是琐碎的事情,于是乎觉得每天不写他几百行代码就tmd不爽,先翻译一个ruby的版本,熟练一下ruby的一些用法,不如之前不知道的begin-end-while来伪装do-while, 和unicode的正则匹配,刚跑了一下测试的代码,和原作者网站上的在线服务测试那个autocomplete.jsa good example结果一样了,小开心一下,排解一下空荡荡房子无所事事的无聊情绪。js版本的js_beautifier.js和嵌入webdeveloper随后奉上。

原作者elfz老大的原话,我quote说:

“This script was intended to be useful to explore the scripts compacted in one line (CAPXOUS autocomplete, recently renamed to CreateWebApp for some stupid reason, is a good example). That's what I wrote it for—all the other beautifiers really sucked. As the time went, I improved to suit your pretty-formatting javascript needs better.”

=======================我是code monkey的分界线======================

#!/usr/bin/env ruby
$IN_EXPR = 1
$IN_BLOCK = 2

$TK_UNKNOWN = 3
$TK_WHITESPACE = 4
$TK_WORD = 5
$TK_START_EXPR = 6
$TK_END_EXPR = 7
$TK_START_BLOCK = 8
$TK_END_BLOCK = 9
$TK_END_COMMAND = 10
$TK_EOF = 11
$TK_STRING = 12

$TK_BLOCK_COMMENT = 13
$TK_COMMENT = 14

$TK_PUNCT = 15

# internal flags
$PRINT_NONE = 16
$PRINT_SPACE = 17
$PRINT_NL = 18

def js_beautify(js_source_text)
  $output = $token_text = $lastword = ''
  $input = prepare_utf(js_source_text)
  $input_length = $input.length
  lastword = ''

  # words which should always start on new line.
  # simple hack for cases when lines aren't ending with semicolon.
  # feel free to tell me about the ones that need to be added.
  line_starters = 'var,if,switch,case,default,for,while,break'.split(",")

  # states showing if we are currently in expression (i.e. "if" case) - IN_EXPR, or in usual block (like, procedure), IN_BLOCK.
  # some formatting depends on that.
  $in = $IN_BLOCK
  $ins = [$in,]
  $indent = 0

  $pos = 0

  $lasttok = $TK_EOF

  while true
    $token_text, token_type = get_next_token
    break if token_type == $TK_EOF

    case token_type
    when $TK_START_EXPR
      in_context($IN_EXPR)
      if $lasttok == $TK_END_EXPR
        nl() if $token_text != '['
      elsif not [$TK_WORD, $TK_START_EXPR, $TK_PUNCT].include? $lasttok
        space
      elsif line_starters.include? $token_text
        space
      end
      token
    when $TK_END_EXPR
      token
      in_pop
    when $TK_START_BLOCK
      in_context($IN_BLOCK)
      space if $lasttok != $TK_PUNCT
      token
      indent
    when $TK_END_BLOCK
      if $lasttok == $TK_END_EXPR
        nl
        unindent
      elsif $lasttok == $TK_END_BLOCK
        unindent
        nl
      elsif $lasttok == $TK_START_BLOCK
        #nothing
        unindent
      else
        unindent
        nl
      end
      token
      in_pop
    when $TK_WORD
      prefix = $PRINT_NONE
      if $lasttok == $TK_END_BLOCK
        if $token_text.downcase != 'else'
          prefix = $PRINT_NL
        else
          prefix = $PRINT_SPACE
          space
        end
      elsif $lasttok == $TK_END_COMMAND and $in == $IN_BLOCK
        prefix = $PRINT_NL
      elsif $lasttok == $TK_END_COMMAND and $in == $IN_EXPR
        prefix = $PRINT_SPACE
      elsif $lasttok == $TK_WORD
        prefix = $PRINT_SPACE
      elsif $lasttok == $TK_START_BLOCK
        prefix = $PRINT_NL
      elsif $lasttok == $TK_END_EXPR
        $indent += 1
        nl
        $indent -= 1
      end

      if line_starters.include? $token_text or prefix == $PRINT_NL
        nl
      elsif prefix == $PRINT_SPACE
        space
      end
      token

    when $TK_END_COMMAND
      token
    when $TK_STRING
      if $lasttok == $TK_START_BLOCK
        nl
      elsif $lasttok == $TK_WORD
        space
      end
      token
    when $TK_PUNCT
      if $token_text == ','
        if $in == $IN_EXPR
          token
          space
        else
          token
          nl
        end
      else
        start_delim = true
        end_delim = true
        if $lasttok == $TK_PUNCT
          start_delim = false
          end_delim = false
        elsif $token_text == '.'
          start_delim = false
          end_delim = false
        elsif $token_text == ':'
          start_delim = false
        elsif $lasttok == $TK_WORD
        end

        space if start_delim
        token
        space if end_delim
      end
    when $TK_BLOCK_COMMENT
      nl if $lasttok != $TK_EOF # was echo "\n"
      token
    when $TK_COMMENT
      nl if $lasttok != $TK_COMMENT and $lasttok != $TK_EOF
      token
    when $TK_UNKNOWN
      token
    end

    $lasttok = token_type
    $lastword = $token_text.downcase
  end
  $output
end

def prepare_utf(str)
  str.scan(/.|\s/u)
end

def nl(ignore_repeated = true)
  if ignore_repeated
    if $output and $output[-1] == "\n"
      return
    end
  end
  $output += "\n" + "    "*$indent
end

def space
  $output += " "
end

def token
  $output += $token_text
end

def indent
  $indent += 1
end

def unindent
  $indent -= 1 if $indent
end

def in_context(where)
  $ins << $in
  $in = where
end

def in_pop
  $in = $ins.pop
end

def make_array(str)
  str.scan(/.|\s/u)
end

def get_next_token
  $whitespace ||= make_array("\n\r\t ")
  $wordchar ||= make_array('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$')
  $punct ||= make_array(".,=?:*&%^+-*<>!|")
  num_newlines = 0
  begin
    return ['', $TK_EOF] if $pos >= $input_length
    c = $input[$pos]
    $pos += 1
    num_newlines += 1 if c == "\r" or c == "\n"
  end while $whitespace.include? c

  if num_newlines
    num_newlines.times do
      nl(false)
    end
  end
  if $wordchar.include? c
    if $pos < $input_length
      while $wordchar.include? $input[$pos]
        c += $input[$pos]
        $pos += 1
        break if $pos == $input_length
      end
    end
    return [c, $TK_WORD]
  end

  return [c, $TK_START_EXPR] if c == '(' or c == '['
  return [c, $TK_END_EXPR] if c == ')' or c == ']'
  return [c, $TK_START_BLOCK] if c == '{'
  return [c, $TK_END_BLOCK] if c == '}'
  return [c, $TK_END_COMMAND] if c == ';'

  if c == '/'
    # peak for comment /* ... */
    if $input[$pos] == '*'
      comment = ''
      $pos += 1
      if $pos < $input_length
        while not ($input[$pos] == '*' and $input[$pos+1] and $input[$pos+1] and $pos < $input_length)
          comment += $input[$pos]
          $pos += 1
          break if $pos >= $input_length
        end
      end
      $pos += 2
      return ["/*#{comment}*/", $TK_BLOCK_COMMENT]
    end

    # peek for comment // ...
    if $input[$pos] == '/'
      comment = c
      while $input[$pos] != "\r" and $input[$pos] != "\n"
        comment += $input[$pos]
        $pos += 1
        break if $pos >= $input_length
      end
      $pos += 1
      return [comment, $TK_COMMENT]
    end
  end

  if (c == "'" or #string
      c == '"' or #string
      (c == '/' and ($lasttok == $TK_START_EXPR or $lasttok == $TK_PUNCT or $lasttok == $TK_EOF or $lasttok == $TK_END_COMMAND)))
    sep = c
    c = ''
    esc = false
    in_rect = false

    if $pos < $input_length
      while esc or $input[$pos] != sep or ($input[$pos] == '/' and in_rect)
        c += $input[$pos]
        if not esc
          esc = $input[$pos] == '\\'
        else
          esc = false
        end
        $pos += 1
        in_rect = true if $input[$pos] == '['
        in_rect = false if $input[$pos] == ']'
        break if $pos >= $input_length
      end
    end

    $pos += 1
    nl if $lasttok == $TK_END_COMMAND
    return [sep+c+sep, $TK_STRING]
  end

  if $punct.include? c
    if $pos < $input_length
      while $punct.include? $input[$pos]
        c += $input[$pos]
        $pos += 1
        break if $pos >= $input_length
      end
    end
    return [c, $TK_PUNCT]
  end

  return [c, $TK_PUNCT] if c == '/'
  return [c, $TK_UNKNOWN]
end

if $0 == __FILE__
  string = open(ARGV[0]).read
  puts js_beautify(string)
end

2007/9/29

Ruby's Hidden do {} while () Loop (zz)

from http://www.jvoorhis.com/articles/2007/06/13/ruby-hidden-do-while-loop

今天翻译一段php代码到ruby, 才发现ruby没有do-while:P, 偷懒网上搜到这个方法,呵呵,挺有趣

Posted by Jeremy Voorhis Tue, 12 Jun 2007 22:13:00 GMT

I found the following snippet while reading the source for Tempfile#initialize in the Ruby core library:


begin
  tmpname = File.join(tmpdir, make_tmpname(basename, n))
  lock = tmpname + '.lock'
  n += 1
end while @@cleanlist.include?(tmpname) or
  File.exist?(lock) or File.exist?(tmpname)
At first glance, I assumed the while modifier would be evaluated before the contents of begin...end, but that is not the case. Observe:

>> begin
?>   puts "do {} while ()" 
>> end while false
do {} while ()
=> nil
As you would expect, the loop will continue to execute while the modifier is true.

>> n = 3
=> 3
>> begin
?>   puts n
>>   n -= 1
>> end while n > 0
3
2
1
=> nil
While I would be happy to never see this idiom again, begin...end is quite powerful. The following is a common idiom to memoize a one-liner method with no params:

def expensive
  @expensive ||= 2 + 2
end
Here is an ugly, but quick way to memoize something more complex:

def expensive
  @expensive ||=
    begin
      n = 99
      buf = "" 
      begin
        buf << "#{n} bottles of beer on the wall\n" 
        # ...
        n -= 1
      end while n > 0
      buf << "no more bottles of beer" 
    end
end
2007/9/28

不能说的秘密

那个女生好好看,好有气质啊~~

2007/9/27

心情好

最近心情特别的好,参加了小熊主持的中心的晚会,两三年可能更多没参加学校的活动了,特别的开心,我想我更适合学校的环境,让人很放松,不去考虑很多很烦很恶心的东东,更不要说能看到美女:)

延续了好几年的惯例,和从前qqq版上的朋友们吃了中秋饭,很久没有见这些小朋友们了,大家变化都挺大,ahka同学变的好肥:P, 哈哈,对比一下,我还是控制的满不错的。大家吃饭聊天分月饼,很是开心。和never gg讨论些工作和职业的问题,感觉挺忽悠人家的,因为自己的前途未卜,且鼠目寸光看不清前面的路,希望他能够做的比我好:)

DVD卡住弹不出来了,刚上线的改动服务器挂了又回滚了,mapbar提供的地图服务真是不好用的说,但是这一切的一切,都休想扰乱我的好心情。

2007/9/23

CMU Professor Randy Pausch's 'Last Lecture'

在课程展示的时候,那个"oh, not Ctrl-Alt-Delete" and the world crashed. 太赞了,呵呵,好感动,achieving your dreams and enables the dreams of others.
Brick walls are there for a reason: they let us prove how badly we want things
还有那段对mm们的忠告,"ignore all the things man said, pay real attentions to what they do", 汗,真切。
强烈推荐去youtube上看看,it will change your world:)

CMU Professor Randy Pausch's 'Last Lecture'

Posted by CmdrTaco on Friday September 21, @12:15PM
from the worth-your-time dept.

This is a bit of an unusual story for Slashdot- it's the "Last Lecture" of a professor at CMU who is terminally ill. His early research in VR has benefited everyone and even if you have never heard of Randy Pausch I think this is worth your time. It's a 2 hour long wmv filled with insight, laughs and wisdom from a man who has really done some amazing work. I've been watching it all morning and I think it would really be worth your time if you can spare it to listen to what he has to say. From virtual reality to education to stuffed animals and childhood dreams, there's a lot here worth your time. Thanks drew for the link. Update: 09/21 15:44 GMT by Z : The link is already a little shakey, so you might want to turn to this cut up YouTube version of the talk instead.

2007/9/22

太阳照常升起

人十有八九都控制不了自己,人生十有八九都不如意,而太阳照常升起!

 

依旧的艺术片,看不懂,思仲找的评论上面很多的内容感觉我根本没有看出来,难道是原书中的内容。

 

http://news.beelink.com.cn/20070921/2400212.shtml

========================我是来自上面链接的评论==========================
《太阳照常升起》是一部勇敢的电影
《太阳》是一部勇敢的电影,说它勇敢一是题材十分敏感,没有勇气的导演不敢碰;二是叙述方式十分冒险,采用了先锋叙述,需要观众参与创作才成为一部完整的作品。前者对电影审查部门是个挑战,后者对观众更是个挑战。
《太阳》是我看到的第一部用先锋叙述的方法拍摄的中国电影,先锋叙述在中国小说界已经流行20年了,在电影界还是新东西。先锋一个本质的特征就是障碍,由于创作者意识到本人与社会有障碍,叙述就有了障碍,对阅读者是一个挑战。这种叙述方式每句话只说半句,只有阅读者进行再创作,脉络才会变得清晰起来,因此对有冒险精神的读者来说是乐趣,对懒惰的读者来说就是噩梦。
《太阳》的故事可以讲得很清楚,只是出于对当年《鬼子来了》禁播的反省,而这次的题材又更加敏感,姜文才决定用先锋叙述的方法来讲这个故事。采用这种方法,故事可以讲清楚,但是表象会很晦涩。姜文寄希望用这种方法迷惑电影审查部门的领导,却让真正的观众看清楚。通过这个努力,姜文实现了《太阳》公映的目的,只是为难了一部分不喜欢动脑子的观众。
《太阳》表面讲了四个故事,其实始终在讲一个故事,一个姜文对50-70年代中国的认知。姜文借助独立却又交织的两个家庭的故事来描述这个认知。
一个是李家,李不空是抗美援朝的志愿军,在战场上奋勇杀敌,立下赫赫战功,成为国家的英雄,还给大学生做报告,因为贪慕一位18岁大学生的美色,他强行占有了她,也就是周韵饰演的疯妈。因为李不空的英雄身份(可能组织给她做了思想工作,也可能是她为英雄的光芒所迷惑),她嫁给了他,离开了自己的家乡,来到丈夫的家乡。
之后,李不空的部队领命支援新疆建设,在异乡他官越做越大。因为长期与妻子两地分居,他与多名女子有染,最终事情败露,因为生活作风是个大问题,他被枪毙了。他从一个国家的英雄变成了狗熊。
李不空死的时候,他的妻子已经怀有身孕,她孤身一人到新疆去给丈夫收尸。丈夫从一个英雄变成了狗熊,这个打击是巨大的,她甚至想死了就算了。然而她的孩子诞生了,为了孩子她总得想办法活下去,虽然生活是不如意的,但是总归要活着,黑夜的尽头太阳不又照常升起了吗?
李不空的战友将她母子送回李的家乡,家乡的人都知道了她丈夫的事,一个弱女子拖着个孩子在这种舆论压力下生活是很艰难的,她只有装疯卖傻来博得别人的同情。只有在自己秘密筑在森林隐秘处的石洞,她才能像一个正常人那样呆会,想一想、恨一恨她的丈夫,她很怀念丈夫还是一个英雄的时候,她甚至想丈夫要是当年战死在朝鲜战场就好了。然而幻想都是暂时的,是转瞬就破碎的,她还得回到生活中装疯卖傻,日复一日。
出于对她儿子有个疯妈的同情,村里人让她儿子做了小队长。这个新的身份意味着在村里是不至于有人欺负她儿子了。她觉得,对儿子来说,自己已经不再是依靠,而只是负担了。她决定离开这个伤心的地方,消失不见。
另外一个家庭是唐家。老唐、老梁都曾经是援疆工人,勘探石油的,回城后在同一学校工作,两人有兄弟一般的友情。因为老梁多才多艺,学校很多女工都喜欢他,但是他对谁也没有动心,他有一个知识分子的自尊和优越感。老唐曾经疯狂追求一个南洋回来的女孩,在新疆时就和这个女孩结了婚,但是现在过上了两地分居的生活,他和学校里风骚的林护士有了私情。
因为当时社会性压抑的氛围,夜里集体看电影时常有女的被人摸屁股,甚至有的女人会故意弄出点声势,满足自己的性幻想,单身的老梁也免不了有性幻想,但他从未采取过行动。一次,看电影时,又有女的喊“抓流氓”,当时老梁恰好脑子里有性幻想,他受了惊吓,撒腿就跑,被当作了流氓。
在跑的过程中他无意中发现了老唐和林护士偷情,唐为了掩盖自己的奸情,在明知梁没有流氓行为的前提下,央求梁主动写检讨向组织坦白,梁在检讨上签了字。
林护士为了梁不袒露她和唐的奸情,也虚伪的来向他示爱。学校领导出于自己政迹的考虑,并没有把梁的检交给组织,在风波过去后将检讨当着梁和唐的面撕了。
作为一个知识分子,黄秋生从这些事情上参透了,从来就没有人关心事情的真相,人们只是关心自己的利益,领导也好,多年的兄弟也好,全是一些本能的动物,他决心离开这个肮脏的世界。那天,他重温了和唐兄弟一般的生活,微笑着上吊自杀了。梁的自杀让唐有很深的内疚感,他无心继续呆在学校,带着老婆下放到房祖名的生产队。
在这里他带着一群孩子成天打猎,似乎忘记了一切的不愉快,过着无忧无虑的生活。然而他的老婆忍受不了性饥渴,勾引了生产队长李小飞。唐本想杀了小李,但他内心知道在这件事上小李、老婆都没有错,他们不需要用死亡来承担这件事,生活该怎样还怎样,人总得活着,就像太阳照常会升起。
《太阳》不是一部赏心悦目的电影,而只能是一部让观众痛苦的电影。姜文寄希望于借助它来和观众共同探讨人生的意义,他不是要讲述一个结论,而是想让观众参与思考。思考人性,思考命运。本能到底对人的一生起着怎样的作用?我们应该怎样看待人性?我们为什么要活着?
人十有八九都控制不了自己,人生十有八九都不如意,而太阳照常升起!
==================================================

2007/9/21

The Unbearable Lightness of Being

Individual composes his life according to the laws of beauty even in the times of greatest distress。
 

航天器上跑的代码长什么样呢?

 

Figure A

Figure A: The first manned spaceflight program to use computers continuously in all mission phases was Apollo. Here mission controllers watch computer-driven displays while astronauts explore the lunar surface after a computer-controlled descent.

 

NASA Spaceshuttle Programming Language Exposed

Ever wondered what the programming language that runs a space shuttle looks like?

Below are a few pages of code from NASA’s high-level programming language “HAL/S”. A very sweet look at what was running some of the most sophisticated equipment known to man at the time.

Figure II-2 appears to be some sort of cron job, while the rest of the code is pretty straightforward. Enjoy!

HAL/S

HAL/S is a high-level programming language commissioned by NASA in the late 1960s to meet the real-time programming needs of the Agency. At the time, programs used on board spacecraft were either written in assembly languages or in interpreted languages. The former make programs difficult to write and maintain, and the latter are insufficiently robust and slow. Also, future systems were expected to be much larger and more complex, and cost would be moderated by the use of a high-level language.

GOAL

Below is some code from another high-level language “GOAL”. Notice they use GO TO! Tsk tsk.

GOAL is a high-level language that uses the terminology of test engineers to write tests and procedures to certify that a Shuttle vehicle is ready for launch. When the first automated preflight checkout programs were written in the mid-1960s, Marshall Space Flight Center originated ATOLL, a special high-level language for use in preparing test procedures. GOAL superseded that language in the early 1970s.

Source

2007/9/6

High Performance Web Sites: 14 Rules for Faster Pages now available on yahoo video!

Steve Souders — High Performance Web Sites: 14 Rules for Faster Pages

Yahoo! Performance guru Steve Souders offers 14 rules for faster websites.

37 minutes; source: Yahoo! Video (Flash) or download.yahoo.com (M4V, iPod/iPhone-compatible)

Steve Souders is Yahoo's chief peformance guru and the author of High Performance Web Sites. For the past three years, Steve has led a team investigating the root causes of poor page performance and applying the lessons learned to Yahoo!'s high-traffic, media-rich properties.