Add inner_html tag attribute

Allows providing tag contant in the site configuration file.

This is a rebase of #60 on top of the latest master.
This commit is contained in:
Alexey Leshchenko 2022-03-19 14:13:41 +02:00 committed by Martynas Mickevičius
parent 46c77076ab
commit 5c9ff6a7ea
No known key found for this signature in database
GPG Key ID: E735DF276C508071
2 changed files with 29 additions and 11 deletions

View File

@ -162,6 +162,13 @@ theme = "dark"
type="text/javascript" type="text/javascript"
src="/example/custom-script.js" src="/example/custom-script.js"
# the following injects this script in the <head>:
# <script>console.log("Hello, world!")</script>
[[site.inject.body.script]]
inner_html="""
console.log("Hello, world!")
"""
## Individual Page Settings ## ## Individual Page Settings ##
# the [pages] table defines override settings for individual pages, by defining # the [pages] table defines override settings for individual pages, by defining
# a sub-table named after the page url (or part of the url, but careful about # a sub-table named after the page url (or part of the url, but careful about

View File

@ -625,6 +625,12 @@ class Parser:
for element in elements: for element in elements:
injected_tag = soup.new_tag(tag) injected_tag = soup.new_tag(tag)
for attr, value in element.items(): for attr, value in element.items():
# `inner_html` refers to the tag's inner content
# and will be added later
if attr == "inner_html":
continue
injected_tag[attr] = value injected_tag[attr] = value
# if the value refers to a file, copy it to the dist folder # if the value refers to a file, copy it to the dist folder
if attr.lower() in ["href", "src"]: if attr.lower() in ["href", "src"]:
@ -636,6 +642,11 @@ class Parser:
# shutil.copyfile(source, destination) # shutil.copyfile(source, destination)
injected_tag[attr] = str(cached_custom_file) # source.name injected_tag[attr] = str(cached_custom_file) # source.name
log.debug(f"Injecting <{section}> tag: {injected_tag}") log.debug(f"Injecting <{section}> tag: {injected_tag}")
# adding `inner_html` as the tag's content
if "inner_html" in element:
injected_tag.string = element["inner_html"]
soup.find(section).append(injected_tag) soup.find(section).append(injected_tag)
def inject_loconotion_script_and_css(self, soup): def inject_loconotion_script_and_css(self, soup):