Merge branch 'main' into rss
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| theme: './theme/default' | ||||
| inbox: './inbox' | ||||
| outbox: './outbox' | ||||
| draft: './draft' | ||||
| url: 'https://blog.example.com' | ||||
| title: 'Blog title' | ||||
| presentation: 'Blog presentation.' | ||||
|   | ||||
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -5,6 +5,7 @@ build/ | ||||
| dist/ | ||||
| wheels/ | ||||
| *.egg-info | ||||
| main.spec | ||||
|  | ||||
| # Virtual environments | ||||
| .venv | ||||
| @@ -14,3 +15,4 @@ wheels/ | ||||
| inbox/ | ||||
| outbox/ | ||||
| themes/ | ||||
| draft/ | ||||
|   | ||||
							
								
								
									
										8
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								Makefile
									
									
									
									
									
								
							| @@ -1,3 +1,4 @@ | ||||
| default: build | ||||
| .PHONY: build install-tools install-dep init clean | ||||
|  | ||||
| init: install-dep install-tools | ||||
| @@ -15,3 +16,10 @@ install-dep: | ||||
|  | ||||
| clean: | ||||
| 	@rm -r ./dist ./build | ||||
|  | ||||
| install: | ||||
| ifeq ($(USER), root) | ||||
| 	@cp ./dist/main /usr/local/bin/blog | ||||
| else | ||||
| 	@cp ./dist/main ~/.local/bin/blog | ||||
| endif | ||||
|   | ||||
							
								
								
									
										21
									
								
								blog/blog.py
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								blog/blog.py
									
									
									
									
									
								
							| @@ -13,18 +13,14 @@ class Blog: | ||||
|         self.conf = conf | ||||
|         self.pages = dict() | ||||
|  | ||||
|     def load_pages(self): | ||||
|         """Charge tous les fichiers .md dans le dossier inbox""" | ||||
|         files_list = glob.glob(f"{self.conf.inbox}/*.md") | ||||
|     def make(self, draft: bool = False): | ||||
|         """Convertit les pages en un site html""" | ||||
|  | ||||
|         self.pages = dict() | ||||
|         for file in files_list: | ||||
|             self.pages[Path(file).stem] = Page(Path(file)) | ||||
|         self._load_pages(self.conf.inbox) | ||||
|  | ||||
|     def make(self): | ||||
|         """Convertit les pages en un site html""" | ||||
|         if not self.pages: | ||||
|             self.load_pages() | ||||
|         if draft: | ||||
|             self._load_pages(self.conf.draft) | ||||
|  | ||||
|         env = Environment(loader=FileSystemLoader(self.conf.theme)) | ||||
|  | ||||
| @@ -36,6 +32,12 @@ class Blog: | ||||
|         self._build_index(index_template) | ||||
|         self._build_rss() | ||||
|  | ||||
|     def _load_pages(self, path: Path): | ||||
|         """Charge tous les fichiers .md dans le dossier inbox""" | ||||
|         files_list = glob.glob(f"{path}/*.md") | ||||
|         for file in files_list: | ||||
|             self.pages[Path(file).stem] = Page(Path(file)) | ||||
|  | ||||
|     def _build_all_pages(self, template: Template): | ||||
|         """Convertit les pages markdown dans conf.inbox en html dans conf.outbox""" | ||||
|  | ||||
| @@ -70,6 +72,7 @@ class Blog: | ||||
|             feed.write(rss_file, 'utf-8') | ||||
|  | ||||
|     def _copy_css(self): | ||||
|         """Copie les fichiers CSS du theme vers l'export""" | ||||
|         css_path = Path(self.conf.theme) / "css" | ||||
|         dest_path = Path(self.conf.outbox) / "css" | ||||
|  | ||||
|   | ||||
| @@ -5,7 +5,15 @@ from pathlib import Path | ||||
| class Config: | ||||
|  | ||||
|     _conf = dict() | ||||
|     _list_valid_parameters = {"inbox", "outbox", "theme", "title", "presentation", "url"} | ||||
|     _list_valid_parameters = { | ||||
|         "inbox", | ||||
|         "outbox", | ||||
|         "theme", | ||||
|         "title", | ||||
|         "presentation", | ||||
|         "url", | ||||
|         "draft", | ||||
|     } | ||||
|  | ||||
|     def __init__(self, config_file: Path): | ||||
|         """Constructeur : charge les paramètres depuis le fichier de configuration""" | ||||
|   | ||||
							
								
								
									
										13
									
								
								blog/page.py
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								blog/page.py
									
									
									
									
									
								
							| @@ -4,19 +4,16 @@ import yaml | ||||
