#!/usr/bin/python2 # # Convert recipe exports from Pepperplate to an HTML page # that Copy Me That can easily import. # import sys import os.path import zipfile import ezt def process(name, fp_r, template, fp_html): info = parse(fp_r) def one_line(which): v = info.get(which) if not v: return None assert type(v) is list if len(v) > 1: print('WARNING[%s]: wanted one for "%s", but got multiple' % (name, which,)) return v[0] def want_list(which): v = info.get(which) if not v: return [ ] return v notes = want_list('notes') source = one_line('source') url = one_line('original url') if source and url: notes.append('Source: %s, %s' % (source, url)) elif source: notes.append('Source: %s' % (source,)) elif url: notes.append('Source: %s' % (url,)) tags = want_list('categories') if tags: notes.append('Pepperplate tags: %s' % (', '.join(tags))) data = { 'title': one_line('title'), 'description': one_line('description'), 'img': one_line('image'), 'url': url, 'yield': one_line('yield'), 'active': one_line('active'), 'total': one_line('total'), 'ingredients': want_list('ingredients'), 'instructions': want_list('instructions'), 'notes': notes, } template.generate(fp_html, data) def parse(fp): lines = fp.readlines() recipe = { } last = None seen_notes = False for l in lines: #print l.strip() if l.startswith('\t'): assert last recipe[last].append(l.strip()) elif l.startswith('['): # Section header for ingredients or instructions. Convert to # the CMT format of "TEXT:" value = l.strip('[]\r\n\t ') recipe[last].append(value + ':') elif not l.strip(): # Blank line ends multiline data. Just wait for new subject. pass elif seen_notes: # Append everything to notes, now. if ':' in l: print('WARNING: colon found in "Notes" section. Needs double-check.') recipe[last].append(l.strip()) else: i = l.index(':') last = l[:i].lower() assert last not in recipe value = l[i+1:].strip() if value: recipe[last] = [ value ] else: recipe[last] = [ ] if last == 'notes': seen_notes = True return recipe def usage(): base = os.path.basename(sys.argv[0]) print('ERROR: USAGE:') print(' %s SRC_FILE DST_FILE # convert SRC, write to DST' % (base,)) print(' %s SRC_FILE - # convert SRC, write to stdout' % (base,)) print(' %s SRC_ZIP DST_DIR # extract all to DST_DIR/*.html' % (base,)) print(' %s SRC_FILE # write to *.html' % (base,)) sys.exit(1) if __name__ == '__main__': if len(sys.argv) not in (2, 3): usage() # NOTREACHED this_dir = os.path.dirname(__file__) tname = os.path.join(this_dir, 'pp2cmt.ezt') template = ezt.Template(tname, base_format=ezt.FORMAT_HTML) src = sys.argv[1] name = os.path.basename(src) if len(sys.argv) == 3: dst = sys.argv[2] if zipfile.is_zipfile(src): if not os.path.isdir(dst): usage() # NOTREACHED zfile = zipfile.ZipFile(src) for name in zfile.namelist(): fp = zfile.open(name) print('Processing: %s' % (name,)) fname = os.path.splitext(name)[0] + '.html' dst_html = os.path.join(dst, fname) process(name, fp, template, open(dst_html, 'w')) elif dst == '-': process(name, open(src), template, sys.stdout) else: process(name, open(src), template, open(dst, 'w')) else: fname = os.path.splitext(name)[0] + '.html' dst = os.path.join(os.path.dirname(src), fname) process(name, open(src), template, open(dst, 'w'))