[sc-users] rtf2html

Atte André Jensen atte.jensen at gmail.com
Tue Dec 19 12:55:04 PST 2006


Hi

I became so frustrated over the clumsy handling of rtf under linux, that 
I wrote the attached python script. It's quick'n'dirty but it's a start, 
and I'm gonna polish off the rough edges as they annoy me.

The first problem is how to identify links. How is that handled in rtf 
under mac? Is any occurence of "Stream" a link to "Stream.rtf" or is 
there a special markup for this?

Basically cd to the supercollider/Help directory and run the script. It 
will create a directory named "html" under current directory where all 
output is placed. It assumes you have "unrtf" (and python obviously) 
installed...

-- 
peace, love & harmony
Atte

http://www.atte.dk      | quintet:      http://www.anagrammer.dk
                         | compositions: http://www.atte.dk/compositions
-------------- next part --------------
#!/usr/bin/env python
# -*- coding: latin-1 -*-
import commands, os, string, os.path

top = '.'
html_dir = 'html'
if not os.path.isdir(html_dir):
    os.mkdir(html_dir)
do_files = []
for root, dirs, files in os.walk(top, topdown=False):
    for name in files:
        if name[-4:].lower() == '.rtf':
            basename = name[:-4]
            rtf_file = os.path.join(root, name)
            html_file = basename + '.html'
            prepend = root[2:].replace('/','_') + '_'
            if prepend == '_':
                prepend = ''
            new_entry = {'basename': basename,
                         'prepend': prepend,
                         'path': root,
                         'rtf_file': rtf_file,
                         'html_file': prepend + html_file}
            do_files.append(new_entry)



for i in do_files:
    content = os.popen('unrtf ' + i['rtf_file'])
    html = ''
    for line in content.readlines():
        html = html + line + '\n'
    for j in do_files:
        html = html.replace(j['basename'], '<a href="' + j['html_file'] + '">' + j['basename'] + '</a>')
    f = open(os.path.join(html_dir,i['html_file']), 'w')
    f.write(html)
    f.close()


More information about the sc-users mailing list