1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#!/usr/bin/python3
import sys
sys.path.insert(0, '.')
from zim.fs import *
from zim.notebook import Path
import zim.stores.files
import zim.stores.xml
def walk(store, path):
for page in store.get_pagelist(path):
yield page
for child in walk(store, page):
yield child
def package(dir, file):
if file.exists():
file.remove()
fh = file.open('w')
fh.write('<?xml version="1.0" enconding="utf-8"?>\n')
fh.write('<!-- this file is NOT in store.xml format -->\n')
fh.write('<pagelist>\n')
source = zim.stores.files.Store(None, Path(':'), dir=dir)
for page in walk(source, Path(':')):
if not page.hascontent:
continue
text = page.source.read()
text = text.replace('&', '&')
text = text.replace('<', '<')
text = text.replace('>', '>')
fh.write('<page name="%s">\n' % page.name)
fh.write(text)
fh.write('</page>\n')
fh.write('</pagelist>\n')
fh.close()
def extract(file, dir):
if dir.exists():
raise Exception('dir exists already')
assert False, 'TODO'
if __name__ == '__main__':
if len(sys.argv) == 4 and sys.argv[1] == '--package':
package(Dir(sys.argv[2]), File(sys.argv[3]))
elif len(sys.argv) == 4 and sys.argv[1] == '--extract':
extract(File(sys.argv[2]), Dir(sys.argv[3]))
else:
print('usage: %s --package DIR FILE\n'
' %s --extract FILE DIR' % (sys.argv[0], sys.argv[0]))
|