grab jrnl merge too
This commit is contained in:
parent
be884fefe7
commit
0f62965437
|
@ -1 +0,0 @@
|
|||
/home/anish/usr/dev/jrnl-merger/run.sh
|
144
jrnl-merge/jrnlFix.py
Normal file
144
jrnl-merge/jrnlFix.py
Normal file
|
@ -0,0 +1,144 @@
|
|||
# some code imported from https://github.com/ng110/TiddlPy/blob/master/TiddlPy/TiddlPy.py
|
||||
|
||||
import re
|
||||
import shutil
|
||||
from time import strftime, gmtime
|
||||
import json
|
||||
from bs4 import BeautifulSoup
|
||||
from os import listdir
|
||||
from os.path import isfile, join
|
||||
|
||||
|
||||
def tid2dict(tiddler):
|
||||
"""
|
||||
tid2dict(tiddler)
|
||||
Converts tiddler as a BeautifulSoup object into a Python dictionary
|
||||
"""
|
||||
tiddict = tiddler.attrs
|
||||
tiddict[u'text'] = tiddler.text
|
||||
return tiddict
|
||||
|
||||
|
||||
def openTiddlers(file):
|
||||
twsoup = BeautifulSoup(file, 'html.parser')
|
||||
storearea = twsoup.find('div', id="storeArea")
|
||||
tiddlers = storearea('div')
|
||||
ori_matched_tiddlers = []
|
||||
|
||||
for tid in tiddlers:
|
||||
# this is wrong, it only keeps the last/final tiddler it reads
|
||||
ori_matched_tiddlers.append(tid2dict(tid))
|
||||
return ori_matched_tiddlers
|
||||
|
||||
|
||||
def wikiedit(wiki, tiddlers, deletelist, modi=u'python'):
|
||||
# Try and figure out how this works so you can debug it
|
||||
# so far it's pretty robust
|
||||
"""
|
||||
wiki is the wikifilename.
|
||||
Deletes tiddlers in the deletelist and the tiddlers list, if they currently
|
||||
exist, then writes the tiddlers list, adding/replacing the modifier
|
||||
attribute with the specified or default value. Timestamps modified and/or
|
||||
created dates in UTC.
|
||||
Returns (writtenlist, deletedlist) - list of tiddlers successfully deleted
|
||||
and written.
|
||||
"""
|
||||
encoding = 'utf-8'
|
||||
savelist = []
|
||||
writtenlist = []
|
||||
deletedlist = []
|
||||
created = {}
|
||||
for tid in tiddlers:
|
||||
savelist.append(tid[u'title'])
|
||||
with open(wiki, 'br') as fhi, open('temp__.html', 'bw') as fho:
|
||||
for line in fhi:
|
||||
if re.search('^<div id="storeArea"', line.decode(encoding)):
|
||||
fho.write(line)
|
||||
break
|
||||
fho.write(line)
|
||||
else:
|
||||
print('Store area not found')
|
||||
raise SystemError
|
||||
enddiv = False
|
||||
|
||||
for line in fhi:
|
||||
if re.search(b'^<div', line):
|
||||
enddiv = False
|
||||
if len(re.findall(b'title=".*?"', line)) < 1:
|
||||
print('error - no title in line: ', line)
|
||||
raise SystemError
|
||||
title = (re.findall(b'title=".*?"', line)
|
||||
[0][7:-1]).decode(encoding)
|
||||
if len(title) <= 0:
|
||||
print('title not found in tiddler', line)
|
||||
raise SystemError
|
||||
if title not in deletelist and title not in savelist:
|
||||
fho.write(line)
|
||||
continue
|
||||
deletedlist.append(title)
|
||||
if re.search(b'created=".*?"', line):
|
||||
createddate = re.findall(b'created=".*?"', line)[0][9:-1]
|
||||
created[title] = createddate
|
||||
for line2 in fhi:
|
||||
if re.search(b'^</div>', line2):
|
||||
enddiv = True
|
||||
break
|
||||
elif re.search(b'^</div>', line):
|
||||
if enddiv:
|
||||
break
|
||||
else:
|
||||
fho.write(line)
|
||||
enddiv = True
|
||||
else:
|
||||
fho.write(line)
|
||||
else:
|
||||
print('end of Store area not found')
|
||||
raise SystemExit
|
||||
|
||||
for tiddler in tiddlers:
|
||||
tiddler['modifier'] = modi
|
||||
tiddler['modified'] = strftime('%Y%m%d%H%M%S', gmtime())
|
||||
if tiddler['title'] not in deletedlist:
|
||||
tiddler['created'] = tiddler['modified']
|
||||
elif tiddler['title'] in created:
|
||||
tiddler['created'] = created[tiddler['title']].decode(encoding)
|
||||
fho.write(b'<div')
|
||||
for key in tiddler:
|
||||
if key == 'text':
|
||||
continue
|
||||
fho.write(b' ' + bytes(key, encoding)
|
||||
+ b'="' + bytes(tiddler[key], encoding) + b'"')
|
||||
fho.write(
|
||||
'>\n<pre>{}</pre>\n</div>\n'.format(tiddler['text']).encode(encoding))
|
||||
writtenlist.append(tiddler['title'])
|
||||
|
||||
fho.write(line) # delayed write of </div> line at end of storearea
|
||||
for line in fhi:
|
||||
fho.write(line)
|
||||
# end 'with'
|
||||
|
||||
shutil.copyfile('temp__.html', wiki)
|
||||
return (writtenlist, deletedlist)
|
||||
|
||||
|
||||
originalTiddlers = {}
|
||||
conflictTiddlers = {}
|
||||
|
||||
with open('./jrnls/jrnl.html') as file:
|
||||
originalTiddlers = openTiddlers(file)
|
||||
|
||||
syncConflicts = [f for f in listdir(
|
||||
'./jrnls/') if isfile(join('./jrnls/', f)) and 'sync' in f]
|
||||
|
||||
writeList = []
|
||||
if len(syncConflicts) > 0:
|
||||
for conflict in syncConflicts:
|
||||
json = openTiddlers(open(join('./jrnls/', conflict), 'r'))
|
||||
for e in json:
|
||||
if not e in originalTiddlers:
|
||||
if not e['title'][0] == '$':
|
||||
writeList.append(e)
|
||||
|
||||
wikiedit('./jrnls/jrnl.html', writeList, [])
|
||||
else:
|
||||
print("Nothing to do")
|
3
jrnl-merge/run.sh
Executable file
3
jrnl-merge/run.sh
Executable file
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/env bash
|
||||
cp ~/tiddly/jrnl.html ./
|
||||
cp ~/tiddly/jrnl.sync-conflict-* ./
|
Loading…
Reference in a new issue