-
-
Save st98/846f81d5bd40c22f5079 to your computer and use it in GitHub Desktop.
Movable Type のアレを JSON にするヤツ、自分用なので動けばいいという感じ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os.path | |
import re | |
import sys | |
def parse(prog): | |
result = [] | |
entry = {} | |
mkey = None | |
mvalue = None | |
multiline = False | |
multiline_str = False | |
pat = re.compile(r'^([0-9A-Z ]+):\s*(.*)$') | |
for line in prog.splitlines(): | |
if line == '-----': | |
multiline = False | |
multiline_str = False | |
if not mkey and not mvalue: | |
continue | |
if mkey in entry: | |
if not isinstance(entry[mkey], list): | |
value = entry[mkey] | |
entry[mkey] = [value] | |
entry[mkey].append(mvalue) | |
else: | |
entry[mkey] = mvalue | |
mkey = None | |
mvalue = None | |
continue | |
if line == '--------': | |
result.append(entry) | |
entry = {} | |
continue | |
m = pat.findall(line) | |
if multiline: | |
if m: | |
key, value = m[0] | |
if multiline_str: | |
if isinstance(mvalue, str): | |
mvalue += line + '\n' | |
else: | |
mvalue['value'] += line + '\n' | |
else: | |
if key: | |
mvalue[key] = value | |
else: | |
mvalue['value'] = line + '\n' | |
multiline_str = True | |
continue | |
if not m: | |
continue | |
key, value = m[0] | |
if not line: | |
continue | |
if key in ('BODY', 'EXTENDED BODY', 'EXCERPT', 'KEYWORDS', 'COMMENT', 'PING'): | |
mkey = key | |
multiline = True | |
multiline_str = key not in ('COMMENT', 'PING') | |
mvalue = '' if multiline_str else {} | |
continue | |
entry[key] = value | |
return result | |
def usage(path): | |
print('usage: python {} [-h] [-e encoding] [-o output] file'.format(path), file=sys.stderr) | |
def main(path='', *argv): | |
''' | |
python p.py [-h] [-e encoding] [-o output] file | |
''' | |
import getopt | |
output = None | |
enc = 'utf-8' | |
opts, args = getopt.getopt(argv, 'he:o:') | |
for opt, arg in opts: | |
if opt == '-h': | |
usage(path) | |
return 1 | |
elif opt == '-e': | |
enc = arg | |
elif opt == '-o': | |
output = arg | |
if len(args) < 1: | |
usage(path) | |
return 1 | |
prog = open(args[0], 'rb').read().decode(enc) | |
t = parse(prog) | |
if output is None: | |
print(json.dumps(t)) | |
else: | |
with open(output, 'wb') as f: | |
f.write(json.dumps(t).encode(enc)) | |
return 0 | |
if __name__ == '__main__': | |
sys.exit(main(*sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment