-
Notifications
You must be signed in to change notification settings - Fork 1
Sourcery Starbot ⭐ refactored amitkot/obsidian-html #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,9 +21,8 @@ | |
| # Helper functions | ||
| # -------------------------------- | ||
| def get_paths(): | ||
| paths = {} | ||
| this_file_path = os.path.realpath(__file__) # (..)/obsidian-html/ci/tests/__filename__ | ||
| paths['ci_tests'] = Path(this_file_path).parent # (..)/obsidian-html/ci/tests/ | ||
| paths = {'ci_tests': Path(this_file_path).parent} | ||
| paths['root'] = paths['ci_tests'].parent.parent # (..)/obsidian-html/ | ||
| paths['sys_default_config'] = paths['root'].joinpath('obsidianhtml/src/defaults_config.yml') # (..)/obsidian-html/obsidianhtml/src/defaults_config.yml | ||
| paths['ci_configs'] = paths['root'].joinpath('ci/configs') # (..)/obsidian-html/ci/configs | ||
|
|
@@ -32,7 +31,7 @@ def get_paths(): | |
| paths['temp_cfg'] = paths['temp_dir'].joinpath('config.yml') # (..)/obsidian-html/tmp/config.yml | ||
| paths['html_output_folder'] = paths['temp_dir'].joinpath('html') # (..)/obsidian-html/tmp/html | ||
| paths['config_yaml'] = paths['ci_configs'].joinpath('default_settings.yml') # (..)/obsidian-html/ci/configs/default_settings.yml | ||
|
|
||
| return paths | ||
|
|
||
| def convert_vault(): | ||
|
|
@@ -70,10 +69,7 @@ def html_get(path, output_dict=False, convert=False): | |
| else: | ||
| soup = BeautifulSoup(response.text, 'html.parser', features="html5lib") | ||
|
|
||
| if output_dict: | ||
| return {'soup': soup, 'url': url} | ||
| else: | ||
| return soup | ||
| return {'soup': soup, 'url': url} if output_dict else soup | ||
|
Comment on lines
-73
to
+72
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def get_default_config(): | ||
| paths = get_paths() | ||
|
|
@@ -90,10 +86,7 @@ def get_default_config(): | |
| with open(paths['config_yaml'], 'r', encoding="utf-8") as f: | ||
| default_config = yaml.safe_load(f.read()) | ||
|
|
||
| # Merge the two | ||
| config = MergeDictRecurse(sys_config, default_config) | ||
|
|
||
| return config | ||
| return MergeDictRecurse(sys_config, default_config) | ||
|
Comment on lines
-93
to
+89
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| def customize_default_config(items, write_to_tmp_config=True): | ||
| # Example Input | ||
|
|
@@ -274,7 +267,7 @@ def test_C_rss(self): | |
| self.scribe('rss should exist and the values should be filled in correctly.') | ||
|
|
||
| rss = GetRssSoup('obs.html/rss/feed.xml') | ||
|
|
||
|
Comment on lines
-277
to
+270
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| # test setting title, description, pubdate with frontmatter yaml | ||
| item1 = [x for x in rss['articles'] if x['link'].strip() == "https://localhost:8888/rss/rss_index.html"][0] | ||
| self.assertEqual(item1['title'], 'test_value_title') | ||
|
|
@@ -286,7 +279,13 @@ def test_C_rss(self): | |
| self.assertEqual(item2['title'], 'rss_h1_test') | ||
|
|
||
| # test folder exclusion | ||
| self.assertTrue(len([x for x in rss['articles'] if x['link'].strip() == "https://localhost:8888/rss_exclude1.html"]) == 0) | ||
| self.assertTrue( | ||
| not [ | ||
| x | ||
| for x in rss['articles'] | ||
| if x['link'].strip() == "https://localhost:8888/rss_exclude1.html" | ||
| ] | ||
| ) | ||
|
|
||
| def test_D_images(self): | ||
| self.scribe('image link should point to correct location and the image should be downloadable.') | ||
|
|
@@ -453,17 +452,17 @@ def test_if_custom_template_is_used(self): | |
| # Args | ||
| run_setup = False | ||
| run_cleanup = False | ||
| for i, v in enumerate(sys.argv): | ||
| if v == '-r': | ||
| run_setup = True | ||
| for v in sys.argv: | ||
| if v == '-c': | ||
| run_cleanup = True | ||
| if v == 'v': | ||
| elif v == '-r': | ||
| run_setup = True | ||
| elif v == 'v': | ||
| verbose = True | ||
|
|
||
| # get paths | ||
| paths = get_paths() | ||
|
|
||
|
Comment on lines
-456
to
+465
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| # Create temp dir | ||
| os.chdir(paths['root']) | ||
| paths['temp_dir'].mkdir(exist_ok=True) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,10 +54,7 @@ def CreateIndexFromTags(pb): | |
|
|
||
| # Find notes with given tags | ||
| _files = {} | ||
| index_dict = {} | ||
| for t in include_tags: | ||
| index_dict[t] = [] | ||
|
|
||
| index_dict = {t: [] for t in include_tags} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| for k in files.keys(): | ||
| # Determine src file path | ||
| page_path_str = files[k]['fullpath'] | ||
|
|
@@ -86,7 +83,7 @@ def CreateIndexFromTags(pb): | |
| if pb.gc('toggles','verbose_printout'): | ||
| print(f'\t\tMatched note {k} on tag {t}') | ||
| matched = True | ||
|
|
||
| if matched == False: | ||
| continue | ||
|
|
||
|
|
@@ -98,37 +95,36 @@ def CreateIndexFromTags(pb): | |
| # determine sorting value | ||
| sort_value = None | ||
| if method not in ('none', 'creation_time', 'modified_time'): | ||
| if method == 'key_value': | ||
| # key can be multiple levels deep, like this: level1:level2 | ||
| # get the value of the key | ||
| key_found = True | ||
| value = metadata | ||
| for key in key_path.split(':'): | ||
| if key not in value.keys(): | ||
| key_found = False | ||
| break | ||
| value = value[key] | ||
| if method != 'key_value': | ||
| raise Exception(f'Sort method {method} not implemented. Check spelling.') | ||
|
|
||
| # key can be multiple levels deep, like this: level1:level2 | ||
| # get the value of the key | ||
| key_found = True | ||
| value = metadata | ||
| for key in key_path.split(':'): | ||
| if key not in value.keys(): | ||
| key_found = False | ||
| break | ||
| value = value[key] | ||
| # only continue if the key is found in the current note, otherwise the | ||
| # default sort value of None is kept | ||
| if key_found: | ||
| # for a list find all items that start with the value prefix | ||
| # then remove the value_prefix, and check if we have 1 item left | ||
| if isinstance(value, list): | ||
| items = [x.replace(value_prefix, '', 1) for x in value if x.startswith(value_prefix)] | ||
| if len(items) == 1: | ||
| sort_value = items[0] | ||
| # done | ||
| if isinstance(value, str): | ||
| sort_value = value.replace(value_prefix, '', 1) | ||
| if isinstance(value, bool): | ||
| sort_value = str(int(value)) | ||
| if isinstance(value, int) or isinstance(value, float): | ||
| sort_value = str(value) | ||
| if isinstance(value, datetime.datetime): | ||
| sort_value = value.isoformat() | ||
| else: | ||
| raise Exception(f'Sort method {method} not implemented. Check spelling.') | ||
|
|
||
| if key_found: | ||
| # for a list find all items that start with the value prefix | ||
| # then remove the value_prefix, and check if we have 1 item left | ||
| if isinstance(value, list): | ||
| items = [x.replace(value_prefix, '', 1) for x in value if x.startswith(value_prefix)] | ||
| if len(items) == 1: | ||
| sort_value = items[0] | ||
| # done | ||
| if isinstance(value, str): | ||
| sort_value = value.replace(value_prefix, '', 1) | ||
| if isinstance(value, bool): | ||
| sort_value = str(int(value)) | ||
| if isinstance(value, (int, float)): | ||
| sort_value = str(value) | ||
| if isinstance(value, datetime.datetime): | ||
| sort_value = value.isoformat() | ||
| # Get sort_value from files dict | ||
| if method in ('creation_time', 'modified_time'): | ||
| # created time is not really accessible under Linux, we might add a case for OSX | ||
|
|
@@ -158,19 +154,16 @@ def CreateIndexFromTags(pb): | |
| ) | ||
|
|
||
| if len(_files.keys()) == 0: | ||
| raise Exception(f"No notes found with the given tags.") | ||
| raise Exception("No notes found with the given tags.") | ||
|
|
||
| if pb.gc('toggles','verbose_printout'): | ||
| print(f'\tBuilding index.md') | ||
|
|
||
| index_md_content = f'# {pb.gc("site_name")}\n' | ||
| for t in index_dict.keys(): | ||
| for t, notes in index_dict.items(): | ||
| # Add header | ||
| index_md_content += f'## {t}\n' | ||
|
|
||
| # shorthand | ||
| notes = index_dict[t] | ||
|
|
||
| # fill in none types | ||
| # - get max value | ||
| input_val = '' | ||
|
|
@@ -191,7 +184,7 @@ def CreateIndexFromTags(pb): | |
| input_val = '' | ||
| if sort_reverse: | ||
| input_val = max_val | ||
|
|
||
| # - fill in none types | ||
| for n in notes: | ||
| if n['sort_value'] is None: | ||
|
|
@@ -218,14 +211,14 @@ def CreateIndexFromTags(pb): | |
|
|
||
| if pb.gc('toggles','verbose_printout'): | ||
| print(f'\tAdding graph links between index.md and the matched notes') | ||
|
|
||
| node = pb.network_tree.NewNode() | ||
| node['id'] = 'index' | ||
| node['url'] = f'{pb.gc("html_url_prefix")}/index.html' | ||
| pb.network_tree.AddNode(node) | ||
| bln = node | ||
| for t in index_dict.keys(): | ||
| for n in index_dict[t]: | ||
| for t, value_ in index_dict.items(): | ||
| for n in value_: | ||
| node = pb.network_tree.NewNode() | ||
| node['id'] = n['graph_name'] | ||
| node['url'] = f'{pb.gc("html_url_prefix")}/{n["md_rel_path_str"][:-3]}.html' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,23 +56,22 @@ def ConvertMarkdownToHeaderTree(code): | |
| # no string after '[#]* ' makes an invalid header, discard | ||
| if len(line) < i: | ||
| break | ||
| # Header found | ||
| else: | ||
| new_element = _newElement() | ||
| new_element['level'] = level | ||
| new_element['title'] = line[i+1:len(line)] | ||
| new_element['title'] = line[i+1:] | ||
| md_title = ConvertTitleToMarkdownId(new_element['title']) | ||
| if md_title in header_dict.keys(): | ||
|
|
||
| if md_title in header_dict: | ||
| i = 1 | ||
| while (md_title + '_' + str(i)) in header_dict.keys(): | ||
| i += 1 | ||
| md_title = md_title + '_' + str(i) | ||
| while f'{md_title}_{i}' in header_dict: | ||
| i += 1 | ||
| md_title = f'{md_title}_' + str(i) | ||
| new_element['md-title'] = md_title | ||
|
|
||
| # Move up in the tree until both levels are equal, or current_element['level'] is higher than level | ||
| while level < current_element['level']: | ||
| current_element = current_element['parent'] | ||
| current_element = current_element['parent'] | ||
|
Comment on lines
-59
to
+74
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| if level > current_element['level']: | ||
| # add to children of current_element | ||
| current_element['content'].append(new_element) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,12 +99,11 @@ def ParsePaths(self): | |
| # path/file.md --> page_path + url | ||
| if self.url[0] == '/': | ||
| self.src_path = self.root_path.joinpath(self.url[1:]).resolve() | ||
| elif self.relative_path_md: | ||
| self.src_path = self.page_path.parent.joinpath(self.url).resolve() | ||
| else: | ||
| if self.relative_path_md: | ||
| self.src_path = self.page_path.parent.joinpath(self.url).resolve() | ||
| else: | ||
| self.src_path = self.root_path.joinpath(self.url).resolve() | ||
|
|
||
| self.src_path = self.root_path.joinpath(self.url).resolve() | ||
|
|
||
|
Comment on lines
+102
to
+106
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| # Determine if relative to root | ||
| if self.src_path.is_relative_to(self.root_path): | ||
| self.inRoot = True | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,11 +53,14 @@ def StripCodeSections(self): | |
| """(Temporarily) Remove codeblocks/-lines so that they are not altered in all the conversions. Placeholders are inserted.""" | ||
| self.codeblocks = re.findall("^```([\s\S]*?)```[\s]*?$", self.page, re.MULTILINE) | ||
| for i, match in enumerate(self.codeblocks): | ||
| self.page = self.page.replace("```"+match+"```", f'%%%codeblock-placeholder-{i}%%%') | ||
|
|
||
| self.page = self.page.replace( | ||
| f"```{match}```", f'%%%codeblock-placeholder-{i}%%%' | ||
| ) | ||
|
|
||
|
|
||
| self.codelines = re.findall("`(.*?)`", self.page) | ||
| for i, match in enumerate(self.codelines): | ||
| self.page = self.page.replace("`"+match+"`", f'%%%codeline-placeholder-{i}%%%') | ||
| self.page = self.page.replace(f"`{match}`", f'%%%codeline-placeholder-{i}%%%') | ||
|
Comment on lines
-56
to
+63
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def RestoreCodeSections(self): | ||
| """Undo the action of StripCodeSections.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,13 +61,13 @@ def StringifyDateRecurse(tree): | |
| for key, value in tree.items(): | ||
| if isinstance(value, date): | ||
| tree[key] = value.isoformat() | ||
| elif isinstance(value, list) or isinstance(value, dict): | ||
| elif isinstance(value, (list, dict)): | ||
| tree[key] = StringifyDateRecurse(value) | ||
| if isinstance(tree, list): | ||
| for key, value in enumerate(tree): | ||
| if isinstance(value, date): | ||
| tree[key] = value.isoformat() | ||
| elif isinstance(value, list) or isinstance(value, dict): | ||
| elif isinstance(value, (list, dict)): | ||
|
Comment on lines
-64
to
+70
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| tree[key] = StringifyDateRecurse(value) | ||
|
|
||
| return tree | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
get_pathsrefactored with the following changes:move-assign-in-block)merge-dict-assign)This removes the following comments ( why? ):