| import jinja2 | ||||
| import textwrap | ||||
| import markdown | ||||
| import subprocess | ||||
| from pathlib import Path | ||||
| from datetime import date | ||||
|  | ||||
| # from bs4 import BeautifulSoup as bs | ||||
|  | ||||
|  | ||||
| def new_page(title: str): | ||||
| def new_page(title: str, path: Path) -> Path: | ||||
|     """Crée un nouvel article à partir du titre de celui ci.""" | ||||
|  | ||||
|     today = date.today().strftime("%Y-%m-%d") | ||||
|  | ||||
|     filename = "./inbox/" + re.sub("[^a-zA-Z0-9-]", "_", title) + ".md" | ||||
|     filename = path + '/' + re.sub("[^a-zA-Z0-9-]", "_", title) + ".md" | ||||
|  | ||||
|     content = textwrap.dedent( | ||||
|         f"""\ | ||||
| @@ -27,7 +24,6 @@ def new_page(title: str): | ||||
|     tags: | ||||
|         - | ||||
|     --- | ||||
|     # {title} | ||||
|  | ||||
|     """ | ||||
|     ) | ||||
| @@ -36,7 +32,7 @@ def new_page(title: str): | ||||
|         with open(filename, "w") as file: | ||||
|             file.write(content) | ||||
|  | ||||
|     subprocess.run(["nvim", "+normal G$", "+startinsert", filename]) | ||||
|     return filename | ||||
|  | ||||
|  | ||||
| class Page: | ||||
| @@ -63,9 +59,6 @@ class Page: | ||||
|         if name not in self.meta: | ||||
|             raise AttributeError(f"L'attribut '{name}' n'existe pas.") | ||||
|  | ||||
|         # if name == date: | ||||
|         #     return datetime(self.meta['date'], "%Y-%m-%d") | ||||
|  | ||||
|         return self.meta[name] | ||||
|  | ||||
|     def _extract_meta_and_markdown(self, content: str) -> list: | ||||
|   | ||||
							
								
								
									
										36
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								main.py
									
									
									
									
									
								
							| @@ -2,6 +2,7 @@ | ||||
|  | ||||
| import os | ||||
| import argparse | ||||
| import subprocess | ||||
| from pathlib import Path | ||||
| from blog.page import new_page | ||||
| from blog.config import Config | ||||
| @@ -18,35 +19,42 @@ def load_args(): | ||||
|     parser = argparse.ArgumentParser(description="Gestion du blog") | ||||
|     parser.add_argument("action", choices=actions_list, help="L'action a réaliser.") | ||||
|     parser.add_argument( | ||||
|         "-c", | ||||
|         "--config", | ||||
|         default=".blog", | ||||
|         help="Chemin vers le fichier de configuration", | ||||
|     ) | ||||
|     parser.add_argument( | ||||
|         "-i", | ||||
|         "--inbox", | ||||
|         default="./inbox", | ||||
|         help="Chemin vers les fichiers markdown du blog", | ||||
|     ) | ||||
|     parser.add_argument("all", nargs=argparse.REMAINDER) | ||||
|     parser.add_argument( | ||||
|         "-d", | ||||
|         action="store_true", | ||||
|         help="Construit le blog avec les brouillons si spécifié.", | ||||
|     ) | ||||
|     parser.add_argument( | ||||
|         "--draft", | ||||
|         default="./draft", | ||||
|         help="Chemin vers les brouillons", | ||||
|     ) | ||||
|     parser.add_argument("all", nargs=argparse.REMAINDER, help=argparse.SUPPRESS) | ||||
|     return vars(parser.parse_args()) | ||||
|  | ||||
|  | ||||
| def load_make_args(args: str) -> dict: | ||||
|     """Charge les paramêtres spécifiques à l'action make""" | ||||
|     parser = argparse.ArgumentParser(description="Compile le blog") | ||||
|  | ||||
|     parser.add_argument( | ||||
|         "-t", | ||||
|         "--theme", | ||||
|         default="./themes/default", | ||||
|         help="Chemin vers le theme utilisé pour exporter le blog", | ||||
|         help="Chemin vers le theme utilisé", | ||||
|     ) | ||||
|     parser.add_argument( | ||||
|         "-o", | ||||
|         "--output", | ||||
|         default="./output", | ||||
|         help="Nom du dossier ou sera exporté le blog en html", | ||||
|         help="Nom du dossier où sera exporté le blog en html", | ||||
|     ) | ||||
|     return vars(parser.parse_args(args)) | ||||
|  | ||||
| @@ -65,15 +73,21 @@ def main(): | ||||
|     match args["action"]: | ||||
|         case "new": | ||||
|             page_title = " ".join(args["all"]) | ||||
|             new_page(page_title) | ||||
|  | ||||
|             path = conf.inbox | ||||
|             if args["d"]: | ||||
|                 path = conf.draft | ||||
|             subprocess.run( | ||||
|                 ["nvim", "+normal G$", "+startinsert", new_page(page_title, path)] | ||||
|             ) | ||||
|             os._exit(0) | ||||
|  | ||||
|         case "make": | ||||
|             args = load_make_args(args["all"]) | ||||
|             conf.overload(args) | ||||
|             make_args = load_make_args(args["all"]) | ||||
|             conf.overload(make_args) | ||||
|  | ||||
|             blog = Blog(conf) | ||||
|             blog.make() | ||||
|             blog.make(draft=args["d"]) | ||||
|  | ||||
|             os._exit(0) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user