From 727121201d4aa99c046d2a3efafbe6e3ffdbfb8a Mon Sep 17 00:00:00 2001 From: Alexey Leshchenko Date: Fri, 18 Feb 2022 11:23:43 +0300 Subject: [PATCH] Move `inject_custom_tags` out of `parse_page` Makes `parse_page` more readable --- loconotion/notionparser.py | 49 ++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/loconotion/notionparser.py b/loconotion/notionparser.py index 17b527f..3d6dba8 100644 --- a/loconotion/notionparser.py +++ b/loconotion/notionparser.py @@ -507,27 +507,8 @@ class Parser: # inject any custom elements to the page custom_injects = self.get_page_config(url).get("inject", {}) - def injects_custom_tags(section): - section_custom_injects = custom_injects.get(section, {}) - for tag, elements in section_custom_injects.items(): - for element in elements: - injected_tag = soup.new_tag(tag) - for attr, value in element.items(): - injected_tag[attr] = value - # if the value refers to a file, copy it to the dist folder - if attr.lower() == "href" or attr.lower() == "src": - log.debug(f"Copying injected file '{value}'") - cached_custom_file = self.cache_file( - (Path.cwd() / value.strip("/")) - ) - # destination = (self.dist_folder / source.name) - # shutil.copyfile(source, destination) - injected_tag[attr] = str(cached_custom_file) # source.name - log.debug(f"Injecting <{section}> tag: {str(injected_tag)}") - soup.find(section).append(injected_tag) - - injects_custom_tags("head") - injects_custom_tags("body") + self.inject_custom_tags("head", soup, custom_injects) + self.inject_custom_tags("body", soup, custom_injects) # inject loconotion's custom stylesheet and script loconotion_custom_css = self.cache_file(Path("bundles/loconotion.css")) @@ -660,6 +641,32 @@ class Parser: # if so, run the function again self.open_toggle_blocks(timeout, opened_toggles) + def inject_custom_tags(self, section: str, soup, custom_injects: dict): + """Inject custom tags to the given section. + + Args: + section (str): Section / tag name to insert into. + soup (BeautifulSoup): a BeautifulSoup element holding the whole page. + custom_injects (dict): description of custom tags to inject. + """ + section_custom_injects = custom_injects.get(section, {}) + for tag, elements in section_custom_injects.items(): + for element in elements: + injected_tag = soup.new_tag(tag) + for attr, value in element.items(): + injected_tag[attr] = value + # if the value refers to a file, copy it to the dist folder + if attr.lower() in ["href", "src"]: + log.debug(f"Copying injected file '{value}'") + cached_custom_file = self.cache_file( + (Path.cwd() / value.strip("/")) + ) + # destination = (self.dist_folder / source.name) + # shutil.copyfile(source, destination) + injected_tag[attr] = str(cached_custom_file) # source.name + log.debug(f'Injecting <{section}> tag: {injected_tag}') + soup.find(section).append(injected_tag) + def load(self, url): self.driver.get(url) WebDriverWait(self.driver, 60).until(notion_page_loaded())