python 写的javascript格式化程序
2010-03-16 16:25
281 查看
最近用python语言写了一个js格式化文件,支持 单个文件或者目录下所有js文件的格式化,功能还行.算法的思想来自于Einar Lielmanis,详见http://jsbeautifier.org/
这个程序花了我几天的时间,熬了几个晚上,加上感冒,几次想放弃...所以写下来,纪念下.截取部分代码如下:
代码一:
代码二:
代码三:
最后提供一个可执行程序的下载:
http://files.cnblogs.com/ms_config/dist.rar
源程序:
http://files.cnblogs.com/ms_config/python_js_beautiful_source.rar
这个程序花了我几天的时间,熬了几个晚上,加上感冒,几次想放弃...所以写下来,纪念下.截取部分代码如下:
代码一:
while (True) : token_text, token_type= get_next_token() if (token_type == 'TK_EOF') : break eval("proc_"+token_type)() constants.last_last_text = constants.last_text constants.last_type = token_type constants.last_text = token_text return re.sub(r"[\n]+$","",''.join(constants.output))
代码二:
def proc_TK_START_BLOCK(): if (constants.last_word == 'do') : set_mode('DO_BLOCK') else : set_mode('BLOCK') if (constants.last_type == 'TK_OPERATOR' and constants.last_type == 'TK_START_EXPR') : if (constants.last_type == 'TK_START_BLOCK') : print_newline() else : print_single_space() indent() print_token()
代码三:
def proc_TK_END_BLOCK(): #pdb.set_trace() restore_mode() if (constants.last_type == 'TK_START_BLOCK') : # nothing if (constants.just_added_newline) : remove_indent() else : trim_output() else : print_newline() print_token()
最后提供一个可执行程序的下载:
http://files.cnblogs.com/ms_config/dist.rar
源程序:
http://files.cnblogs.com/ms_config/python_js_beautiful_source.rar
相关文章推荐
- [转]JavaScript格式化代码程序
- [转]JavaScript格式化代码程序
- [python] python抓取有道词典并格式化输出的程序
- 字符串输出格式化(Java、Python、JavaScript、Kotlin)
- python练习程序(运算,math,格式化)
- 1026. 程序运行时间(15)--Python
- python中日期和时间格式化输出的方法小结
- [No000063]Python格式化输出
- 编写高效的JavaScript程序--转载
- python程序:备份MS SQL数据库到压缩文件
- [转载] 使用PyInstaller打包Python程序
- JavaScript时间戳与其格式化
- Python基础-HTML与CSS与JavaScript
- python程序如何执行
- 编写 Window 下 Python C程序
- 编写高质量代码--改善python程序的建议(一)
- JavaScript 事件流、事件处理程序及事件对象总结
- 程序员的七夕怎么过?我用Python打造最强表白程序
- [python] PyInstaller 库 -- 将 python 程序打包为 exe
- Numeral.js – 格式化和操作数字的 JavaScript 库