diff --git a/jrnl-merge b/jrnl-merge deleted file mode 120000 index f9a50ce..0000000 --- a/jrnl-merge +++ /dev/null @@ -1 +0,0 @@ -/home/anish/usr/dev/jrnl-merger/run.sh \ No newline at end of file diff --git a/jrnl-merge/jrnlFix.py b/jrnl-merge/jrnlFix.py new file mode 100644 index 0000000..19ecb8c --- /dev/null +++ b/jrnl-merge/jrnlFix.py @@ -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('^
', line2): + enddiv = True + break + elif re.search(b'^
', 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'\n
{}
\n\n'.format(tiddler['text']).encode(encoding)) + writtenlist.append(tiddler['title']) + + fho.write(line) # delayed write of 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") diff --git a/jrnl-merge/run.sh b/jrnl-merge/run.sh new file mode 100755 index 0000000..023a558 --- /dev/null +++ b/jrnl-merge/run.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +cp ~/tiddly/jrnl.html ./ +cp ~/tiddly/jrnl.sync-conflict-* ./