Separated img alt to title and title to alt conversion

This commit is contained in:
2022-08-24 16:11:26 +02:00
parent 9911018114
commit 36db6c04ae
3 changed files with 11 additions and 9 deletions

View File

@@ -34,13 +34,13 @@ def StripTags(HTML, ToStrip): # Remove desired tags from the HTML
t.replace_with('')
return str(Soup)
def WriteImgAltAndTitle(HTML): # Adds alt or title attr. to <img> which only have one of them
def WriteImgAltAndTitle(HTML, AltToTitle, TitleToAlt): # Adds alt or title attr. to <img> which only have one of them
Soup = MkSoup(HTML)
Tags = Soup.find_all('img')
for t in Tags:
if 'alt' in t.attrs and 'title' not in t.attrs:
if AltToTitle and 'alt' in t.attrs and 'title' not in t.attrs:
t.attrs.update({'title': t.attrs['alt']})
elif 'title' in t.attrs and 'alt' not in t.attrs:
elif TitleToAlt and 'title' in t.attrs and 'alt' not in t.attrs:
t.attrs.update({'alt': t.attrs['title']})
return str(Soup)

View File

@@ -373,7 +373,7 @@ def DoMinifyHTML(HTML):
convert_charrefs=True,
keep_pre=True)
def MakeSite(OutputDir, TemplatesText, StaticPartsText, DynamicParts, DynamicPartsText, ConfMenu, GlobalMacros, SiteName, BlogName, SiteTagline, SiteTemplate, SiteDomain, SiteRoot, FolderRoots, SiteLang, Locale, Minify, NoScripts, ImgAltAndTitle, Sorting, MarkdownExts, AutoCategories):
def MakeSite(OutputDir, TemplatesText, StaticPartsText, DynamicParts, DynamicPartsText, ConfMenu, GlobalMacros, SiteName, BlogName, SiteTagline, SiteTemplate, SiteDomain, SiteRoot, FolderRoots, SiteLang, Locale, Minify, NoScripts, ImgAltToTitle, ImgTitleToAlt, Sorting, MarkdownExts, AutoCategories):
PagesPaths, PostsPaths, Pages, MadePages, Categories = [], [], [], [], {}
for Ext in FileExtensions['Pages']:
for File in Path('Pages').rglob(f"*.{Ext}"):
@@ -495,8 +495,8 @@ def MakeSite(OutputDir, TemplatesText, StaticPartsText, DynamicParts, DynamicPar
HTML = DoMinifyHTML(HTML)
if NoScripts:
HTML = StripTags(HTML, ['script'])
if ImgAltAndTitle:
HTML = WriteImgAltAndTitle(HTML)
if ImgAltToTitle or ImgTitleToAlt:
HTML = WriteImgAltAndTitle(HTML, ImgAltToTitle, ImgTitleToAlt)
WriteFile(PagePath, HTML)
MadePages += [[File, Content, Titles, Meta, ContentHTML, SlimHTML, Description, Image]]