# 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")