mirror of
				https://gitlab.com/octtspacc/staticoso
				synced 2025-06-05 22:09:23 +02:00 
			
		
		
		
	Add local macros
This commit is contained in:
		| @@ -21,6 +21,7 @@ Feel free to experiment with all of this stuff! | |||||||
| - [html2gmi](https://github.com/LukeEmmet/html2gmi) | - [html2gmi](https://github.com/LukeEmmet/html2gmi) | ||||||
|  |  | ||||||
| ## Features roadmap | ## Features roadmap | ||||||
|  | - [ ] Local (per-page) and global (per-site) macros | ||||||
| - [x] ActivityPub (Mastodon) support (Feed + embedded comments) | - [x] ActivityPub (Mastodon) support (Feed + embedded comments) | ||||||
| - [ ] Polished Gemtext generation | - [ ] Polished Gemtext generation | ||||||
| - [x] Autodetection of pages and posts | - [x] Autodetection of pages and posts | ||||||
|   | |||||||
| @@ -71,7 +71,7 @@ def GetConfMenu(Entries, MarkdownExts): | |||||||
|  |  | ||||||
| def Main(Args, FeedEntries): | def Main(Args, FeedEntries): | ||||||
| 	HavePages, HavePosts = False, False | 	HavePages, HavePosts = False, False | ||||||
| 	SiteConf = LoadConf('Site.ini') | 	SiteConf = LoadConfFile('Site.ini') | ||||||
|  |  | ||||||
| 	SiteName = Args.SiteName if Args.SiteName else ReadConf(SiteConf, 'Site', 'Name') if ReadConf(SiteConf, 'Site', 'Name') else '' | 	SiteName = Args.SiteName if Args.SiteName else ReadConf(SiteConf, 'Site', 'Name') if ReadConf(SiteConf, 'Site', 'Name') else '' | ||||||
| 	BlogName = Args.BlogName if Args.BlogName else ReadConf(SiteConf, 'Site', 'BlogName') if ReadConf(SiteConf, 'Site', 'BlogName') else '' | 	BlogName = Args.BlogName if Args.BlogName else ReadConf(SiteConf, 'Site', 'BlogName') if ReadConf(SiteConf, 'Site', 'BlogName') else '' | ||||||
|   | |||||||
| @@ -7,14 +7,22 @@ | |||||||
| |   Copyright (C) 2022, OctoSpacc     | | |   Copyright (C) 2022, OctoSpacc     | | ||||||
| | ================================= """ | | ================================= """ | ||||||
|  |  | ||||||
|  | import io | ||||||
| import configparser | import configparser | ||||||
| from ast import literal_eval | from ast import literal_eval | ||||||
|  |  | ||||||
| def LoadConf(File): | def LoadConfFile(File): | ||||||
| 	Conf = configparser.ConfigParser() | 	Conf = configparser.ConfigParser() | ||||||
|  | 	Conf.optionxform = lambda option: option | ||||||
| 	Conf.read(File) | 	Conf.read(File) | ||||||
| 	return Conf | 	return Conf | ||||||
|  |  | ||||||
|  | def LoadConfStr(Str): | ||||||
|  | 	Conf = configparser.ConfigParser() | ||||||
|  | 	Conf.optionxform = lambda option: option | ||||||
|  | 	Conf.read_string(Str) | ||||||
|  | 	return Conf | ||||||
|  |  | ||||||
| def ReadConf(Conf, Sect, Opt=None): | def ReadConf(Conf, Sect, Opt=None): | ||||||
| 	if Opt: | 	if Opt: | ||||||
| 		if Conf.has_option(Sect, Opt): | 		if Conf.has_option(Sect, Opt): | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ | |||||||
|  |  | ||||||
| from Libs import htmlmin | from Libs import htmlmin | ||||||
| from Libs.bs4 import BeautifulSoup | from Libs.bs4 import BeautifulSoup | ||||||
|  | from Modules.Config import * | ||||||
| from Modules.HTML import * | from Modules.HTML import * | ||||||
| from Modules.Markdown import * | from Modules.Markdown import * | ||||||
| from Modules.Pug import * | from Modules.Pug import * | ||||||
| @@ -112,7 +113,7 @@ def GetHTMLPagesList(Pages, BlogName, SiteRoot, PathPrefix, Unite=[], Type='Page | |||||||
|  |  | ||||||
| def Preprocessor(Path, SiteRoot): | def Preprocessor(Path, SiteRoot): | ||||||
| 	File = ReadFile(Path) | 	File = ReadFile(Path) | ||||||
| 	Content, Titles, DashyTitles, HTMLTitlesFound, Meta = '', [], [], False, { | 	Content, Titles, DashyTitles, HTMLTitlesFound, Macros, Meta = '', [], [], False, '', { | ||||||
| 		'Template': 'Standard.html', | 		'Template': 'Standard.html', | ||||||
| 		'Style': '', | 		'Style': '', | ||||||
| 		'Type': '', | 		'Type': '', | ||||||
| @@ -122,6 +123,7 @@ def Preprocessor(Path, SiteRoot): | |||||||
| 		'HTMLTitle': '', | 		'HTMLTitle': '', | ||||||
| 		'Description': '', | 		'Description': '', | ||||||
| 		'Image': '', | 		'Image': '', | ||||||
|  | 		'Macros': {}, | ||||||
| 		'Categories': [], | 		'Categories': [], | ||||||
| 		'CreatedOn': '', | 		'CreatedOn': '', | ||||||
| 		'EditedOn': '', | 		'EditedOn': '', | ||||||
| @@ -134,7 +136,9 @@ def Preprocessor(Path, SiteRoot): | |||||||
| 				ItemText = '{}: '.format(Item) | 				ItemText = '{}: '.format(Item) | ||||||
| 				if lss.startswith(ItemText): | 				if lss.startswith(ItemText): | ||||||
| 					Meta[Item] = lss[len(ItemText):] | 					Meta[Item] = lss[len(ItemText):] | ||||||
| 			if lss.startswith('Categories: '): | 			if lss.startswith('$'): | ||||||
|  | 				Macros += lss[1:].lstrip() + '\n' | ||||||
|  | 			elif lss.startswith('Categories: '): | ||||||
| 				for i in lss[len('Categories: '):].split(' '): | 				for i in lss[len('Categories: '):].split(' '): | ||||||
| 					Meta['Categories'] += [i] | 					Meta['Categories'] += [i] | ||||||
| 			elif lss.startswith('Background: '): | 			elif lss.startswith('Background: '): | ||||||
| @@ -181,6 +185,12 @@ def Preprocessor(Path, SiteRoot): | |||||||
| 							Content += MakeLinkableTitle(l, Title, DashTitle, 'pug') + '\n' | 							Content += MakeLinkableTitle(l, Title, DashTitle, 'pug') + '\n' | ||||||
| 				else: | 				else: | ||||||
| 					Content += l + '\n' | 					Content += l + '\n' | ||||||
|  | 	#if Macros: | ||||||
|  | 	Meta['Macros'] = ReadConf(LoadConfStr('[Macros]\n' + Macros), 'Macros') | ||||||
|  | 	#	Macros = '[Macros]\n' + Macros | ||||||
|  | 	#	Macros = LoadConfStr(Macros) | ||||||
|  | 	#	Macros = ReadConf(Macros, 'Macros') | ||||||
|  | 	#	Meta['Macros'] = Macros | ||||||
| 	return Content, Titles, Meta | 	return Content, Titles, Meta | ||||||
|  |  | ||||||
| def MakeListTitle(File, Meta, Titles, Prefer, SiteRoot, BlogName, PathPrefix=''): | def MakeListTitle(File, Meta, Titles, Prefer, SiteRoot, BlogName, PathPrefix=''): | ||||||
| @@ -279,19 +289,23 @@ def PatchHTML(File, HTML, PartsText, ContextParts, ContextPartsText, HTMLPagesLi | |||||||
| 	HTML = ReplWithEsc(HTML, '[HTML:Site:Name]', SiteName) | 	HTML = ReplWithEsc(HTML, '[HTML:Site:Name]', SiteName) | ||||||
| 	HTML = ReplWithEsc(HTML, '[HTML:Site:AbsoluteRoot]', SiteRoot) | 	HTML = ReplWithEsc(HTML, '[HTML:Site:AbsoluteRoot]', SiteRoot) | ||||||
| 	HTML = ReplWithEsc(HTML, '[HTML:Site:RelativeRoot]', GetPathLevels(PagePath)) | 	HTML = ReplWithEsc(HTML, '[HTML:Site:RelativeRoot]', GetPathLevels(PagePath)) | ||||||
| 	for i in FolderRoots: | 	for e in Meta['Macros']: | ||||||
| 		HTML = HTML.replace('[HTML:Folder:{}:AbsoluteRoot]'.format(i), FolderRoots[i]) | 		HTML = HTML.replace(f"{{{{{e}}}}}", Meta['Macros'][e]).replace(f"{{{{ {e} }}}}", Meta['Macros'][e]) | ||||||
| 	for i in Categories: | 	for e in FolderRoots: | ||||||
| 		HTML = HTML.replace('<span>[HTML:Category:{}]</span>'.format(i), Categories[i]) | 		HTML = HTML.replace('[HTML:Folder:{}:AbsoluteRoot]'.format(e), FolderRoots[e]) | ||||||
|  | 	for e in Categories: | ||||||
|  | 		HTML = HTML.replace('<span>[HTML:Category:{}]</span>'.format(e), Categories[e]) | ||||||
|  |  | ||||||
| 	# TODO: Clean this doubling? | 	# TODO: Clean this doubling? | ||||||
| 	ContentHTML = Content | 	ContentHTML = Content | ||||||
| 	ContentHTML = ContentHTML.replace('[HTML:Site:AbsoluteRoot]', SiteRoot) | 	ContentHTML = ContentHTML.replace('[HTML:Site:AbsoluteRoot]', SiteRoot) | ||||||
| 	ContentHTML = ContentHTML.replace('[HTML:Site:RelativeRoot]', GetPathLevels(PagePath)) | 	ContentHTML = ContentHTML.replace('[HTML:Site:RelativeRoot]', GetPathLevels(PagePath)) | ||||||
| 	for i in FolderRoots: | 	for e in Meta['Macros']: | ||||||
| 		ContentHTML = ContentHTML.replace('[HTML:Folder:{}:AbsoluteRoot]'.format(i), FolderRoots[i]) | 		ContentHTML = ContentHTML.replace(f"{{{{{e}}}}}", Meta['Macros'][e]).replace(f"{{{{ {e} }}}}", Meta['Macros'][e]) | ||||||
| 	for i in Categories: | 	for e in FolderRoots: | ||||||
| 		ContentHTML = ContentHTML.replace('<span>[HTML:Category:{}]</span>'.format(i), Categories[i]) | 		ContentHTML = ContentHTML.replace('[HTML:Folder:{}:AbsoluteRoot]'.format(e), FolderRoots[e]) | ||||||
|  | 	for e in Categories: | ||||||
|  | 		ContentHTML = ContentHTML.replace('<span>[HTML:Category:{}]</span>'.format(e), Categories[e]) | ||||||
| 	SlimHTML = HTMLPagesList + ContentHTML | 	SlimHTML = HTMLPagesList + ContentHTML | ||||||
|  |  | ||||||
| 	return HTML, ContentHTML, SlimHTML, Description, Image | 	return HTML, ContentHTML, SlimHTML, Description, Image | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user