Last active
December 25, 2015 11:39
-
-
Save punchagan/6970578 to your computer and use it in GitHub Desktop.
Scripts to import blog from o-blog to Nikola.
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
""" Imports the old links page as new posts pages. """ | |
import orgparse | |
DATE_FORMAT = '%Y-%m-%d' | |
BOOKMARK_FORMAT = """ | |
- [[%(link)s][%(heading)s]] | |
tags: %(tags)s | |
%(description)s | |
""" | |
POST_FORMAT = """\ | |
#+BEGIN_COMMENT | |
.. link: | |
.. description: | |
.. tags: bookmarks | |
.. date: %(post_date)s 23:59:59 | |
.. title: Bookmarks [%(post_date)s] | |
.. slug: %(slug)s | |
#+END_COMMENT | |
%(content)s | |
""" | |
def find_links_heading(root): | |
""" Find the node with the heading 'Links' """ | |
matches = [node for node in root.children if node.heading == 'Links'] | |
if len(matches) == 0: | |
raise KeyError('Links heading not found.') | |
return matches[0] | |
def get_bookmark_text(node): | |
""" Return a text string to bookmark given a node. """ | |
body = node.body.strip().splitlines() | |
context = dict( | |
link = body[0], | |
description = '\n'.join(body[1:]), | |
heading = node.heading, | |
tags = ', '.join(node.tags), | |
) | |
return BOOKMARK_FORMAT % context | |
def make_a_post(node_list): | |
""" Make a post out of a given list of posts. """ | |
node_list = sorted(node_list, key=lambda x: x.closed) | |
content = '\n'.join(get_bookmark_text(node) for node in node_list) | |
post_date_title = node_list[-1].closed.start.strftime(DATE_FORMAT) | |
post_date = post_date_title.replace('-', '/') | |
slug = '%s-%s' % ('bookmarks', post_date_title) | |
context = dict( | |
post_date = post_date, | |
slug = slug, | |
content = content, | |
) | |
with open('posts/%s.org' % slug, 'w') as f: | |
text = POST_FORMAT % context | |
f.write(text.encode('utf8')) | |
print 'Wrote post', slug | |
def make_posts(infile): | |
""" Make a set of posts out of all links, given a path to a file. """ | |
links = find_links_heading(orgparse.load(infile)) | |
for i in range(len(links.children)/5 + 1): | |
make_a_post(links.children[5*i:5*(i+1)]) | |
if __name__ == '__main__': | |
import sys | |
infile = sys.argv[1] | |
make_posts(infile) |
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 codecs | |
import os | |
from os.path import exists, join | |
from orgparse import load | |
from nikola.utils import slugify | |
DATE_FORMAT = '%Y/%m/%d %H:%M:%S' | |
def to_text(node): | |
""" Get the writable format of a node. | |
For level 3 nodes, just write the body, after stripping off 4 spaces. | |
For other nodes, write the heading and the body (after stripping of 4 | |
spaces from the left) | |
""" | |
if node.level > 3: | |
stars = '*' * ((node.level - 1)/2) | |
heading = '%s %s' % (stars, node.heading) | |
else: | |
heading = '' | |
lines = ['', heading] | |
for line in node.body.splitlines(): | |
if line[:4].strip() == '': | |
lines.append(line[4:]) | |
else: | |
lines.append(line) | |
lines.append('') | |
text = '\n'.join(lines) | |
return text | |
def nikola_write(post): | |
post_slug = slugify(post.heading) | |
post_file = join('posts', '%s.org' % post_slug) | |
with codecs.open(post_file, 'w') as f: | |
f.write('#+BEGIN_COMMENT\n') | |
f.write('.. title: %s\n' % post.heading.encode('utf8')) | |
f.write('.. date: %s\n' % post.closed.start.strftime(DATE_FORMAT)) | |
f.write('.. tags: %s\n' % ', '.join(sorted(post.tags))) | |
f.write('.. slug: %s\n' % post_slug) | |
f.write('#+END_COMMENT\n') | |
f.write('\n') # blank line | |
f.write(to_text(post).encode('utf8')) | |
def write_children(root, fp): | |
for node in root.children: | |
fp.write(to_text(node).encode('utf8')) | |
write_children(node, fp) | |
write_children(post, f) | |
def import_blog(infile): | |
root = load(infile) | |
blog = root.children[0] | |
for post in blog.children: | |
nikola_write(post) | |
if __name__ == '__main__': | |
import sys | |
infile = sys.argv[1] | |
if not exists('posts'): | |
os.mkdir('posts') | |
import_blog(infile) |
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
from os.path import basename | |
SITE.scan_posts() | |
base_url = SITE.config['SITE_URL'] | |
with open('translate-links.csv', 'w') as f: | |
for post in SITE.timeline: | |
old_slug = '%sblog/%s/%s' % ( | |
base_url, | |
post.date.strftime('%Y/%m'), | |
basename(post.destination_path()) | |
) | |
new_slug = '%s%s' % (base_url, post.destination_path()) | |
f.write('%s, %s\n' % (old_slug, new_slug)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment