From 9a98577fdaf5a238d98a9c0b584a0d367caf7c5e Mon Sep 17 00:00:00 2001 From: octospacc Date: Tue, 14 Mar 2023 15:21:27 +0100 Subject: [PATCH] Handle lowercase extensions; Misc fixes --- App/Source/Build.py | 4 ++-- App/Source/Modules/Meta.py | 3 ++- App/Source/Modules/Site.py | 32 ++++++++++++++++---------------- App/Source/Modules/Social.py | 3 +++ App/Source/Modules/Utils.py | 21 ++++++++++++++++++++- App/TODO | 4 +--- README.md | 1 + 7 files changed, 45 insertions(+), 23 deletions(-) diff --git a/App/Source/Build.py b/App/Source/Build.py index fda61d1..f7b8de2 100644 --- a/App/Source/Build.py +++ b/App/Source/Build.py @@ -35,10 +35,10 @@ def ResetOutDir(OutDir:str): def DelTmp(OutDir:str): for Ext in FileExtensions['Tmp']: - for File in Path(OutDir).rglob(f'*.{Ext}'): + for File in Path(OutDir).rglob(AnyCaseGlob(f'*.{Ext}')): os.remove(File) for Dir in (OutDir, f'{OutDir}.gmi'): - for File in Path(Dir).rglob('*.tmp'): + for File in Path(Dir).rglob(AnyCaseGlob('*.tmp')): os.remove(File) def SetSorting(Sorting:dict): diff --git a/App/Source/Modules/Meta.py b/App/Source/Modules/Meta.py index a0915e6..b8e9af3 100644 --- a/App/Source/Modules/Meta.py +++ b/App/Source/Modules/Meta.py @@ -58,10 +58,11 @@ def GetHTMLPagesList(Flags:dict, Pages:list, PathPrefix:str, CallbackFile=None, if LastParent != CurParent and ShowPaths: LastParent = CurParent Levels = '.' * ((Depth-2+i) if not Flatten else 0) + ':' - # If search node endswith index, it's a page; else, it's a folder + # It's a page if StripExt(File).endswith('index'): Title = MakeListTitle(File, Meta, Titles, 'HTMLTitle', f.BlogName, PathPrefix) DoneCount += 1 + # It's a folder else: Title = CurParent[Depth-2+i] if SingleLine: diff --git a/App/Source/Modules/Site.py b/App/Source/Modules/Site.py index 9805830..4db9c07 100644 --- a/App/Source/Modules/Site.py +++ b/App/Source/Modules/Site.py @@ -49,23 +49,23 @@ def HandleDynamicParts(Flags:dict, Html:str, Snippets:dict): # TODO: This would need to be handled either fully before or fully after after all pages' content has been transformed to HTML, else other markups end up in HTML and the page is broken def HandleTransclusions(Base:str, Caller:str, Pages:list): - Targets = [] - Finding = Base - Start = Finding.find('{{') - while Start != -1: - Start = Start + 2 - Finding = Finding[Start:] - Stop = Finding.find('}}') - if Stop != -1: - Targets += [Finding[:Stop]] - Start = Finding.find('{{') - for Target in Targets: - # We should show an error message on inexistant transclusion and possible recursive transclusion, as currently this doesn't handle escaped tokens - if Target != Caller: + for Target in StrFindWrapped(Base, '{{', '}}'): + # Recursive transclusion + if Target.lower() in (Caller.lower(), StripExt(Caller.lower())): + Base = ReplWithEsc(Base, '{{' + Target + '}}', f'(staticoso - Error - Recursive transclusion of "{Target}")') + # Allowed transclusion + else: + Transed = False for File, Content, _, _ in Pages: - if File.lower() == Target.lower(): + # Target exists, transclude its content + if Target.lower() in (File.lower(), StripExt(File.lower())): + print(Target) Base = ReplWithEsc(Base, '{{' + Target + '}}', Content) + Transed = True break + # No target with that name, replace with blank string + if not Transed: + Base = ReplWithEsc(Base, '{{' + Target + '}}', '') return Base def PatchHtml(Flags:dict, Pages:list, Page:dict, Context:dict, Snippets:dict, Locale:dict, LightRun): @@ -338,7 +338,7 @@ def FindPagesPaths(): Paths = {"Pages":[], "Posts":[]} for Ext in FileExtensions['Pages']: for Type in ('Pages', 'Posts'): - for File in Path(Type).rglob(f'*.{Ext}'): + for File in Path(Type).rglob(AnyCaseGlob(f'*.{Ext}')): Paths[Type] += [FileToStr(File, f'{Type}/')] return Paths @@ -369,7 +369,7 @@ def MakeAutoCategories(Flags:dict, Categories): Dir = f'{OutDir}/Categories' for Cat in Categories: Exists = False - for File in Path(Dir).rglob(str(Cat)+'.*'): + for File in Path(Dir).rglob(AnyCaseGlob(str(Cat)+'.*')): Exists = True break if not Exists: diff --git a/App/Source/Modules/Social.py b/App/Source/Modules/Social.py index 72583d4..15f54c3 100644 --- a/App/Source/Modules/Social.py +++ b/App/Source/Modules/Social.py @@ -44,7 +44,10 @@ def ApplySocialIntegrations(Flags, Pages, LimitFiles, Locale): break #Content = ReplWithEsc(Content, '[staticoso:Comments]', Post) Content = ReplWithEsc(Content, '', Post) + # BeautifulSoup issue Content = ReplWithEsc(Content, '', Post) + Content = ReplWithEsc(Content, '', Post) + #Content = ReplWithEsc(Content, '', Post) WriteFile(File, Content) FinalPaths += [File] diff --git a/App/Source/Modules/Utils.py b/App/Source/Modules/Utils.py index 47241d4..ea6584d 100644 --- a/App/Source/Modules/Utils.py +++ b/App/Source/Modules/Utils.py @@ -38,6 +38,12 @@ def WriteFile(p, c, m='w'): logging.error(f"[E] Error writing file {p}") return False +def AnyCaseGlob(Glob:str): + New = '' + for c in Glob: + New += '[%s%s]' % (c.lower(), c.upper()) if c.isalpha() else c + return New + def FileToStr(File:str, Truncate:str=''): return str(File)[len(Truncate):] @@ -49,7 +55,7 @@ def LoadFromDir(Dir:str, Matchs:list): Contents = {} Matchs = SureList(Matchs) for Match in Matchs: - for File in Path(Dir).rglob(Match): + for File in Path(Dir).rglob(AnyCaseGlob(Match)): if os.path.isfile(File): Name = str(File)[len(Dir)+1:] Contents.update({Name: ReadFile(File)}) @@ -89,6 +95,19 @@ def FindAllIndex(Str:str, Sub:str): yield i i = Str.find(Sub, i+1) +# Find all occurrences of substrings wrapped inside some characters +def StrFindWrapped(Str:str, Left:str, Right:str): + Res = [] + Start = Str.find(Left) + while Start != -1: + Start = Start + len(Left) + Str = Str[Start:] + Stop = Str.find(Right) + if Stop != -1: + Res += [Str[:Stop]] + Start = Str.find(Left) + return Res + # Replace substrings in a string, except when an escape char is prepended def ReplWithEsc(Str:str, Find:str, Repl:str, Html:bool=True, Esc:str='\\'): New = '' diff --git a/App/TODO b/App/TODO index bce004c..b9d5f9a 100644 --- a/App/TODO +++ b/App/TODO @@ -30,11 +30,10 @@ - Override internal HTML snippets (meta lines, page lists, redirects, ...) with config file in Templates/NAME.ini - Specify input folder(s) - Show page size/words/time in meta line -- Add feed support for HTML Journal pages +- Add feed support for HTML Journal pages directly, without the external tool - Fix excess whitespace in some section/menu titles - Parity presence for [] and <> internal macro enclosure, + streamline the code for that - Investigate a strange bug with Macros -- Handle file extensions with any case sensitivity, not just lowercase; currently the bulk of the issue is finding the files on disk - Test sorting by date for files not starting with date, and dated folders - Fix arguments - some are only callable from CLI and not Site.ini - make them coherent with INI categories - Change FolderRoots arg name to CustomPaths @@ -53,4 +52,3 @@ - Exporting sites to different formats (?) (single-page HTML, PDF, EPUB, ...) - Disable ActivityPub feed for a specific page - Symlinks and direct copies as option instead of redirect pages -- Automatic redirects/symlinks for making pages work without .html suffix diff --git a/README.md b/README.md index 45f8cc7..14d40ef 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ All of this is because some crucial things might be changed from one commit to a ### Features +- [ ] Page transclusions - [x] Generation of simplified pages compliant with the [HTML Journal standard](https://journal.miso.town) - [x] HTML feeds (pages with list of N most recent posts) - [x] Lists of all pages in a site directory