Skip to content

Accept content with accents and change to TOML front matter #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions ghost2hugo.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sqlite3
import json
from datetime import datetime


conn = sqlite3.connect('ghost.db')
# optional usage to remove accents on tag names
# -------------
import unicodedata
def remove_accents(input_str):
nkfd_form = unicodedata.normalize('NFKD', unicode(input_str))
return u"".join([c for c in nkfd_form if not unicodedata.combining(c)])
# -------------

conn = sqlite3.connect('ghost-dev.db')
conn.row_factory = sqlite3.Row

c = conn.cursor()
c2 = conn.cursor()

l = []

for i in c.execute('''
SELECT id, title, meta_description as description, slug, markdown as text,
status as draft, page, meta_title, image,
DATE(published_at/1000, "unixepoch") as date,
DATE(created_at/1000, "unixepoch") as date2
FROM posts
'''):
FROM posts'''):
g = {i.keys()[e]: tuple(i)[e] for e in range(len(i.keys()))}
t = (i['id'],)
g['tags'] = [e['name'] for e in c2.execute('''
Expand All @@ -33,22 +42,53 @@
g['draft'] = True
g.pop('date2')

# post description
if g['description'] == None:
g['description'] = ""

# post content
text = g.pop('text')
text = text.replace("# ", "#")
text = text.replace("#", "# ")
text = text.replace("# # # ", "### ")
text = text.replace("# # ", "## ")
text = text.replace("\# ", "\#")

# post type
if g['page'] == True:
page = 'page'
else:
page = 'post'
g['type'] = page
g.pop('page')
g.pop('page')

with open('./content/%s.md' % (g['slug']), 'w') as post_file:
post_file.write('+++\n')
post_file.write('type = "%s"\n' % g['type'])
post_file.write('date = "%s"\n' % g['date'])
post_file.write('title = "%s"\n' % g['title'].encode('utf8'))
post_file.write('description = "%s"\n' % g['description'].encode('utf8'))
post_file.write('slug = "%s"\n' % g['slug'])

post_file.write('tags = [')

# encode each tag to accept accents or removed them
# and add a comma to separate each one
for i in xrange(0, len(g['tags'])):
if i < len(g['tags']) - 1 :
separator = ", "
else:
separator = ""

# encode string to keep accents etc. E.g. "Introdução e Avaliações"
tag = g['tags'][i].encode('utf8')

# uncomment if you like to remove accents. E.g. "Introducao e Avaliacoes"
tag = remove_accents(g['tags'][i])

post_file.write('"%s"' % tag+separator)

post_file.write(']\n')

f = open("./content/%s.md" % (g['title']), "w")
f.write(json.dumps(g, sort_keys=True, indent=4, separators=(',', ': ')))
f.write('\n\n\n')
f.write(text)
f.close()
post_file.write('+++\n\n')
post_file.write(text.encode('utf8'))