由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - python高手请进,RFC文件转换成kindle格式的代码
相关主题
问一道C++面试题opencv快还是imagemagick快?
C++ formatted output question如何用tornado web server写可以处理curl命令的程序
谁能帮看下python要搞type hint了
Array in CSIP eBook (转载)
MatLab Codegss_wrap token size
C -> assemblyurl header问题
C ++ 问题下载软件的断点续传功能是怎么实现的?
Help: font size decreases by itself in IE9 in 1 of the PC研讨一下TCP fragment的问题
相关话题的讨论汇总
话题: font话题: image话题: false话题: output话题: input
进入Programming版参与讨论
1 (共1页)
f*******8
发帖数: 3612
1
这是一个将RFC 文件转换成 kindle格式的程序,
来自:
https://github.com/pingwin/RFC-2-Kindle
我是windows上的python 2.71, 运行完全仿照该网站的要求
结果出来是找不到font MONOSPACE, 不知道MONOspace是不是个字体库或别的什么。
running output:
D:\>rfctxt2kindlehtml.py -i rfc791.txt -o rfc791.html && ./ki
ndlegen rfc791.html
Unable to find font: /usr/share/cups/fonts/Monospace
Convert IETF RFC TXT file to HTML for kindlegen
-h --help This message
-v verbosity
-i --input input file
-o --output output file
-f --font font file to use for monospace images (default:/usr/share/cups/
fonts/Monospace)
copy 了一个字体库,rename as monospace,还是不行。
Invalid Parameter - mono821n.ttf
=====================================================
https://github.com/pingwin/RFC-2-Kindle
Description:
This is for anyone who has a kindle and are frustrated with the options for
reading IETF RFC documents on the Kindle.
This is by far a perfect script, but it should be a good start for most
documents thus far.
TODO:
* Section Links
* Smarter Title Finding
* Cleaner header display
Packages Required:
* ImageMagick
* KindleGen http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000234621
Execution:
./rfctxt2kindlehtml.py -i rfc1034.txt -o rfc1034.html && ./kindlegen rfc1034
.html
=========================================================
source code:
#!/usr/bin/env python
"""
Author: Brian Smith
Date: 2010/10/21
Description:
Convert a IETF RFC txt format into an html document readable on the kindle.
./rfctxt2kindlehtml.py rfc1034.txt > rfc1034.html
"""
import sys, logging, getopt, os
"""_default_font = '/usr/share/cups/fonts/Monospace' """
_default_font = 'mono821n.ttf'
_font = _default_font
def usage():
global _default_font
""" print usage message """
print ("Convert IETF RFC TXT file to HTML for kindlegen")
print ("-h --help This message")
print ("-v verbosity")
print ("-i --input input file")
print ("-o --output output file")
print ("-f --font font file to use for monospace images (default:%s)"
) %\
_default_font
sys.exit(2)
def find_open_file(c=0):
try:
c += 1
open('_file_%d.gif' % c)
except IOError:
return '_file_%d.gif' % c
return find_open_file(c)
def create_image(picture_me):
global _font
img = find_open_file()
os.system("convert -font %s label:'%s' %s" % \
(_font, picture_me.replace("'", "\'"), img))
return img
def is_image_part(line):
chars = [
'+-',
'|',
'---',
'0 1',
'0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5',
' / '
]
for c in chars:
if line.find(c) != -1:
return True
return False
def main():
""" Begin process """
global _font
try:
opts, args = getopt.getopt(sys.argv[1:],
"hvi:o:f:",
[
'help',
'input',
'output',
'font'
])
except getopt.GetoptError, err:
logging.exception(err)
usage()
input = None
output = None
log_level = logging.ERROR
for opt, a in opts:
if opt in ('-h', '--help'):
usage()
if opt == '-v' and log_level > 10:
log_level -= 10
if opt in ('-i', '--input'):
input = a
if opt in ('-o', '--output'):
output = a
if opt in ('-f', '--font'):
_font = a
if not input or not output:
usage()
try:
open(_font)
except:
print "Unable to find font: %s" % _font
usage()

input = open(input, 'r')
output = open(output, 'w+')
in_p = False
has_title = False
in_toc = False
in_image = False
toc_itm = 0
buffer = []
buffer.append('')
for line in input:
if line.find('Table of Contents') != -1 or in_toc:
# image
if not in_toc:
image = []
if in_toc:
image.append(line.replace('...', '.'))
toc_itm += 1
in_toc = True
if len(line) < 2 and toc_itm > 1:
buffer.append('' % create_image(''.join(
image)))
in_toc = False
continue

if line[:2] == ' ':
if is_image_part(line):
# image
if not in_image:
image = []
in_image = True
image.append(line)
continue
if not has_title:
output.write('%s' % line)
has_title = True
if in_image:
in_image = False
buffer.append('' % create_image(''.join(image)))


if len(line) < 2:
if not in_p:
buffer.append('

')
in_p = True
else:
buffer.append('


')
in_p = False
continue

buffer.append(line.replace("\n", ''))
buffer.append('')
buffer = ''.join(buffer)
output.write(buffer)
input.close()
output.close()
if __name__ == "__main__":
main()
1 (共1页)
进入Programming版参与讨论
相关主题
研讨一下TCP fragment的问题MatLab Code
json是一种革命性的创造C -> assembly
我终于开源过了C ++ 问题
说说我以前做的ultra low latency架构吧Help: font size decreases by itself in IE9 in 1 of the PC
问一道C++面试题opencv快还是imagemagick快?
C++ formatted output question如何用tornado web server写可以处理curl命令的程序
谁能帮看下python要搞type hint了
Array in CSIP eBook (转载)
相关话题的讨论汇总
话题: font话题: image话题: false话题: output话题: input