~ubuntu-branches/ubuntu/karmic/zim/karmic

« back to all changes in this revision

Viewing changes to contrib/tzim.py

  • Committer: Bazaar Package Importer
  • Author(s): Emfox Zhou
  • Date: 2007-08-25 13:04:42 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20070825130442-q7xffffj9usah8b4
Tags: 0.20-1
* New upstream release
* Fix the bug which cause wrong internal links (Closes: #438612)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#################################################################################################
 
3
#                                                                                               #
 
4
# tzim.py - Simple conversion module to convert a Tomboy notes database to zim format.          #
 
5
#           _Usage_:                                                                            #
 
6
#           If not alraeady executable,                                                         #
 
7
#           $ chmod a+x tzim.py                                                                 #
 
8
#           cd to target directory, i.e. where the zim notes shall appear (for example          #
 
9
#           to ~/.zim). Run                                                                     #
 
10
#           $ <dir-path to tzim.py>/tzim.py                                                     #
 
11
#           follow instructions. When conversed, open zim and add repository (i.e. current dir) #
 
12
#                                                                                               #
 
13
#           GPL statement:                                                                      #
 
14
#           This program is free software; you can redistribute it and/or modify                #
 
15
#           it under the terms of the GNU General Public License as published by                #
 
16
#           the Free Software Foundation; either version 3 of the License, or                   #
 
17
#           (at your option) any later version.                                                 #
 
18
#                                                                                               #
 
19
#           This program is distributed in the hope that it will be useful,                     #
 
20
#           but WITHOUT ANY WARRANTY; without even the implied warranty of                      #
 
21
#           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                       #
 
22
#           GNU General Public License for more details.                                        #
 
23
#                                                                                               #
 
24
#           You should have received a copy of the GNU General Public License                   #
 
25
#           along with this program.  If not, see <http://www.gnu.org/licenses/>.               #
 
26
#                                                                                               #
 
27
#           Copyright 2007 Bengt J. Olsson                                                      #
 
28
#                                                                                               #
 
29
# Filename: tzim.py                                                                             #
 
30
# Rev:      1.0                                                                                 #
 
31
# Date:     2007-07-28                                                                          #
 
32
# Changes:                                                                                      #
 
33
#                                                                                               #
 
34
#################################################################################################
 
35
import os
 
36
import os.path
 
37
import sys
 
38
import glob
 
39
import re
 
40
 
 
41
def main():
 
42
        tomboynotes = raw_input("Path to tomboy notes directory (default ~/.tomboy): ")
 
43
        if tomboynotes == "":
 
44
                tomboynotes = os.path.expanduser('~')
 
45
                tomboynotes += '/.tomboy/'
 
46
        if not tomboynotes.rstrip == '/':
 
47
                tomboynotes += '/*.note'
 
48
        else:
 
49
                tomboynotes += '*.note'
 
50
        files = glob.glob(tomboynotes)                          # Read tomboy notes file names
 
51
        if len(files) == 0 :
 
52
                print "No note files."                                  # Exit if no note files in directory
 
53
                sys.exit()
 
54
        for fil in files:
 
55
                infile = open(fil,'r')
 
56
                infile.readline() # get rid of first lines in tomboy note
 
57
                line = infile.readline()
 
58
                if not re.search('note version="0.2"',line):
 
59
                        print "Only tested with tomboy notes format version 0.2"
 
60
                line = infile.readline() # Third line contains title of note
 
61
                line = line.rstrip()
 
62
                line = re.sub('<\/?title>','',line) # remove <title> and </title>
 
63
                line = re.sub('^\s*','',line) # remove heading whitespaces
 
64
                print "converting note ", line
 
65
                outfilename = re.sub(' ','_',line) + '.txt' # zim file name for note
 
66
                outfilename = re.sub('[/]','_',outfilename) # get rid of "dangerous" chars in filename
 
67
                outfile = open(outfilename,'w') 
 
68
                line = '====== ' + line + ' ======' + '\n\n'
 
69
                outfile.write(line)
 
70
                infile.readline() # get rid of two first lines more
 
71
                infile.readline() #
 
72
                line = infile.readline()
 
73
                line = format(line)
 
74
                while not re.search('</text>$',line):
 
75
                        outfile.write(line)
 
76
                        line = infile.readline()
 
77
                        line = format(line)
 
78
                line = re.sub('<\/note-content><\/text>','',line)
 
79
                outfile.write(line)
 
80
                infile.readline() # throw away
 
81
                line = infile.readline()
 
82
                line = re.search('.*(\d\d\d\d-\d\d-\d\d).*',line)
 
83
                line = line.group(1)
 
84
                line = "\nNote created " + line + "\n"
 
85
                outfile.write(line)
 
86
                infile.close()
 
87
                outfile.close()                 
 
88
 
 
89
def format(line):                                               #various format substitutions of lines
 
90
        line = re.sub('</?bold>','**',line)
 
91
        line = re.sub('</?italic>','//',line)
 
92
        line = re.sub('</?strikethrough>','~~',line)
 
93
        line = re.sub('</?highlight>','__',line)
 
94
        line = re.sub('</?size:(small|large|huge)>','',line)# Can't handle tomboy sizes
 
95
        line = re.sub('</?monospace>','',line)                          # or fixed-width
 
96
        line = re.sub('<link:(internal|url)>','[[',line)
 
97
        line = re.sub('</link:(internal|url)>',']]',line)
 
98
        line = re.sub('<list-item dir="ltr">','* ',line)# List handling in tomboy to complexfor this
 
99
        line = re.sub('(</?list>|</list-item>)','',line)# this simple converter; generating a one-level
 
100
        return(line)                                                                    # list only
 
101
                
 
102
main()