Last active
November 6, 2016 19:58
-
-
Save kumy/fac092e097a233a308ed115e6361371b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 | |
import re | |
import sys | |
from yaml import load | |
from slugify import slugify | |
class OldFaq(dict): | |
"""A faq page from the old format""" | |
def __init__(self, data): | |
self['title'] = data['title'] | |
self['items'] = [] | |
for item in data['items']: | |
_item = dict() | |
_item['id'] = item['id'] | |
_item['title'] = item['title'] | |
_item['content'] = item['content'] | |
self['items'].append(_item) | |
def convert(old): | |
"""A simplified version of a yaml renderer""" | |
print('- id: %s' % slugify(old['title'])) | |
print(' title:') | |
print(' en: %s' % old['title']) | |
if len(old['items']): | |
print(' items:') | |
for item in old['items']: | |
print(' - id: %s' % item['id']) | |
print(' en:') | |
print(' title: %s' % item['title']) | |
print(' content: |') | |
print(re.sub(b'^', ' ', item['content'].encode('utf-8'), flags=re.MULTILINE)) | |
print('') | |
if len(sys.argv) < 2: | |
print("usage: %s <filename>" % sys.argv[0]) | |
print("Read cgeo faq yaml from the untranslated format, and reformat it to the new format") | |
sys.exit(1) | |
try: | |
with open(sys.argv[1], 'r') as f: | |
doc = load(f) | |
except Exception as e: | |
print("can't read file: %s" % sys.argv[1]) | |
print(e) | |
sys.exit(1) | |
for faq_entry in doc: | |
convert(OldFaq(faq_entry)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment