-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticles.py
More file actions
104 lines (84 loc) · 3.2 KB
/
Copy patharticles.py
File metadata and controls
104 lines (84 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import datetime
import math
from os import listdir
from os.path import isfile, join
import re
from markdown import markdown
class Article:
def __init__(self, directory, filename):
self.find_image = re.compile(r'<p>(<img.+>)</p>')
self.find_summary = re.compile(r'<h2>(.+)</h2>')
self.find_title = re.compile(r'<h1>(.+)</h1>')
self.filename = filename
try:
with open(directory + filename, encoding='UTF-8') as file:
self.contents = markdown(file.read(), extensions=['fenced_code'])
except FileNotFoundError as fnfe:
raise ArticleNotFoundException(f'Could not find article file {filename}') from fnfe
def get_contents(self):
return self.contents
def get_content_only(self):
title = self.find_title.search(self.contents)
summary = self.find_summary.search(self.contents)
contents = self.contents.replace(title.group(0), '')
if summary:
return contents.replace(summary.group(0), '')
return contents
def get_date(self):
'''Expects datetime to be in beginning of file name'''
find_date = re.compile(r'^(\d{4}-\d{2}-\d{2})_')
date = find_date.match(self.filename)
if not date:
raise InvalidDateForArticleException(
f'Could not parse date for article {self.filename}')
return datetime.datetime.strptime(date.group(1), '%Y-%m-%d')
def get_image(self):
image = self.find_image.search(self.contents)
if not image:
return None
return image.group(1)
def get_name(self):
return self.filename[:-3]
def get_summary(self):
summary = self.find_summary.search(self.contents)
if not summary:
return None
return summary.group(1)
def get_title(self):
title = self.find_title.search(self.contents)
if not title:
raise InvalidTitleForArticleException(
f'Could not parse title for article {self.filename}')
return title.group(1)
def __parse_articles(directory, files):
parsed = []
for file in files:
article = Article(directory, file)
parsed.append(article)
return parsed
def get_article(directory, file):
return Article(directory, file + '.md')
def get_paginated_articles(directory, page=1, per_page=10):
files = [f for f in listdir(directory) if isfile(join(directory, f))]
files.sort()
files.reverse()
first_entry = (page - 1) * per_page
last_entry = first_entry + per_page
files = files[first_entry:last_entry]
return __parse_articles(directory, files)
def get_all_articles(directory):
files = [f for f in listdir(directory) if isfile(join(directory, f))]
files.sort()
files.reverse()
return __parse_articles(directory, files)
def get_pages(directory, per_page=10):
files = [f for f in listdir(directory) if isfile(join(directory, f))]
return math.ceil(len(files) / per_page)
class ArticleException(Exception):
pass
class ArticleNotFoundException(ArticleException):
pass
class InvalidTitleForArticleException(ArticleException):
pass
class InvalidDateForArticleException(ArticleException):
pass