Skip to content
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

Fix recommendation when article name is named as directory #12

Merged
merged 2 commits into from Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion prelims/post.py
Expand Up @@ -52,7 +52,8 @@ def save(self):
content = self.raw_content.replace(
m.group(1),
yaml.dump(self.front_matter, allow_unicode=True,
default_flow_style=flow_style)
default_flow_style=flow_style,
sort_keys=False)
)
f.write(content)

Expand Down
2 changes: 2 additions & 0 deletions prelims/processor/recommender.py
Expand Up @@ -109,4 +109,6 @@ def __path_to_permalink(self, path):
excluding a file extension.
"""
file, _ = os.path.splitext(os.path.basename(path))
if file == 'index':
file = os.path.basename(os.path.dirname(path))
return urljoin(f'{self.permalink_base}/', f'{file}/')
23 changes: 18 additions & 5 deletions prelims/processor/tests/test_recommender.py
Expand Up @@ -14,18 +14,23 @@ def setUp(self):
os.path.join(os.sep, 'path', 'to', 'articles', 'a.md'))
self.path_b = os.path.abspath(
os.path.join(os.sep, 'path', 'to', 'articles', 'b.md'))
self.path_c = os.path.abspath(
os.path.join(os.sep, 'path', 'to', 'articles', 'c', 'index.md'))

# urls
self.permalink_base = '/diary/post'
self.permalink_a = '/diary/post/a/'
self.permalink_b = '/diary/post/b/'
self.permalink_c = '/diary/post/c/'

def test_process(self):
post_a = Post(self.path_a, {'title': 'foo'},
'', 'Hello world.', 'utf-8')
post_b = Post(self.path_b, {'title': 'bar'},
'', 'This is a pen.', 'utf-8')
posts = [post_a, post_b]
post_c = Post(self.path_c, {'title': 'buzz'},
'', 'There is a man in the high castle.', 'utf-8')
posts = [post_a, post_b, post_c]

recommender = Recommender(permalink_base=self.permalink_base,
stop_words='english')
Expand All @@ -36,14 +41,22 @@ def test_process(self):
post_a.front_matter['keywords'])
self.assertEqual(post_a.front_matter, {
'title': 'foo',
'recommendations': [self.permalink_b],
'keywords': ['hello', 'pen', 'world']
'recommendations': [self.permalink_c, self.permalink_b],
'keywords': ['castle', 'hello', 'high', 'man', 'pen', 'world']
})

post_b.front_matter['keywords'] = sorted(
post_b.front_matter['keywords'])
self.assertEqual(post_b.front_matter, {
'title': 'bar',
'recommendations': [self.permalink_a],
'keywords': ['hello', 'pen', 'world']
'recommendations': [self.permalink_c, self.permalink_a],
'keywords': ['castle', 'hello', 'high', 'man', 'pen', 'world']
})

post_c.front_matter['keywords'] = sorted(
post_c.front_matter['keywords'])
self.assertEqual(post_c.front_matter, {
'title': 'buzz',
'recommendations': [self.permalink_b, self.permalink_a],
'keywords': ['castle', 'hello', 'high', 'man', 'pen', 'world']
})
6 changes: 4 additions & 2 deletions prelims/tests/test_post.py
Expand Up @@ -8,6 +8,7 @@
content = """
---
aaa: xxx
ccc: xxx
bbb: [xxx]
---

Expand Down Expand Up @@ -50,7 +51,7 @@ def tearDown(self):
def test_load(self):
post = Post.load(self.mdfile.name, "utf-8")
self.assertEqual(post.path, self.mdfile.name)
self.assertEqual(post.front_matter, {'aaa': 'xxx', 'bbb': ['xxx']})
self.assertEqual(post.front_matter, {'aaa': 'xxx', 'ccc': 'xxx', 'bbb': ['xxx']})
self.assertEqual(post.raw_content, content)
self.assertEqual(post.content, 'Hello world.')

Expand All @@ -75,7 +76,7 @@ def test_update(self):

self.assertEqual(post.path, self.mdfile.name)
self.assertEqual(post.front_matter,
{'aaa': 'xxx', 'bbb': ['zzz'], 'foo': 'bar'})
{'aaa': 'xxx', 'ccc': 'xxx', 'bbb': ['zzz'], 'foo': 'bar'})

def test_save(self):
post = Post.load(self.mdfile.name, "utf-8")
Expand All @@ -85,6 +86,7 @@ def test_save(self):
expected_content = """
---
aaa: xxx
ccc: xxx
bbb: [xxx]
foo: bar
---
Expand Down