Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions ci/tests/basic_regression_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Comment on lines -24 to +25
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_paths refactored with the following changes:

This removes the following comments ( why? ):

# (..)/obsidian-html/ci/tests/

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
Expand All @@ -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():
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function html_get refactored with the following changes:


def get_default_config():
paths = get_paths()
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_default_config refactored with the following changes:

This removes the following comments ( why? ):

# Merge the two


def customize_default_config(items, write_to_tmp_config=True):
# Example Input
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestDefaultMode.test_C_rss refactored with the following changes:

# 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')
Expand All @@ -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.')
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 456-466 refactored with the following changes:

# Create temp dir
os.chdir(paths['root'])
paths['temp_dir'].mkdir(exist_ok=True)
Expand Down
79 changes: 36 additions & 43 deletions obsidianhtml/CreateIndexFromTags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CreateIndexFromTags refactored with the following changes:

for k in files.keys():
# Determine src file path
page_path_str = files[k]['fullpath']
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 = ''
Expand All @@ -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:
Expand All @@ -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'
Expand Down
15 changes: 7 additions & 8 deletions obsidianhtml/HeaderTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ConvertMarkdownToHeaderTree refactored with the following changes:

This removes the following comments ( why? ):

# Header found

if level > current_element['level']:
# add to children of current_element
current_element['content'].append(new_element)
Expand Down
9 changes: 4 additions & 5 deletions obsidianhtml/MarkdownLink.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MarkdownLink.ParsePaths refactored with the following changes:

# Determine if relative to root
if self.src_path.is_relative_to(self.root_path):
self.inRoot = True
Expand Down
9 changes: 6 additions & 3 deletions obsidianhtml/MarkdownPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MarkdownPage.StripCodeSections refactored with the following changes:


def RestoreCodeSections(self):
"""Undo the action of StripCodeSections."""
Expand Down
4 changes: 2 additions & 2 deletions obsidianhtml/NetworkTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function StringifyDateRecurse refactored with the following changes:

tree[key] = StringifyDateRecurse(value)

return tree
Loading