about summary refs log tree commit diff
path: root/src/article_handler.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/article_handler.py')
-rw-r--r--src/article_handler.py22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/article_handler.py b/src/article_handler.py
index 6ee0420..00321a7 100644
--- a/src/article_handler.py
+++ b/src/article_handler.py
@@ -5,8 +5,7 @@ import logging
 import pathlib
 import glob
 import typing
-from typing import Tuple
-
+import markdown
 import yaml
 
 
@@ -40,25 +39,33 @@ class MetaData():
     Created: {self.created}'''
 
 
+def truncate(text: str, max_length: int=50):
+    if len(text) < max_length:
+        return text
+    else:
+        return text[0:max_length - 1]
+
 @dataclasses.dataclass
 class Article(WebPage):
     title: str
-    meta_data: MetaData
+    metadata: MetaData
     web_dir: tuple
     source_path: str | os.PathLike
     source: str
+    html: str
     folder_path: typing.Union[None, str | os.PathLike] = None
 
     @property
     def pretty_print(self) -> str:
         return f'''
 Title:       {self.title}
-Meta data:   {self.meta_data.pretty_print}
+Metadata:   {self.metadata.pretty_print}
 Web dir:     {self.web_dir}
 URL:         {self.url}
 Source path: {self.source_path}
 Folder path: {self.folder_path}
-Source:      {f'{self.source[0:49]}...' if len(self.source) >= 50 else self.source}'''  # Truncate long source text
+HTML:        {truncate(self.html)}
+Source:      {truncate(self.source)}'''
 
 
 @dataclasses.dataclass
@@ -78,7 +85,7 @@ def parse_article_meta_data(source: str) -> typing.Tuple[str, MetaData]:
     if source.startswith('---'):
         meta_data_yml_end_char_index = source.find('---', 3)
         meta_data_yml = source[3:meta_data_yml_end_char_index]
-        # Strip meta data text from source, before feeding it to the markdown reader
+        # Strip metadata text from source, before feeding it to the markdown reader
         source = source[meta_data_yml_end_char_index + 3:]
         meta_data = yaml.safe_load(meta_data_yml)
 
@@ -114,7 +121,8 @@ def get_article(path: str | os.PathLike) -> Article:
 
     with open(path, 'r') as file:
         source = file.read()
-        article_args['source'], article_args['meta_data'] = parse_article_meta_data(source)
+    article_args['source'], article_args['metadata'] = parse_article_meta_data(source)
+    article_args['html'] = markdown.markdown(article_args['source'], extensions=['fenced_code', 'codehilite', 'tables'])
 
     article = Article(**article_args)
     return article