forked from barryclark/jekyll-now
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Tool to add related posts
- Loading branch information
1 parent
44b7f71
commit 360ee5469cb1ef3ae89908973c33118b6e2e81c7
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
2008-12-14-commu | ||
2008-12-20-commus-new-release | ||
2009-07-10-commu2009071 | ||
|
||
2008-12-15-netviz | ||
2010-04-12-social-networks-local-visualization | ||
|
||
2008-12-19-wet-folding-first-attempt | ||
2009-01-25-two-more-attempts | ||
|
||
2008-12-27-how-to-take-lecture-notes-with-latex | ||
2008-12-29-latex-class-for-lecture-notes | ||
2014-02-17-suboptimal-latex-1-intro | ||
2014-02-24-suboptimal-latex-2-spacing | ||
2014-03-03-suboptimal-latex-3-mathematical-environments | ||
2014-03-10-suboptimal-latex-4-mathematics | ||
2014-05-25-suboptimal-latex-5-miscellanea | ||
|
||
2012-10-01-ioi-2012-my-experience | ||
2012-10-09-ioi-translation-systems-a-survey-of-possible-approaches | ||
2012-10-15-ioi-translation-systems-an-ideal-workflow | ||
2012-10-24-ioi-translation-systems-an-actual-solution | ||
|
||
2009-03-22-my-two-cents-on-the-daylight-saving-time | ||
2014-01-06-how-much-is-time-wrong-around-the-world | ||
2014-06-02-emotional-time | ||
|
||
2012-02-29-pydepgraph-a-dependencies-analyzer-for-python | ||
2013-10-25-give-python-a-bit-of-type-safety-with-pydoc-checker |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
2012-10-09-ioi-translation-systems-a-survey-of-possible-approaches | ||
2012-10-15-ioi-translation-systems-an-ideal-workflow | ||
2012-10-24-ioi-translation-systems-an-actual-solution | ||
|
||
2014-02-17-suboptimal-latex-1-intro | ||
2014-02-24-suboptimal-latex-2-spacing | ||
2014-03-03-suboptimal-latex-3-mathematical-environments | ||
2014-03-10-suboptimal-latex-4-mathematics | ||
2014-05-25-suboptimal-latex-5-miscellanea | ||
|
||
2014-07-07-if-it-looks-like-a-words-then-it-is-a-word | ||
2014-08-25-if-it-looks-like-a-word-it-is-a-word-one-third-of-the-time |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#!/usr/bin/env python | ||
|
||
import re | ||
import sys | ||
|
||
from glob import glob | ||
|
||
|
||
SEP = "\n<!-- DO NOT EDIT BELOW THIS LINE -->\n* * *" | ||
|
||
|
||
def load(filename): | ||
"""Load a file containing groups of posts to be cross-linked. | ||
The format is: a post per line, group separated by an empty line. | ||
""" | ||
groups = open(filename).read().split("\n\n") | ||
groups = [set(group.strip().split("\n")) for group in groups] | ||
return groups | ||
|
||
|
||
def check(groups, posts): | ||
"""Check that each referenced post exist.""" | ||
for group in groups: | ||
for post in group: | ||
if post not in posts: | ||
print >> sys.stderr, "Post %s not found" % post | ||
return False | ||
return True | ||
|
||
|
||
def find_contents_and_titles(posts): | ||
"""Extract the non-auto-generated content and the title of each post.""" | ||
contents = {} | ||
titles = {} | ||
for post in posts: | ||
contents[post] = open("./_posts/%s.md" % post).read().split(SEP)[0] | ||
title_match = re.search(r'title: (.*)', contents[post]) | ||
if title_match is None: | ||
print >> sys.stderr, "Cannot find title for post %s" % post | ||
titles[post] = "" | ||
else: | ||
titles[post] = title_match.group(1).strip("'") | ||
return contents, titles | ||
|
||
def build_links(series, related, titles): | ||
"""Build and return the cross-link part of the post.""" | ||
if series == set() and related == set(): | ||
return "" | ||
|
||
# Keep only related posts that are not part of the series. | ||
related.difference_update(series) | ||
|
||
links = [SEP] | ||
if series != set(): | ||
links.append("\n\n### Part of this series\n\n") | ||
for i, post in enumerate(sorted(series)): | ||
links.append("1. [%s][%d]\n" % (titles[post], i + 1000)) | ||
links.append("\n") | ||
for i, post in enumerate(sorted(series)): | ||
links.append(" [%d]: {%% post_url %s %%}\n" % (i + 1000, post)) | ||
|
||
if related != set(): | ||
links.append("\n\n### See also\n\n") | ||
for i, post in enumerate(sorted(related)): | ||
links.append("1. [%s][%d]\n" % (titles[post], i + 2000)) | ||
links.append("\n") | ||
for i, post in enumerate(sorted(related)): | ||
links.append(" [%d]: {%% post_url %s %%}\n" % (i + 2000, post)) | ||
|
||
return "".join(links) | ||
|
||
|
||
def run(): | ||
"""Main function. | ||
Load all cross-linked content from series.txt and related.txt, and | ||
rewrites all posts to update the cross-link part. | ||
""" | ||
posts = [path.replace("./_posts/", "").replace(".md", "") | ||
for path in glob("./_posts/*.md")] | ||
|
||
series = load("./series.txt") | ||
if not check(series, posts): | ||
return 1 | ||
|
||
related = load("./related.txt") | ||
if not check(related, posts): | ||
return 1 | ||
|
||
contents, titles = find_contents_and_titles(posts) | ||
|
||
for post in posts: | ||
this_series = set() | ||
this_related = set() | ||
|
||
# Find posts to link | ||
for group in series: | ||
if post in group: | ||
this_series = set(group) | ||
for group in related: | ||
if post in group: | ||
this_related = set(group) | ||
|
||
this_related.discard(post) | ||
|
||
links = build_links(this_series, this_related, titles) | ||
|
||
with open("./_posts/%s.md" % post, "w") as f: | ||
f.write(contents[post] + links) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(run()); |