Works I think
This commit is contained in:
		
							parent
							
								
									af84ba47b1
								
							
						
					
					
						commit
						f6208bc081
					
				
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| 
						 | 
					@ -1,3 +1,4 @@
 | 
				
			||||||
venv/
 | 
					venv/
 | 
				
			||||||
testdata/
 | 
					testdata/
 | 
				
			||||||
 | 
					__pycache__/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										37
									
								
								__main__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								__main__.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,37 @@
 | 
				
			||||||
 | 
					import markdown
 | 
				
			||||||
 | 
					import pathlib
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					current_dir = pathlib.Path(__file__).parent
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					with (current_dir / "base_feed.xml").open("r") as f:
 | 
				
			||||||
 | 
					    base_feed = f.read()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					with (current_dir / "base_item.xml").open("r") as f:
 | 
				
			||||||
 | 
					    base_item = f.read()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def convert_case(string: str):
 | 
				
			||||||
 | 
					    while string.find("_") != -1:
 | 
				
			||||||
 | 
					        string = string.replace("_" + string[string.index("_") + 1], string[string.index("_") +1].upper())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def article_to_xml(article_path: pathlib.Path):
 | 
				
			||||||
 | 
					    with (article_path / "article.md").open("r") as f:
 | 
				
			||||||
 | 
					        markdown_data = f.read()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    markdown_instance = markdown.Markdown(extensions=['meta', 'nl2br'])
 | 
				
			||||||
 | 
					    html = markdown_instance.convert(markdown_data)
 | 
				
			||||||
 | 
					    metadata = markdown_instance.Meta
 | 
				
			||||||
 | 
					    tags = [f"<{convert_case(key)}>{metadata[key][0]}</{convert_case(key)}>" for key in metadata.keys()]
 | 
				
			||||||
 | 
					    tags.append(f"<description>\n{html}\n</description>")
 | 
				
			||||||
 | 
					    return base_item.format(tags="\n".join(tags))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def make_rss_feed(path, channel_name, channel_link, description):
 | 
				
			||||||
 | 
					    articles = "".join([article_to_xml(path) for path in (pathlib.Path.cwd() / path).glob("*")])
 | 
				
			||||||
 | 
					    return base_feed \
 | 
				
			||||||
 | 
					            .replace("{title}", channel_name)       \
 | 
				
			||||||
 | 
					            .replace("{link}", channel_link)        \
 | 
				
			||||||
 | 
					            .replace("{description}", description)  \
 | 
				
			||||||
 | 
					            .replace("{items}", articles)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										11
									
								
								base_feed.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								base_feed.xml
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,11 @@
 | 
				
			||||||
 | 
					<?xml version="1.0" encoding="UTF-8" ?>
 | 
				
			||||||
 | 
					<rss version="2.0">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<channel>
 | 
				
			||||||
 | 
					<title>{title}</title>
 | 
				
			||||||
 | 
					<link>{link}</link>
 | 
				
			||||||
 | 
					<description>{description}</description>
 | 
				
			||||||
 | 
					{items}
 | 
				
			||||||
 | 
					</channel>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					</rss>
 | 
				
			||||||
							
								
								
									
										3
									
								
								base_item.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								base_item.xml
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,3 @@
 | 
				
			||||||
 | 
					<item>
 | 
				
			||||||
 | 
					{tags}
 | 
				
			||||||
 | 
					</item>
 | 
				
			||||||
		Loading…
	
		Reference in a new issue