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
|
import os
from datetime import datetime
from markdown import Markdown
from .database import Database
from .page import Page
# parser of md files, stores list of pages and tags
class MDParser:
def __init__(self, src: str,
files: list[str],
db: Database):
self.md: Markdown = Markdown(extensions=['extra', 'meta', 'sane_lists',
'smarty', 'toc', 'wikilinks'],
output_format='html5')
self.src: str = src
self.files: list[str] = files
self.db: Database = db
self.all_pages: list[Page] = None
self.updated_pages: list[Page] = None
self.all_tags: list[str] = None
def parse(self):
# initialize lists
self.all_pages = []
self.updated_pages = []
self.all_tags = []
for f in self.files:
src_file: str = os.path.join(self.src, f)
# get flag if update is successful
updated: bool = self.db.update(src_file, remove=f'{self.src}/')
page: Page = None
content: str = self.md.reset().convert(open(src_file).read())
page = Page(f, self.db.e[f][0], self.db.e[f][1], content, self.md.Meta)
# keep a separated list for all and updated pages
if updated:
self.updated_pages.append(page)
self.all_pages.append(page)
# parse tags
if page.tags is not None:
# add its tag to corresponding db entry if existent
self.db.update_tags(f, page.tags)
# update all_tags attribute
for t in page.tags:
if t not in self.all_tags:
self.all_tags.append(t)
# sort list of tags for consistency
self.all_tags.sort()
self.updated_pages.sort()
|